Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly handle x,y offset for Bitmap.CopyPixels #15510

Merged
merged 3 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/Avalonia.Base/Media/Imaging/Bitmap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,20 +182,23 @@ public void Save(Stream stream, int? quality = null)
private protected unsafe void CopyPixelsCore(PixelRect sourceRect, IntPtr buffer, int bufferSize, int stride,
ILockedFramebuffer fb)
{
if (Format == null)
throw new NotSupportedException("CopyPixels is not supported for this bitmap type");

if ((sourceRect.Width <= 0 || sourceRect.Height <= 0) && (sourceRect.X != 0 || sourceRect.Y != 0))
throw new ArgumentOutOfRangeException(nameof(sourceRect));

if (sourceRect.X < 0 || sourceRect.Y < 0)
throw new ArgumentOutOfRangeException(nameof(sourceRect));

if (sourceRect.Width <= 0)
sourceRect = sourceRect.WithWidth(PixelSize.Width);
if (sourceRect.Height <= 0)
sourceRect = sourceRect.WithHeight(PixelSize.Height);

if (sourceRect.Right > PixelSize.Width || sourceRect.Bottom > PixelSize.Height)
throw new ArgumentOutOfRangeException(nameof(sourceRect));

int minStride = checked(((sourceRect.Width * fb.Format.BitsPerPixel) + 7) / 8);
if (stride < minStride)
throw new ArgumentOutOfRangeException(nameof(stride));
Expand All @@ -204,14 +207,16 @@ private protected unsafe void CopyPixelsCore(PixelRect sourceRect, IntPtr buffer
if (minBufferSize > bufferSize)
throw new ArgumentOutOfRangeException(nameof(bufferSize));

var offsetX = checked(((sourceRect.X * Format.Value.BitsPerPixel) + 7) / 8);

for (var y = 0; y < sourceRect.Height; y++)
{
var srcAddress = fb.Address + fb.RowBytes * y;
var srcAddress = fb.Address + fb.RowBytes * (sourceRect.Y + y) + offsetX;
var dstAddress = buffer + stride * y;
Unsafe.CopyBlock(dstAddress.ToPointer(), srcAddress.ToPointer(), (uint)minStride);
}
}

public virtual void CopyPixels(PixelRect sourceRect, IntPtr buffer, int bufferSize, int stride)
{
if (
Expand Down
29 changes: 29 additions & 0 deletions tests/Avalonia.RenderTests/Media/BitmapTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -246,5 +246,34 @@ public unsafe void CopyPixelsShouldWorkForNonTranscodedBitmaps()
bmp.CopyPixels(default, new IntPtr(pCopyTo), data.Length, stride);
Assert.Equal(data, copyTo);
}

[Fact]
public unsafe void Should_CopyPixels_With_Source_Rect()
{
var size = 80;
var partSize = 20;
var bitmap = new RenderTargetBitmap(new PixelSize(size, size));

using (var context = bitmap.CreateDrawingContext())
{
context.FillRectangle(Brushes.Black,
new Rect(0, 0, bitmap.PixelSize.Width, bitmap.PixelSize.Height));
context.FillRectangle(Brushes.White, new Rect(partSize, partSize, partSize, partSize));
}

var bpp = bitmap.Format!.Value.BitsPerPixel / 8;
var buffer = new byte[partSize * partSize * bpp];

fixed (byte* pointer = buffer)
{
bitmap.CopyPixels(new PixelRect(partSize, partSize, partSize, partSize), (IntPtr)pointer,
buffer.Length, partSize * bpp);
}

foreach (var t in buffer)
{
Assert.Equal(byte.MaxValue, t);
}
}
}
}
Loading