git.iko.soy/typeable/pandoc
on master help sign in
ImageSize: default to DPI 72 if the format specifies DPI of 0.

This shouldn't happen, in general, but it can happen with JPEGs that don't conform to the spec. Having a DPI of 0 will blow up size calculations (division by 0). Closes #6880.

commit2f110265ff4a1dc429d58e7401d68968e42b6db1 authorJohn MacFarlane <jgm@berkeley.edu> dateMon 23 Nov 2020, 09:38 (6 years ago) parentf7d80b4
1 file changed+7−1
src/Text/Pandoc/ImageSize.hs+7−1
src/Text/Pandoc/ImageSize.hs+7−1
@@ -120,7 +120,7 @@ findSvgTag :: ByteString -> Bool
120120findSvgTag img = "<svg" `B.isInfixOf` img || "<SVG" `B.isInfixOf` img
121121
122122imageSize :: WriterOptions -> ByteString -> Either T.Text ImageSize
123−imageSize opts img =
123+imageSize opts img = checkDpi <$>
124124 case imageType img of
125125 Just Png -> mbToEither "could not determine PNG size" $ pngSize img
126126 Just Gif -> mbToEither "could not determine GIF size" $ gifSize img
@@ -132,6 +132,12 @@ imageSize opts img =
132132 Nothing -> Left "could not determine image type"
133133 where mbToEither msg Nothing = Left msg
134134 mbToEither _ (Just x) = Right x
135+ -- see #6880, some defective JPEGs may encode dpi 0, so default to 72
136+ -- if that value is 0
137+ checkDpi size =
138+ size{ dpiX = if dpiX size == 0 then 72 else dpiX size
139+ , dpiY = if dpiY size == 0 then 72 else dpiY size }
140+
135141
136142defaultSize :: (Integer, Integer)
137143defaultSize = (72, 72)