From 3418537b43bcf1b81316cb8a89cd550d5f405cb2 Mon Sep 17 00:00:00 2001 From: "H. Utku Maden" Date: Wed, 1 May 2024 16:52:20 +0300 Subject: [PATCH] Add image copy functions to QImage. --- Quik/Media/QImage.cs | 56 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) diff --git a/Quik/Media/QImage.cs b/Quik/Media/QImage.cs index fc3975f..4cd8bdc 100644 --- a/Quik/Media/QImage.cs +++ b/Quik/Media/QImage.cs @@ -1,5 +1,4 @@ using System; - namespace Quik.Media { public abstract class QImage : IDisposable @@ -62,5 +61,60 @@ namespace Quik.Media Depth = depth; 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); + } + } } } \ No newline at end of file