module Codec.Picture (
readImage
, readImageWithMetadata
, decodeImage
, decodeImageWithMetadata
, pixelMap
, generateImage
, generateFoldImage
, withImage
, Traversal
, imagePixels
, imageIPixels
, saveBmpImage
, saveJpgImage
, saveGifImage
, savePngImage
, saveTiffImage
, saveRadianceImage
, BmpEncodable
, writeBitmap
, encodeBitmap
, readBitmap
, decodeBitmap
, encodeDynamicBitmap
, writeDynamicBitmap
, readGif
, readGifImages
, decodeGif
, decodeGifImages
, encodeGifImage
, writeGifImage
, encodeGifImageWithPalette
, writeGifImageWithPalette
, encodeColorReducedGifImage
, writeColorReducedGifImage
, encodeGifImages
, writeGifImages
, GifDelay
, GifLooping( .. )
, encodeGifAnimation
, writeGifAnimation
, readJpeg
, decodeJpeg
, encodeJpeg
, encodeJpegAtQuality
, PngSavable( .. )
, readPng
, decodePng
, writePng
, encodePalettedPng
, encodeDynamicPng
, writeDynamicPng
, readTGA
, decodeTga
, TgaSaveable
, encodeTga
, writeTga
, readTiff
, TiffSaveable
, decodeTiff
, encodeTiff
, writeTiff
, readHDR
, decodeHDR
, encodeHDR
, writeHDR
, PaletteCreationMethod(..)
, PaletteOptions(..)
, palettize
, Image( .. )
, DynamicImage( .. )
, Palette
, Pixel( .. )
, Pixel8
, Pixel16
, PixelF
, PixelYA8( .. )
, PixelYA16( .. )
, PixelRGB8( .. )
, PixelRGB16( .. )
, PixelRGBF( .. )
, PixelRGBA8( .. )
, PixelRGBA16( .. )
, PixelYCbCr8( .. )
, PixelCMYK8( .. )
, PixelCMYK16( .. )
, imageFromUnsafePtr
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative( (<$>) )
#endif
import Control.DeepSeq( NFData, deepseq )
import qualified Control.Exception as Exc ( catch, IOException )
import Codec.Picture.Metadata( Metadatas )
import Codec.Picture.Bitmap( BmpEncodable
, decodeBitmap
, decodeBitmapWithMetadata
, writeBitmap, encodeBitmap
, encodeDynamicBitmap, writeDynamicBitmap )
import Codec.Picture.Jpg( decodeJpeg
, decodeJpegWithMetadata
, encodeJpeg
, encodeJpegAtQuality )
import Codec.Picture.Png( PngSavable( .. )
, decodePng
, decodePngWithMetadata
, writePng
, encodeDynamicPng
, encodePalettedPng
, writeDynamicPng
)
import Codec.Picture.Gif( GifDelay
, GifLooping( .. )
, decodeGif
, decodeGifWithMetadata
, decodeGifImages
, encodeGifImage
, encodeGifImageWithPalette
, encodeGifImages
, writeGifImage
, writeGifImageWithPalette
, writeGifImages
)
import Codec.Picture.HDR( decodeHDR
, decodeHDRWithMetadata
, encodeHDR
, writeHDR
)
import Codec.Picture.Tiff( decodeTiff
, decodeTiffWithMetadata
, TiffSaveable
, encodeTiff
, writeTiff )
import Codec.Picture.Tga( TgaSaveable
, decodeTga
, decodeTgaWithMetadata
, encodeTga
, writeTga
)
import Codec.Picture.Saving
import Codec.Picture.Types
import Codec.Picture.ColorQuant
import Codec.Picture.VectorByteConversion( imageFromUnsafePtr )
#ifdef WITH_MMAP_BYTESTRING
import System.IO.MMap ( mmapFileByteString )
#endif
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
eitherLoad :: c -> [(String, c -> Either String b)] -> Either String b
eitherLoad v = inner ""
where inner errAcc [] = Left $ "Cannot load file\n" ++ errAcc
inner errAcc ((hdr, f) : rest) = case f v of
Left err -> inner (errAcc ++ hdr ++ " " ++ err ++ "\n") rest
Right rez -> Right rez
encodeColorReducedGifImage :: Image PixelRGB8 -> Either String L.ByteString
encodeColorReducedGifImage img = encodeGifImageWithPalette indexed pal
where (indexed, pal) = palettize defaultPaletteOptions img
writeColorReducedGifImage :: FilePath -> Image PixelRGB8 -> Either String (IO ())
writeColorReducedGifImage path img =
L.writeFile path <$> encodeColorReducedGifImage img
encodeGifAnimation :: GifDelay -> GifLooping
-> [Image PixelRGB8] -> Either String L.ByteString
encodeGifAnimation delay looping lst =
encodeGifImages looping
[(pal, delay, img)
| (img, pal) <- palettize defaultPaletteOptions <$> lst]
writeGifAnimation :: FilePath -> GifDelay -> GifLooping
-> [Image PixelRGB8] -> Either String (IO ())
writeGifAnimation path delay looping img =
L.writeFile path <$> encodeGifAnimation delay looping img
withImageDecoder :: (NFData a)
=> (B.ByteString -> Either String a) -> FilePath
-> IO (Either String a)
withImageDecoder decoder path = Exc.catch doit
(\e -> return . Left $ show (e :: Exc.IOException))
where doit = force . decoder <$> get
#ifdef WITH_MMAP_BYTESTRING
get = mmapFileByteString path Nothing
#else
get = B.readFile path
#endif
force x = x `deepseq` x
readImage :: FilePath -> IO (Either String DynamicImage)
readImage = withImageDecoder decodeImage
readImageWithMetadata :: FilePath -> IO (Either String (DynamicImage, Metadatas))
readImageWithMetadata = withImageDecoder decodeImageWithMetadata
decodeImage :: B.ByteString -> Either String DynamicImage
decodeImage = fmap fst . decodeImageWithMetadata
decodeImageWithMetadata :: B.ByteString -> Either String (DynamicImage, Metadatas)
decodeImageWithMetadata str = eitherLoad str
[ ("Jpeg", decodeJpegWithMetadata)
, ("PNG", decodePngWithMetadata)
, ("Bitmap", decodeBitmapWithMetadata)
, ("GIF", decodeGifWithMetadata)
, ("HDR", decodeHDRWithMetadata)
, ("Tiff", decodeTiffWithMetadata)
, ("TGA", decodeTgaWithMetadata)
]
readPng :: FilePath -> IO (Either String DynamicImage)
readPng = withImageDecoder decodePng
readGif :: FilePath -> IO (Either String DynamicImage)
readGif = withImageDecoder decodeGif
readTiff :: FilePath -> IO (Either String DynamicImage)
readTiff = withImageDecoder decodeTiff
readGifImages :: FilePath -> IO (Either String [DynamicImage])
readGifImages = withImageDecoder decodeGifImages
readJpeg :: FilePath -> IO (Either String DynamicImage)
readJpeg = withImageDecoder decodeJpeg
readBitmap :: FilePath -> IO (Either String DynamicImage)
readBitmap = withImageDecoder decodeBitmap
readHDR :: FilePath -> IO (Either String DynamicImage)
readHDR = withImageDecoder decodeHDR
readTGA :: FilePath -> IO (Either String DynamicImage)
readTGA = withImageDecoder decodeTga
saveJpgImage :: Int -> FilePath -> DynamicImage -> IO ()
saveJpgImage quality path img = L.writeFile path $ imageToJpg quality img
saveGifImage :: FilePath -> DynamicImage -> Either String (IO ())
saveGifImage path img = L.writeFile path <$> imageToGif img
saveTiffImage :: FilePath -> DynamicImage -> IO ()
saveTiffImage path img = L.writeFile path $ imageToTiff img
saveRadianceImage :: FilePath -> DynamicImage -> IO ()
saveRadianceImage path = L.writeFile path . imageToRadiance
savePngImage :: FilePath -> DynamicImage -> IO ()
savePngImage path img = L.writeFile path $ imageToPng img
saveBmpImage :: FilePath -> DynamicImage -> IO ()
saveBmpImage path img = L.writeFile path $ imageToBitmap img