Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fitz.Pixmap colorspace conversion. #3060

Merged
merged 1 commit into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}'
Loading