From 09b2b3539bf779d88827d0df3c7e9cad2fd15845 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Thu, 18 Jan 2024 16:59:42 -0500 Subject: [PATCH] LibGfx/JPEG+CMYKBitmap: Extract the CMYK to RGB conversion code Right now, the JPEG decoder is the only one supporting CMYK, but that won't last for long. So, let's move the conversion to CMYKBitmap. --- Userland/Libraries/LibGfx/CMYKBitmap.cpp | 17 ++++++++++++++++ Userland/Libraries/LibGfx/CMYKBitmap.h | 5 +++++ .../LibGfx/ImageFormats/JPEGLoader.cpp | 20 +------------------ 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/Userland/Libraries/LibGfx/CMYKBitmap.cpp b/Userland/Libraries/LibGfx/CMYKBitmap.cpp index b587bc570e..bc1822035b 100644 --- a/Userland/Libraries/LibGfx/CMYKBitmap.cpp +++ b/Userland/Libraries/LibGfx/CMYKBitmap.cpp @@ -15,4 +15,21 @@ ErrorOr> CMYKBitmap::create_with_size(IntSize const& s return adopt_ref(*new CMYKBitmap(size, move(data))); } +ErrorOr> CMYKBitmap::to_low_quality_rgb() const +{ + if (!m_rgb_bitmap) { + m_rgb_bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_size.width(), m_size.height() })); + + for (int y = 0; y < m_size.height(); ++y) { + for (int x = 0; x < m_size.width(); ++x) { + auto const& cmyk = scanline(y)[x]; + u8 k = 255 - cmyk.k; + m_rgb_bitmap->scanline(y)[x] = Color((255 - cmyk.c) * k / 255, (255 - cmyk.m) * k / 255, (255 - cmyk.y) * k / 255).value(); + } + } + } + + return m_rgb_bitmap; +} + } diff --git a/Userland/Libraries/LibGfx/CMYKBitmap.h b/Userland/Libraries/LibGfx/CMYKBitmap.h index e24bb2913e..497e864616 100644 --- a/Userland/Libraries/LibGfx/CMYKBitmap.h +++ b/Userland/Libraries/LibGfx/CMYKBitmap.h @@ -9,6 +9,7 @@ #include #include #include +#include #include namespace Gfx { @@ -32,6 +33,8 @@ public: [[nodiscard]] CMYK* begin(); [[nodiscard]] CMYK* end(); + ErrorOr> to_low_quality_rgb() const; + private: CMYKBitmap(IntSize const& size, ByteBuffer data) : m_size(size) @@ -41,6 +44,8 @@ private: IntSize m_size; ByteBuffer m_data; + + mutable RefPtr m_rgb_bitmap; }; inline CMYK* CMYKBitmap::scanline(int y) diff --git a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp index 470f6d4ee8..7147e040a7 100644 --- a/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp +++ b/Userland/Libraries/LibGfx/ImageFormats/JPEGLoader.cpp @@ -1694,24 +1694,6 @@ static void invert_colors_for_adobe_images(JPEGLoadingContext const& context, Ve } } -static ErrorOr cmyk_to_rgb(JPEGLoadingContext& context) -{ - VERIFY(context.cmyk_bitmap); - VERIFY(!context.bitmap); - - context.bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { context.frame.width, context.frame.height })); - - for (int y = 0; y < context.frame.height; ++y) { - for (int x = 0; x < context.frame.width; ++x) { - auto const& cmyk = context.cmyk_bitmap->scanline(y)[x]; - u8 k = 255 - cmyk.k; - context.bitmap->scanline(y)[x] = Color((255 - cmyk.c) * k / 255, (255 - cmyk.m) * k / 255, (255 - cmyk.y) * k / 255).value(); - } - } - - return {}; -} - static void ycck_to_cmyk(Vector& macroblocks) { // 7 - Conversions between colour encodings @@ -2026,7 +2008,7 @@ ErrorOr JPEGImageDecoderPlugin::frame(size_t index, Option } if (m_context->cmyk_bitmap && !m_context->bitmap) - TRY(cmyk_to_rgb(*m_context)); + return ImageFrameDescriptor { TRY(m_context->cmyk_bitmap->to_low_quality_rgb()), 0 }; return ImageFrameDescriptor { m_context->bitmap, 0 }; }