{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE TypeApplications #-}
-- |
-- Module      : Data.Massiv.Array.IO.Image
-- Copyright   : (c) Alexey Kuleshevich 2018-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Massiv.Array.IO.Image
  ( module Data.Massiv.Array.IO.Image.JuicyPixels
  , module Data.Massiv.Array.IO.Image.Netpbm
  -- ** Helper image functions
  , selectFileFormat
  , Encode(..)
  , encodeImageM
  , encodeAdhocM
  , writableAdhoc
  , imageWriteFormats
  , imageWriteAutoFormats
  , Decode(..)
  , decodeImageM
  , decodeAdhocM
  , readableAdhoc
  , imageReadFormats
  , imageReadAutoFormats
  ) where

import qualified Data.ByteString as B (ByteString)
import qualified Data.ByteString.Lazy as BL (ByteString)
import Data.Massiv.Array
import Data.Massiv.Array.IO.Base
import Data.Massiv.Array.IO.Image.JuicyPixels
import Data.Massiv.Array.IO.Image.Netpbm
import Graphics.Pixel.ColorSpace
import Prelude as P



-- | Adhoc encoder
data Encode out where
  -- | Provide a custom way to encode a particular file format. This is an alternative
  -- approach to `Writable` class
  --
  -- @since 0.4.1
  Encode
    :: FileFormat f
    => f
    -> (forall m. MonadThrow m => f -> out -> m BL.ByteString)
    -> Encode out

instance Show (Encode out) where
  show :: Encode out -> String
show (Encode f
f forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString
_) = f -> String
forall a. Show a => a -> String
show f
f

instance FileFormat (Encode out) where
  ext :: Encode out -> String
ext (Encode f
f forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString
_) = f -> String
forall f. FileFormat f => f -> String
ext f
f

  exts :: Encode out -> [String]
exts (Encode f
f forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString
_) = f -> [String]
forall f. FileFormat f => f -> [String]
exts f
f

-- | Decode binary data without requiring `Readable` instances
--
-- @since 0.4.1
encodeAdhocM :: MonadThrow m => Encode out -> out -> m BL.ByteString
encodeAdhocM :: Encode out -> out -> m ByteString
encodeAdhocM (Encode f
f forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString
enc) = f -> out -> m ByteString
forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString
enc f
f

-- | Utilize a Writable instance in order to construct an adhoc Encode type
--
-- @since 0.4.1
writableAdhoc :: Writable f out => f -> Encode out
writableAdhoc :: f -> Encode out
writableAdhoc f
f = f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode f
f (f -> WriteOptions f -> out -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
`encodeM` WriteOptions f
forall a. Default a => a
def)


-- | Encode an image into a lazy `BL.ByteString`, while selecting the appropriate format from the
-- file extension.
--
-- @since 0.2.0
encodeImageM
  :: MonadThrow m
  => [Encode (Image r cs e)] -- ^ List of image formats to choose from (useful lists are
                             -- `imageWriteFormats` and `imageWriteAutoFormats`)
  -> FilePath -- ^ File name with extension, so the format can be inferred
  -> Image r cs e -- ^ Image to encode
  -> m BL.ByteString
encodeImageM :: [Encode (Image r cs e)] -> String -> Image r cs e -> m ByteString
encodeImageM [Encode (Image r cs e)]
formats String
path Image r cs e
img = do
  Encode (Image r cs e)
f <- [Encode (Image r cs e)] -> String -> m (Encode (Image r cs e))
forall f (m :: * -> *).
(FileFormat f, MonadThrow m) =>
[f] -> String -> m f
selectFileFormat [Encode (Image r cs e)]
formats String
path
  Encode (Image r cs e) -> Image r cs e -> m ByteString
forall (m :: * -> *) out.
MonadThrow m =>
Encode out -> out -> m ByteString
encodeAdhocM Encode (Image r cs e)
f Image r cs e
img


-- | List of image formats that can be encoded without any color space conversion.
imageWriteFormats :: (Source r Ix2 (Pixel cs e), ColorModel cs e) => [Encode (Image r cs e)]
imageWriteFormats :: [Encode (Image r cs e)]
imageWriteFormats =
  [ PNG
-> (forall (m :: * -> *).
    MonadThrow m =>
    PNG -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode PNG
PNG (\ PNG
f -> PNG -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
PNG -> Image S cs e -> m ByteString
encodePNG PNG
f (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , TIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    TIF -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode TIF
TIF (\ TIF
f -> TIF -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TIF -> Image S cs e -> m ByteString
encodeTIF TIF
f (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , JPG
-> (forall (m :: * -> *).
    MonadThrow m =>
    JPG -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode JPG
JPG (\ JPG
f -> JPG -> JpegOptions -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
JPG -> JpegOptions -> Image S cs e -> m ByteString
encodeJPG JPG
f JpegOptions
forall a. Default a => a
def (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , BMP
-> (forall (m :: * -> *).
    MonadThrow m =>
    BMP -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode BMP
BMP (\ BMP
f -> BMP -> BitmapOptions -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
BMP -> BitmapOptions -> Image S cs e -> m ByteString
encodeBMP BMP
f BitmapOptions
forall a. Default a => a
def (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , GIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    GIF -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode GIF
GIF (\ GIF
f -> GIF -> GifOptions -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
GIF -> GifOptions -> Image S cs e -> m ByteString
encodeGIF GIF
f GifOptions
forall a. Default a => a
def (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , TGA
-> (forall (m :: * -> *).
    MonadThrow m =>
    TGA -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode TGA
TGA (\ TGA
f -> TGA -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TGA -> Image S cs e -> m ByteString
encodeTGA TGA
f (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  , HDR
-> (forall (m :: * -> *).
    MonadThrow m =>
    HDR -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode HDR
HDR (\ HDR
f -> HDR -> HdrOptions -> Image S cs e -> m ByteString
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
HDR -> HdrOptions -> Image S cs e -> m ByteString
encodeHDR HDR
f HdrOptions
forall a. Default a => a
def (Image S cs e -> m ByteString)
-> (Image r cs e -> Image S cs e) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall ix e r'.
(Mutable S ix e, Source r' ix e) =>
Array r' ix e -> Array S ix e
forall r ix e r'.
(Mutable r ix e, Source r' ix e) =>
Array r' ix e -> Array r ix e
computeSource @S)
  ]

-- | List of image formats that can be encoded with any necessary color space conversions.
imageWriteAutoFormats ::
     (Source r Ix2 (Pixel cs e), ColorSpace cs i e, ColorSpace (BaseSpace cs) i e)
  => [Encode (Image r cs e)]
imageWriteAutoFormats :: [Encode (Image r cs e)]
imageWriteAutoFormats =
  [ Auto PNG
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto PNG -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (PNG -> Auto PNG
forall f. f -> Auto f
Auto PNG
PNG) (\Auto PNG
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto PNG -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace (BaseSpace cs) i e, ColorSpace cs i e,
 Source r Ix2 (Pixel cs e)) =>
Auto PNG -> Image r cs e -> ByteString
encodeAutoPNG Auto PNG
f)
  , Auto TIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto TIF -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (TIF -> Auto TIF
forall f. f -> Auto f
Auto TIF
TIF) (\Auto TIF
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto TIF -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace (BaseSpace cs) i e, ColorSpace cs i e,
 Source r Ix2 (Pixel cs e)) =>
Auto TIF -> Image r cs e -> ByteString
encodeAutoTIF Auto TIF
f)
  , Auto JPG
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto JPG -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (JPG -> Auto JPG
forall f. f -> Auto f
Auto JPG
JPG) (\Auto JPG
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto JPG -> JpegOptions -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace (BaseSpace cs) i e, ColorSpace cs i e,
 Source r Ix2 (Pixel cs e)) =>
Auto JPG -> JpegOptions -> Image r cs e -> ByteString
encodeAutoJPG Auto JPG
f JpegOptions
forall a. Default a => a
def)
  , Auto BMP
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto BMP -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (BMP -> Auto BMP
forall f. f -> Auto f
Auto BMP
BMP) (\Auto BMP
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto BMP -> BitmapOptions -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace (BaseSpace cs) i e, ColorSpace cs i e,
 Source r Ix2 (Pixel cs e)) =>
Auto BMP -> BitmapOptions -> Image r cs e -> ByteString
encodeAutoBMP Auto BMP
f BitmapOptions
forall a. Default a => a
def)
  , Auto GIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto GIF -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (GIF -> Auto GIF
forall f. f -> Auto f
Auto GIF
GIF) (Auto GIF -> GifOptions -> Image r cs e -> m ByteString
forall r cs i e (m :: * -> *).
(ColorSpace cs i e, Source r Ix2 (Pixel cs e), MonadThrow m) =>
Auto GIF -> GifOptions -> Image r cs e -> m ByteString
`encodeAutoGIF` GifOptions
forall a. Default a => a
def)
  , Auto HDR
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto HDR -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (HDR -> Auto HDR
forall f. f -> Auto f
Auto HDR
HDR) (\Auto HDR
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto HDR -> HdrOptions -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace cs i e, Source r Ix2 (Pixel cs e)) =>
Auto HDR -> HdrOptions -> Image r cs e -> ByteString
encodeAutoHDR Auto HDR
f HdrOptions
forall a. Default a => a
def)
  , Auto TGA
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto TGA -> Image r cs e -> m ByteString)
-> Encode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> out -> m ByteString)
-> Encode out
Encode (TGA -> Auto TGA
forall f. f -> Auto f
Auto TGA
TGA) (\Auto TGA
f -> ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString)
-> (Image r cs e -> ByteString) -> Image r cs e -> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto TGA -> Image r cs e -> ByteString
forall r cs i e.
(ColorSpace (BaseSpace cs) i e, ColorSpace cs i e,
 Source r Ix2 (Pixel cs e)) =>
Auto TGA -> Image r cs e -> ByteString
encodeAutoTGA Auto TGA
f)
  ]


-- | Adhoc decoder
data Decode out where
  -- | Provide a custom way to encode a particular file format. This is an alternative
  -- approach to `Writable` class
  --
  -- @since 0.4.1
  Decode
    :: FileFormat f
    => f
    -> (forall m. MonadThrow m => f -> B.ByteString -> m out)
    -> Decode out

instance Show (Decode out) where
  show :: Decode out -> String
show (Decode f
f forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out
_) = f -> String
forall a. Show a => a -> String
show f
f

instance FileFormat (Decode (Image r cs e)) where
  ext :: Decode (Image r cs e) -> String
ext (Decode f
f forall (m :: * -> *).
MonadThrow m =>
f -> ByteString -> m (Image r cs e)
_) = f -> String
forall f. FileFormat f => f -> String
ext f
f

  exts :: Decode (Image r cs e) -> [String]
exts (Decode f
f forall (m :: * -> *).
MonadThrow m =>
f -> ByteString -> m (Image r cs e)
_) = f -> [String]
forall f. FileFormat f => f -> [String]
exts f
f

-- | Decode binary data without requiring `Readable` instances
--
-- @since 0.4.1
decodeAdhocM :: MonadThrow m => Decode out -> B.ByteString -> m out
decodeAdhocM :: Decode out -> ByteString -> m out
decodeAdhocM (Decode f
f forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out
dec) = f -> ByteString -> m out
forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out
dec f
f


-- | Utilize a Readable instance in order to construct an adhoc Decode type
--
-- @since 0.4.1
readableAdhoc :: Readable f out => f -> Decode out
readableAdhoc :: f -> Decode out
readableAdhoc f
f = f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode f
f forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m arr
forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out
decodeM


-- | Decode an image from the strict `ByteString` while inferring format the image is encoded in
-- from the file extension
--
-- @since 0.2.0
decodeImageM
  :: MonadThrow m
  => [Decode (Image r cs e)] -- ^ List of available formats to choose from
  -> FilePath -- ^ File name with extension, so format can be inferred
  -> B.ByteString -- ^ Encoded image
  -> m (Image r cs e)
decodeImageM :: [Decode (Image r cs e)] -> String -> ByteString -> m (Image r cs e)
decodeImageM [Decode (Image r cs e)]
formats String
path ByteString
bs = do
  Decode (Image r cs e)
f <- [Decode (Image r cs e)] -> String -> m (Decode (Image r cs e))
forall f (m :: * -> *).
(FileFormat f, MonadThrow m) =>
[f] -> String -> m f
selectFileFormat [Decode (Image r cs e)]
formats String
path
  Decode (Image r cs e) -> ByteString -> m (Image r cs e)
forall (m :: * -> *) out.
MonadThrow m =>
Decode out -> ByteString -> m out
decodeAdhocM Decode (Image r cs e)
f ByteString
bs

-- | List of image formats decodable with no color space conversion
imageReadFormats :: ColorModel cs e => [Decode (Image S cs e)]
imageReadFormats :: [Decode (Image S cs e)]
imageReadFormats =
  [ PNG
-> (forall (m :: * -> *).
    MonadThrow m =>
    PNG -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode PNG
PNG forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
PNG -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
PNG -> ByteString -> m (Image S cs e)
decodePNG
  , TIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    TIF -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode TIF
TIF forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TIF -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
TIF -> ByteString -> m (Image S cs e)
decodeTIF
  , JPG
-> (forall (m :: * -> *).
    MonadThrow m =>
    JPG -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode JPG
JPG forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
JPG -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
JPG -> ByteString -> m (Image S cs e)
decodeJPG
  , BMP
-> (forall (m :: * -> *).
    MonadThrow m =>
    BMP -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode BMP
BMP forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
BMP -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
BMP -> ByteString -> m (Image S cs e)
decodeBMP
  , GIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    GIF -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode GIF
GIF forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
GIF -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
GIF -> ByteString -> m (Image S cs e)
decodeGIF
  , HDR
-> (forall (m :: * -> *).
    MonadThrow m =>
    HDR -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode HDR
HDR forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
HDR -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
HDR -> ByteString -> m (Image S cs e)
decodeHDR
  , TGA
-> (forall (m :: * -> *).
    MonadThrow m =>
    TGA -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode TGA
TGA forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TGA -> ByteString -> m (Image S cs e)
forall (m :: * -> *).
MonadThrow m =>
TGA -> ByteString -> m (Image S cs e)
decodeTGA
  , PBM
-> (forall (m :: * -> *).
    MonadThrow m =>
    PBM -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode PBM
PBM (\PBM
f -> ((Image S cs e, Maybe ByteString) -> Image S cs e)
-> m (Image S cs e, Maybe ByteString) -> m (Image S cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image S cs e, Maybe ByteString) -> Image S cs e
forall a b. (a, b) -> a
fst (m (Image S cs e, Maybe ByteString) -> m (Image S cs e))
-> (ByteString -> m (Image S cs e, Maybe ByteString))
-> ByteString
-> m (Image S cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PBM -> ByteString -> m (Image S cs e, Maybe ByteString)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage PBM
f)
  , PGM
-> (forall (m :: * -> *).
    MonadThrow m =>
    PGM -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode PGM
PGM (\PGM
f -> ((Image S cs e, Maybe ByteString) -> Image S cs e)
-> m (Image S cs e, Maybe ByteString) -> m (Image S cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image S cs e, Maybe ByteString) -> Image S cs e
forall a b. (a, b) -> a
fst (m (Image S cs e, Maybe ByteString) -> m (Image S cs e))
-> (ByteString -> m (Image S cs e, Maybe ByteString))
-> ByteString
-> m (Image S cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGM -> ByteString -> m (Image S cs e, Maybe ByteString)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage PGM
f)
  , PPM
-> (forall (m :: * -> *).
    MonadThrow m =>
    PPM -> ByteString -> m (Image S cs e))
-> Decode (Image S cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode PPM
PPM (\PPM
f -> ((Image S cs e, Maybe ByteString) -> Image S cs e)
-> m (Image S cs e, Maybe ByteString) -> m (Image S cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image S cs e, Maybe ByteString) -> Image S cs e
forall a b. (a, b) -> a
fst (m (Image S cs e, Maybe ByteString) -> m (Image S cs e))
-> (ByteString -> m (Image S cs e, Maybe ByteString))
-> ByteString
-> m (Image S cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PPM -> ByteString -> m (Image S cs e, Maybe ByteString)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage PPM
f)
  ]

-- | List of image formats decodable with automatic colorspace conversion
imageReadAutoFormats
  :: (Mutable r Ix2 (Pixel cs e), ColorSpace cs i e)
  => [Decode (Image r cs e)]
imageReadAutoFormats :: [Decode (Image r cs e)]
imageReadAutoFormats =
  [ Auto PNG
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto PNG -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (PNG -> Auto PNG
forall f. f -> Auto f
Auto PNG
PNG) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto PNG -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto PNG -> ByteString -> m (Image r cs e)
decodeAutoPNG
  , Auto TIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto TIF -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (TIF -> Auto TIF
forall f. f -> Auto f
Auto TIF
TIF) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto TIF -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto TIF -> ByteString -> m (Image r cs e)
decodeAutoTIF
  , Auto JPG
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto JPG -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (JPG -> Auto JPG
forall f. f -> Auto f
Auto JPG
JPG) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto JPG -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto JPG -> ByteString -> m (Image r cs e)
decodeAutoJPG
  , Auto BMP
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto BMP -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (BMP -> Auto BMP
forall f. f -> Auto f
Auto BMP
BMP) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto BMP -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto BMP -> ByteString -> m (Image r cs e)
decodeAutoBMP
  , Auto GIF
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto GIF -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (GIF -> Auto GIF
forall f. f -> Auto f
Auto GIF
GIF) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto GIF -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto GIF -> ByteString -> m (Image r cs e)
decodeAutoGIF
  , Auto HDR
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto HDR -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (HDR -> Auto HDR
forall f. f -> Auto f
Auto HDR
HDR) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto HDR -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto HDR -> ByteString -> m (Image r cs e)
decodeAutoHDR
  , Auto TGA
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto TGA -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (TGA -> Auto TGA
forall f. f -> Auto f
Auto TGA
TGA) forall r cs e i (m :: * -> *).
(Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m) =>
Auto TGA -> ByteString -> m (Image r cs e)
forall (m :: * -> *).
MonadThrow m =>
Auto TGA -> ByteString -> m (Image r cs e)
decodeAutoTGA
  , Auto PBM
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto PBM -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (PBM -> Auto PBM
forall f. f -> Auto f
Auto PBM
PBM) (\Auto PBM
f -> ((Image r cs e, Maybe ByteString) -> Image r cs e)
-> m (Image r cs e, Maybe ByteString) -> m (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image r cs e, Maybe ByteString) -> Image r cs e
forall a b. (a, b) -> a
fst (m (Image r cs e, Maybe ByteString) -> m (Image r cs e))
-> (ByteString -> m (Image r cs e, Maybe ByteString))
-> ByteString
-> m (Image r cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto PBM -> ByteString -> m (Image r cs e, Maybe ByteString)
forall f r cs e (m :: * -> *) i.
(FileFormat f, Mutable r Ix2 (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage Auto PBM
f)
  , Auto PGM
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto PGM -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (PGM -> Auto PGM
forall f. f -> Auto f
Auto PGM
PGM) (\Auto PGM
f -> ((Image r cs e, Maybe ByteString) -> Image r cs e)
-> m (Image r cs e, Maybe ByteString) -> m (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image r cs e, Maybe ByteString) -> Image r cs e
forall a b. (a, b) -> a
fst (m (Image r cs e, Maybe ByteString) -> m (Image r cs e))
-> (ByteString -> m (Image r cs e, Maybe ByteString))
-> ByteString
-> m (Image r cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto PGM -> ByteString -> m (Image r cs e, Maybe ByteString)
forall f r cs e (m :: * -> *) i.
(FileFormat f, Mutable r Ix2 (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage Auto PGM
f)
  , Auto PPM
-> (forall (m :: * -> *).
    MonadThrow m =>
    Auto PPM -> ByteString -> m (Image r cs e))
-> Decode (Image r cs e)
forall f out.
FileFormat f =>
f
-> (forall (m :: * -> *). MonadThrow m => f -> ByteString -> m out)
-> Decode out
Decode (PPM -> Auto PPM
forall f. f -> Auto f
Auto PPM
PPM) (\Auto PPM
f -> ((Image r cs e, Maybe ByteString) -> Image r cs e)
-> m (Image r cs e, Maybe ByteString) -> m (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (Image r cs e, Maybe ByteString) -> Image r cs e
forall a b. (a, b) -> a
fst (m (Image r cs e, Maybe ByteString) -> m (Image r cs e))
-> (ByteString -> m (Image r cs e, Maybe ByteString))
-> ByteString
-> m (Image r cs e)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Auto PPM -> ByteString -> m (Image r cs e, Maybe ByteString)
forall f r cs e (m :: * -> *) i.
(FileFormat f, Mutable r Ix2 (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage Auto PPM
f)
  ]