예제 #1
0
        private static Texture2D LoadTexture2D(DDSImageInfo image, BinaryReader input, SurfaceFormat format)
        {
            DDSHeader header = image.Header;

            int  width            = header.DwWidth;
            int  height           = header.DwHeight;
            int  flags            = header.ImageFormat.DwFlags;
            bool compressedFormat = IsSet(flags, (int)DDSPixelFlags.FourCC);

            int[]     mipMapSizes = new int[header.DwMipMapCount];
            Texture2D tex         = new Texture2D(width, height, mipMapSizes.Length > 1, format);

            if (compressedFormat)
            {
                int num = 16;
                if (format == SurfaceFormat.DXT1)
                {
                    num = 8;
                }
                int mipLevel = 0;
                while (mipLevel < header.DwMipMapCount)
                {
                    byte[] data = input.ReadBytes(CalculateMipMapSize(true, width, height, num, mipLevel));
                    tex.SetData <byte>(data, mipLevel, null, 0, data.Length);

                    width  = System.Math.Max(width / 2, 1);
                    height = System.Math.Max(height / 2, 1);
                    mipLevel++;
                }
            }
            return(tex);
        }
예제 #2
0
        public override Texture Load(IResource resource, LoaderParameters parameters)
        {
            Stream       stream = resource.OpenStream();
            BinaryReader input  = new BinaryReader(stream);

            DDSImageInfo image = ReadHeaderInfo(input);

            SurfaceFormat     format     = DetermineFormat(image);
            TextureDimensions dimensions = DetermineTextureDimensions(image);

            switch (dimensions)
            {
            case TextureDimensions.Two:
                Texture2D tex2d = LoadTexture2D(image, input, format);
                tex2d.Name = resource.Name;
                input.Close();
                return(tex2d);

            case TextureDimensions.Three:
                Texture3D tex3d = LoadTexture3D(image, input, format);
                tex3d.Name = resource.Name;
                input.Close();
                return(tex3d);

            case TextureDimensions.Cube:
                TextureCube texcube = LoadTextureCube(image, input, format);
                texcube.Name = resource.Name;
                input.Close();
                return(texcube);

            default:
                input.Close();
                throw new InvalidOperationException("Error loading DDS file");
            }
        }
예제 #3
0
        private static SurfaceFormat DetermineFormat(DDSImageInfo image)
        {
            DDSHeader   header   = image.Header;
            DDSHeader10 header10 = image.Header10;

            int  flags            = header.ImageFormat.DwFlags;
            bool compressedFormat = IsSet(flags, (int)DDSPixelFlags.FourCC);
            bool rgb         = IsSet(flags, (int)DDSPixelFlags.RGB);
            bool alphaPixels = IsSet(flags, (int)DDSPixelFlags.AlphaPixels);
            bool alpha       = IsSet(flags, (int)DDSPixelFlags.Alpha);
            bool luminance   = IsSet(flags, (int)DDSPixelFlags.Luminance);

            if (compressedFormat)
            {
                int fourCC = header.ImageFormat.DwFourCC;
                if (fourCC == GetInt("DXT1"))
                {
                    return(SurfaceFormat.DXT1);
                }
                else if (fourCC == GetInt("DXT3"))
                {
                    return(SurfaceFormat.DXT3);
                }
                else if (fourCC == GetInt("DXT5"))
                {
                    return(SurfaceFormat.DXT5);
                }
            }
            else
            {
                if (rgb)
                {
                    int bpp = header.ImageFormat.DwRGBBitCount;
                    switch (bpp)
                    {
                    case 32:
                        return(SurfaceFormat.Color);
                    }
                }
                else if (alphaPixels && alpha && (header.ImageFormat.DwRGBBitCount == 8))
                {
                    return(SurfaceFormat.Alpha8);
                }
            }

            throw new InvalidDataException("Unsupported DDS format.");
        }
예제 #4
0
        private static TextureDimensions DetermineTextureDimensions(DDSImageInfo image)
        {
            DDSHeader   header   = image.Header;
            DDSHeader10 header10 = image.Header10;

            if (IsSet(header.DwCaps2, (int)DDSCaps2.CubeMap))
            {
                return(TextureDimensions.Cube);
            }
            else if (IsSet(header.DwCaps2, (int)DDSCaps2.Volume))
            {
                return(TextureDimensions.Three);
            }
            else
            {
                return(TextureDimensions.Two);
            }
        }
예제 #5
0
        private static DDSImageInfo ReadHeaderInfo(BinaryReader input)
        {
            int dwMagic = input.ReadInt32();

            if (dwMagic != GetInt("DDS "))
            {
                throw new InvalidOperationException("Not a DDS file");
            }

            DDSImageInfo image = new DDSImageInfo();

            image.Header = ReadHeader(input);
            if (image.Header.ImageFormat.DwFourCC == GetInt("DX10"))
            {
                image.Header10 = ReadHeader10(input);
            }
            else
            {
                image.Header10.Exists = false;
            }
            return(image);
        }
예제 #6
0
 private static Texture3D LoadTexture3D(DDSImageInfo image, BinaryReader input, SurfaceFormat format)
 {
     throw new NotImplementedException();
 }