From 4a162c4e2d63998801100551a52dde83c7838fec Mon Sep 17 00:00:00 2001 From: dirk Date: Sun, 30 Oct 2016 11:24:21 +0100 Subject: [PATCH] Added methods to copy data from the PixelAccessor to a PixelRow. --- src/ImageSharp/Image/PixelAccessor.cs | 61 +++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/src/ImageSharp/Image/PixelAccessor.cs b/src/ImageSharp/Image/PixelAccessor.cs index 6e0e45d5e..58568db72 100644 --- a/src/ImageSharp/Image/PixelAccessor.cs +++ b/src/ImageSharp/Image/PixelAccessor.cs @@ -157,6 +157,27 @@ namespace ImageSharp } } + public void CopyTo(PixelRow row, int sourceY) + { + switch (row.ComponentOrder) + { + case ComponentOrder.BGR: + this.CopyToBGR(row, sourceY, Math.Min(row.Width, this.Width)); + break; + case ComponentOrder.BGRA: + this.CopyToBGRA(row, sourceY, Math.Min(row.Width, this.Width)); + break; + case ComponentOrder.RGB: + this.CopyToRGB(row, sourceY, Math.Min(row.Width, this.Width)); + break; + case ComponentOrder.RGBA: + this.CopyToRGBA(row, sourceY, Math.Min(row.Width, this.Width)); + break; + default: + throw new NotSupportedException(); + } + } + /// /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// @@ -258,6 +279,46 @@ namespace ImageSharp } } + protected virtual void CopyToBGR(PixelRow row, int sourceY, int width) + { + int offset = 0; + for (int x = 0; x < width; x++) + { + this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.BGR); + offset += 3; + } + } + + protected virtual void CopyToBGRA(PixelRow row, int sourceY, int width) + { + int offset = 0; + for (int x = 0; x < width; x++) + { + this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.BGRA); + offset += 4; + } + } + + protected virtual void CopyToRGB(PixelRow row, int sourceY, int width) + { + int offset = 0; + for (int x = 0; x < width; x++) + { + this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.RGB); + offset += 3; + } + } + + protected virtual void CopyToRGBA(PixelRow row, int sourceY, int width) + { + int offset = 0; + for (int x = 0; x < width; x++) + { + this[x, sourceY].ToBytes(row.Data, offset, ComponentOrder.RGBA); + offset += 4; + } + } + protected byte* GetRowPointer(int targetY) { return this.pixelsBase + ((targetY * this.Width) * Unsafe.SizeOf());