Skip to content

Truly independent web browser

LibGfx: Encode CMYK JPEGs as YCCK

Andreas Kling6261c2891ded
diff --git a/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp b/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
index e633c976006..c976195985b 100644
--- a/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
+++ b/Libraries/LibGfx/ImageFormats/JPEGWriter.cpp
@@ -61,6 +61,13 @@ ErrorOr<void> JPEGWriter::encode_impl(Stream& stream, auto const& bitmap, Option
// that calls setjmp() have indeterminate values after the longjmp().
Vector<u8> buffer;
MemoryDestinationManager dest_manager(buffer);
+
+ // NB: We store CMYK data inverted, as Photoshop does. This is the convention our decoder (and every browser)
+ // expects from CMYK JPEGs with an Adobe marker, which libjpeg writes for YCCK.
+ Vector<JSAMPLE> inverted_row;
+ if (color_space == ColorSpace::CMYK)
+ TRY(inverted_row.try_resize(bitmap.size().width() * 4));
+
struct JPEGErrorManager jerr;
struct jpeg_compress_struct cinfo {};
@@ -99,7 +106,8 @@ ErrorOr<void> JPEGWriter::encode_impl(Stream& stream, auto const& bitmap, Option
}
jpeg_set_defaults(&cinfo);
- jpeg_set_colorspace(&cinfo, JCS_YCbCr);
+ // NB: libjpeg can only convert CMYK input to YCCK, not to YCbCr.
+ jpeg_set_colorspace(&cinfo, color_space == ColorSpace::CMYK ? JCS_YCCK : JCS_YCbCr);
jpeg_set_quality(&cinfo, options.quality, TRUE);
jpeg_start_compress(&cinfo, TRUE);
@@ -111,6 +119,11 @@ ErrorOr<void> JPEGWriter::encode_impl(Stream& stream, auto const& bitmap, Option
while (cinfo.next_scanline < cinfo.image_height) {
auto const* row_ptr = reinterpret_cast<u8 const*>(bitmap.scanline(cinfo.next_scanline));
JSAMPROW row_pointer = const_cast<JSAMPROW>(row_ptr);
+ if (color_space == ColorSpace::CMYK) {
+ for (size_t i = 0; i < inverted_row.size(); ++i)
+ inverted_row[i] = 255 - row_ptr[i];
+ row_pointer = inverted_row.data();
+ }
jpeg_write_scanlines(&cinfo, &row_pointer, 1);
}
diff --git a/Tests/LibGfx/TestImageWriter.cpp b/Tests/LibGfx/TestImageWriter.cpp
index fdef26f93ec..496b12715d3 100644
--- a/Tests/LibGfx/TestImageWriter.cpp
+++ b/Tests/LibGfx/TestImageWriter.cpp
@@ -6,6 +6,7 @@
#include <AK/MemoryStream.h>
#include <LibGfx/Bitmap.h>
+#include <LibGfx/CMYKBitmap.h>
#include <LibGfx/ImageFormats/BMPLoader.h>
#include <LibGfx/ImageFormats/BMPWriter.h>
#include <LibGfx/ImageFormats/JPEGLoader.h>
@@ -108,6 +109,35 @@ TEST_CASE(test_jpeg)
(void)TRY_OR_FAIL((get_roundtrip_bitmap<Gfx::JPEGWriter, Gfx::JPEGImageDecoderPlugin>(TRY_OR_FAIL(create_test_rgb_bitmap()))));
}
+TEST_CASE(test_jpeg_cmyk)
+{
+ auto bitmap = TRY_OR_FAIL(Gfx::CMYKBitmap::create_with_size({ 47, 33 }));
+ for (int y = 0; y < bitmap->size().height(); ++y)
+ for (int x = 0; x < bitmap->size().width(); ++x)
+ bitmap->scanline(y)[x] = { static_cast<u8>(x * 5), static_cast<u8>(y * 7), static_cast<u8>(x + y), 30 };
+
+ AllocatingMemoryStream stream;
+ TRY_OR_FAIL(Gfx::JPEGWriter::encode(stream, *bitmap));
+ auto encoded_data = TRY_OR_FAIL(stream.read_until_eof());
+
+ auto decoder = TRY_OR_FAIL(Gfx::JPEGImageDecoderPlugin::create(encoded_data));
+ EXPECT_EQ(decoder->natural_frame_format(), Gfx::NaturalFrameFormat::CMYK);
+ auto decoded = TRY_OR_FAIL(decoder->cmyk_frame());
+ EXPECT_EQ(decoded->size(), bitmap->size());
+
+ // Lossy compression and chroma subsampling allow some error, but every channel must keep its meaning.
+ for (int y = 0; y < bitmap->size().height(); ++y) {
+ for (int x = 0; x < bitmap->size().width(); ++x) {
+ auto expected = bitmap->scanline(y)[x];
+ auto actual = decoded->scanline(y)[x];
+ EXPECT(abs(expected.c - actual.c) <= 16);
+ EXPECT(abs(expected.m - actual.m) <= 16);
+ EXPECT(abs(expected.y - actual.y) <= 16);
+ EXPECT(abs(expected.k - actual.k) <= 16);
+ }
+ }
+}
+
TEST_CASE(test_jpeg_icc_profile)
{
// The profile is larger than one APP2 marker can hold, so libjpeg has to split it.