module JPEGparser where import Data.ByteString.Lazy.Char8(unpack) -- | Parse just enough of a JPEG file to obtain the size -- (width and height in pixels) of the JPEG image. sizeOfJPEG = sizeOfJPEG' . unpack -- Based on https://web.archive.org/web/20131016210645/http://www.64lines.com/jpeg-width-height sizeOfJPEG' = size0 . drop 4 where size0 (hi:lo:s) = size (drop (be hi lo-2) s) size ('\xff':'\xc0':_:_:_:h1:h2:w1:w2:_) = Just (be w1 w2,be h1 h2) size ('\xff':'\xc2':_:_:_:h1:h2:w1:w2:_) = Just (be w1 w2,be h1 h2) size ('\xff':_:s) = size0 s size _ = Nothing be hi lo = 256*fromEnum hi+fromEnum lo