imagesize-conduit 0.2.0 → 0.3.0
raw patch · 2 files changed
+33/−19 lines, 2 filesdep ~conduitPVP ok
version bump matches the API change (PVP)
Dependency ranges changed: conduit
API changes (from Hackage documentation)
+ Data.Conduit.ImageSize: GIF :: FileFormat
+ Data.Conduit.ImageSize: JPG :: FileFormat
+ Data.Conduit.ImageSize: PNG :: FileFormat
+ Data.Conduit.ImageSize: data FileFormat
+ Data.Conduit.ImageSize: instance Enum FileFormat
+ Data.Conduit.ImageSize: instance Eq FileFormat
+ Data.Conduit.ImageSize: instance Ord FileFormat
+ Data.Conduit.ImageSize: instance Read FileFormat
+ Data.Conduit.ImageSize: instance Show FileFormat
+ Data.Conduit.ImageSize: sinkImageInfo :: Monad m => Sink ByteString m (Maybe (Size, FileFormat))
- Data.Conduit.ImageSize: sinkImageSize :: Resource m => Sink ByteString m (Maybe Size)
+ Data.Conduit.ImageSize: sinkImageSize :: Monad m => Sink ByteString m (Maybe Size)
Files
- Data/Conduit/ImageSize.hs +31/−17
- imagesize-conduit.cabal +2/−2
Data/Conduit/ImageSize.hs view
@@ -2,6 +2,8 @@ module Data.Conduit.ImageSize ( sinkImageSize , Size (..)+ , sinkImageInfo+ , FileFormat (..) ) where import qualified Data.ByteString.Lazy as L@@ -15,23 +17,33 @@ data Size = Size { width :: Int, height :: Int } deriving (Show, Eq, Ord, Read) -sinkImageSize :: C.Resource m => C.Sink S.ByteString m (Maybe Size)-sinkImageSize =- C.SinkData (pushHeader id) close+data FileFormat = GIF | PNG | JPG+ deriving (Show, Eq, Ord, Read, Enum)++-- | Specialized version of 'sinkImageInfo' that returns only the+-- image size.+sinkImageSize :: Monad m => C.Sink S.ByteString m (Maybe Size)+sinkImageSize = fmap (fmap fst) sinkImageInfo++-- | Find out the size of an image. Also returns the file format+-- that parsed correctly. Note that this function does not+-- verify that the file is indeed in the format that it returns,+-- since it looks only at a small part of the header.+sinkImageInfo :: Monad m => C.Sink S.ByteString m (Maybe (Size, FileFormat))+sinkImageInfo =+ C.Processing (pushHeader id) close where close = return Nothing pushHeader front bs' | S.length bs >= 11 && S.take 5 (S.drop 6 bs) == S.pack [0x4A, 0x46, 0x49, 0x46, 0x00] =- C.sinkPush jpg $ S.drop 4 bs+ sinkPush jpg $ S.drop 4 bs | S.length bs >= 6 && S.take 6 bs `elem` gifs =- C.sinkPush gif $ S.drop 6 bs+ sinkPush gif $ S.drop 6 bs | S.length bs >= 8 && S.take 8 bs == S.pack [137, 80, 78, 71, 13, 10, 26, 10] =- C.sinkPush png $ S.drop 8 bs- | S.length bs < 11 = do- return $ C.Processing (pushHeader $ S.append bs) close- | otherwise = do- return $ C.Done (Just bs) Nothing+ sinkPush png $ S.drop 8 bs+ | S.length bs < 11 = C.Processing (pushHeader $ S.append bs) close+ | otherwise = C.Done (Just bs) Nothing where bs = front bs' @@ -40,7 +52,7 @@ b <- CB.take 4 let go x y = fromIntegral x + (fromIntegral y) * 256 return $ case L.unpack b of- [w1, w2, h1, h2] -> Just $ Size (go w1 w2) (go h1 h2)+ [w1, w2, h1, h2] -> Just (Size (go w1 w2) (go h1 h2), GIF) _ -> Nothing png = do@@ -50,9 +62,11 @@ then do mw <- getInt 4 0 mh <- getInt 4 0- return $ Size <$> mw <*> mh+ return $ (\w h -> (Size w h, PNG)) <$> mw <*> mh else return Nothing + sinkPush (C.Processing push _) x = push x+ sinkPush _ _ = error "Data.Conduit.ImageSize.sinkPush" jpg = do mi <- getInt 2 0@@ -69,15 +83,15 @@ my <- CB.head case my of Just 0xC0 -> do- _ <- CB.take 3- h <- getInt 2 0- w <- getInt 2 0- return $ Size <$> w <*> h+ _ <- CB.take 3+ mh <- getInt 2 0+ mw <- getInt 2 0+ return $ (\w h -> (Size w h, JPG)) <$> mw <*> mh Just _ -> jpg Nothing -> return Nothing _ -> return Nothing -getInt :: (C.Resource m, Integral i)+getInt :: (Monad m, Integral i) => Int -> i -> C.Sink S.ByteString m (Maybe i)
imagesize-conduit.cabal view
@@ -1,5 +1,5 @@ Name: imagesize-conduit-Version: 0.2.0+Version: 0.3.0 Synopsis: Determine the size of some common image formats. Description: Currently supports PNG, GIF, and JPEG. This package provides a Sink that will consume the minimum number of bytes necessary to determine the image dimensions. License: BSD3@@ -18,7 +18,7 @@ Library Exposed-modules: Data.Conduit.ImageSize Build-depends: base >= 4 && < 5- , conduit >= 0.2 && < 0.3+ , conduit >= 0.3 && < 0.4 , bytestring >= 0.9 ghc-options: -Wall