Esempio n. 1
0
        public bool ExtractUInt32(Stream stream, out UInt32 result)
        {
            const int UINT32_NO_OF_BYTES = 4;

            byte[] chunk     = new byte[UINT32_NO_OF_BYTES];
            int    bytesRead = stream.Read(chunk, 0, UINT32_NO_OF_BYTES);

            if (bytesRead != UINT32_NO_OF_BYTES)
            {
                result = 0;
                return(false);
            }
            if (Endianness == KTX_ENDIAN_REF_REV)
            {
                KTXHeader.SwapEndian32(chunk, UINT32_NO_OF_BYTES);
            }
            result = BitConverter.ToUInt32(chunk, 0);
            return(true);
        }
Esempio n. 2
0
        KTXError ExtractFace(
            Stream src,
            Stream dest,
            KTXHeader header,
            KTXMipmapData mipmap,
            byte[] dataBuffer,
            KTXFaceData[] faces
            )
        {
            mipmap.Common.PixelWidth  = Math.Max(1, header.PixelWidth >> mipmap.Common.Level);
            mipmap.Common.PixelHeight = Math.Max(1, header.PixelHeight >> mipmap.Common.Level);
            mipmap.Common.PixelDepth  = Math.Max(1, header.PixelDepth >> mipmap.Common.Level);

            for (int face = 0; face < header.NumberOfFaces; ++face)
            {
                var bytesRead = src.Read(dataBuffer, 0, mipmap.SizeRounded);
                if (bytesRead != mipmap.SizeRounded)
                {
                    return(KTXError.UnexpectedEndOfFile);
                }

                var bytesToRead = (int)mipmap.Common.Size;

                /* Perform endianness conversion on texture data */
                if (header.RequiresSwap() && header.GlTypeSize == 2)
                {
                    KTXHeader.SwapEndian16(dataBuffer, bytesToRead);
                }
                else if (header.RequiresSwap() && header.GlTypeSize == 4)
                {
                    KTXHeader.SwapEndian32(dataBuffer, bytesToRead);
                }

                mipmap.Common.NumberOfFaces     = (int)header.NumberOfFaces;
                mipmap.ViewType                 = header.Instructions.ViewType;
                mipmap.Common.IsCompressed      = header.Instructions.IsCompressed;
                mipmap.Common.TextureDimensions = header.Instructions.TextureDimensions;
                mipmap.Common.FaceIndex         = face;

                if (header.Instructions.TextureDimensions == 1)
                {
                }
                else if (header.Instructions.TextureDimensions == 2)
                {
                    if (header.NumberOfArrayElements > 0)
                    {
                        mipmap.Common.PixelHeight = header.NumberOfArrayElements;
                    }
                }
                else if (header.Instructions.TextureDimensions == 3)
                {
                    if (header.NumberOfArrayElements > 0)
                    {
                        mipmap.Common.PixelDepth = header.NumberOfArrayElements;
                    }
                }

                faces[face].Mipmaps[mipmap.Common.Level] = new MgImageMipmap
                {
                    // TODO : MAKE SURE IT DOES NOT "EXPLODE" LATER
                    Offset = (uint)dest.Position,
                    Width  = mipmap.Common.PixelWidth,
                    Height = mipmap.Common.PixelHeight,
                    Size   = mipmap.Common.Size,
                };

                dest.Write(dataBuffer, 0, bytesToRead);


                //            // Renderion is returning INVALID_VALUE. Oops!!
                //            if (mETCUnpacker.IsRequired (header.Instructions, mipmap.GLError))
                //{
                //	var result = mETCUnpacker.UnpackCompressedTexture (
                //		header.Instructions,
                //		mipmap.Common.Level,
                //		face,
                //		mipmap.Common.PixelWidth,
                //		mipmap.Common.PixelHeight,
                //		mipmap.Common.Data);

                //	if (result != KTXError.Success)
                //	{
                //		return result;
                //	}

                //	mipmap.GLError = mPlatform.GetError ();
                //}
            }
            return(KTXError.Success);
        }