-- | Image loading with the JuicyPixels package module DynamicImage(module DynamicImage,decodeImage) where import qualified Data.ByteString.Lazy as BS import Codec.Picture import AllFudgets import DialogueIO hiding (IOError) import PNMgraphics import PNM newtype ImageFile = ImageFile FilePath imageFile = ImageFile instance Graphic ImageFile where measureGraphicK = measureImageK instance Graphic DynamicImage where measureGraphicK = measureImageK instance PixmapGen ImageFile where convToPixmapK (ImageFile filename) k = hIO (ReadBinFile filename) $ \ (Bn bs) -> case decodeImage (BS.toStrict bs) of Right image -> convToPixmapK image k Left e -> error ("Image parse error: "++show filename++": "++e) --hmm instance PixmapGen DynamicImage where convToPixmapK = convToPixmapK . image2pnm image2pnm :: DynamicImage -> PNM image2pnm image = PNM (w,h) (PPM 65535 pixels) where i = convertRGB16 image -- quick hack w = imageWidth i h = imageHeight i pixels = [pnmrgb (pixelAt i x y)|y<-[0..h-1],x<-[0..w-1]] pnmrgb (PixelRGB16 r g b) = fmap fromEnum (PNM.RGB r g b)