Beispiel #1
0
        public static Image ToImage(this ITexture tex)
        {
            var d = tex.GetTextureData();
            int w = tex.Width, h = tex.Height;

            switch (tex.Format)
            {
            case D3DFMT_A8R8G8B8: return(Image.LoadPixelData <Bgra32>(d, w, h));

            case D3DFMT_L8:       return(Image.LoadPixelData <L8>(d, w, h));

            case D3DFMT_A8:       return(Image.LoadPixelData <A8>(d, w, h));

            case D3DFMT_A1R5G5B5: return(Image.LoadPixelData <Bgra5551>(d, w, h));

            case D3DFMT_A8B8G8R8: return(Image.LoadPixelData <Rgba32>(d, w, h));
            }

            var uncompressed = new byte[D3DFMT_A8B8G8R8.Size(w, h)];

            if (!FuckDX.decode(d, tex.Format, w, h, uncompressed))
            {
                throw new NotSupportedException(
                          $"Failed to decode {tex.Name}: I guess {tex.Format} is not supported :( Mention @Kng if you care"
                          );
            }

            return(Image.LoadPixelData <Rgba32>(uncompressed, w, h));
        }
Beispiel #2
0
        public static void SaveTextureData(ITexture texture, string fileName)
        {
            var data = texture.GetTextureData();

            var format = DXGI_FORMAT.DXGI_FORMAT_UNKNOWN;

            switch (texture.Format)
            {
            // compressed
            case TextureFormat.D3DFMT_DXT1: format = DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM; break;

            case TextureFormat.D3DFMT_DXT3: format = DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM; break;

            case TextureFormat.D3DFMT_DXT5: format = DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM; break;

            case TextureFormat.D3DFMT_ATI1: format = DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM; break;

            case TextureFormat.D3DFMT_ATI2: format = DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM; break;

            case TextureFormat.D3DFMT_BC7: format = DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM; break;

            // uncompressed
            case TextureFormat.D3DFMT_A1R5G5B5: format = DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM; break;

            case TextureFormat.D3DFMT_A8: format = DXGI_FORMAT.DXGI_FORMAT_A8_UNORM; break;

            case TextureFormat.D3DFMT_A8B8G8R8: format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM; break;

            case TextureFormat.D3DFMT_L8: format = DXGI_FORMAT.DXGI_FORMAT_R8_UNORM; break;

            case TextureFormat.D3DFMT_A8R8G8B8: format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; break;
            }

            DirectXTex.ImageStruct img = new DirectXTex.ImageStruct();
            img.Data         = data;
            img.Width        = texture.Width;
            img.Height       = texture.Height;
            img.MipMapLevels = texture.MipMapLevels;
            img.Format       = (int)format;

            DirectXTex.DDSIO.WriteDDS(fileName, img);
        }
        public static byte[] GetRgbaImage(ITexture model, int mipMapLevel = 0)
        {
            var x = model.GetTextureData(mipMapLevel);
            switch (model.Format)
            {
                // compressed formats...
                case TextureFormat.D3DFMT_DXT1:
                    return TextureCompressionHelper.DecompressBC1(x, model.Width, model.Height);

                case TextureFormat.D3DFMT_DXT3:
                    {
                        return TextureCompressionHelper.DecompressBC2(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_DXT5:
                    {
                        return TextureCompressionHelper.DecompressBC3(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_ATI1:
                    {
                        return TextureCompressionHelper.DecompressBC4(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_ATI2:
                    {
                        return TextureCompressionHelper.DecompressBC5(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_BC7:
                    {
                        return TextureCompressionHelper.DecompressBC7(x, model.Width, model.Height);
                    }

                // uncompressed formats...
                case TextureFormat.D3DFMT_A8:
                    {
                        return TextureConvert.MakeRGBAFromA8(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_L8:
                    {
                        return TextureConvert.MakeARGBFromL8(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_A1R5G5B5:
                    {
                        return TextureConvert.MakeARGBFromA1R5G5B5(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_A8B8G8R8:
                    {
                        return TextureConvert.MakeRGBAFromA8B8G8R8(x, model.Width, model.Height);
                    }
                case TextureFormat.D3DFMT_A8R8G8B8:
                    {
                        return TextureConvert.MakeRGBAFromA8R8G8B8(x, model.Width, model.Height);
                    }

                default:
                    throw new System.Exception("unknown format");
            }
        }
Beispiel #4
0
        public static byte[] GetRgbaImage(ITexture model, int mipMapLevel = 0)
        {
            var x = model.GetTextureData(mipMapLevel);

            switch (model.Format)
            {
            // compressed formats...
            case TextureFormat.D3DFMT_DXT1:
                return(TextureCompressionHelper.DecompressBC1(x, model.Width, model.Height));

            case TextureFormat.D3DFMT_DXT3:
            {
                return(TextureCompressionHelper.DecompressBC2(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_DXT5:
            {
                return(TextureCompressionHelper.DecompressBC3(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_ATI1:
            {
                return(TextureCompressionHelper.DecompressBC4(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_ATI2:
            {
                return(TextureCompressionHelper.DecompressBC5(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_BC7:
            {
                return(TextureCompressionHelper.DecompressBC7(x, model.Width, model.Height));
            }

            // uncompressed formats...
            case TextureFormat.D3DFMT_A8:
            {
                return(TextureConvert.MakeRGBAFromA8(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_L8:
            {
                return(TextureConvert.MakeARGBFromL8(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_A1R5G5B5:
            {
                return(TextureConvert.MakeARGBFromA1R5G5B5(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_A8B8G8R8:
            {
                return(TextureConvert.MakeRGBAFromA8B8G8R8(x, model.Width, model.Height));
            }

            case TextureFormat.D3DFMT_A8R8G8B8:
            {
                return(TextureConvert.MakeRGBAFromA8R8G8B8(x, model.Width, model.Height));
            }

            default:
                throw new System.Exception("unknown format");
            }
        }
Beispiel #5
0
        public static void SaveTextureData(ITexture texture, string fileName)
        {
            var data = texture.GetTextureData();

            var format = DXGI_FORMAT.DXGI_FORMAT_UNKNOWN;
            switch (texture.Format)
            {
                // compressed
                case TextureFormat.D3DFMT_DXT1: format = DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM; break;
                case TextureFormat.D3DFMT_DXT3: format = DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM; break;
                case TextureFormat.D3DFMT_DXT5: format = DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM; break;
                case TextureFormat.D3DFMT_ATI1: format = DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM; break;
                case TextureFormat.D3DFMT_ATI2: format = DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM; break;
                case TextureFormat.D3DFMT_BC7: format = DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM; break;

                // uncompressed
                case TextureFormat.D3DFMT_A1R5G5B5: format = DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM; break;
                case TextureFormat.D3DFMT_A8: format = DXGI_FORMAT.DXGI_FORMAT_A8_UNORM; break;
                case TextureFormat.D3DFMT_A8B8G8R8: format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM; break;
                case TextureFormat.D3DFMT_L8: format = DXGI_FORMAT.DXGI_FORMAT_R8_UNORM; break;
                case TextureFormat.D3DFMT_A8R8G8B8: format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; break;
            }

            DirectXTex.ImageStruct img = new DirectXTex.ImageStruct();
            img.Data = data;
            img.Width = texture.Width;
            img.Height = texture.Height;
            img.MipMapLevels = texture.MipMapLevels;
            img.Format = (int)format;

            DirectXTex.DDSIO.WriteDDS(fileName, img);
        }
Beispiel #6
0
        public static void SaveTextureData(ITexture texture, string fileName)
        {
            var data = texture.GetTextureData();

            var format = DXGI_FORMAT.DXGI_FORMAT_UNKNOWN;
            switch (texture.Format)
            {
                // compressed
                case TextureFormat.D3DFMT_DXT1: format = DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM; break;
                case TextureFormat.D3DFMT_DXT3: format = DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM; break;
                case TextureFormat.D3DFMT_DXT5: format = DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM; break;
                case TextureFormat.D3DFMT_ATI1: format = DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM; break;
                case TextureFormat.D3DFMT_ATI2: format = DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM; break;
                case TextureFormat.D3DFMT_BC7: format = DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM; break;

                // uncompressed
                case TextureFormat.D3DFMT_A1R5G5B5: format = DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM; break;
                case TextureFormat.D3DFMT_A8: format = DXGI_FORMAT.DXGI_FORMAT_A8_UNORM; break;
                case TextureFormat.D3DFMT_A8B8G8R8: format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM; break;
                case TextureFormat.D3DFMT_L8: format = DXGI_FORMAT.DXGI_FORMAT_R8_UNORM; break;
                case TextureFormat.D3DFMT_A8R8G8B8: format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; break;
            }

            DXTexdds.SaveDDSImage(fileName, data, 0, texture.Width, texture.Height, 2 * texture.Width, texture.MipMapLevels, (int)format);
        }
Beispiel #7
0
        public static void SaveTextureData(ITexture texture, Stream stream)
        {
            var data = texture.GetTextureData();

            var format = DXGI_FORMAT.DXGI_FORMAT_UNKNOWN;
            switch (texture.Format)
            {
                // compressed
                case TextureFormat.D3DFMT_DXT1: format = DXGI_FORMAT.DXGI_FORMAT_BC1_UNORM; break;
                case TextureFormat.D3DFMT_DXT3: format = DXGI_FORMAT.DXGI_FORMAT_BC2_UNORM; break;
                case TextureFormat.D3DFMT_DXT5: format = DXGI_FORMAT.DXGI_FORMAT_BC3_UNORM; break;
                case TextureFormat.D3DFMT_ATI1: format = DXGI_FORMAT.DXGI_FORMAT_BC4_UNORM; break;
                case TextureFormat.D3DFMT_ATI2: format = DXGI_FORMAT.DXGI_FORMAT_BC5_UNORM; break;
                case TextureFormat.D3DFMT_BC7: format = DXGI_FORMAT.DXGI_FORMAT_BC7_UNORM; break;

                // uncompressed
                case TextureFormat.D3DFMT_A1R5G5B5: format = DXGI_FORMAT.DXGI_FORMAT_B5G5R5A1_UNORM; break;
                case TextureFormat.D3DFMT_A8: format = DXGI_FORMAT.DXGI_FORMAT_A8_UNORM; break;
                case TextureFormat.D3DFMT_A8B8G8R8: format = DXGI_FORMAT.DXGI_FORMAT_R8G8B8A8_UNORM; break;
                case TextureFormat.D3DFMT_L8: format = DXGI_FORMAT.DXGI_FORMAT_R8_UNORM; break;
                case TextureFormat.D3DFMT_A8R8G8B8: format = DXGI_FORMAT.DXGI_FORMAT_B8G8R8A8_UNORM; break;
            }

            byte[] strbuf = new byte[16777216];
            int strlen = 16777216;
            DXTexdds.SaveDDSImageToStream(strbuf, ref strlen, data, 0, texture.Width, texture.Height, 2 * texture.Width, texture.MipMapLevels, (int)format);
            if (strlen > 16777216)
            {
                strbuf = new byte[strlen];
                DXTexdds.SaveDDSImageToStream(strbuf, ref strlen, data, 0, texture.Width, texture.Height, 2 * texture.Width, texture.MipMapLevels, (int)format);
            }

            stream.Write(strbuf, 0, strlen);
        }