public static Bitmap GetBitmap(IImageProvider piggyFile, Palette palette, int index, int scale = 1) { int[] rgbTable = new int[256]; PIGImage image = piggyFile.Bitmaps[index]; int newWidth = (int)(image.Width * scale); int newHeight = (int)(image.Height * scale); Bitmap bitmap = new Bitmap(newWidth, newHeight); for (int j = 0; j < 256; j++) { rgbTable[j] = ((j == 255 ? 0 : 255) << 24) + (palette[j].R << 16) + (palette[j].G << 8) + (palette[j].B); } int[] rgbData = new int[newWidth * newHeight]; byte[] rawData = image.GetData(); switch (scale) { case 1: Scale1X(image.Width, image.Height, newWidth, newHeight, rawData, rgbData, rgbTable); break; case 2: Scale2X(image.Width, image.Height, newWidth, newHeight, rawData, rgbData, rgbTable); break; case 3: Scale3X(image.Width, image.Height, newWidth, newHeight, rawData, rgbData, rgbTable); break; case 4: Scale4X(image.Width, image.Height, newWidth, newHeight, rawData, rgbData, rgbTable); break; } BitmapData bits = bitmap.LockBits(new Rectangle(0, 0, newWidth, newHeight), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(rgbData, 0, bits.Scan0, newWidth * newHeight); bitmap.UnlockBits(bits); return(bitmap); }
public static void SetAverageColor(PIGImage image, byte[] palette) { byte[] data = image.GetData(); int c; int totalr = 0, totalg = 0, totalb = 0; for (int i = 0; i < data.Length; i++) { c = data[i] * 3; totalr += palette[c]; totalg += palette[c + 1]; totalb += palette[c + 2]; } totalr /= data.Length; totalg /= data.Length; totalb /= data.Length; image.AverageIndex = (byte)GetNearestColorIndex(totalr, totalg, totalb, palette); }
public static void SetAverageColor(PIGImage image, Palette palette) { byte[] data = image.GetData(); byte c; int totalr = 0, totalg = 0, totalb = 0; for (int i = 0; i < data.Length; i++) { c = data[i]; totalr += palette[c, 0]; totalg += palette[c, 1]; totalb += palette[c, 2]; } totalr /= data.Length; totalg /= data.Length; totalb /= data.Length; image.AverageIndex = (byte)palette.GetNearestColor(totalr, totalg, totalb); }
public static Bitmap GetBitmap(PIGImage image, Palette palette) { Bitmap bitmap = new Bitmap(image.Width, image.Height); int[] rgbData = new int[image.Width * image.Height]; byte[] rawData = image.GetData(); byte b; for (int i = 0; i < rawData.Length; i++) { b = rawData[i]; rgbData[i] = ((b == 255 ? 0 : 255) << 24) + (palette[b].R << 16) + (palette[b].G << 8) + (palette[b].B); } BitmapData bits = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.WriteOnly, PixelFormat.Format32bppArgb); System.Runtime.InteropServices.Marshal.Copy(rgbData, 0, bits.Scan0, image.Width * image.Height); bitmap.UnlockBits(bits); return(bitmap); }
public int WriteBBM(PIGImage image, Palette palette, bool overrideAlpha, BinaryWriter bw) { int bytesWritten = 0; byte[] data = image.GetData(); //write FORM header bw.Write(0x4D524F46); //will update later on long loc = bw.BaseStream.Position; bw.Write(0); //Note it as a PBM bw.Write(0x204D4250); bytesWritten += 4; //Write the BITMAP header bw.Write(0x44484D42);// 42 4D 48 44 WriteInt32BE(bw, 0x14); bytesWritten += 8; //resolution WriteUInt16BE(bw, (ushort)image.Width); WriteUInt16BE(bw, (ushort)image.Height); //offset WriteUInt16BE(bw, 0); WriteUInt16BE(bw, 0); //num planes bw.Write((byte)8); //mask mode if (overrideAlpha) { bw.Write((byte)2); } else { bw.Write((byte)0); } //no compression bw.Write((byte)0); //pad bw.Write((byte)0); //transparent index WriteUInt16BE(bw, 255); //aspect ratio bw.Write((byte)5); bw.Write((byte)6); //screen size WriteUInt16BE(bw, 320); WriteUInt16BE(bw, 200); bytesWritten += 0x14; //Write the palette //43 4D 41 50 bw.Write(0x50414D43); WriteInt32BE(bw, 0x300); bytesWritten += 8; for (int c = 0; c < 256; c++) { bw.Write(palette[c, 0]); bw.Write(palette[c, 1]); bw.Write(palette[c, 2]); } bytesWritten += 0x300; //Write the body //42 4F 44 59 int pixelcount = image.Width * image.Height; bool alignmentHack = false; //To this day I still have no clue what this hack is supposed to be for, //but the original game's IFF decoder seems to require it. It causes problems in DPaint, //which confuses me. if (image.Width % 2 != 0) { pixelcount = (image.Width + 1) * image.Height; alignmentHack = true; } bw.Write(0x59444F42); WriteInt32BE(bw, pixelcount); bytesWritten += 8; if (alignmentHack) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { bw.Write(data[y * image.Width + x]); } bw.Write((byte)0); } } else { for (int i = 0; i < pixelcount; i++) { bw.Write(data[i]); } } bytesWritten += pixelcount; long end = bw.BaseStream.Position; bw.BaseStream.Seek(loc, SeekOrigin.Begin); WriteInt32BE(bw, bytesWritten); bw.BaseStream.Seek(end, SeekOrigin.Begin); return(bytesWritten + 8); }
public int WriteABMFrame(PIGImage image, Palette palette, BinaryWriter bw) { int bytesWritten = 0; byte[] data = image.GetData(); //write FORM header bw.Write(0x4D524F46); //will update later on long loc = bw.BaseStream.Position; bw.Write(0); //Note it as a PBM bw.Write(0x204D4250); bytesWritten += 4; //Write the ANHD header //41 4E 48 44 bw.Write(0x44484E41); WriteInt32BE(bw, 40); bytesWritten += 8; //delta mode bw.Write((byte)0); //mask bw.Write((byte)0); //wh for xor mode WriteInt16BE(bw, (short)image.Width); WriteInt16BE(bw, (short)image.Height); //xy for xor mode WriteInt16BE(bw, 0); WriteInt16BE(bw, 0); //abstime WriteInt32BE(bw, 1); //reltime WriteInt32BE(bw, 1); //interleave bw.Write((byte)0); //pad bw.Write((byte)0); //flags WriteInt32BE(bw, 0); //pad bw.Write(new byte[16]); bytesWritten += 40; //Write the body //42 4F 44 59 int pixelcount = image.Width * image.Height; bool alignmentHack = false; //To this day I still have no clue what this hack is supposed to be for, //but the original game's IFF decoder seems to require it. It causes problems in DPaint, //which confuses me. if (image.Width % 2 != 0) { pixelcount = (image.Width + 1) * image.Height; alignmentHack = true; } bw.Write(0x59444F42); WriteInt32BE(bw, pixelcount); bytesWritten += 8; if (alignmentHack) { for (int y = 0; y < image.Height; y++) { for (int x = 0; x < image.Width; x++) { bw.Write(data[y * image.Width + x]); } bw.Write((byte)0); } } else { for (int i = 0; i < pixelcount; i++) { bw.Write(data[i]); } } bytesWritten += pixelcount; long end = bw.BaseStream.Position; bw.BaseStream.Seek(loc, SeekOrigin.Begin); WriteInt32BE(bw, bytesWritten); bw.BaseStream.Seek(end, SeekOrigin.Begin); return(bytesWritten + 8); }