From 97746fb574e79060e7a672102c30604eb34d20f8 Mon Sep 17 00:00:00 2001 From: Jamie Mansfield Date: Thu, 14 Aug 2025 00:48:33 +0100 Subject: [PATCH] LibGfx: Fix colour with Photoshop JPEG files using CMYK This is based on the original functionality Lucus wrote prior to using libjpeg. Co-authored-by: Lucas CHOLLET --- Libraries/LibGfx/ImageFormats/JPEGLoader.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index 886ec6d259c..143e9ba8444 100644 --- a/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -145,6 +145,24 @@ ErrorOr JPEGLoadingContext::decode() } } } + + // Photoshop writes inverted CMYK data (i.e. Photoshop's 0 should be 255). We convert this + // to expected values. + if (cinfo.saw_Adobe_marker) { + for (int i = 0; i < cmyk_bitmap->size().height(); ++i) { + auto* line = cmyk_bitmap->scanline(i); + + for (int j = 0; j < cmyk_bitmap->size().width(); ++j) { + auto const& cmyk = line[j]; + line[j] = { + static_cast(255 - cmyk.c), + static_cast(255 - cmyk.m), + static_cast(255 - cmyk.y), + static_cast(255 - cmyk.k), + }; + } + } + } } JOCTET* icc_data_ptr = nullptr;