-- | Convert GIF images to PPM images module GIF2PPM(gif2ppm) where import GIF import PNM import Data.Array import Data.List(sortOn) import Utils2(pieces) gif2ppm gif = PNM (w,h) (PPM 255 rgbs) where sd = screen_descriptor gif w = fromEnum (swidth sd) h = fromEnum (sheight sd) scmap = maybe (const (RGB 255 0 0)) (!) $ fmap (cmap (sbitsPerPixel sd)) (global_color_map gif) rgbs = case [(i,deinterlaced_raster_data i) | Right i <- data_blocks gif] of (i,r):_ -> map icmap r where id = image_descriptor i icmap = maybe scmap ((!) . cmap (ibitsPerPixel id)) (local_colorMap i) cmap bits = listArray (0,2^bits-1) . map (fmap fromEnum) deinterlaced_raster_data image = (if interlace d then deinterlace (iwidth d) (iheight d) else id) (raster_data image) where d = image_descriptor image deinterlace :: Short -> Short -> Pixels -> Pixels deinterlace w h pixels = concat . reorder . lines $ pixels where lines = pieces (fromEnum w) reorder = map snd . sortOn fst . zip poslist poslist = [0,8..l]++[4,12..l]++[2,6..l]++[1,3..l] l=h-1