Skip to content

Commit

Permalink
disable bitmap buffer padding
Browse files Browse the repository at this point in the history
  • Loading branch information
Aytackydln committed Nov 4, 2024
1 parent 36bd86c commit 577c068
Showing 1 changed file with 14 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ namespace AuroraRgb.Bitmaps.GdiPlus;
public sealed class GdiPartialCopyBitmapReader : IBitmapReader
{
private const int SmallestBufferLength = 32;
private static readonly Dictionary<int, BitmapData> Bitmaps = new(20);
private static readonly Dictionary<Size, BitmapData> Bitmaps = new(20);

// ReSharper disable once CollectionNeverQueried.Local //to keep reference
private static readonly Dictionary<int, int[]> BitmapBuffers = new(20);
private static readonly Dictionary<Size, int[]> BitmapBuffers = new(20);

private readonly Bitmap _bitmap;
private readonly RectangleF _dimension;
Expand Down Expand Up @@ -46,18 +46,19 @@ public ref readonly Color GetRegionColor(Rectangle rectangle)
return ref _transparentColor;

var area = rectangle.Width * rectangle.Height;
var bufferArea = Math.Max(area, SmallestBufferLength);
if (!Bitmaps.TryGetValue(bufferArea, out var buff))
//var size = Math.Max(area, SmallestBufferLength);
var size = rectangle.Size;
if (!Bitmaps.TryGetValue(size, out var buff))
{
buff = CreateBuffer(rectangle);
Bitmaps[bufferArea] = buff;
Bitmaps[size] = buff;
}

if (area < SmallestBufferLength)
{
// clear the padded array
Array.Copy(_emptySmallestBuffer, 0, BitmapBuffers[SmallestBufferLength], 0, _emptySmallestBuffer.Length);
}
//if (area < SmallestBufferLength)
//{
// // clear the padded array
// Array.Copy(_emptySmallestBuffer, 0, BitmapBuffers[SmallestBufferLength], 0, _emptySmallestBuffer.Length);
//}

var srcData = _bitmap.LockBits(
rectangle,
Expand Down Expand Up @@ -166,9 +167,9 @@ private static long SumVector256(in Vector256<int> vector)
private static BitmapData CreateBuffer(in Rectangle rectangle)
{
var area = rectangle.Width * rectangle.Height;
var bufferArea = Math.Max(area, SmallestBufferLength);
var bitmapBuffer = new int[bufferArea];
BitmapBuffers[bufferArea] = bitmapBuffer;
//var bufferArea = Math.Max(area, SmallestBufferLength);
var bitmapBuffer = new int[area];
BitmapBuffers[rectangle.Size] = bitmapBuffer;

var buffer = Marshal.AllocHGlobal(bitmapBuffer.Length * sizeof(int));
Marshal.Copy(bitmapBuffer, 0, buffer, bitmapBuffer.Length);
Expand All @@ -185,12 +186,5 @@ private static BitmapData CreateBuffer(in Rectangle rectangle)

public void Dispose()
{
foreach (var bitmapData in Bitmaps.Values)
{
Marshal.FreeHGlobal(bitmapData.Scan0);
}

Bitmaps.Clear();
BitmapBuffers.Clear();
}
}

0 comments on commit 577c068

Please sign in to comment.