ImageSize: use exif width and height when available.

After the move to JuicyPixels, we were getting incorrect
width and heigh information for some images (see #6936, test-3.jpg).

The correct information was encoded in Exif tags that
JuicyPixels seemed to ignore. So we check these first
before looking at the Width and Height identified by
JuicyPixels.

Closes #6936.
This commit is contained in:
John MacFarlane 2020-12-14 09:39:07 -08:00
parent c43e2dc0f4
commit 39153ea6e2

View file

@ -50,6 +50,7 @@ import qualified Data.Text.Encoding as TE
import Control.Applicative
import qualified Data.Attoparsec.ByteString.Char8 as A
import qualified Codec.Picture.Metadata as Metadata
import qualified Codec.Picture.Metadata.Exif as Exif
import Codec.Picture (decodeImageWithMetadata)
-- quick and dirty functions to get image sizes
@ -300,8 +301,16 @@ getSize img =
Left e -> Left (T.pack e)
Right (_, meta) -> do
pxx <- maybe (Left "Could not determine width") Right $
-- first look for exif image width, then width
(Metadata.lookup
(Metadata.Exif (Exif.TagUnknown 0xA002)) meta >>=
exifDataToWord) <|>
Metadata.lookup Metadata.Width meta
pxy <- maybe (Left "Could not determine height") Right $
-- first look for exif image height, then height
(Metadata.lookup
(Metadata.Exif (Exif.TagUnknown 0xA003)) meta >>=
exifDataToWord) <|>
Metadata.lookup Metadata.Height meta
dpix <- maybe (Right 72) Right $ Metadata.lookup Metadata.DpiX meta
dpiy <- maybe (Right 72) Right $ Metadata.lookup Metadata.DpiY meta
@ -310,6 +319,10 @@ getSize img =
, pxY = fromIntegral pxy
, dpiX = fromIntegral dpix
, dpiY = fromIntegral dpiy }
where
exifDataToWord (Exif.ExifLong x) = Just $ fromIntegral x
exifDataToWord (Exif.ExifShort x) = Just $ fromIntegral x
exifDataToWord _ = Nothing
svgSize :: WriterOptions -> ByteString -> Maybe ImageSize