From 4be00558c983c26c268a51ca663bc0ed45e00d04 Mon Sep 17 00:00:00 2001 From: "Jorj X. McKie" Date: Thu, 2 Jan 2025 11:54:47 -0400 Subject: [PATCH] Address 4186 JPEG with CMYK color space need inversion of the pixel colors. --- src/__init__.py | 11 +++++++++++ src/extra.i | 3 +++ 2 files changed, 14 insertions(+) diff --git a/src/__init__.py b/src/__init__.py index 39698dbd7..7031f9c86 100644 --- a/src/__init__.py +++ b/src/__init__.py @@ -4208,6 +4208,7 @@ def extract_image(self, xref): raise ValueError( "not an image") o = mupdf.pdf_dict_geta(obj, PDF_NAME('SMask'), PDF_NAME('Mask')) + cs_string = mupdf.pdf_dict_geta(obj, PDF_NAME('ColorSpace'), PDF_NAME('CS')).pdf_to_name() if o.m_internal: smask = mupdf.pdf_to_num(o) @@ -4248,6 +4249,12 @@ def extract_image(self, xref): mupdf.FzColorParams(mupdf.fz_default_color_params), ) ext = "png" + elif ext == "jpeg" and cs_string == "DeviceCMYK": + # avoid incorrect JPG by inverting pixel color + img = mupdf.pdf_load_image(pdf, obj) + res = mupdf.fz_new_buffer_from_image_as_jpeg( + img, mupdf.FzColorParams(mupdf.fz_default_color_params),95,1) + ext = "jpeg" else: img = mupdf.fz_new_image_from_buffer(res) @@ -16555,6 +16562,10 @@ def JM_make_image_block(block, block_dict): else: buf = mupdf.fz_new_buffer_from_image_as_png(image, mupdf.FzColorParams()) ext = "png" + if ext == "jpeg" and n == 4: # JPEG and DeviceCMYK + buf = mupdf.fz_new_buffer_from_image_as_jpeg( + image,mupdf.FzColorParams(mupdf.fz_default_color_params), + 95, 1) bytes_ = JM_BinFromBuffer(buf) block_dict[ dictkey_width] = w block_dict[ dictkey_height] = h diff --git a/src/extra.i b/src/extra.i index f8f88a6c5..2431f079a 100644 --- a/src/extra.i +++ b/src/extra.i @@ -3483,6 +3483,9 @@ void JM_make_image_block(fz_stext_block *block, PyObject *block_dict) buf = freebuf = fz_new_buffer_from_image_as_png(ctx, image, fz_default_color_params); ext = "png"; } + if (n == 4 && strcmp(ext, "jpeg") == 0) { // JPEG CMYK needs another step + buf = freebuf = fz_new_buffer_from_image_as_jpeg(ctx, image, fz_default_color_params, 95, 1); + } bytes = JM_BinFromBuffer(buf); } fz_always(ctx) {