Ejemplo n.º 1
0
        public void read(Reader reader)
        {
            reader.ReadInt16(); // "BM"
            reader.ReadInt32(); // totalFileSize
            reader.ReadInt32(); // Unused
            int pixelPos = reader.ReadInt32();

            reader.seek(14 + 4, System.IO.SeekOrigin.Begin);

            width  = reader.ReadByte();
            width |= reader.ReadByte() << 8;
            width |= reader.ReadByte() << 16;
            width |= reader.ReadByte() << 24;

            height  = reader.ReadByte();
            height |= reader.ReadByte() << 8;
            height |= reader.ReadByte() << 16;
            height |= reader.ReadByte() << 24;

            reader.BaseStream.Position += sizeof(ushort);
            bool indexed = reader.ReadUInt16() <= 8; //bpp

            if (!indexed)
            {
                throw new Exception("RSDK-Formatted Bitmap files must be indexed!");
            }

            reader.BaseStream.Position += 4 * sizeof(int);
            int clrCount = reader.ReadInt32(); // how many colours used

            reader.seek(14 + 40, System.IO.SeekOrigin.Begin);

            for (int c = 0; c < clrCount; c++)
            {
                palette[c].B = reader.ReadByte();
                palette[c].G = reader.ReadByte();
                palette[c].R = reader.ReadByte();
                reader.ReadByte(); // unused
            }

            long expectedPixelPos = (reader.BaseStream.Length - height * width);

            if (pixelPos != expectedPixelPos)
            {
                throw new Exception("RSDK-Formatted Bitmap files must end with the pixel data!");
            }

            // This is how RSDK does it but there's a small chance it could maybe be wrong
            reader.seek(expectedPixelPos, System.IO.SeekOrigin.Begin);

            pixels = new byte[width * height];
            int gfxPos = width * (height - 1);

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    pixels[gfxPos++] = reader.ReadByte();
                }
                gfxPos -= 2 * width;
            }

            reader.Close();
        }