Skip to content

Commit

Permalink
Fix fitz.Pixmap colorspace conversion.
Browse files Browse the repository at this point in the history
Constructor `Pixmap(colorspace, pixmap)` previously always incorrectly
converted via a greyscale proofing colorspace.

Fixed similar bug in `Page.insert_image()` if we process the pixmap.

Also fixed creation of `Colorspace` from a `mupdf.FzColorspace` - this would
previously always created a RGB `Colorspace`.

Address #3058
  • Loading branch information
julian-smith-artifex-com committed Jan 18, 2024
1 parent ae0188d commit d183c2f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2396,7 +2396,9 @@ class Colorspace:

def __init__(self, type_):
"""Supported are GRAY, RGB and CMYK."""
if type_ == CS_GRAY:
if isinstance( type_, mupdf.FzColorspace):
self.this = type_
elif type_ == CS_GRAY:
self.this = mupdf.FzColorspace(mupdf.FzColorspace.Fixed_GRAY)
elif type_ == CS_CMYK:
self.this = mupdf.FzColorspace(mupdf.FzColorspace.Fixed_CMYK)
Expand Down Expand Up @@ -7759,8 +7761,8 @@ def _insert_image(self,
else:
pm = mupdf.fz_convert_pixmap(
arg_pix,
mupdf.FzColorspace(0),
mupdf.FzColorspace(0),
mupdf.FzColorspace(),
mupdf.FzColorspace(),
mupdf.FzDefaultColorspaces(None),
mupdf.FzColorParams(),
1,
Expand Down Expand Up @@ -9429,7 +9431,7 @@ def __init__(self, *args):
self.this = mupdf.fz_convert_pixmap(
spix,
cs,
mupdf.FzColorspace(0),
mupdf.FzColorspace(),
mupdf.FzDefaultColorspaces(None),
mupdf.FzColorParams(),
1
Expand Down
Binary file added tests/resources/test_3058.pdf
Binary file not shown.
20 changes: 20 additions & 0 deletions tests/test_pixmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,23 @@ def product(x, y):
pix.save(os.path.abspath(f'{__file__}/../../tests/test_3050_out.png'))
assert digest1 != digest0
assert digest1 == digest_expected

def test_3058():
doc = fitz.Document(os.path.abspath(f'{__file__}/../../tests/resources/test_3058.pdf'))
images = doc[0].get_images(full=True)
pix = fitz.Pixmap(doc, 17)

# First bug was that `pix.colorspace` was DeviceRGB.
assert str(pix.colorspace) == 'Colorspace(CS_CMYK) - DeviceCMYK'

pix = fitz.Pixmap(fitz.csRGB, pix)
assert str(pix.colorspace) == 'Colorspace(CS_RGB) - DeviceRGB'

# Second bug was that the image was converted to RGB via greyscale proofing
# color space, so image contained only shades of grey. This compressed
# easily to a .png file, so we crudely check the bug is fixed by looking at
# size of .png file.
path = os.path.abspath(f'{__file__}/../../tests/test_3058_out.png')
pix.save(path)
s = os.path.getsize(path)
assert 1800000 < s < 2600000, f'Unexpected size of {path}: {s}'

0 comments on commit d183c2f

Please sign in to comment.