public static STim Decode8(byte[] picbin, byte[] palbin, int tbw, int cx, int cy) { var bitmap = new Bitmap(cx, cy, PixelFormat.Format8bppIndexed); tbw /= 2; byte[] array = Reform8.Decode8(picbin, tbw, Math.Max(1, picbin.Length / 8192 / tbw)); BitmapData bitmapData = bitmap.LockBits(Rectangle.FromLTRB(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { int val = bitmapData.Stride * bitmapData.Height; Marshal.Copy(array, 0, bitmapData.Scan0, Math.Min(array.Length, val)); } finally { bitmap.UnlockBits(bitmapData); } ColorPalette palette = bitmap.Palette; int num = 0; var array2 = new byte[1024]; for (int i = 0; i < 256; i++) { int num2 = KHcv8pal.repl(i); Array.Copy(palbin, 4 * i, array2, 4 * num2, 4); } Array.Copy(array2, 0, palbin, 0, 1024); for (int j = 0; j < 256; j++) { palette.Entries[j] = CUtil.NoGamma( Color.FromArgb(AcUt.GetA(palbin[num + 4 * j + 3]) ^ (j & 1), Math.Min(255, palbin[num + 4 * j] + 1), Math.Min(255, palbin[num + 4 * j + 1] + 1), Math.Min(255, palbin[num + 4 * j + 2] + 1)), 0.5f); } bitmap.Palette = palette; return(new STim(bitmap)); }
/// <summary> /// Function that will parse an IMGD file /// </summary> /// <param name="si">MemoryStream of the file</param> /// <returns></returns> public static PicIMGD TakeIMGD(MemoryStream si) { si.Position = 0L; if (si.ReadByte() != 73) { throw new NotSupportedException("!IMGD"); } if (si.ReadByte() != 77) { throw new NotSupportedException("!IMGD"); } if (si.ReadByte() != 71) { throw new NotSupportedException("!IMGD"); } if (si.ReadByte() != 68) { throw new NotSupportedException("!IMGD"); } var binaryReader = new BinaryReader(si); binaryReader.ReadInt32(); int num = binaryReader.ReadInt32(); int count = binaryReader.ReadInt32(); int num2 = binaryReader.ReadInt32(); int count2 = binaryReader.ReadInt32(); si.Position = 28L; int num3 = binaryReader.ReadUInt16(); int num4 = binaryReader.ReadUInt16(); si.Position = 38L; int num5 = binaryReader.ReadUInt16(); si.Position = 60L; int num6 = binaryReader.ReadByte(); bool flag = num6 == 7; si.Position = num; byte[] array = binaryReader.ReadBytes(count); si.Position = num2; byte[] array2 = binaryReader.ReadBytes(count2); int num7 = num3; int num8 = num4; if (num5 == 19) { int num9 = num3 / 128; int num10 = num4 / 64; int bw = num9; int bh = num10; byte[] array3 = flag ? Reform8.Decode8(Reform32.Encode32(array, num9, num10), bw, bh) : array; var bitmap = new Bitmap(num7, num8, PixelFormat.Format8bppIndexed); BitmapData bitmapData = bitmap.LockBits(Rectangle.FromLTRB(0, 0, num7, num8), ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed); try { Marshal.Copy(array3, 0, bitmapData.Scan0, array3.Length); } finally { bitmap.UnlockBits(bitmapData); } var array4 = new byte[8192]; Array.Copy(array2, 0, array4, 0, Math.Min(8192, array2.Length)); byte[] array5 = array4; ColorPalette palette = bitmap.Palette; for (int i = 0; i < 256; i++) { int num11 = i; int num12 = KHcv8pal_swap34.repl(i); palette.Entries[num12] = Color.FromArgb(Math.Min(255, array5[4 * num11 + 3] * 2), array5[4 * num11], array5[4 * num11 + 1], array5[4 * num11 + 2]); } bitmap.Palette = palette; return(new PicIMGD(bitmap)); } if (num5 == 20) { int num13 = num3 / 128; int num14 = num4 / 128; int bw2 = num13; int bh2 = num14; byte[] array6 = flag ? Reform4.Decode4(Reform32.Encode32(array, num13, num14), bw2, bh2) : HLUt.Swap(array); var bitmap2 = new Bitmap(num7, num8, PixelFormat.Format4bppIndexed); BitmapData bitmapData2 = bitmap2.LockBits(Rectangle.FromLTRB(0, 0, num7, num8), ImageLockMode.WriteOnly, PixelFormat.Format4bppIndexed); try { Marshal.Copy(array6, 0, bitmapData2.Scan0, array6.Length); } finally { bitmap2.UnlockBits(bitmapData2); } var array7 = new byte[8192]; Array.Copy(array2, 0, array7, 0, Math.Min(8192, array2.Length)); byte[] array8 = array7; ColorPalette palette2 = bitmap2.Palette; for (int j = 0; j < 16; j++) { int num15 = j; int num16 = j; palette2.Entries[num16] = Color.FromArgb(Math.Min(255, array8[4 * num15 + 3] * 2), array8[4 * num15], array8[4 * num15 + 1], array8[4 * num15 + 2]); } bitmap2.Palette = palette2; return(new PicIMGD(bitmap2)); } throw new NotSupportedException("v26 = " + num5.ToString("X")); }