public static void WritePigBitmapToPng(string fn, byte[] pal, Pig pig, PigBitmap bmp) { int w = bmp.width, h = bmp.height; byte[] img = new byte[w * h * 4]; int dstOfs = 0; byte[] img8 = pig.GetBitmap(bmp); int srcOfs = 0; int th = bmp.height; for (int y = 0; y < th; y++) { for (int x = 0; x < w; x++) { int colorIdx = img8[srcOfs++] * 3; img[dstOfs++] = pal[colorIdx + 2]; img[dstOfs++] = pal[colorIdx + 1]; img[dstOfs++] = pal[colorIdx]; img[dstOfs++] = (colorIdx >= 254 * 3 ? (byte)0 : (byte)255); } } WritePng(fn, img, w, h); }