Skip to content

Commit

Permalink
Add some helpers to BitmapBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
YoshiRulz committed Jul 15, 2024
1 parent d7e9668 commit 9a5a75e
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions src/BizHawk.Bizware.Graphics/BitmapBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public unsafe class BitmapBuffer : IDisposable
private GCHandle CurrLockHandle;
private BitmapData CurrLock;

/// <summary>same as <see cref="Pixels"/> (<see cref="PixelFormat.Format32bppArgb">A8R8G8B8</see>)</summary>
public Span<int> AsSpan()
=> Pixels;

/// <exception cref="InvalidOperationException">already locked</exception>
/// <remarks>TODO add read/write semantic, for wraps</remarks>
public BitmapData LockBits()
Expand Down Expand Up @@ -213,19 +217,24 @@ public BitmapBuffer Trim(out int xofs, out int yofs)
return new(0, 0);
}

var w = maxx - minx + 1;
var h = maxy - miny + 1;
var bbRet = new BitmapBuffer(w, h);
for (var y = 0; y < h; y++)
{
for (var x = 0; x < w; x++)
{
bbRet.SetPixel(x, y, GetPixel(x + minx, y + miny));
}
}

xofs = minx;
yofs = miny;
return Copy(region: new(x: minx, y: miny, width: maxx - minx + 1, height: maxy - miny + 1));
}

public BitmapBuffer Copy()
=> new(width: Width, height: Height, pixels: Pixels.AsSpan().ToArray());

/// <remarks>TODO surely there's a better implementation --yoshi</remarks>
public BitmapBuffer Copy(Rectangle region)
{
BitmapBuffer bbRet = new(region.Size);
var miny = region.Top;
var minx = region.Left;
for (int y = 0, h = region.Height; y < h; y++) for (int x = 0, w = region.Width; x < w; x++)
{
bbRet.SetPixel(x, y, GetPixel(x + minx, y + miny));
}
return bbRet;
}

Expand Down Expand Up @@ -535,6 +544,8 @@ private static int NextHigher(int k)
return candidate;
}

public bool SequenceEqual(BitmapBuffer other)
=> Width == other.Width && Height == other.Height && AsSpan().SequenceEqual(other.AsSpan());

/// <summary>
/// Dumps this BitmapBuffer to a new System.Drawing.Bitmap
Expand Down

0 comments on commit 9a5a75e

Please sign in to comment.