Add image copy functions to QImage.

This commit is contained in:
H. Utku Maden 2024-05-01 16:52:20 +03:00
parent d831c9b72d
commit 3418537b43

@ -1,5 +1,4 @@
using System; using System;
namespace Quik.Media namespace Quik.Media
{ {
public abstract class QImage : IDisposable public abstract class QImage : IDisposable
@ -62,5 +61,60 @@ namespace Quik.Media
Depth = depth; Depth = depth;
ImagePtr = ptr; ImagePtr = ptr;
} }
public unsafe void CopyTo(QImageLock destination, int x, int y)
{
if (
Width + x > destination.Width ||
Height + y > destination.Height)
{
throw new Exception("Image falls outside the bounds of the destination.");
}
else if (Format != destination.Format)
{
throw new Exception("Image formats must be the same.");
}
int bpp = Format.BytesPerPixel();
for (int i = 0; i < Height; i++)
{
IntPtr srcPtr = (IntPtr)((long)ImagePtr + i * bpp);
long dstPos = x + i * destination.Width;
IntPtr dstPtr = (IntPtr)((long)destination.ImagePtr + dstPos * bpp);
Buffer.MemoryCopy((void*)srcPtr, (void*)dstPtr, Width * bpp, Width * bpp);
}
}
public unsafe void ExtractFrom(QImageLock destination, int x, int y, int width, int height)
{
if (
width != destination.Width ||
height != destination.Height)
{
throw new Exception("Destination is not the same size as the subregion.");
}
else if (x + width > Width || y + height > Height)
{
throw new Exception("The subregion is larger than this image.");
}
else if (Format != destination.Format)
{
throw new Exception("Image formats must be the same.");
}
int bpp = Format.BytesPerPixel();
for (int i = 0; i < height; i++)
{
long srcPos = x + y * i;
IntPtr srcPtr = (IntPtr)((long)ImagePtr + srcPos * bpp);
long dstPos = i * destination.Width;
IntPtr dstPtr = (IntPtr)((long)destination.ImagePtr + dstPos * bpp);
Buffer.MemoryCopy((void*)srcPtr, (void*)dstPtr, width * bpp, width * bpp);
}
}
} }
} }