Пример #1
0
        private int width; // X Resolution of the biggest Mipmap

        // Y Resolution of the biggest Mipmap

        // The color-palette for non-compressed pictures

        public BlpFile(Stream stream)
        {
            this.str = stream;
            fDXTDecompression = new DXTDecompression();

            byte[] buffer = new byte[4];
            // Well, have to fix this... looks weird o.O
            this.str.Read(buffer, 0, 4);

            // Checking for correct Magic-Code
            if ((new ASCIIEncoding()).GetString(buffer) != "BLP2")
                throw new Exception("Invalid BLP Format");

            // Reading type
            str.Read(buffer, 0, 4);
            this.type = BitConverter.ToUInt32(buffer, 0);
            if (this.type != 1)
                throw new Exception("Invalid BLP-Type! Should be 1 but " + this.type + " was found");

            // Reading encoding, alphaBitDepth, alphaEncoding and hasMipmaps
            this.str.Read(buffer, 0, 4);
            this.encoding = buffer[0];
            this.alphaDepth = buffer[1];
            this.alphaEncoding = buffer[2];
            this.hasMipmaps = buffer[3];

            // Reading width
            str.Read(buffer, 0, 4);
            this.width = BitConverter.ToInt32(buffer, 0);

            // Reading height
            str.Read(buffer, 0, 4);
            this.height = BitConverter.ToInt32(buffer, 0);

            // Reading MipmapOffset Array
            for (int i = 0; i < 16; i++)
            {
                stream.Read(buffer, 0, 4);
                this.mipmapOffsets[i] = BitConverter.ToUInt32(buffer, 0);
            }

            // Reading MipmapSize Array
            for (int i = 0; i < 16; i++)
            {
                str.Read(buffer, 0, 4);
                this.mippmapSize[i] = BitConverter.ToUInt32(buffer, 0);
            }

            // When encoding is 1, there is no image compression and we have to read a color palette
            if (this.encoding == 1)
            {
                // Reading palette
                for (int i = 0; i < 256; i++)
                {
                    byte[] color = new byte[4];
                    str.Read(color, 0, 4);
                    this.paletteBGRA[i].blue = color[0];
                    this.paletteBGRA[i].green = color[1];
                    this.paletteBGRA[i].red = color[2];
                    this.paletteBGRA[i].alpha = color[3];
                }
            }
        }