{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- |
-- Module      : Data.Massiv.Array.IO.Image.Netpbm
-- Copyright   : (c) Alexey Kuleshevich 2018-2021
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Massiv.Array.IO.Image.Netpbm
  ( -- * Netpbm formats
    -- ** PBM
    PBM(..)
    -- ** PGM
  , PGM(..)
    -- ** PPM
  , PPM(..)
  , decodeNetpbmImage
  , decodeNetpbmImageSequence
  , decodeAutoNetpbmImage
  , decodeAutoNetpbmImageSequence
  ) where

import Control.Monad (guard)
import Data.Bifunctor (first)
import qualified Data.ByteString as B (ByteString)
import Data.Massiv.Array as A
import Data.Massiv.Array.IO.Base
import Data.Typeable
import qualified Data.Vector.Storable as V
import Graphics.Netpbm as Netpbm hiding (PPM)
import qualified Graphics.Netpbm as Netpbm (PPM(..))
import qualified Graphics.Pixel as CM
import Graphics.Pixel.ColorSpace
import Prelude as P

-- | Netpbm: portable bitmap image with @.pbm@ extension.
data PBM = PBM deriving Int -> PBM -> ShowS
[PBM] -> ShowS
PBM -> String
(Int -> PBM -> ShowS)
-> (PBM -> String) -> ([PBM] -> ShowS) -> Show PBM
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PBM] -> ShowS
$cshowList :: [PBM] -> ShowS
show :: PBM -> String
$cshow :: PBM -> String
showsPrec :: Int -> PBM -> ShowS
$cshowsPrec :: Int -> PBM -> ShowS
Show

instance FileFormat PBM where
  type Metadata PBM = Maybe B.ByteString
  ext :: PBM -> String
ext PBM
_ = String
".pbm"

instance FileFormat (Sequence PBM) where
  type WriteOptions (Sequence PBM) = WriteOptions PBM
  type Metadata (Sequence PBM) = Maybe B.ByteString
  ext :: Sequence PBM -> String
ext Sequence PBM
_ = PBM -> String
forall f. FileFormat f => f -> String
ext PBM
PBM


-- | Netpbm: portable graymap image with @.pgm@ extension.
data PGM = PGM deriving Int -> PGM -> ShowS
[PGM] -> ShowS
PGM -> String
(Int -> PGM -> ShowS)
-> (PGM -> String) -> ([PGM] -> ShowS) -> Show PGM
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PGM] -> ShowS
$cshowList :: [PGM] -> ShowS
show :: PGM -> String
$cshow :: PGM -> String
showsPrec :: Int -> PGM -> ShowS
$cshowsPrec :: Int -> PGM -> ShowS
Show

instance FileFormat PGM where
  type Metadata PGM = Maybe B.ByteString
  ext :: PGM -> String
ext PGM
_ = String
".pgm"

instance FileFormat (Sequence PGM) where
  type WriteOptions (Sequence PGM) = WriteOptions PGM
  type Metadata (Sequence PGM) = Maybe B.ByteString
  ext :: Sequence PGM -> String
ext Sequence PGM
_ = PGM -> String
forall f. FileFormat f => f -> String
ext PGM
PGM


-- | Netpbm: portable pixmap image with @.ppm@ extension.
data PPM = PPM deriving Int -> PPM -> ShowS
[PPM] -> ShowS
PPM -> String
(Int -> PPM -> ShowS)
-> (PPM -> String) -> ([PPM] -> ShowS) -> Show PPM
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [PPM] -> ShowS
$cshowList :: [PPM] -> ShowS
show :: PPM -> String
$cshow :: PPM -> String
showsPrec :: Int -> PPM -> ShowS
$cshowsPrec :: Int -> PPM -> ShowS
Show

instance FileFormat PPM where
  type Metadata PPM = Maybe B.ByteString
  ext :: PPM -> String
ext PPM
_ = String
".ppm"
  exts :: PPM -> [String]
exts PPM
_ = [String
".ppm", String
".pnm"]

instance FileFormat (Sequence PPM) where
  type WriteOptions (Sequence PPM) = WriteOptions PPM
  type Metadata (Sequence PPM) = Maybe B.ByteString
  ext :: Sequence PPM -> String
ext Sequence PPM
_ = PPM -> String
forall f. FileFormat f => f -> String
ext PPM
PPM

-- | Try to decode a Netpbm image
--
-- @since 0.2.0
decodeNetpbmImage ::
     (FileFormat f, ColorModel cs e, MonadThrow m)
  => f
  -> B.ByteString
  -> m (Image S cs e, Maybe B.ByteString)
decodeNetpbmImage :: f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage = (PPM -> Maybe (Image S cs e))
-> f -> ByteString -> m (Image S cs e, Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodePPM PPM -> Maybe (Image S cs e)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage
{-# INLINE decodeNetpbmImage #-}

-- | Try to decode a Netpbm image sequence
--
-- @since 0.2.0
decodeNetpbmImageSequence ::
     (FileFormat (Sequence f), ColorModel cs e, MonadThrow m)
  => Sequence f
  -> B.ByteString
  -> m ([Image S cs e], Maybe B.ByteString)
decodeNetpbmImageSequence :: Sequence f -> ByteString -> m ([Image S cs e], Maybe ByteString)
decodeNetpbmImageSequence = (PPM -> Maybe (Image S cs e))
-> Sequence f -> ByteString -> m ([Image S cs e], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S cs e)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage
{-# INLINE decodeNetpbmImageSequence #-}

-- | Try to decode a Netpbm image, while auto converting the colorspace.
--
-- @since 0.2.0
decodeAutoNetpbmImage ::
     (FileFormat f, Manifest r (Pixel cs e), MonadThrow m, ColorSpace cs i e)
  => f
  -> B.ByteString
  -> m (Image r cs e, Maybe B.ByteString)
decodeAutoNetpbmImage :: f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage = (PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m (Image r cs e, Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodePPM PPM -> Maybe (Image r cs e)
forall cs i e r.
(Manifest r (Pixel cs e), ColorSpace cs i e) =>
PPM -> Maybe (Image r cs e)
fromNetpbmImageAuto
{-# INLINE decodeAutoNetpbmImage #-}

-- | Try to decode a Netpbm image sequence, while auto converting the colorspace.
--
-- @since 0.2.0
decodeAutoNetpbmImageSequence ::
     (FileFormat (Sequence f), Manifest r (Pixel cs e), MonadThrow m, ColorSpace cs i e)
  => Auto (Sequence f)
  -> B.ByteString
  -> m ([Image r cs e], Maybe B.ByteString)
decodeAutoNetpbmImageSequence :: Auto (Sequence f)
-> ByteString -> m ([Image r cs e], Maybe ByteString)
decodeAutoNetpbmImageSequence = (PPM -> Maybe (Image r cs e))
-> Auto (Sequence f)
-> ByteString
-> m ([Image r cs e], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image r cs e)
forall cs i e r.
(Manifest r (Pixel cs e), ColorSpace cs i e) =>
PPM -> Maybe (Image r cs e)
fromNetpbmImageAuto
{-# INLINE decodeAutoNetpbmImageSequence #-}

decodePPMs :: (FileFormat f, Manifest r (Pixel cs e), ColorModel cs e, MonadThrow m) =>
              (Netpbm.PPM -> Maybe (Image r cs e))
           -> f
           -> B.ByteString
           -> m ([Image r cs e], Maybe B.ByteString)
decodePPMs :: (PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image r cs e)
converter f
f ByteString
bs =
  case ByteString -> PpmParseResult
parsePPM ByteString
bs of
    Left String
err -> DecodeError -> m ([Image r cs e], Maybe ByteString)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (DecodeError -> m ([Image r cs e], Maybe ByteString))
-> DecodeError -> m ([Image r cs e], Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ String -> DecodeError
DecodeError String
err
    Right ([], Just ByteString
_) -> DecodeError -> m ([Image r cs e], Maybe ByteString)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (DecodeError -> m ([Image r cs e], Maybe ByteString))
-> DecodeError -> m ([Image r cs e], Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ String -> DecodeError
DecodeError String
"Cannot parse PNM image"
    Right ([PPM]
ppms, Maybe ByteString
leftOver) -> do
      [Image r cs e]
imgs <- (PPM -> m (Image r cs e)) -> [PPM] -> m [Image r cs e]
forall (t :: * -> *) (f :: * -> *) a b.
(Traversable t, Applicative f) =>
(a -> f b) -> t a -> f (t b)
P.traverse (f
-> (PPM -> String)
-> (PPM -> Maybe (Image r cs e))
-> PPM
-> m (Image r cs e)
forall r cs e a f (m :: * -> *).
(ColorModel cs e, FileFormat f, Typeable r, MonadThrow m) =>
f
-> (a -> String)
-> (a -> Maybe (Image r cs e))
-> a
-> m (Image r cs e)
fromMaybeDecode f
f PPM -> String
showNetpbmCS PPM -> Maybe (Image r cs e)
converter) [PPM]
ppms
      ([Image r cs e], Maybe ByteString)
-> m ([Image r cs e], Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([Image r cs e]
imgs, Maybe ByteString
leftOver)
{-# INLINE decodePPMs #-}


decodePPM :: (FileFormat f, Manifest r (Pixel cs e), ColorModel cs e, MonadThrow m) =>
             (Netpbm.PPM -> Maybe (Image r cs e))
          -> f
          -> B.ByteString
          -> m (Image r cs e, Maybe B.ByteString)
decodePPM :: (PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodePPM PPM -> Maybe (Image r cs e)
decoder f
f ByteString
bs =
  case ByteString -> PpmParseResult
parsePPM ByteString
bs of
    Left String
err -> DecodeError -> m (Image r cs e, Maybe ByteString)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (DecodeError -> m (Image r cs e, Maybe ByteString))
-> DecodeError -> m (Image r cs e, Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ String -> DecodeError
DecodeError String
err
    Right ([], Maybe ByteString
Nothing) -> DecodeError -> m (Image r cs e, Maybe ByteString)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (DecodeError -> m (Image r cs e, Maybe ByteString))
-> DecodeError -> m (Image r cs e, Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ String -> DecodeError
DecodeError String
"PNM image is empty"
    Right ([], Maybe ByteString
_) -> DecodeError -> m (Image r cs e, Maybe ByteString)
forall (m :: * -> *) e a. (MonadThrow m, Exception e) => e -> m a
throwM (DecodeError -> m (Image r cs e, Maybe ByteString))
-> DecodeError -> m (Image r cs e, Maybe ByteString)
forall a b. (a -> b) -> a -> b
$ String -> DecodeError
DecodeError String
"Cannot parse PNM image"
    Right (PPM
ppm:[PPM]
_, Maybe ByteString
leftover) -> do
      Image r cs e
img <- f
-> (PPM -> String)
-> (PPM -> Maybe (Image r cs e))
-> PPM
-> m (Image r cs e)
forall r cs e a f (m :: * -> *).
(ColorModel cs e, FileFormat f, Typeable r, MonadThrow m) =>
f
-> (a -> String)
-> (a -> Maybe (Image r cs e))
-> a
-> m (Image r cs e)
fromMaybeDecode f
f PPM -> String
showNetpbmCS PPM -> Maybe (Image r cs e)
decoder PPM
ppm
      (Image r cs e, Maybe ByteString)
-> m (Image r cs e, Maybe ByteString)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Image r cs e
img, Maybe ByteString
leftover)
{-# INLINE decodePPM #-}


fromNetpbmImageUnsafe
  :: (Storable a, Storable (Pixel cs e))
  => Int -> Int -> V.Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe :: Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector a
v = do
  Bool -> Maybe ()
forall (f :: * -> *). Alternative f => Bool -> f ()
guard (Int
n Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
m Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Vector a -> Int
forall a. Storable a => Vector a -> Int
V.length Vector a
v)
  Sz Ix2 -> Vector a -> Maybe (Image S cs e)
forall (m :: * -> *) ix a b.
(MonadThrow m, Index ix, Storable a, Storable b) =>
Sz ix -> Vector a -> m (Array S ix b)
unsafeFromStorableVectorM (Ix2 -> Sz Ix2
forall ix. Index ix => ix -> Sz ix
Sz (Int
m Int -> Int -> Ix2
:. Int
n)) Vector a
v


showNetpbmCS :: Netpbm.PPM -> String
showNetpbmCS :: PPM -> String
showNetpbmCS Netpbm.PPM {PpmPixelData
ppmData :: PPM -> PpmPixelData
ppmData :: PpmPixelData
ppmData} =
  case PpmPixelData
ppmData of
    PbmPixelData Vector PbmPixel
_      -> String
"Image S Y Bit"
    PgmPixelData8 Vector PgmPixel8
_     -> String
"Image S Y Word8"
    PgmPixelData16 Vector PgmPixel16
_    -> String
"Image S Y Word16"
    PpmPixelDataRGB8 Vector PpmPixelRGB8
_  -> String
"Image S RGB Word8"
    PpmPixelDataRGB16 Vector PpmPixelRGB16
_ -> String
"Image S RGB Word16"



instance Readable PBM (Image S CM.X Bit) where
  decodeWithMetadataM :: PBM -> ByteString -> m (Image S X Bit, Metadata PBM)
decodeWithMetadataM = PBM -> ByteString -> m (Image S X Bit, Metadata PBM)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage
instance Readable PBM (Image S (Y D65) Bit) where
  decodeWithMetadataM :: PBM -> ByteString -> m (Image S (Y D65) Bit, Metadata PBM)
decodeWithMetadataM PBM
f = ((Image S X Bit, Maybe ByteString)
 -> (Image S (Y D65) Bit, Maybe ByteString))
-> m (Image S X Bit, Maybe ByteString)
-> m (Image S (Y D65) Bit, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S X Bit -> Image S (Y D65) Bit)
-> (Image S X Bit, Maybe ByteString)
-> (Image S (Y D65) Bit, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S X Bit -> Image S (Y D65) Bit
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S X Bit, Maybe ByteString)
 -> m (Image S (Y D65) Bit, Maybe ByteString))
-> (ByteString -> m (Image S X Bit, Maybe ByteString))
-> ByteString
-> m (Image S (Y D65) Bit, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PBM -> ByteString -> m (Image S X Bit, Metadata PBM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PBM
f


instance Readable (Sequence PBM) [Image S CM.X Bit] where
  decodeWithMetadataM :: Sequence PBM
-> ByteString -> m ([Image S X Bit], Metadata (Sequence PBM))
decodeWithMetadataM = (PPM -> Maybe (Image S X Bit))
-> Sequence PBM
-> ByteString
-> m ([Image S X Bit], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S X Bit)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage
instance Readable (Sequence PBM) [Image S (Y D65) Bit] where
  decodeWithMetadataM :: Sequence PBM
-> ByteString -> m ([Image S (Y D65) Bit], Metadata (Sequence PBM))
decodeWithMetadataM Sequence PBM
f = (([Image S X Bit], Maybe ByteString)
 -> ([Image S (Y D65) Bit], Maybe ByteString))
-> m ([Image S X Bit], Maybe ByteString)
-> m ([Image S (Y D65) Bit], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S X Bit] -> [Image S (Y D65) Bit])
-> ([Image S X Bit], Maybe ByteString)
-> ([Image S (Y D65) Bit], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S X Bit -> Image S (Y D65) Bit)
-> [Image S X Bit] -> [Image S (Y D65) Bit]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S X Bit -> Image S (Y D65) Bit
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S X Bit], Maybe ByteString)
 -> m ([Image S (Y D65) Bit], Maybe ByteString))
-> (ByteString -> m ([Image S X Bit], Maybe ByteString))
-> ByteString
-> m ([Image S (Y D65) Bit], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PBM
-> ByteString -> m ([Image S X Bit], Metadata (Sequence PBM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PBM
f


instance Readable PGM (Image S CM.X Word8) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S X Word8, Metadata PGM)
decodeWithMetadataM = PGM -> ByteString -> m (Image S X Word8, Metadata PGM)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage
instance Readable PGM (Image S CM.X Word16) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S X Word16, Metadata PGM)
decodeWithMetadataM = PGM -> ByteString -> m (Image S X Word16, Metadata PGM)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage


instance Readable PGM (Image S (Y' SRGB) Word8) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S (Y' SRGB) Word8, Metadata PGM)
decodeWithMetadataM PGM
f = ((Image S X Word8, Maybe ByteString)
 -> (Image S (Y' SRGB) Word8, Maybe ByteString))
-> m (Image S X Word8, Maybe ByteString)
-> m (Image S (Y' SRGB) Word8, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S X Word8 -> Image S (Y' SRGB) Word8)
-> (Image S X Word8, Maybe ByteString)
-> (Image S (Y' SRGB) Word8, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S X Word8 -> Image S (Y' SRGB) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S X Word8, Maybe ByteString)
 -> m (Image S (Y' SRGB) Word8, Maybe ByteString))
-> (ByteString -> m (Image S X Word8, Maybe ByteString))
-> ByteString
-> m (Image S (Y' SRGB) Word8, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGM -> ByteString -> m (Image S X Word8, Metadata PGM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PGM
f
instance Readable PGM (Image S (Y' SRGB) Word16) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S (Y' SRGB) Word16, Metadata PGM)
decodeWithMetadataM PGM
f = ((Image S X Word16, Maybe ByteString)
 -> (Image S (Y' SRGB) Word16, Maybe ByteString))
-> m (Image S X Word16, Maybe ByteString)
-> m (Image S (Y' SRGB) Word16, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S X Word16 -> Image S (Y' SRGB) Word16)
-> (Image S X Word16, Maybe ByteString)
-> (Image S (Y' SRGB) Word16, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S X Word16 -> Image S (Y' SRGB) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S X Word16, Maybe ByteString)
 -> m (Image S (Y' SRGB) Word16, Maybe ByteString))
-> (ByteString -> m (Image S X Word16, Maybe ByteString))
-> ByteString
-> m (Image S (Y' SRGB) Word16, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGM -> ByteString -> m (Image S X Word16, Metadata PGM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PGM
f
instance Readable PGM (Image S (Y D65) Word8) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S (Y D65) Word8, Metadata PGM)
decodeWithMetadataM PGM
f = ((Image S X Word8, Maybe ByteString)
 -> (Image S (Y D65) Word8, Maybe ByteString))
-> m (Image S X Word8, Maybe ByteString)
-> m (Image S (Y D65) Word8, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S X Word8 -> Image S (Y D65) Word8)
-> (Image S X Word8, Maybe ByteString)
-> (Image S (Y D65) Word8, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S X Word8 -> Image S (Y D65) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S X Word8, Maybe ByteString)
 -> m (Image S (Y D65) Word8, Maybe ByteString))
-> (ByteString -> m (Image S X Word8, Maybe ByteString))
-> ByteString
-> m (Image S (Y D65) Word8, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGM -> ByteString -> m (Image S X Word8, Metadata PGM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PGM
f
instance Readable PGM (Image S (Y D65) Word16) where
  decodeWithMetadataM :: PGM -> ByteString -> m (Image S (Y D65) Word16, Metadata PGM)
decodeWithMetadataM PGM
f = ((Image S X Word16, Maybe ByteString)
 -> (Image S (Y D65) Word16, Maybe ByteString))
-> m (Image S X Word16, Maybe ByteString)
-> m (Image S (Y D65) Word16, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S X Word16 -> Image S (Y D65) Word16)
-> (Image S X Word16, Maybe ByteString)
-> (Image S (Y D65) Word16, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S X Word16 -> Image S (Y D65) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S X Word16, Maybe ByteString)
 -> m (Image S (Y D65) Word16, Maybe ByteString))
-> (ByteString -> m (Image S X Word16, Maybe ByteString))
-> ByteString
-> m (Image S (Y D65) Word16, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PGM -> ByteString -> m (Image S X Word16, Metadata PGM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PGM
f


instance Readable (Sequence PGM) [Image S CM.X Word8] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString -> m ([Image S X Word8], Metadata (Sequence PGM))
decodeWithMetadataM = (PPM -> Maybe (Image S X Word8))
-> Sequence PGM
-> ByteString
-> m ([Image S X Word8], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S X Word8)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage
instance Readable (Sequence PGM) [Image S CM.X Word16] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString -> m ([Image S X Word16], Metadata (Sequence PGM))
decodeWithMetadataM = (PPM -> Maybe (Image S X Word16))
-> Sequence PGM
-> ByteString
-> m ([Image S X Word16], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S X Word16)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage


instance Readable (Sequence PGM) [Image S (Y' SRGB) Word8] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString
-> m ([Image S (Y' SRGB) Word8], Metadata (Sequence PGM))
decodeWithMetadataM Sequence PGM
f = (([Image S X Word8], Maybe ByteString)
 -> ([Image S (Y' SRGB) Word8], Maybe ByteString))
-> m ([Image S X Word8], Maybe ByteString)
-> m ([Image S (Y' SRGB) Word8], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S X Word8] -> [Image S (Y' SRGB) Word8])
-> ([Image S X Word8], Maybe ByteString)
-> ([Image S (Y' SRGB) Word8], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S X Word8 -> Image S (Y' SRGB) Word8)
-> [Image S X Word8] -> [Image S (Y' SRGB) Word8]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S X Word8 -> Image S (Y' SRGB) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S X Word8], Maybe ByteString)
 -> m ([Image S (Y' SRGB) Word8], Maybe ByteString))
-> (ByteString -> m ([Image S X Word8], Maybe ByteString))
-> ByteString
-> m ([Image S (Y' SRGB) Word8], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PGM
-> ByteString -> m ([Image S X Word8], Metadata (Sequence PGM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PGM
f
instance Readable (Sequence PGM) [Image S (Y' SRGB) Word16] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString
-> m ([Image S (Y' SRGB) Word16], Metadata (Sequence PGM))
decodeWithMetadataM Sequence PGM
f = (([Image S X Word16], Maybe ByteString)
 -> ([Image S (Y' SRGB) Word16], Maybe ByteString))
-> m ([Image S X Word16], Maybe ByteString)
-> m ([Image S (Y' SRGB) Word16], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S X Word16] -> [Image S (Y' SRGB) Word16])
-> ([Image S X Word16], Maybe ByteString)
-> ([Image S (Y' SRGB) Word16], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S X Word16 -> Image S (Y' SRGB) Word16)
-> [Image S X Word16] -> [Image S (Y' SRGB) Word16]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S X Word16 -> Image S (Y' SRGB) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S X Word16], Maybe ByteString)
 -> m ([Image S (Y' SRGB) Word16], Maybe ByteString))
-> (ByteString -> m ([Image S X Word16], Maybe ByteString))
-> ByteString
-> m ([Image S (Y' SRGB) Word16], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PGM
-> ByteString -> m ([Image S X Word16], Metadata (Sequence PGM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PGM
f
instance Readable (Sequence PGM) [Image S (Y D65) Word8] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString
-> m ([Image S (Y D65) Word8], Metadata (Sequence PGM))
decodeWithMetadataM Sequence PGM
f = (([Image S X Word8], Maybe ByteString)
 -> ([Image S (Y D65) Word8], Maybe ByteString))
-> m ([Image S X Word8], Maybe ByteString)
-> m ([Image S (Y D65) Word8], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S X Word8] -> [Image S (Y D65) Word8])
-> ([Image S X Word8], Maybe ByteString)
-> ([Image S (Y D65) Word8], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S X Word8 -> Image S (Y D65) Word8)
-> [Image S X Word8] -> [Image S (Y D65) Word8]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S X Word8 -> Image S (Y D65) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S X Word8], Maybe ByteString)
 -> m ([Image S (Y D65) Word8], Maybe ByteString))
-> (ByteString -> m ([Image S X Word8], Maybe ByteString))
-> ByteString
-> m ([Image S (Y D65) Word8], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PGM
-> ByteString -> m ([Image S X Word8], Metadata (Sequence PGM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PGM
f
instance Readable (Sequence PGM) [Image S (Y D65) Word16] where
  decodeWithMetadataM :: Sequence PGM
-> ByteString
-> m ([Image S (Y D65) Word16], Metadata (Sequence PGM))
decodeWithMetadataM Sequence PGM
f = (([Image S X Word16], Maybe ByteString)
 -> ([Image S (Y D65) Word16], Maybe ByteString))
-> m ([Image S X Word16], Maybe ByteString)
-> m ([Image S (Y D65) Word16], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S X Word16] -> [Image S (Y D65) Word16])
-> ([Image S X Word16], Maybe ByteString)
-> ([Image S (Y D65) Word16], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S X Word16 -> Image S (Y D65) Word16)
-> [Image S X Word16] -> [Image S (Y D65) Word16]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S X Word16 -> Image S (Y D65) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S X Word16], Maybe ByteString)
 -> m ([Image S (Y D65) Word16], Maybe ByteString))
-> (ByteString -> m ([Image S X Word16], Maybe ByteString))
-> ByteString
-> m ([Image S (Y D65) Word16], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PGM
-> ByteString -> m ([Image S X Word16], Metadata (Sequence PGM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PGM
f


instance Readable PPM (Image S CM.RGB Word8) where
  decodeWithMetadataM :: PPM -> ByteString -> m (Image S RGB Word8, Metadata PPM)
decodeWithMetadataM = PPM -> ByteString -> m (Image S RGB Word8, Metadata PPM)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage
instance Readable PPM (Image S CM.RGB Word16) where
  decodeWithMetadataM :: PPM -> ByteString -> m (Image S RGB Word16, Metadata PPM)
decodeWithMetadataM = PPM -> ByteString -> m (Image S RGB Word16, Metadata PPM)
forall f cs e (m :: * -> *).
(FileFormat f, ColorModel cs e, MonadThrow m) =>
f -> ByteString -> m (Image S cs e, Maybe ByteString)
decodeNetpbmImage


instance Readable PPM (Image S (SRGB 'NonLinear) Word8) where
  decodeWithMetadataM :: PPM
-> ByteString -> m (Image S (SRGB 'NonLinear) Word8, Metadata PPM)
decodeWithMetadataM PPM
f = ((Image S RGB Word8, Maybe ByteString)
 -> (Image S (SRGB 'NonLinear) Word8, Maybe ByteString))
-> m (Image S RGB Word8, Maybe ByteString)
-> m (Image S (SRGB 'NonLinear) Word8, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S RGB Word8 -> Image S (SRGB 'NonLinear) Word8)
-> (Image S RGB Word8, Maybe ByteString)
-> (Image S (SRGB 'NonLinear) Word8, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S RGB Word8 -> Image S (SRGB 'NonLinear) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S RGB Word8, Maybe ByteString)
 -> m (Image S (SRGB 'NonLinear) Word8, Maybe ByteString))
-> (ByteString -> m (Image S RGB Word8, Maybe ByteString))
-> ByteString
-> m (Image S (SRGB 'NonLinear) Word8, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PPM -> ByteString -> m (Image S RGB Word8, Metadata PPM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PPM
f
instance Readable PPM (Image S (SRGB 'NonLinear) Word16) where
  decodeWithMetadataM :: PPM
-> ByteString -> m (Image S (SRGB 'NonLinear) Word16, Metadata PPM)
decodeWithMetadataM PPM
f = ((Image S RGB Word16, Maybe ByteString)
 -> (Image S (SRGB 'NonLinear) Word16, Maybe ByteString))
-> m (Image S RGB Word16, Maybe ByteString)
-> m (Image S (SRGB 'NonLinear) Word16, Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Image S RGB Word16 -> Image S (SRGB 'NonLinear) Word16)
-> (Image S RGB Word16, Maybe ByteString)
-> (Image S (SRGB 'NonLinear) Word16, Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Image S RGB Word16 -> Image S (SRGB 'NonLinear) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel) (m (Image S RGB Word16, Maybe ByteString)
 -> m (Image S (SRGB 'NonLinear) Word16, Maybe ByteString))
-> (ByteString -> m (Image S RGB Word16, Maybe ByteString))
-> ByteString
-> m (Image S (SRGB 'NonLinear) Word16, Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PPM -> ByteString -> m (Image S RGB Word16, Metadata PPM)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM PPM
f


instance Readable (Sequence PPM) [Image S CM.RGB Word8] where
  decodeWithMetadataM :: Sequence PPM
-> ByteString -> m ([Image S RGB Word8], Metadata (Sequence PPM))
decodeWithMetadataM = (PPM -> Maybe (Image S RGB Word8))
-> Sequence PPM
-> ByteString
-> m ([Image S RGB Word8], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S RGB Word8)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage
instance Readable (Sequence PPM) [Image S CM.RGB Word16] where
  decodeWithMetadataM :: Sequence PPM
-> ByteString -> m ([Image S RGB Word16], Metadata (Sequence PPM))
decodeWithMetadataM = (PPM -> Maybe (Image S RGB Word16))
-> Sequence PPM
-> ByteString
-> m ([Image S RGB Word16], Maybe ByteString)
forall f r cs e (m :: * -> *).
(FileFormat f, Manifest r (Pixel cs e), ColorModel cs e,
 MonadThrow m) =>
(PPM -> Maybe (Image r cs e))
-> f -> ByteString -> m ([Image r cs e], Maybe ByteString)
decodePPMs PPM -> Maybe (Image S RGB Word16)
forall cs e. ColorModel cs e => PPM -> Maybe (Image S cs e)
fromNetpbmImage


instance Readable (Sequence PPM) [Image S (SRGB 'NonLinear) Word8] where
  decodeWithMetadataM :: Sequence PPM
-> ByteString
-> m ([Image S (SRGB 'NonLinear) Word8], Metadata (Sequence PPM))
decodeWithMetadataM Sequence PPM
f = (([Image S RGB Word8], Maybe ByteString)
 -> ([Image S (SRGB 'NonLinear) Word8], Maybe ByteString))
-> m ([Image S RGB Word8], Maybe ByteString)
-> m ([Image S (SRGB 'NonLinear) Word8], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S RGB Word8] -> [Image S (SRGB 'NonLinear) Word8])
-> ([Image S RGB Word8], Maybe ByteString)
-> ([Image S (SRGB 'NonLinear) Word8], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S RGB Word8 -> Image S (SRGB 'NonLinear) Word8)
-> [Image S RGB Word8] -> [Image S (SRGB 'NonLinear) Word8]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S RGB Word8 -> Image S (SRGB 'NonLinear) Word8
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S RGB Word8], Maybe ByteString)
 -> m ([Image S (SRGB 'NonLinear) Word8], Maybe ByteString))
-> (ByteString -> m ([Image S RGB Word8], Maybe ByteString))
-> ByteString
-> m ([Image S (SRGB 'NonLinear) Word8], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PPM
-> ByteString -> m ([Image S RGB Word8], Metadata (Sequence PPM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PPM
f
instance Readable (Sequence PPM) [Image S (SRGB 'NonLinear) Word16] where
  decodeWithMetadataM :: Sequence PPM
-> ByteString
-> m ([Image S (SRGB 'NonLinear) Word16], Metadata (Sequence PPM))
decodeWithMetadataM Sequence PPM
f = (([Image S RGB Word16], Maybe ByteString)
 -> ([Image S (SRGB 'NonLinear) Word16], Maybe ByteString))
-> m ([Image S RGB Word16], Maybe ByteString)
-> m ([Image S (SRGB 'NonLinear) Word16], Maybe ByteString)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (([Image S RGB Word16] -> [Image S (SRGB 'NonLinear) Word16])
-> ([Image S RGB Word16], Maybe ByteString)
-> ([Image S (SRGB 'NonLinear) Word16], Maybe ByteString)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first ((Image S RGB Word16 -> Image S (SRGB 'NonLinear) Word16)
-> [Image S RGB Word16] -> [Image S (SRGB 'NonLinear) Word16]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap Image S RGB Word16 -> Image S (SRGB 'NonLinear) Word16
forall cs e.
Matrix S (Pixel (BaseModel cs) e) -> Matrix S (Pixel cs e)
fromImageBaseModel)) (m ([Image S RGB Word16], Maybe ByteString)
 -> m ([Image S (SRGB 'NonLinear) Word16], Maybe ByteString))
-> (ByteString -> m ([Image S RGB Word16], Maybe ByteString))
-> ByteString
-> m ([Image S (SRGB 'NonLinear) Word16], Maybe ByteString)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Sequence PPM
-> ByteString -> m ([Image S RGB Word16], Metadata (Sequence PPM))
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM Sequence PPM
f


fromNetpbmImage
  :: forall cs e . ColorModel cs e =>
     Netpbm.PPM -> Maybe (Image S cs e)
fromNetpbmImage :: PPM -> Maybe (Image S cs e)
fromNetpbmImage Netpbm.PPM {PPMHeader
PpmPixelData
ppmHeader :: PPM -> PPMHeader
ppmData :: PpmPixelData
ppmHeader :: PPMHeader
ppmData :: PPM -> PpmPixelData
..} = do
  let m :: Int
m = PPMHeader -> Int
ppmHeight PPMHeader
ppmHeader
      n :: Int
n = PPMHeader -> Int
ppmWidth PPMHeader
ppmHeader
  case PpmPixelData
ppmData of
    PbmPixelData Vector PbmPixel
v      -> do Pixel cs e :~: Pixel X Bit
Refl <- Maybe (Pixel cs e :~: Pixel X Bit)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (Pixel cs e :~: Pixel CM.X Bit)
                              Int -> Int -> Vector PbmPixel -> Maybe (Image S cs e)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PbmPixel
v
    PgmPixelData8 Vector PgmPixel8
v     -> do Pixel cs e :~: Pixel X Word8
Refl <- Maybe (Pixel cs e :~: Pixel X Word8)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (Pixel cs e :~: Pixel CM.X Word8)
                              Int -> Int -> Vector PgmPixel8 -> Maybe (Image S cs e)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PgmPixel8
v
    PgmPixelData16 Vector PgmPixel16
v    -> do Pixel cs e :~: Pixel X Word16
Refl <- Maybe (Pixel cs e :~: Pixel X Word16)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (Pixel cs e :~: Pixel CM.X Word16)
                              Int -> Int -> Vector PgmPixel16 -> Maybe (Image S cs e)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PgmPixel16
v
    PpmPixelDataRGB8 Vector PpmPixelRGB8
v  -> do Pixel cs e :~: Pixel RGB Word8
Refl <- Maybe (Pixel cs e :~: Pixel RGB Word8)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (Pixel cs e :~: Pixel CM.RGB Word8)
                              Int -> Int -> Vector PpmPixelRGB8 -> Maybe (Image S cs e)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PpmPixelRGB8
v
    PpmPixelDataRGB16 Vector PpmPixelRGB16
v -> do Pixel cs e :~: Pixel RGB Word16
Refl <- Maybe (Pixel cs e :~: Pixel RGB Word16)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (Pixel cs e :~: Pixel CM.RGB Word16)
                              Int -> Int -> Vector PpmPixelRGB16 -> Maybe (Image S cs e)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PpmPixelRGB16
v


instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto PBM) (Image r cs e) where
  decodeWithMetadataM :: Auto PBM -> ByteString -> m (Image r cs e, Metadata (Auto PBM))
decodeWithMetadataM = Auto PBM -> ByteString -> m (Image r cs e, Metadata (Auto PBM))
forall f r cs e (m :: * -> *) i.
(FileFormat f, Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage
instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto (Sequence PBM)) [Image r cs e] where
  decodeWithMetadataM :: Auto (Sequence PBM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PBM)))
decodeWithMetadataM = Auto (Sequence PBM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PBM)))
forall f r cs e (m :: * -> *) i.
(FileFormat (Sequence f), Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
Auto (Sequence f)
-> ByteString -> m ([Image r cs e], Maybe ByteString)
decodeAutoNetpbmImageSequence

instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto PGM) (Image r cs e) where
  decodeWithMetadataM :: Auto PGM -> ByteString -> m (Image r cs e, Metadata (Auto PGM))
decodeWithMetadataM = Auto PGM -> ByteString -> m (Image r cs e, Metadata (Auto PGM))
forall f r cs e (m :: * -> *) i.
(FileFormat f, Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage
instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto (Sequence PGM)) [Image r cs e] where
  decodeWithMetadataM :: Auto (Sequence PGM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PGM)))
decodeWithMetadataM = Auto (Sequence PGM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PGM)))
forall f r cs e (m :: * -> *) i.
(FileFormat (Sequence f), Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
Auto (Sequence f)
-> ByteString -> m ([Image r cs e], Maybe ByteString)
decodeAutoNetpbmImageSequence

instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto PPM) (Image r cs e) where
  decodeWithMetadataM :: Auto PPM -> ByteString -> m (Image r cs e, Metadata (Auto PPM))
decodeWithMetadataM = Auto PPM -> ByteString -> m (Image r cs e, Metadata (Auto PPM))
forall f r cs e (m :: * -> *) i.
(FileFormat f, Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
f -> ByteString -> m (Image r cs e, Maybe ByteString)
decodeAutoNetpbmImage
instance (Manifest r (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto (Sequence PPM)) [Image r cs e] where
  decodeWithMetadataM :: Auto (Sequence PPM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PPM)))
decodeWithMetadataM = Auto (Sequence PPM)
-> ByteString -> m ([Image r cs e], Metadata (Auto (Sequence PPM)))
forall f r cs e (m :: * -> *) i.
(FileFormat (Sequence f), Manifest r (Pixel cs e), MonadThrow m,
 ColorSpace cs i e) =>
Auto (Sequence f)
-> ByteString -> m ([Image r cs e], Maybe ByteString)
decodeAutoNetpbmImageSequence

fromNetpbmImageAuto
  :: forall cs i e r . (Manifest r (Pixel cs e), ColorSpace cs i e) =>
     Netpbm.PPM -> Maybe (Image r cs e)
fromNetpbmImageAuto :: PPM -> Maybe (Image r cs e)
fromNetpbmImageAuto Netpbm.PPM {PPMHeader
PpmPixelData
ppmData :: PpmPixelData
ppmHeader :: PPMHeader
ppmHeader :: PPM -> PPMHeader
ppmData :: PPM -> PpmPixelData
..} = do
  let m :: Int
m = PPMHeader -> Int
ppmHeight PPMHeader
ppmHeader
      n :: Int
n = PPMHeader -> Int
ppmWidth PPMHeader
ppmHeader
  case PpmPixelData
ppmData of
    PbmPixelData Vector PbmPixel
v ->
      Array D Ix2 (Pixel cs e) -> Image r cs e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 (Pixel cs e) -> Image r cs e)
-> (Image S (Y D65) Bit -> Array D Ix2 (Pixel cs e))
-> Image S (Y D65) Bit
-> Image r cs e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Y D65) Bit -> Array D Ix2 (Pixel cs e)
forall r' cs' e' i' cs i e.
(Source r' (Pixel cs' e'), ColorSpace cs' i' e',
 ColorSpace cs i e) =>
Image r' cs' e' -> Image D cs e
convertImage (Image S (Y D65) Bit -> Image r cs e)
-> Maybe (Image S (Y D65) Bit) -> Maybe (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Int -> Vector PbmPixel -> Maybe (Image S (Y D65) Bit)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PbmPixel
v :: Maybe (Image S (Y D65) Bit))
    PgmPixelData8 Vector PgmPixel8
v ->
      Array D Ix2 (Pixel cs e) -> Image r cs e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 (Pixel cs e) -> Image r cs e)
-> (Image S (Y D65) Word8 -> Array D Ix2 (Pixel cs e))
-> Image S (Y D65) Word8
-> Image r cs e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Y D65) Word8 -> Array D Ix2 (Pixel cs e)
forall r' cs' e' i' cs i e.
(Source r' (Pixel cs' e'), ColorSpace cs' i' e',
 ColorSpace cs i e) =>
Image r' cs' e' -> Image D cs e
convertImage (Image S (Y D65) Word8 -> Image r cs e)
-> Maybe (Image S (Y D65) Word8) -> Maybe (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Int -> Vector PgmPixel8 -> Maybe (Image S (Y D65) Word8)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PgmPixel8
v :: Maybe (Image S (Y D65) Word8))
    PgmPixelData16 Vector PgmPixel16
v ->
      Array D Ix2 (Pixel cs e) -> Image r cs e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 (Pixel cs e) -> Image r cs e)
-> (Image S (Y D65) Word16 -> Array D Ix2 (Pixel cs e))
-> Image S (Y D65) Word16
-> Image r cs e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Y D65) Word16 -> Array D Ix2 (Pixel cs e)
forall r' cs' e' i' cs i e.
(Source r' (Pixel cs' e'), ColorSpace cs' i' e',
 ColorSpace cs i e) =>
Image r' cs' e' -> Image D cs e
convertImage (Image S (Y D65) Word16 -> Image r cs e)
-> Maybe (Image S (Y D65) Word16) -> Maybe (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (Int -> Int -> Vector PgmPixel16 -> Maybe (Image S (Y D65) Word16)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PgmPixel16
v :: Maybe (Image S (Y D65) Word16))
    PpmPixelDataRGB8 Vector PpmPixelRGB8
v ->
      Array D Ix2 (Pixel cs e) -> Image r cs e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 (Pixel cs e) -> Image r cs e)
-> (Image S (SRGB 'NonLinear) Word8 -> Array D Ix2 (Pixel cs e))
-> Image S (SRGB 'NonLinear) Word8
-> Image r cs e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (SRGB 'NonLinear) Word8 -> Array D Ix2 (Pixel cs e)
forall r' cs' e' i' cs i e.
(Source r' (Pixel cs' e'), ColorSpace cs' i' e',
 ColorSpace cs i e) =>
Image r' cs' e' -> Image D cs e
convertImage (Image S (SRGB 'NonLinear) Word8 -> Image r cs e)
-> Maybe (Image S (SRGB 'NonLinear) Word8) -> Maybe (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      (Int
-> Int
-> Vector PpmPixelRGB8
-> Maybe (Image S (SRGB 'NonLinear) Word8)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PpmPixelRGB8
v :: Maybe (Image S (SRGB 'NonLinear) Word8))
    PpmPixelDataRGB16 Vector PpmPixelRGB16
v ->
      Array D Ix2 (Pixel cs e) -> Image r cs e
forall r ix e r'.
(Manifest r e, Load r' ix e) =>
Array r' ix e -> Array r ix e
compute (Array D Ix2 (Pixel cs e) -> Image r cs e)
-> (Image S (SRGB 'NonLinear) Word16 -> Array D Ix2 (Pixel cs e))
-> Image S (SRGB 'NonLinear) Word16
-> Image r cs e
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (SRGB 'NonLinear) Word16 -> Array D Ix2 (Pixel cs e)
forall r' cs' e' i' cs i e.
(Source r' (Pixel cs' e'), ColorSpace cs' i' e',
 ColorSpace cs i e) =>
Image r' cs' e' -> Image D cs e
convertImage (Image S (SRGB 'NonLinear) Word16 -> Image r cs e)
-> Maybe (Image S (SRGB 'NonLinear) Word16) -> Maybe (Image r cs e)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$>
      (Int
-> Int
-> Vector PpmPixelRGB16
-> Maybe (Image S (SRGB 'NonLinear) Word16)
forall a cs e.
(Storable a, Storable (Pixel cs e)) =>
Int -> Int -> Vector a -> Maybe (Image S cs e)
fromNetpbmImageUnsafe Int
m Int
n Vector PpmPixelRGB16
v :: Maybe (Image S (SRGB 'NonLinear) Word16))