/// <summary> /// Packed Bitmap decoder. Calls from ConvertCustomToBitmap if the bitmap is packed, not snes. /// </summary> /// <param name="_buffer">Bitmap data</param> /// <param name="_palette">palette data</param> /// <param name="bpp">bits-per-pixel</param> /// <returns>Bitmap</returns> private static Bitmap PackedDecode(byte[] _buffer, Color[] _palette, int bpp) { IBitformat format = SnesGFX.AvaiableFormats[Settings.Default.Codec]; // let's get the width int width = bitmapInfo.Width; int height = _buffer.Length; if (format.Type == BitformatType.BITFORMAT_PACKED) { height = height * 8 / format.BitsPerPixel; } height /= width; //int packedtype; //if (!Options.PreserveWidth) //{ // width = 128; //} //if (Options.PackPacked) //{ // switch (Settings.Default.Codec) // { // case 4: // packedtype = 2; // height *= 4; // break; // case 5: // packedtype = 4; // height *= 2; // break; // case 6: // case 7: // packedtype = 8; // break; // default: // packedtype = 2; // height /= 4; // break; // } //} //else //{ // packedtype = 8; //} //height /= width; Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette pal = bmp.Palette; int x = 0; while (x < _palette.Length) { pal.Entries[x] = _palette[x++]; } bmp.Palette = pal; byte[] output = format.Decode(_buffer); //ExGraphics.CodecInfo codec = new SuperFX.ExGraphics.CodecInfo(-1); //codec.Decode = true; //codec.SysName = string.Format("Pack{0}BPP", packedtype); //codec.Data = _buffer; //ExGraphics.StartCodec = codec; var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); IntPtr ptr = bmpData.Scan0; int bytes = Math.Abs(bmpData.Stride) * height; Marshal.Copy(output, 0, ptr, bytes); bmp.UnlockBits(bmpData); return(bmp); }
/// <summary> /// Converts a SNES/PACKED BIT-MAP to Bitmap. /// </summary> /// <param name="_buffer">The raw data</param> /// <param name="_palette">Palette</param> /// <param name="bpp">bits-per-pixel</param> /// <returns>The Bitmap</returns> public static Bitmap ConvertCustomToBitmap(byte[] _buffer, Color[] _palette, int bpp) { if (SnesGFX.AvaiableFormats[Settings.Default.Codec].Type != BitformatType.BITFORMAT_PLANAR) { return(PackedDecode(_buffer, _palette, bpp)); } long a8x8 = 0; long size = _buffer.Length; a8x8 = size / ((64 * bpp) / 8); int height = (int)((a8x8 / 16) * 8); int width = (int)((a8x8 > 16) ? 128 : a8x8 * 8); if (height == 0) { height = 0; } if (height % 8 != 0) { height += 8 - (height % 8); } Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed); ColorPalette pal = bmp.Palette; int x = 0; while (x < _palette.Length) { pal.Entries[x] = _palette[x++]; } if (Options.AllowTransparency) { pal.Entries[0] = Color.Transparent; } bmp.Palette = pal; IBitformat format = SnesGFX.AvaiableFormats[Settings.Default.Codec]; byte[] output = format.Decode(_buffer); var bmpData = bmp.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); IntPtr ptr = bmpData.Scan0; int bytes = Math.Abs(bmpData.Stride) * height; //fixed (byte* ptr2 = output) //{ // byte[] rgbValues = SuperFX.ExGraphics.a8x8ToTilemap(ptr2, width, codec.output.Length); // Marshal.Copy(rgbValues, 0, ptr, bytes); // rgbValues = null; //} Marshal.Copy(output, 0, ptr, bytes); bmp.UnlockBits(bmpData); return(bmp); }