module BitmapOps(bits2bitmap,doubleBM,explodeBM,bm,Bitmap(..),E(..)) where import Geometry(Point(..),Size) import Bitmap --import Data.List.Split(chunksOf) default (Int) data E = E Size [[Bit]] deriving Eq type Bit = Bool bits2bitmap sz@(Point w h) bits = implodeE (E sz (chunksOf w bits)) doubleBM = implodeE . doubleE . explodeBM doubleE (E sz rows) = E (2*sz) (double (map double rows)) double = foldr (\x r->x:x:r) [] {- instance Show E where show (E _ rows) = unlines [[".*"!!fromEnum b|b<-row]|row<-rows] showList = (++) . unlines . map show -} explodeBM (Bitmap s@(Point w h) _ bytes) = E s rows where b = (w+7) `div` 8 rows = map (take w . concatMap explode) (chunksOf b bytes) implodeE (E s@(Point w h) rows) = Bitmap s Nothing bytes where bytes = concatMap (map implode . chunksOf 8) rows implode = foldr (\b r->fromEnum (b::Bit)+2*r) 0 explode = take 8.expl where expl n = (n `mod` 2 /= 0):expl (n `div` 2) --- chunksOf n [] = [] chunksOf n xs = xs1:chunksOf n xs2 where (xs1,xs2) = splitAt n xs