public void read(Reader reader, bool skipHeader = false, int clrCnt = 0x80) { #region Header if (!skipHeader) { reader.seek(6, System.IO.SeekOrigin.Begin); // GIF89a width = reader.ReadByte(); width |= (ushort)(reader.ReadByte() << 8); height = reader.ReadByte(); height |= (ushort)(reader.ReadByte() << 8); byte info = reader.ReadByte(); // Palette Size clrCnt = (info & 0x7) + 1; if (clrCnt > 0) { clrCnt = 1 << clrCnt; } reader.ReadByte(); // background colour index reader.ReadByte(); // unused if (clrCnt != 0x100) { throw new Exception("RSDK-Formatted Gif files must use 256 colours!"); } } for (int c = 0; c < clrCnt; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } #endregion #region Blocks byte blockType = reader.ReadByte(); while (blockType != 0 && blockType != ';') { switch (blockType) { default: // Unknown Console.WriteLine($"Unknown Block Type ({blockType})"); break; case (byte)'!': // Extension { byte extensionType = reader.ReadByte(); switch (extensionType) { case 0xF9: // Graphics Control Extension { int blockSize = reader.ReadByte(); byte disposalFlag = reader.ReadByte(); ushort frameDelay = reader.ReadUInt16(); byte transparentIndex = reader.ReadByte(); reader.ReadByte(); // terminator } break; case 0x01: // Plain Text Extension case 0xFE: // Comment Extension case 0xFF: // Application Extension { int blockSize = reader.ReadByte(); while (blockSize != 0) { // Read block reader.BaseStream.Position += blockSize; blockSize = reader.ReadByte(); // next block Size, if its 0 we know its the end of block } } break; default: // Unknown Console.WriteLine($"Unknown Extension Type ({extensionType})"); return; } } break; case (byte)',': // Image descriptor { int left = reader.ReadUInt16(); int top = reader.ReadUInt16(); int right = reader.ReadUInt16(); int bottom = reader.ReadUInt16(); byte info2 = reader.ReadByte(); bool interlaced = (info2 & 0x40) != 0; if (info2 >> 7 == 1) { for (int c = 0x80; c < 0x100; ++c) { palette[c].R = reader.ReadByte(); palette[c].G = reader.ReadByte(); palette[c].B = reader.ReadByte(); } } readPictureData(width, height, interlaced, reader); } break; } blockType = reader.ReadByte(); } #endregion if (!skipHeader) { reader.Close(); } }
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(); }