diff --git a/Userland/Libraries/LibGfx/CMYKBitmap.cpp b/Userland/Libraries/LibGfx/CMYKBitmap.cpp index b587bc570e6..bc1822035b0 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 e24bb2913ea..497e864616c 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 470f6d4ee8d..7147e040a73 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 }; }