git.iko.soy/typeable/pandoc
on master help sign in
Added Text.Pandoc.ImageSize.

This is intened for use in docx and odt writers, so the size of image boxes can be calculated.

commit51365b3b363ce7c7a39a3a124d236eb1e3857889 authorJohn MacFarlane <fiddlosopher@gmail.com> dateSun 8 Jan 2012, 19:16 (15 years ago) parent0299ae6
2 files changed+111
pandoc.cabal+1 src/Text/Pandoc/ImageSize.hs+110
pandoc.cabal+1
@@ -277,6 +277,7 @@ Library
277277 Text.Pandoc.UTF8,
278278 Text.Pandoc.MIME,
279279 Text.Pandoc.UUID,
280+ Text.Pandoc.ImageSize,
280281 Paths_pandoc
281282
282283 if flag(library)
src/Text/Pandoc/ImageSize.hsnew file+110
@@ -0,0 +1,110 @@
1+{-# LANGUAGE OverloadedStrings, ScopedTypeVariables #-}
2+{-
3+Copyright (C) 2011 John MacFarlane <jgm@berkeley.edu>
4+
5+This program is free software; you can redistribute it and/or modify
6+it under the terms of the GNU General Public License as published by
7+the Free Software Foundation; either version 2 of the License, or
8+(at your option) any later version.
9+
10+This program is distributed in the hope that it will be useful,
11+but WITHOUT ANY WARRANTY; without even the implied warranty of
12+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+GNU General Public License for more details.
14+
15+You should have received a copy of the GNU General Public License
16+along with this program; if not, write to the Free Software
17+Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18+-}
19+
20+{- |
21+ Module : Text.Pandoc.ImageSize
22+ Copyright : Copyright (C) 2011 John MacFarlane
23+ License : GNU GPL, version 2 or above
24+
25+ Maintainer : John MacFarlane <jgm@berkeley.edu>
26+ Stability : alpha
27+ Portability : portable
28+
29+Functions for determining the size of a PNG, JPEG, or GIF image.
30+
31+Algorithms borrowwed from wwwis.pl (c) 2005 Alex K, released
32+under the GPL.
33+-}
34+module Text.Pandoc.ImageSize ( imageType, imageSize, readImageSize ) where
35+import Data.ByteString.Lazy (ByteString, unpack)
36+import qualified Data.ByteString.Lazy.Char8 as B
37+import Control.Monad
38+import Data.Bits
39+
40+-- quick and dirty functions to get image sizes
41+-- algorithms borrowed from wwwis.pl
42+
43+data ImageType = Png | Gif | Jpeg deriving Show
44+
45+type Height = Integer
46+type Width = Integer
47+
48+readImageSize :: FilePath -> IO (Maybe (Width,Height))
49+readImageSize fp = imageSize `fmap` B.readFile fp
50+
51+imageType :: ByteString -> Maybe ImageType
52+imageType img = case B.take 4 img of
53+ "\x89\x50\x4e\x47" -> return Png
54+ "\x47\x49\x46\x38" -> return Gif
55+ "\xff\xd8\xff\xe0" -> return Jpeg
56+ _ -> fail "Unknown image type"
57+
58+imageSize :: ByteString -> Maybe (Width,Height)
59+imageSize img = do
60+ t <- imageType img
61+ case t of
62+ Png -> pngSize img
63+ Gif -> gifSize img
64+ Jpeg -> jpegSize img
65+
66+pngSize :: ByteString -> Maybe (Width,Height)
67+pngSize img = do
68+ let (h, rest) = B.splitAt 8 img
69+ guard $ h == "\x8a\x4d\x4e\x47\x0d\x0a\x1a\x0a" ||
70+ h == "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a"
71+ let (i, rest') = B.splitAt 4 $ B.drop 4 rest
72+ guard $ i == "MHDR" || i == "IHDR"
73+ case map fromIntegral $ unpack $ B.take 8 rest' of
74+ ([w1,w2,w3,w4,h1,h2,h3,h4] :: [Integer]) -> return
75+ ((shift w1 24) + (shift w2 16) + (shift w3 8) + w4,
76+ (shift h1 24) + (shift h2 16) + (shift h3 8) + h4)
77+ _ -> fail "PNG parse error"
78+
79+gifSize :: ByteString -> Maybe (Width,Height)
80+gifSize img = do
81+ let (h, rest) = B.splitAt 6 img
82+ guard $ h == "GIF87a" || h == "GIF89a"
83+ case map fromIntegral $ unpack $ B.take 4 rest of
84+ [w2,w1,h2,h1] -> return (shift w1 8 + w2, shift h1 8 + h2)
85+ _ -> fail "GIF parse error"
86+
87+jpegSize :: ByteString -> Maybe (Width,Height)
88+jpegSize img = do
89+ let (h, rest) = B.splitAt 2 img
90+ guard $ h == "\xff\xd8"
91+ findJpegLength rest
92+
93+findJpegLength :: ByteString -> Maybe (Width,Height)
94+findJpegLength bs = do
95+ let bs' = B.dropWhile (=='\xff') $ B.dropWhile (/='\xff') bs
96+ case B.uncons bs' of
97+ Just (c,bs'') | c >= '\xc0' && c <= '\xc3' -> do
98+ case map fromIntegral $ unpack $ B.take 4 $ B.drop 3 bs'' of
99+ [h1,h2,w1,w2] -> return (shift w1 8 + w2, shift h1 8 + h2)
100+ _ -> fail "JPEG parse error"
101+ Just (_,bs'') -> do
102+ case map fromIntegral $ unpack $ B.take 2 bs'' of
103+ [c1,c2] -> do
104+ let len = shift c1 8 + c2
105+ -- skip variables
106+ findJpegLength $ B.drop len bs''
107+ _ -> fail "JPEG parse error"
108+ Nothing -> fail "Did not find length record"
109+
110+