1
0
Fork 0
forked from josch/img2pdf

Interlaced PNGs cannot be directly embedded but have to be re-encoded

This commit is contained in:
Johannes 'josch' Schauer 2018-03-24 18:59:02 +01:00
parent 7244d2c6ed
commit d9a6c9db03
Signed by untrusted user: josch
GPG key ID: F2CBA5C78FBD83E1

View file

@ -759,6 +759,8 @@ def read_images(rawdata, colorspace, first_frame_only=False):
# depending on the input format, determine whether to pass the raw
# image or the zlib compressed color information
# JPEG and JPEG2000 can be embedded into the PDF as-is
if imgformat == ImageFormat.JPEG or imgformat == ImageFormat.JPEG2000:
color, ndpi, imgwidthpx, imgheightpx = get_imgmetadata(
imgdata, imgformat, default_dpi, colorspace, rawdata)
@ -770,12 +772,22 @@ def read_images(rawdata, colorspace, first_frame_only=False):
raise JpegColorspaceError("jpeg can't have an alpha channel")
im.close()
return [(color, ndpi, imgformat, rawdata, imgwidthpx, imgheightpx, [])]
elif imgformat == ImageFormat.PNG:
# We can directly embed the IDAT chunk of PNG images if the PNG is not
# interlaced
#
# PIL does not provide the information whether a PNG was stored interlaced
# or not. Thus, we retrieve that info manually by looking at byte 13 in the
# IHDR chunk. We know where to find that in the file because the IHDR chunk
# must be the first chunk.
if imgformat == ImageFormat.PNG and rawdata[28] == 0:
color, ndpi, imgwidthpx, imgheightpx = get_imgmetadata(
imgdata, imgformat, default_dpi, colorspace, rawdata)
pngidat, palette = parse_png(rawdata)
return [(color, ndpi, imgformat, pngidat, imgwidthpx, imgheightpx, palette)]
else:
# Everything else has to be encoded
result = []
img_page_count = 0
# loop through all frames of the image (example: multipage TIFF)