Beispiel #1
0
        /// <summary>
        /// Loads a KTX file from a stream.
        /// </summary>
        public static KtxFile Load(Stream s)
        {
            using (BinaryReader br = new BinaryReader(s, UTF8, true))
            {
                KtxHeader header = br.ReadStruct <KtxHeader>();

                if (header.NumberOfArrayElements > 0)
                {
                    throw new NotSupportedException("KTX files with arrays are not supported.");
                }

                KtxFile ktx = new KtxFile(header);

                int keyValuePairBytesRead = 0;
                while (keyValuePairBytesRead < header.BytesOfKeyValueData)
                {
                    KtxKeyValuePair kvp = KtxKeyValuePair.ReadKeyValuePair(br, out int read);
                    keyValuePairBytesRead += read;
                    ktx.KeyValuePairs.Add(kvp);
                }

                uint numberOfFaces = Math.Max(1, header.NumberOfFaces);
                ktx.MipMaps.Capacity = (int)header.NumberOfMipmapLevels;
                for (uint mipLevel = 0; mipLevel < header.NumberOfMipmapLevels; mipLevel++)
                {
                    uint imageSize = br.ReadUInt32();
                    uint mipWidth  = header.PixelWidth / (uint)(Math.Pow(2, mipLevel));
                    uint mipHeight = header.PixelHeight / (uint)(Math.Pow(2, mipLevel));

                    ktx.MipMaps.Add(new KtxMipmap(imageSize, mipWidth, mipHeight, numberOfFaces));

                    bool cubemap = header.NumberOfFaces > 1 && header.NumberOfArrayElements == 0;
                    for (uint face = 0; face < numberOfFaces; face++)
                    {
                        byte[] faceData = br.ReadBytes((int)imageSize);
                        ktx.MipMaps[(int)mipLevel].Faces[(int)face] = new KtxMipFace(faceData, mipWidth, mipHeight);
                        if (cubemap)
                        {
                            uint cubePadding = 0u;
                            cubePadding = 3 - ((imageSize + 3) % 4);
                            br.SkipPadding(cubePadding);
                        }
                    }

                    uint mipPaddingBytes = 3 - ((imageSize + 3) % 4);
                    br.SkipPadding(mipPaddingBytes);
                }

                return(ktx);
            }
        }
Beispiel #2
0
        public static KtxHeader InitializeCompressed(int width, int height, GlInternalFormat internalFormat, GLFormat baseInternalFormat)
        {
            KtxHeader   header = new KtxHeader();
            Span <byte> id     = stackalloc byte[] { 0xAB, 0x4B, 0x54, 0x58, 0x20, 0x31, 0x31, 0xBB, 0x0D, 0x0A, 0x1A, 0x0A };

            for (int i = 0; i < id.Length; i++)
            {
                header.Identifier[i] = id[i];
            }
            header.Endianness           = 0x04030201;
            header.PixelWidth           = (uint)width;
            header.PixelHeight          = (uint)height;
            header.GlType               = 0;
            header.GlTypeSize           = 1;
            header.GlFormat             = 0;
            header.GlInternalFormat     = internalFormat;
            header.GlBaseInternalFormat = baseInternalFormat;

            return(header);
        }
Beispiel #3
0
 public KtxFile(KtxHeader header)
 {
     Header = header;
 }