public GraphicsImage(Reader reader, bool dcGFX = false) { if (dcGFX) { reader.ReadByte(); } width = (ushort)(reader.ReadByte() << 8); width |= reader.ReadByte(); height = (ushort)(reader.ReadByte() << 8); height |= reader.ReadByte(); // Create image gfxImage = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette cp = gfxImage.Palette; // Read & Process palette for (int i = 0; i < 255; i++) { GFXpal[i].R = reader.ReadByte(); GFXpal[i].G = reader.ReadByte(); GFXpal[i].B = reader.ReadByte(); cp.Entries[i] = Color.FromArgb(255, GFXpal[i].R, GFXpal[i].G, GFXpal[i].B); } gfxImage.Palette = cp; //Read Image Data byte[] buf = new byte[3]; bool finished = false; int cnt = 0; int loop = 0; data = new byte[(width * height) + 1]; while (!finished) { buf[0] = reader.ReadByte(); if (buf[0] == 255) { buf[1] = reader.ReadByte(); if (buf[1] == 255) { finished = true; break; } else { buf[2] = reader.ReadByte(); loop = 0; // Repeat value needs to decreased by one to decode // the graphics from the Dreamcast demo if (dcGFX) { buf[2]--; } while (loop < buf[2] && !reader.IsEof) { data[cnt++] = buf[1]; loop++; } } } else { data[cnt++] = buf[0]; } } //Console.Write("file Length = " + reader.BaseStream.Length + " file pos = " + reader.Pos + " data remaining = " + (reader.BaseStream.Length - reader.Pos)); // Write data to image int pixel = 0; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { BitmapData ImgData = gfxImage.LockBits(new Rectangle(new Point(w, h), new Size(1, 1)), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); byte b = System.Runtime.InteropServices.Marshal.ReadByte(ImgData.Scan0); System.Runtime.InteropServices.Marshal.WriteByte(ImgData.Scan0, (data[pixel])); gfxImage.UnlockBits(ImgData); pixel++; } } 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(); }
public gfx(Reader reader, bool dcGFX) { if (dcGFX) { reader.ReadByte(); } width = reader.ReadByte() << 8; width |= reader.ReadByte(); height = reader.ReadByte() << 8; height |= reader.ReadByte(); // Create image GFXpal = new gfxPalette(); gfxImage = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette cp = gfxImage.Palette; // Read & Process palette, ISSUE HERE for (int i = 0; i < 255; i++) { GFXpal.r[i] = reader.ReadByte(); GFXpal.g[i] = reader.ReadByte(); GFXpal.b[i] = reader.ReadByte(); cp.Entries[i] = Color.FromArgb(255, GFXpal.r[i], GFXpal.g[i], GFXpal.b[i]); } gfxImage.Palette = cp; //Read Image Data int[] buf = new int[3]; bool finished = false; int cnt = 0; int loop = 0; data = new int[width * height]; while (!reader.IsEof) { buf[0] = reader.ReadByte(); if (buf[0] != 0xFF && !reader.IsEof) { data[cnt++] = buf[0]; } else { buf[1] = reader.ReadByte(); if (buf[1] != 0xFF && !reader.IsEof) { buf[2] = reader.ReadByte(); loop = 0; // Repeat value needs to decreased by one to decode // the graphics from the Dreamcast demo if (dcGFX) { buf[2]--; } while (loop < buf[2] && !reader.IsEof) { data[cnt++] = buf[1]; loop++; } } else { finished = true; } } } // Write data to image int pixel = 0; for (int h = 0; h < height; h++) { for (int w = 0; w < width; w++) { BitmapData ImgData = gfxImage.LockBits(new Rectangle(new Point(w, h), new Size(1, 1)), System.Drawing.Imaging.ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format8bppIndexed); byte b = System.Runtime.InteropServices.Marshal.ReadByte(ImgData.Scan0); System.Runtime.InteropServices.Marshal.WriteByte(ImgData.Scan0, (byte)(data[pixel])); gfxImage.UnlockBits(ImgData); pixel++; } } reader.Close(); }