Beispiel #1
0
        private void InitWithDDSBitmap(BinaryReader reader)
        {
            UInt32 magic = reader.ReadUInt32();
            UInt32 size  = reader.ReadUInt32();

            if (magic != DDSMagic || size != 124)
            {
                throw new InvalidDataException("Invalid DDS file header");
            }
            // UInt32 flags =
            reader.ReadUInt32();
            int height            = reader.ReadInt32();
            int width             = reader.ReadInt32();
            int pitchOrLinearSize = (int)reader.ReadUInt32();

            // UInt32 depth =
            reader.ReadUInt32();
            UInt32 mipMapCount = reader.ReadUInt32();

            reader.ReadBytes(11 * 4);
            // Read pixel format
            reader.ReadUInt32();             // Structure size (32 bytes)
            DDSPFFlags pfFlags  = (DDSPFFlags)reader.ReadUInt32();
            UInt32     pfFourCC = reader.ReadUInt32();

            // UInt32 pfRGBBitCount =
            reader.ReadUInt32();
            // UInt32 pfRBitMask =
            reader.ReadUInt32();
            // UInt32 pfGBitMask =
            reader.ReadUInt32();
            // UInt32 pfBBitMask =
            reader.ReadUInt32();
            // UInt32 pfABitMask =
            reader.ReadUInt32();
            // read the rest of header
            // UInt32 caps =
            reader.ReadUInt32();
            // UInt32 caps2 =
            reader.ReadUInt32();
            // UInt32 caps3 =
            reader.ReadUInt32();
            // UInt32 caps4 =
            reader.ReadUInt32();
            // UInt32 reserved2 =
            reader.ReadUInt32();

            SurfaceSize = ImageSize = new Size(width, height);
            mipMapCount = 1;
            Action glCommands = PrepareOpenGLTexture;

            MemoryUsed = 0;
            for (int level = 0; level < mipMapCount; level++)
            {
                if (width < 8 || height < 8)
                {
                    break;
                }
                if ((pfFlags & DDSPFFlags.RGB) != 0)
                {
                    ReadRGBAImage(ref glCommands, reader, level, width, height, pitchOrLinearSize);
                }
                else if ((pfFlags & DDSPFFlags.FourCC) != 0)
                {
                    ReadCompressedImage(ref glCommands, reader, level, width, height, pitchOrLinearSize, pfFourCC);
                }
                else
                {
                    throw new InvalidDataException("Error reading DDS");
                }
                pitchOrLinearSize /= 4;
                width             /= 2;
                height            /= 2;
            }
            Application.InvokeOnMainThread(glCommands);
        }
Beispiel #2
0
        private void InitWithDDSBitmap(BinaryReader reader)
        {
            UInt32 magic = reader.ReadUInt32();
            UInt32 size  = reader.ReadUInt32();

            if (magic != DDSMagic || size != 124)
            {
                throw new InvalidDataException("Invalid DDS file header");
            }
            // UInt32 flags =
            reader.ReadUInt32();
            int height            = reader.ReadInt32();
            int width             = reader.ReadInt32();
            int pitchOrLinearSize = (int)reader.ReadUInt32();

            // UInt32 depth =
            reader.ReadUInt32();
            UInt32 mipMapCount = reader.ReadUInt32();

            reader.ReadBytes(11 * 4);
            // Read pixel format
            reader.ReadUInt32();             // Structure size (32 bytes)
            DDSPFFlags pfFlags  = (DDSPFFlags)reader.ReadUInt32();
            UInt32     pfFourCC = reader.ReadUInt32();

            // UInt32 pfRGBBitCount =
            reader.ReadUInt32();
            // UInt32 pfRBitMask =
            reader.ReadUInt32();
            // UInt32 pfGBitMask =
            reader.ReadUInt32();
            // UInt32 pfBBitMask =
            reader.ReadUInt32();
            // UInt32 pfABitMask =
            reader.ReadUInt32();
            // read the rest of header
            // UInt32 caps =
            reader.ReadUInt32();
            // UInt32 caps2 =
            reader.ReadUInt32();
            // UInt32 caps3 =
            reader.ReadUInt32();
            // UInt32 caps4 =
            reader.ReadUInt32();
            // UInt32 reserved2 =
            reader.ReadUInt32();

            Format format;

            if ((pfFlags & DDSPFFlags.RGB) != 0)
            {
                format = Format.R8G8B8A8_UNorm;
            }
            else
            {
                switch ((DDSFourCC)pfFourCC)
                {
                case DDSFourCC.DXT1:
                    format = Format.BC1_RGB_UNorm_Block;
                    break;

                case DDSFourCC.DXT3:
                    format = Format.BC2_UNorm_Block;
                    break;

                case DDSFourCC.DXT5:
                    format = Format.BC3_UNorm_Block;
                    break;

                default:
                    throw new InvalidDataException("Unsupported texture format");
                }
            }
            SurfaceSize = ImageSize = new Size(width, height);
            mipMapCount = 1;
            if (mipMapCount > 1 && mipMapCount != GraphicsUtility.CalculateMipLevelCount(width, height))
            {
                throw new NotSupportedException();
            }
            Action deferredCommands = () => EnsurePlatformTexture(format, width, height, mipMapCount > 1);

            MemoryUsed = 0;
            for (int level = 0; level < mipMapCount; level++)
            {
                var levelCopy = level;
                var buffer    = ReadTextureData(reader, GraphicsUtility.CalculateMipLevelDataSize(levelCopy, format, width, height));
                deferredCommands += () => platformTexture.SetData(levelCopy, buffer);
            }
            Window.Current.InvokeOnRendering(deferredCommands);
        }