Esempio n. 1
0
        private void ParseInternal(Stream stream)
        {
            using (BinaryReader br = new BinaryReader(stream))
            {
                br.ReadInt32(); // unknown
                m_cmpType   = (Blp2CompressionType)br.ReadByte();
                m_alphaBits = br.ReadByte();
                int paletteBits = br.ReadByte();
                br.ReadByte(); // unknown
                int  width         = br.ReadInt32();
                int  height        = br.ReadInt32();
                Size fullImageSize = new Size(width, height);
                m_mainSize = fullImageSize;

                int[] mipmapOffsets = new int[MAX_MIPMAP_COUNT];
                for (int i = 0; i < MAX_MIPMAP_COUNT; i++)
                {
                    mipmapOffsets[i] = br.ReadInt32();
                }

                int[] sizes = new int[MAX_MIPMAP_COUNT];
                for (int i = 0; i < MAX_MIPMAP_COUNT; i++)
                {
                    sizes[i] = br.ReadInt32();
                }


                if (m_cmpType == Blp2CompressionType.Palette)
                {
                    if (paletteBits != 8)
                    {
                        throw new ArgumentException("Palettized data can only have 8 bits per pixel.", "stream");
                    }
                }
                else if (m_cmpType == Blp2CompressionType.DirectX)
                {
                    if (m_alphaBits != 0 && m_alphaBits != 8 && m_alphaBits != 1)
                    {
                        throw new ArgumentException("DXT2/DXT4-compressed images are not yet supported.");
                    }
                }
                else if (m_cmpType == Blp2CompressionType.Jpeg)
                {
                }
                else
                {
                    throw new ArgumentException("Unknown file format.");
                }

                if (m_cmpType == Blp2CompressionType.Palette)
                {
                    ParsePalette(stream, br);
                }
                else if (m_cmpType == Blp2CompressionType.Jpeg)
                {
                    ParseJpegHeader(stream, br);
                }

                for (int i = 0; i < MAX_MIPMAP_COUNT; i++)
                {
                    int currentOffset = mipmapOffsets[i];
                    int currentSize   = sizes[i];

                    if (currentOffset == 0 || currentSize == 0)
                    {
                        break;
                    }

                    Size   mipmapSize = Blp1Parser.GetSize(fullImageSize, i);
                    byte[] data       = new byte[currentSize];
                    stream.Seek(currentOffset, SeekOrigin.Begin);
                    br.Read(data, 0, currentSize);
                    Blp2MipMap mipmap = new Blp2MipMap(mipmapSize, i, data);
                    m_mipmaps.Add(mipmap);
                }
            }
        }
Esempio n. 2
0
 public override Size GetSizeOfMipmap(int mipmapIndex)
 {
     return(Blp1Parser.GetSize(m_mainSize, mipmapIndex));
 }