From c72a05ab4a93398804004fb8dda9d20f9c1ddc1a Mon Sep 17 00:00:00 2001 From: Joshua Granick Date: Fri, 5 May 2017 14:39:59 -0700 Subject: [PATCH] Clean up the blit row code and add a Haxe alternative --- lime/graphics/utils/ImageDataUtil.hx | 33 +++++++++++++++----- project/include/graphics/ImageBuffer.h | 1 - project/src/graphics/ImageBuffer.cpp | 14 +-------- project/src/graphics/utils/ImageDataUtil.cpp | 21 ++++++------- 4 files changed, 35 insertions(+), 34 deletions(-) diff --git a/lime/graphics/utils/ImageDataUtil.hx b/lime/graphics/utils/ImageDataUtil.hx index d69d95108..1e91b412e 100644 --- a/lime/graphics/utils/ImageDataUtil.hx +++ b/lime/graphics/utils/ImageDataUtil.hx @@ -186,23 +186,40 @@ class ImageDataUtil { var destFormat = image.buffer.format; var sourcePremultiplied = sourceImage.buffer.premultiplied; var destPremultiplied = image.buffer.premultiplied; + var sourceBitsPerPixel = sourceImage.buffer.bitsPerPixel; + var destBitsPerPixel = image.buffer.bitsPerPixel; var sourcePosition, destPosition, sourcePixel:RGBA; if (!mergeAlpha || !sourceImage.transparent) { - for (y in 0...destView.height) { + if (sourceFormat == destFormat && sourcePremultiplied == destPremultiplied && sourceBitsPerPixel == destBitsPerPixel) { - sourcePosition = sourceView.row (y); - destPosition = destView.row (y); + for (y in 0...destView.height) { + + sourcePosition = sourceView.row (y); + destPosition = destView.row (y); + + destData.buffer.blit (destPosition, sourceData.buffer, sourcePosition, destView.width * destBitsPerPixel); + + } - for (x in 0...destView.width) { + } else { + + for (y in 0...destView.height) { - sourcePixel.readUInt8 (sourceData, sourcePosition, sourceFormat, sourcePremultiplied); - sourcePixel.writeUInt8 (destData, destPosition, destFormat, destPremultiplied); + sourcePosition = sourceView.row (y); + destPosition = destView.row (y); - sourcePosition += 4; - destPosition += 4; + for (x in 0...destView.width) { + + sourcePixel.readUInt8 (sourceData, sourcePosition, sourceFormat, sourcePremultiplied); + sourcePixel.writeUInt8 (destData, destPosition, destFormat, destPremultiplied); + + sourcePosition += 4; + destPosition += 4; + + } } diff --git a/project/include/graphics/ImageBuffer.h b/project/include/graphics/ImageBuffer.h index 64860e310..d286a2cc2 100644 --- a/project/include/graphics/ImageBuffer.h +++ b/project/include/graphics/ImageBuffer.h @@ -20,7 +20,6 @@ namespace lime { ~ImageBuffer (); void Blit (const unsigned char *data, int x, int y, int width, int height); - void BlitRow (const unsigned char *data, int sourcePosition, int destPosition, int sourceW); void Resize (int width, int height, int bitsPerPixel = 32); int Stride (); value Value (); diff --git a/project/src/graphics/ImageBuffer.cpp b/project/src/graphics/ImageBuffer.cpp index 00ad4b4f3..de535ec17 100644 --- a/project/src/graphics/ImageBuffer.cpp +++ b/project/src/graphics/ImageBuffer.cpp @@ -94,19 +94,7 @@ namespace lime { } - } - - - void ImageBuffer::BlitRow (const unsigned char *data, int sourcePosition, int destPosition, int sourceW) { - - int stride = (sourceW * (((bitsPerPixel + 3) & ~0x3) >> 3)); - - unsigned char *bytes = this->data->Data (); - - memcpy (&bytes[destPosition], &data[sourcePosition], stride); - - } - + } void ImageBuffer::Resize (int width, int height, int bitsPerPixel) { diff --git a/project/src/graphics/utils/ImageDataUtil.cpp b/project/src/graphics/utils/ImageDataUtil.cpp index a32e830f2..b8ac2c1dd 100644 --- a/project/src/graphics/utils/ImageDataUtil.cpp +++ b/project/src/graphics/utils/ImageDataUtil.cpp @@ -124,26 +124,23 @@ namespace lime { bool sourcePremultiplied = sourceImage->buffer->premultiplied; bool destPremultiplied = image->buffer->premultiplied; + int sourceBitsPerPixel = sourceImage->buffer->bitsPerPixel; + int destBitsPerPixel = image->buffer->bitsPerPixel; if (!mergeAlpha || !sourceImage->buffer->transparent) { - if(sourceFormat == destFormat && sourcePremultiplied == destPremultiplied) { + if (sourceFormat == destFormat && sourcePremultiplied == destPremultiplied && sourceBitsPerPixel == destBitsPerPixel) { - if (false == (destView.x < 0 || destView.x + sourceView.width > image->width || destView.y < 0 || destView.y + destView.height > image->height)) { + for (int y = 0; y < destView.height; y++) { - for (int y = 0; y < destView.height; y++) { - - sourcePosition = sourceView.Row (y); - destPosition = destView.Row (y); - - image->buffer->BlitRow(sourceData, sourcePosition, destPosition, sourceView.width); - - } + sourcePosition = sourceView.Row (y); + destPosition = destView.Row (y); + + memcpy (&destData[destPosition], &sourceData[sourcePosition], destView.width * destBitsPerPixel); } - } - else { + } else { for (int y = 0; y < destView.height; y++) {