@@ -1,4 +1,5 @@
1{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
1{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
2+{-# OPTIONS_GHC -fno-warn-type-defaults #-}
3 Copyright (C) 2011-2014 John MacFarlane <jgm@berkeley.edu>
4 Copyright (C) 2011-2014 John MacFarlane <jgm@berkeley.edu>
@@ -38,8 +39,11 @@ import Control.Monad
38import Data.Bits
39import Data.Bits
39import Data.Binary
40import Data.Binary
40import Data.Binary.Get
41import Data.Binary.Get
41−import Text.Pandoc.Shared (safeRead)
42+import Text.Pandoc.Shared (safeRead, hush)
42import qualified Data.Map as M
43import qualified Data.Map as M
44+import Text.Pandoc.Compat.Except
45+import Control.Monad.Trans
46+import Data.Maybe (fromMaybe)
44-- quick and dirty functions to get image sizes
48-- quick and dirty functions to get image sizes
45-- algorithms borrowed from wwwis.pl
49-- algorithms borrowed from wwwis.pl
@@ -64,7 +68,7 @@ imageType img = case B.take 4 img of
65 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
69 | (B.take 4 $ B.drop 1 $ B.dropWhile (/=' ') img) == "EPSF"
66 -> return Eps
70 -> return Eps
67− _ -> fail "Unknown image type"
71+ _ -> (hush . Left) "Unknown image type"
69imageSize :: ByteString -> Maybe ImageSize
73imageSize :: ByteString -> Maybe ImageSize
70imageSize img = do
74imageSize img = do
@@ -114,7 +118,7 @@ pngSize img = do
114 ([w1,w2,w3,w4,h1,h2,h3,h4] :: [Integer]) -> return
118 ([w1,w2,w3,w4,h1,h2,h3,h4] :: [Integer]) -> return
115 ((shift w1 24) + (shift w2 16) + (shift w3 8) + w4,
119 ((shift w1 24) + (shift w2 16) + (shift w3 8) + w4,
116 (shift h1 24) + (shift h2 16) + (shift h3 8) + h4)
120 (shift h1 24) + (shift h2 16) + (shift h3 8) + h4)
117− _ -> fail "PNG parse error"
121+ _ -> (hush . Left) "PNG parse error"
118 let (dpix, dpiy) = findpHYs rest''
122 let (dpix, dpiy) = findpHYs rest''
119 return $ ImageSize { pxX = x, pxY = y, dpiX = dpix, dpiY = dpiy }
123 return $ ImageSize { pxX = x, pxY = y, dpiX = dpix, dpiY = dpiy }
@@ -143,7 +147,7 @@ gifSize img = do
143 dpiX = 72,
147 dpiX = 72,
144 dpiY = 72
148 dpiY = 72
146− _ -> fail "GIF parse error"
150+ _ -> (hush . Left) "GIF parse error"
148jpegSize :: ByteString -> Maybe ImageSize
152jpegSize :: ByteString -> Maybe ImageSize
149jpegSize img = do
153jpegSize img = do
@@ -174,36 +178,37 @@ findJfifSize bs = do
174 Just (c,bs'') | c >= '\xc0' && c <= '\xc3' -> do
178 Just (c,bs'') | c >= '\xc0' && c <= '\xc3' -> do
175 case map fromIntegral $ unpack $ B.take 4 $ B.drop 3 bs'' of
179 case map fromIntegral $ unpack $ B.take 4 $ B.drop 3 bs'' of
176 [h1,h2,w1,w2] -> return (shift w1 8 + w2, shift h1 8 + h2)
180 [h1,h2,w1,w2] -> return (shift w1 8 + w2, shift h1 8 + h2)
177− _ -> fail "JPEG parse error"
181+ _ -> (hush . Left) "JPEG parse error"
178 Just (_,bs'') -> do
182 Just (_,bs'') -> do
179 case map fromIntegral $ unpack $ B.take 2 bs'' of
183 case map fromIntegral $ unpack $ B.take 2 bs'' of
180 [c1,c2] -> do
184 [c1,c2] -> do
181 let len = shift c1 8 + c2
185 let len = shift c1 8 + c2
182 -- skip variables
186 -- skip variables
183 findJfifSize $ B.drop len bs''
187 findJfifSize $ B.drop len bs''
184− _ -> fail "JPEG parse error"
188+ _ -> (hush . Left) "JPEG parse error"
185− Nothing -> fail "Did not find length record"
189+ Nothing -> (hush . Left) "Did not find length record"
187exifSize :: ByteString -> Maybe ImageSize
191exifSize :: ByteString -> Maybe ImageSize
188−exifSize bs = runGet (Just <$> exifHeader bl) bl
192+exifSize bs = hush . runGet header $ bl
189 where bl = BL.fromChunks [bs]
193 where bl = BL.fromChunks [bs]
194+ header = runExceptT $ exifHeader bl
190-- NOTE: It would be nicer to do
195-- NOTE: It would be nicer to do
191-- runGet ((Just <$> exifHeader) <|> return Nothing)
196-- runGet ((Just <$> exifHeader) <|> return Nothing)
192-- which would prevent pandoc from raising an error when an exif header can't
197-- which would prevent pandoc from raising an error when an exif header can't
193-- be parsed. But we only get an Alternative instance for Get in binary 0.6,
198-- be parsed. But we only get an Alternative instance for Get in binary 0.6,
194-- and binary 0.5 ships with ghc 7.6.
199-- and binary 0.5 ships with ghc 7.6.
196−exifHeader :: BL.ByteString -> Get ImageSize
201+exifHeader :: BL.ByteString -> ExceptT String Get ImageSize
197exifHeader hdr = do
202exifHeader hdr = do
198− _app1DataSize <- getWord16be
203+ _app1DataSize <- lift getWord16be
199− exifHdr <- getWord32be
204+ exifHdr <- lift getWord32be
200− unless (exifHdr == 0x45786966) $ fail "Did not find exif header"
205+ unless (exifHdr == 0x45786966) $ throwError "Did not find exif header"
201− zeros <- getWord16be
206+ zeros <- lift getWord16be
202− unless (zeros == 0) $ fail "Expected zeros after exif header"
207+ unless (zeros == 0) $ throwError "Expected zeros after exif header"
203 -- beginning of tiff header -- we read whole thing to use
208 -- beginning of tiff header -- we read whole thing to use
204 -- in getting data from offsets:
209 -- in getting data from offsets:
205 let tiffHeader = BL.drop 8 hdr
210 let tiffHeader = BL.drop 8 hdr
206− byteAlign <- getWord16be
211+ byteAlign <- lift getWord16be
207 let bigEndian = byteAlign == 0x4d4d
212 let bigEndian = byteAlign == 0x4d4d
208 let (getWord16, getWord32, getWord64) =
213 let (getWord16, getWord32, getWord64) =
209 if bigEndian
214 if bigEndian
@@ -213,17 +218,17 @@ exifHeader hdr = do
213 num <- getWord32
218 num <- getWord32
214 den <- getWord32
219 den <- getWord32
215 return $ fromIntegral num / fromIntegral den
220 return $ fromIntegral num / fromIntegral den
216− tagmark <- getWord16
221+ tagmark <- lift getWord16
217− unless (tagmark == 0x002a) $ fail "Failed alignment sanity check"
222+ unless (tagmark == 0x002a) $ throwError "Failed alignment sanity check"
218− ifdOffset <- getWord32
223+ ifdOffset <- lift getWord32
219− skip (fromIntegral ifdOffset - 8) -- skip to IDF
224+ lift $ skip (fromIntegral ifdOffset - 8) -- skip to IDF
220− numentries <- getWord16
225+ numentries <- lift getWord16
221− let ifdEntry = do
226+ let ifdEntry :: ExceptT String Get (TagType, DataFormat)
222− tag <- getWord16 >>= \t ->
227+ ifdEntry = do
223− maybe (return UnknownTagType) return
224− (M.lookup t tagTypeTable)
228+ tag <- fromMaybe UnknownTagType . flip M.lookup tagTypeTable
225− dataFormat <- getWord16
230+ dataFormat <- lift getWord16
226− numComponents <- getWord32
231+ numComponents <- lift getWord32
227 (fmt, bytesPerComponent) <-
232 (fmt, bytesPerComponent) <-
228 case dataFormat of
233 case dataFormat of
229 1 -> return (UnsignedByte . runGet getWord8, 1)
234 1 -> return (UnsignedByte . runGet getWord8, 1)
@@ -238,9 +243,10 @@ exifHeader hdr = do
238 10 -> return (SignedRational . runGet getRational, 8)
243 10 -> return (SignedRational . runGet getRational, 8)
239 11 -> return (SingleFloat . runGet getWord32 {- TODO -}, 4)
244 11 -> return (SingleFloat . runGet getWord32 {- TODO -}, 4)
240 12 -> return (DoubleFloat . runGet getWord64 {- TODO -}, 8)
245 12 -> return (DoubleFloat . runGet getWord64 {- TODO -}, 8)
241− _ -> fail $ "Unknown data format " ++ show dataFormat
246+ _ -> throwError $ "Unknown data format " ++ show dataFormat
242 let totalBytes = fromIntegral $ numComponents * bytesPerComponent
247 let totalBytes = fromIntegral $ numComponents * bytesPerComponent
243− payload <- if totalBytes <= 4 -- data is right here
249+ if totalBytes <= 4 -- data is right here
244 then fmt <$>
250 then fmt <$>
245 (getLazyByteString (fromIntegral totalBytes) <*
251 (getLazyByteString (fromIntegral totalBytes) <*
246 skip (4 - totalBytes))
252 skip (4 - totalBytes))
@@ -252,9 +258,9 @@ exifHeader hdr = do
252 entries <- sequence $ replicate (fromIntegral numentries) ifdEntry
258 entries <- sequence $ replicate (fromIntegral numentries) ifdEntry
253 subentries <- case lookup ExifOffset entries of
259 subentries <- case lookup ExifOffset entries of
254 Just (UnsignedLong offset) -> do
260 Just (UnsignedLong offset) -> do
255− pos <- bytesRead
261+ pos <- lift bytesRead
256− skip (fromIntegral offset - (fromIntegral pos - 8))
262+ lift $ skip (fromIntegral offset - (fromIntegral pos - 8))
257− numsubentries <- getWord16
263+ numsubentries <- lift getWord16
258 sequence $
264 sequence $
259 replicate (fromIntegral numsubentries) ifdEntry
265 replicate (fromIntegral numsubentries) ifdEntry
260 _ -> return []
266 _ -> return []