From ec34739a85c25917d2078fe8265f85150d97ce15 Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Mon, 13 Jul 2015 13:03:46 -0700 Subject: [PATCH] Crop rectangle in image.copyChannel --- lime/graphics/utils/ImageDataUtil.hx | 43 +++++++++++++++----- project/src/graphics/utils/ImageDataUtil.cpp | 43 +++++++++++++++----- 2 files changed, 66 insertions(+), 20 deletions(-) diff --git a/lime/graphics/utils/ImageDataUtil.hx b/lime/graphics/utils/ImageDataUtil.hx index 6413ebd06..547b1d04f 100644 --- a/lime/graphics/utils/ImageDataUtil.hx +++ b/lime/graphics/utils/ImageDataUtil.hx @@ -128,19 +128,47 @@ class ImageDataUtil { #end { + var width = sourceRect.width; + var height = sourceRect.height; + + if (destPoint.x + width > image.width) { + + width = image.width - destPoint.x; + + } + + if (sourceRect.x + width > sourceImage.width) { + + width = sourceImage.width - sourceRect.x; + + } + + if (destPoint.y + height > image.height) { + + height = image.height - destPoint.y; + + } + + if (sourceRect.y + height > sourceImage.height) { + + height = sourceImage.height - sourceRect.y; + + } + + if (width <= 0 || height <= 0) return; + var srcStride = Std.int (sourceImage.buffer.width * 4); var srcPosition = Std.int (((sourceRect.x + sourceImage.offsetX) * 4) + (srcStride * (sourceRect.y + sourceImage.offsetY)) + srcIdx); - var srcRowOffset = srcStride - Std.int (4 * (sourceRect.width + sourceImage.offsetX)); - var srcRowEnd = Std.int (4 * (sourceRect.x + sourceImage.offsetX + sourceRect.width)); + var srcRowOffset = srcStride - Std.int (4 * width); + var srcRowEnd = Std.int (4 * (sourceRect.x + sourceImage.offsetX + width)); var srcData = sourceImage.buffer.data; var destStride = Std.int (image.buffer.width * 4); var destPosition = Std.int (((destPoint.x + image.offsetX) * 4) + (destStride * (destPoint.y + image.offsetY)) + destIdx); - var destRowOffset = destStride - Std.int (4 * (sourceRect.width + image.offsetX)); - var destRowEnd = Std.int (4 * (destPoint.x + image.offsetX + sourceRect.width)); + var destRowOffset = destStride - Std.int (4 * width); var destData = image.buffer.data; - var length = Std.int (sourceRect.width * sourceRect.height); + var length = Std.int (width * height); for (i in 0...length) { @@ -152,11 +180,6 @@ class ImageDataUtil { if ((srcPosition % srcStride) > srcRowEnd) { srcPosition += srcRowOffset; - - } - - if ((destPosition % destStride) > destRowEnd) { - destPosition += destRowOffset; } diff --git a/project/src/graphics/utils/ImageDataUtil.cpp b/project/src/graphics/utils/ImageDataUtil.cpp index 20bfd4307..3636e1488 100644 --- a/project/src/graphics/utils/ImageDataUtil.cpp +++ b/project/src/graphics/utils/ImageDataUtil.cpp @@ -87,19 +87,47 @@ namespace lime { void ImageDataUtil::CopyChannel (Image* image, Image* sourceImage, Rectangle* sourceRect, Vector2* destPoint, int srcChannel, int destChannel) { + int width = sourceRect->width; + int height = sourceRect->height; + + if (destPoint->x + width > image->width) { + + width = image->width - destPoint->x; + + } + + if (sourceRect->x + width > sourceImage->width) { + + width = sourceImage->width - sourceRect->x; + + } + + if (destPoint->y + height > image->height) { + + height = image->height - destPoint->y; + + } + + if (sourceRect->y + height > sourceImage->height) { + + height = sourceImage->height - sourceRect->y; + + } + + if (width <= 0 || height <= 0) return; + int srcStride = sourceImage->buffer->Stride (); int srcPosition = ((sourceRect->x + sourceImage->offsetX) * 4) + (srcStride * (sourceRect->y + sourceImage->offsetY)) + srcChannel; - int srcRowOffset = srcStride - int (4 * (sourceRect->width + sourceImage->offsetX)); - int srcRowEnd = 4 * (sourceRect->x + sourceImage->offsetX + sourceRect->width); + int srcRowOffset = srcStride - int (4 * width); + int srcRowEnd = 4 * (sourceRect->x + sourceImage->offsetX + width); uint8_t* srcData = (uint8_t*)sourceImage->buffer->data->Data (); int destStride = image->buffer->Stride (); int destPosition = ((destPoint->x + image->offsetX) * 4) + (destStride * (destPoint->y + image->offsetY)) + destChannel; - int destRowOffset = destStride - int (4 * (sourceRect->width + image->offsetX)); - int destRowEnd = 4 * (destPoint->x + image->offsetX + sourceRect->width); + int destRowOffset = destStride - int (4 * width); uint8_t* destData = (uint8_t*)image->buffer->data->Data (); - int length = sourceRect->width * sourceRect->height; + int length = width * height; for (int i = 0; i < length; i++) { @@ -111,11 +139,6 @@ namespace lime { if ((srcPosition % srcStride) > srcRowEnd) { srcPosition += srcRowOffset; - - } - - if ((destPosition % destStride) > destRowEnd) { - destPosition += destRowOffset; }