Skip to content

Commit

Permalink
Fix image size calculation when SMask dimensions do not match image
Browse files Browse the repository at this point in the history
Closes [Bug]: Ghostscript rasterizing failed #1351
  • Loading branch information
jbarlow83 committed Jul 16, 2024
1 parent 08be7c8 commit 621d6a0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/ocrmypdf/pdfinfo/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,18 @@ def __init__(
pim = PdfImage(pdfimage)
else:
raise ValueError("Either pdfimage or inline must be set")
self._width = pim.width
self._height = pim.height
if pim.obj.get(Name.SMask, None) is not None:
# SMask is pretty much an alpha channel, but in PDF it's possible
# for channel to have different dimensions than the image
# itself. Some PDF writers use this to create a grayscale stencil
# mask. For our purposes, the effective size is the size of the
# larger component (image or smask).
smask = pim.obj[Name.SMask]
self._width = max(smask.get(Name.Width, 0), pim.width)
self._height = max(smask.get(Name.Height, 0), pim.height)
else:
self._width = pim.width
self._height = pim.height

# If /ImageMask is true, then this image is a stencil mask
# (Images that draw with this stencil mask will have a reference to
Expand Down Expand Up @@ -1071,7 +1081,6 @@ def page_dpi_profile(self) -> PageResolutionProfile | None:

arg_max_dpi = image_dpis.index(max_dpi)
max_area_ratio = image_areas[arg_max_dpi] / total_drawn_area

return PageResolutionProfile(
weighted_dpi,
max_dpi,
Expand Down

0 comments on commit 621d6a0

Please sign in to comment.