Пример #1
0
        private static TextureLoadInfo ParseHeader(ref BlpHeader header)
        {
            var ret = new TextureLoadInfo
            {
                Height = header.Height,
                Width  = header.Width
            };

            if (header.Compression == 2)
            {
                switch (header.AlphaCompression)
                {
                case 0:
                    ret.Format    = SharpDX.DXGI.Format.BC1_UNorm;
                    ret.BlockSize = 8;
                    break;

                case 1:
                    ret.Format    = SharpDX.DXGI.Format.BC2_UNorm;
                    ret.BlockSize = 16;
                    break;

                case 7:
                    ret.Format    = SharpDX.DXGI.Format.BC3_UNorm;
                    ret.BlockSize = 16;
                    break;
                }
            }
            else
            {
                ret.Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm;
            }

            return(ret);
        }
Пример #2
0
        private static BlpHeader WriteHeader(BinaryWriter output, Image image, Format format, bool hasMips)
        {
            var header = new BlpHeader
            {
                Magic            = 0x32504C42,
                Width            = image.Width,
                Height           = image.Height,
                Compression      = 2,
                AlphaCompression = (byte)(format == Format.BC1_UNorm ? 0 : (format == Format.BC2_UNorm ? 1 : 7)),
                AlphaDepth       = 0,
                MipLevels        = (byte)(hasMips ? 1 : 0),
                Version          = 1
            };

            output.Write(header);
            return(header);
        }
Пример #3
0
        private static void ParseUncompressedLayer(int layer, ref uint[] palette, BinaryReader baseReader, ref BlpHeader header, TextureLoadInfo loadInfo)
        {
            if (header.Compression == 3)
            {
                return;
            }

            if (palette.Length == 0)
            {
                baseReader.BaseStream.Position = SizeCache <BlpHeader> .Size;
                palette = baseReader.ReadArray <uint>(256);
            }

            var w       = Math.Max(header.Width >> layer, 1);
            var h       = Math.Max(header.Height >> layer, 1);
            var indices = loadInfo.Layers[layer];
            var colors  = new byte[w * h * 4];

            if (header.AlphaDepth == 8)
            {
                DecompPaletteFastPath(ref palette, ref indices, ref colors);
            }
            else
            {
                DecompPaletteA8R8G8B8(header.AlphaDepth, ref palette, ref indices, ref colors);
            }

            loadInfo.Layers[layer] = colors;
        }
Пример #4
0
        private static unsafe void ParseLayer(int layer, ref uint[] palette, BinaryReader reader, BlpHeader header, TextureLoadInfo loadInfo)
        {
            if (header.Sizes[layer] == 0)
            {
                return;
            }

            var w = Math.Max(1, header.Width >> layer);

            reader.BaseStream.Position = header.Offsets[layer];
            loadInfo.Layers.Add(reader.ReadBytes(header.Sizes[layer]));
            if (loadInfo.Format != SharpDX.DXGI.Format.R8G8B8A8_UNorm)
            {
                loadInfo.RowPitchs.Add(((w + 3) / 4) * loadInfo.BlockSize);
            }
            else
            {
                ParseUncompressedLayer(layer, ref palette, reader, ref header, loadInfo);
                loadInfo.RowPitchs.Add(w * 4);
            }
        }
Пример #5
0
        private static TextureLoadInfo ParseHeader(ref BlpHeader header)
        {
            var ret = new TextureLoadInfo
            {
                Height = header.Height,
                Width = header.Width
            };

            if (header.Compression == 2)
            {
                switch (header.AlphaCompression)
                {
                    case 0:
                        ret.Format = SharpDX.DXGI.Format.BC1_UNorm;
                        ret.BlockSize = 8;
                        break;

                    case 1:
                        ret.Format = SharpDX.DXGI.Format.BC2_UNorm;
                        ret.BlockSize = 16;
                        break;

                    case 7:
                        ret.Format = SharpDX.DXGI.Format.BC3_UNorm;
                        ret.BlockSize = 16;
                        break;
                }
            }
            else
                ret.Format = SharpDX.DXGI.Format.R8G8B8A8_UNorm;

            return ret;
        }
Пример #6
0
        private static void ParseUncompressedLayer(int layer, ref uint[] palette, BinaryReader baseReader, ref BlpHeader header, TextureLoadInfo loadInfo)
        {
            if (header.Compression == 3)
                return;

            if (palette.Length == 0)
            {
                baseReader.BaseStream.Position = SizeCache<BlpHeader>.Size;
                palette = baseReader.ReadArray<uint>(256);
            }

            var w = Math.Max(header.Width >> layer, 1);
            var h = Math.Max(header.Height >> layer, 1);
            var indices = loadInfo.Layers[layer];
            var colors = new byte[w * h * 4];

            if (header.AlphaDepth == 8)
                DecompPaletteFastPath(ref palette, ref indices, ref colors);
            else
                DecompPaletteA8R8G8B8(header.AlphaDepth, ref palette, ref indices, ref colors);

            loadInfo.Layers[layer] = colors;
        }
Пример #7
0
        private static unsafe void ParseLayer(int layer, ref uint[] palette, BinaryReader reader, BlpHeader header, TextureLoadInfo loadInfo)
        {
            if (header.Sizes[layer] == 0)
                return;

            var w = Math.Max(1, header.Width >> layer);

            reader.BaseStream.Position = header.Offsets[layer];
            loadInfo.Layers.Add(reader.ReadBytes(header.Sizes[layer]));
            if (loadInfo.Format != SharpDX.DXGI.Format.R8G8B8A8_UNorm)
                loadInfo.RowPitchs.Add(((w + 3) / 4) * loadInfo.BlockSize);
            else
            {
                ParseUncompressedLayer(layer, ref palette, reader, ref header, loadInfo);
                loadInfo.RowPitchs.Add(w * 4);
            }
        }