From 86732069c74abeb8ce7e5372feae17f546474ba1 Mon Sep 17 00:00:00 2001 From: reuk Date: Thu, 15 May 2025 19:27:23 +0100 Subject: [PATCH] Image: Respect pixelStride when converting between image formats Previously, this code assumed that a single channel image would always have a pixel stride of 1. For image types where this assumption did not hold, such as OpenGL, this function would produce incorrect results. --- modules/juce_graphics/images/juce_Image.cpp | 22 +++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/modules/juce_graphics/images/juce_Image.cpp b/modules/juce_graphics/images/juce_Image.cpp index b52931c03d..2aef206168 100644 --- a/modules/juce_graphics/images/juce_Image.cpp +++ b/modules/juce_graphics/images/juce_Image.cpp @@ -768,7 +768,7 @@ Image Image::convertedToFormat (PixelFormat newFormat) const auto type = image->createType(); Image newImage (type->create (newFormat, w, h, false)); - if (newFormat == SingleChannel) + if (newImage.getFormat() == SingleChannel) { if (! hasAlphaChannel()) { @@ -781,26 +781,28 @@ Image Image::convertedToFormat (PixelFormat newFormat) const for (int y = 0; y < h; ++y) { - auto src = reinterpret_cast (srcData.getLinePointer (y)); - auto dst = destData.getLinePointer (y); - for (int x = 0; x < w; ++x) - dst[x] = src[x].getAlpha(); + { + auto* dstPtr = reinterpret_cast (destData.getPixelPointer (x, y)); + auto* srcPtr = reinterpret_cast (srcData.getPixelPointer (x, y)); + dstPtr->set (*srcPtr); + } } } } - else if (image->pixelFormat == SingleChannel && newFormat == Image::ARGB) + else if (image->pixelFormat == SingleChannel && newImage.getFormat() == ARGB) { const BitmapData destData (newImage, { w, h }, BitmapData::writeOnly); const BitmapData srcData (*this, { w, h }, BitmapData::readOnly); for (int y = 0; y < h; ++y) { - auto src = reinterpret_cast (srcData.getLinePointer (y)); - auto dst = reinterpret_cast (destData.getLinePointer (y)); - for (int x = 0; x < w; ++x) - dst[x].set (src[x]); + { + auto* dstPtr = reinterpret_cast (destData.getPixelPointer (x, y)); + auto* srcPtr = reinterpret_cast (srcData.getPixelPointer (x, y)); + dstPtr->set (*srcPtr); + } } } else