private static ColorPalette LoadPalette(Stream stream) { stream.Seek(0, SeekOrigin.Begin); BinaryReader reader = new BinaryReader(stream); ColorPalette pal = new ColorPalette(); for (int i = 0; i < 256; i++) { pal.colors[i] = Color.FromArgb(reader.ReadByte(), reader.ReadByte(), reader.ReadByte()); } return pal; }
private Bitmap SimpleRender(int width, int height, byte[] data, ColorPalette palette, ImageType type) { Bitmap image = new Bitmap(width, height); System.Drawing.Imaging.BitmapData bmd = image.LockBits(new Rectangle(0, 0, image.Width, image.Height), System.Drawing.Imaging.ImageLockMode.WriteOnly, image.PixelFormat); for (int y = 0; y < bmd.Height; y++) { byte[] row = new byte[Marshal.SizeOf(bmd.Scan0 + (y * bmd.Stride))]; Marshal.Copy((bmd.Scan0 + (y * bmd.Stride)), row, 0, Marshal.SizeOf(bmd.Scan0 + (y * bmd.Stride))); for (int x = 0; x < bmd.Width; x++) { int colorIndex = 0; if (type == ImageType.EPF) { colorIndex = data[x * height + y]; } else { colorIndex = data[y * width + x]; } if (colorIndex > 0) { if (bmd.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb) { row[x * 4] = palette[colorIndex].B; row[x * 4 + 1] = palette[colorIndex].G; row[x * 4 + 2] = palette[colorIndex].R; row[x * 4 + 3] = palette[colorIndex].A; } else if (bmd.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb) { row[x * 3] = palette[colorIndex].B; row[x * 3 + 1] = palette[colorIndex].G; row[x * 3 + 2] = palette[colorIndex].R; } else if (bmd.PixelFormat == System.Drawing.Imaging.PixelFormat.Format16bppRgb555) { ushort colorWORD = (ushort)(((palette[colorIndex].R & 248) << 7) + ((palette[colorIndex].G & 248) << 2) + (palette[colorIndex].B >> 3)); row[x * 2] = (byte)(colorWORD % 256); row[x * 2 + 1] = (byte)(colorWORD / 256); } else if (bmd.PixelFormat == System.Drawing.Imaging.PixelFormat.Format16bppRgb565) { ushort colorWORD = (ushort)(((palette[colorIndex].R & 248) << 8) + ((palette[colorIndex].G & 252) << 3) + (palette[colorIndex].B >> 3)); row[x * 2] = (byte)(colorWORD % 256); row[x * 2 + 1] = (byte)(colorWORD / 256); } } } } image.UnlockBits(bmd); if (type == ImageType.EPF) { image.RotateFlip(RotateFlipType.Rotate90FlipX); } return image; }
public Bitmap RenderTile(byte[] tileData, ColorPalette palette) { return SimpleRender(Tileset.TileWidth, Tileset.TileHeight, tileData, palette, ImageType.Tile); }
public Bitmap RenderImage(MPFFrame mpf, ColorPalette palette) { return SimpleRender(mpf.Width, mpf.Height, mpf.RawData, palette, ImageType.MPF); }
public Bitmap RenderImage(EPFFrame epf, ColorPalette palette) { return SimpleRender(epf.Width, epf.Height, epf.RawData, palette, ImageType.EPF); }
public Bitmap RenderImage(HPF hpf, ColorPalette palette) { return SimpleRender(hpf.Width, hpf.Height, hpf.RawData, palette, ImageType.HPF); }