{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE UndecidableInstances #-}
-- |
-- Module      : Data.Massiv.Array.IO.Image.JuicyPixels.TGA
-- Copyright   : (c) Alexey Kuleshevich 2019-2020
-- License     : BSD3
-- Maintainer  : Alexey Kuleshevich <lehins@yandex.ru>
-- Stability   : experimental
-- Portability : non-portable
--
module Data.Massiv.Array.IO.Image.JuicyPixels.TGA
  ( TGA(..)
  , decodeTGA
  , decodeWithMetadataTGA
  , decodeAutoTGA
  , decodeAutoWithMetadataTGA
  , encodeTGA
  , encodeAutoTGA
  ) where

import qualified Codec.Picture as JP
import qualified Codec.Picture.Metadata as JP
import qualified Codec.Picture.Tga as JP
import Control.Monad (msum)
import Data.Bifunctor (first)
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as BL (ByteString)
import Data.Massiv.Array as A
import Data.Massiv.Array.IO.Base
import Data.Massiv.Array.IO.Image.JuicyPixels.Base
import Data.Maybe (fromMaybe)
import Data.Typeable
import qualified Graphics.Pixel as CM
import Graphics.Pixel.ColorSpace
import Prelude as P


--------------------------------------------------------------------------------
-- TGA Format ------------------------------------------------------------------
--------------------------------------------------------------------------------


-- | Truevision Graphics Adapter image with .tga extension.
data TGA = TGA deriving Int -> TGA -> ShowS
[TGA] -> ShowS
TGA -> String
(Int -> TGA -> ShowS)
-> (TGA -> String) -> ([TGA] -> ShowS) -> Show TGA
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [TGA] -> ShowS
$cshowList :: [TGA] -> ShowS
show :: TGA -> String
$cshow :: TGA -> String
showsPrec :: Int -> TGA -> ShowS
$cshowsPrec :: Int -> TGA -> ShowS
Show

instance FileFormat TGA where
  type Metadata TGA = JP.Metadatas
  ext :: TGA -> String
ext TGA
_ = String
".tga"

instance Writable TGA (Image A.S CM.X Bit) where
  encodeM :: TGA -> WriteOptions TGA -> Image S X Bit -> m ByteString
encodeM TGA
f WriteOptions TGA
opts Image S X Bit
img = TGA
-> WriteOptions TGA -> Array S Ix2 (Pixel X Word8) -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
f WriteOptions TGA
opts (Image S X Bit -> Array S Ix2 (Pixel X Word8)
coerceBinaryImage Image S X Bit
img)

instance Writable TGA (Image S CM.X Word8) where
  encodeM :: TGA
-> WriteOptions TGA -> Array S Ix2 (Pixel X Word8) -> m ByteString
encodeM TGA
TGA WriteOptions TGA
_ Array S Ix2 (Pixel X Word8)
img = ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString) -> ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ Image Word8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Array S Ix2 (Pixel X Word8) -> Image Word8
forall r.
Source r Ix2 (Pixel X Word8) =>
Image r X Word8 -> Image Word8
toJPImageY8 Array S Ix2 (Pixel X Word8)
img)

instance Writable TGA (Image S CM.RGB Word8) where
  encodeM :: TGA -> WriteOptions TGA -> Image S RGB Word8 -> m ByteString
encodeM TGA
TGA WriteOptions TGA
_ Image S RGB Word8
img = ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString) -> ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ Image PixelRGB8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image S RGB Word8 -> Image PixelRGB8
forall r.
Source r Ix2 (Pixel RGB Word8) =>
Image r RGB Word8 -> Image PixelRGB8
toJPImageRGB8 Image S RGB Word8
img)

instance Writable TGA (Image S (Alpha CM.RGB) Word8) where
  encodeM :: TGA
-> WriteOptions TGA -> Image S (Alpha RGB) Word8 -> m ByteString
encodeM TGA
TGA WriteOptions TGA
_ Image S (Alpha RGB) Word8
img = ByteString -> m ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> m ByteString) -> ByteString -> m ByteString
forall a b. (a -> b) -> a -> b
$ Image PixelRGBA8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image S (Alpha RGB) Word8 -> Image PixelRGBA8
forall r.
Source r Ix2 (Pixel (Alpha RGB) Word8) =>
Image r (Alpha RGB) Word8 -> Image PixelRGBA8
toJPImageRGBA8 Image S (Alpha RGB) Word8
img)


instance Writable TGA (Image S (Y' SRGB) Word8) where
  encodeM :: TGA -> WriteOptions TGA -> Image S (Y' SRGB) Word8 -> m ByteString
encodeM TGA
f WriteOptions TGA
opts = TGA
-> WriteOptions TGA -> Array S Ix2 (Pixel X Word8) -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
f WriteOptions TGA
opts (Array S Ix2 (Pixel X Word8) -> m ByteString)
-> (Image S (Y' SRGB) Word8 -> Array S Ix2 (Pixel X Word8))
-> Image S (Y' SRGB) Word8
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Y' SRGB) Word8 -> Array S Ix2 (Pixel X Word8)
forall cs e.
Array S Ix2 (Pixel cs e) -> Array S Ix2 (Pixel (BaseModel cs) e)
toImageBaseModel

instance Writable TGA (Image S (Y D65) Word8) where
  encodeM :: TGA -> WriteOptions TGA -> Image S (Y D65) Word8 -> m ByteString
encodeM TGA
f WriteOptions TGA
opts = TGA
-> WriteOptions TGA -> Array S Ix2 (Pixel X Word8) -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
f WriteOptions TGA
opts (Array S Ix2 (Pixel X Word8) -> m ByteString)
-> (Image S (Y D65) Word8 -> Array S Ix2 (Pixel X Word8))
-> Image S (Y D65) Word8
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Y D65) Word8 -> Array S Ix2 (Pixel X Word8)
forall cs e.
Array S Ix2 (Pixel cs e) -> Array S Ix2 (Pixel (BaseModel cs) e)
toImageBaseModel

instance Writable TGA (Image S (SRGB 'NonLinear) Word8) where
  encodeM :: TGA
-> WriteOptions TGA
-> Image S (SRGB 'NonLinear) Word8
-> m ByteString
encodeM TGA
f WriteOptions TGA
opts = TGA -> WriteOptions TGA -> Image S RGB Word8 -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
f WriteOptions TGA
opts (Image S RGB Word8 -> m ByteString)
-> (Image S (SRGB 'NonLinear) Word8 -> Image S RGB Word8)
-> Image S (SRGB 'NonLinear) Word8
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (SRGB 'NonLinear) Word8 -> Image S RGB Word8
forall cs e.
Array S Ix2 (Pixel cs e) -> Array S Ix2 (Pixel (BaseModel cs) e)
toImageBaseModel

instance Writable TGA (Image S (Alpha (SRGB 'NonLinear)) Word8) where
  encodeM :: TGA
-> WriteOptions TGA
-> Image S (Alpha (SRGB 'NonLinear)) Word8
-> m ByteString
encodeM TGA
f WriteOptions TGA
opts = TGA
-> WriteOptions TGA -> Image S (Alpha RGB) Word8 -> m ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
f WriteOptions TGA
opts (Image S (Alpha RGB) Word8 -> m ByteString)
-> (Image S (Alpha (SRGB 'NonLinear)) Word8
    -> Image S (Alpha RGB) Word8)
-> Image S (Alpha (SRGB 'NonLinear)) Word8
-> m ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image S (Alpha (SRGB 'NonLinear)) Word8
-> Image S (Alpha RGB) Word8
forall cs e.
Array S Ix2 (Pixel cs e) -> Array S Ix2 (Pixel (BaseModel cs) e)
toImageBaseModel


instance (ColorSpace cs i e, ColorSpace (BaseSpace cs) i e, Source r Ix2 (Pixel cs e)) =>
         Writable (Auto TGA) (Image r cs e) where
  encodeM :: Auto TGA -> WriteOptions (Auto TGA) -> Image r cs e -> m ByteString
encodeM Auto TGA
f WriteOptions (Auto TGA)
_ = 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


instance Readable TGA (Image S CM.X Word8) where
  decodeWithMetadataM :: TGA -> ByteString -> m (Array S Ix2 (Pixel X Word8), Metadata TGA)
decodeWithMetadataM = TGA -> ByteString -> m (Array S Ix2 (Pixel X Word8), Metadata TGA)
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TGA -> ByteString -> m (Image S cs e, Metadatas)
decodeWithMetadataTGA

instance Readable TGA (Image S CM.RGB Word8) where
  decodeWithMetadataM :: TGA -> ByteString -> m (Image S RGB Word8, Metadata TGA)
decodeWithMetadataM = TGA -> ByteString -> m (Image S RGB Word8, Metadata TGA)
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TGA -> ByteString -> m (Image S cs e, Metadatas)
decodeWithMetadataTGA

instance Readable TGA (Image S (Alpha CM.RGB) Word8) where
  decodeWithMetadataM :: TGA -> ByteString -> m (Image S (Alpha RGB) Word8, Metadata TGA)
decodeWithMetadataM = TGA -> ByteString -> m (Image S (Alpha RGB) Word8, Metadata TGA)
forall cs e (m :: * -> *).
(ColorModel cs e, MonadThrow m) =>
TGA -> ByteString -> m (Image S cs e, Metadatas)
decodeWithMetadataTGA


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

instance Readable TGA (Image S (Y D65) Word8) where
  decodeWithMetadataM :: TGA -> ByteString -> m (Image S (Y D65) Word8, Metadata TGA)
decodeWithMetadataM TGA
f = ((Array S Ix2 (Pixel X Word8), Metadatas)
 -> (Image S (Y D65) Word8, Metadatas))
-> m (Array S Ix2 (Pixel X Word8), Metadatas)
-> m (Image S (Y D65) Word8, Metadatas)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap ((Array S Ix2 (Pixel X Word8) -> Image S (Y D65) Word8)
-> (Array S Ix2 (Pixel X Word8), Metadatas)
-> (Image S (Y D65) Word8, Metadatas)
forall (p :: * -> * -> *) a b c.
Bifunctor p =>
(a -> b) -> p a c -> p b c
first Array S Ix2 (Pixel X Word8) -> Image S (Y D65) Word8
forall cs e.
Array S Ix2 (Pixel (BaseModel cs) e) -> Array S Ix2 (Pixel cs e)
fromImageBaseModel) (m (Array S Ix2 (Pixel X Word8), Metadatas)
 -> m (Image S (Y D65) Word8, Metadatas))
-> (ByteString -> m (Array S Ix2 (Pixel X Word8), Metadatas))
-> ByteString
-> m (Image S (Y D65) Word8, Metadatas)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TGA -> ByteString -> m (Array S Ix2 (Pixel X Word8), Metadata TGA)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM TGA
f

instance Readable TGA (Image S (SRGB 'NonLinear) Word8) where
  decodeWithMetadataM :: TGA
-> ByteString -> m (Image S (SRGB 'NonLinear) Word8, Metadata TGA)
decodeWithMetadataM TGA
f = ((Image S RGB Word8, Metadatas)
 -> (Image S (SRGB 'NonLinear) Word8, Metadatas))
-> m (Image S RGB Word8, Metadatas)
-> m (Image S (SRGB 'NonLinear) Word8, Metadatas)
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, Metadatas)
-> (Image S (SRGB 'NonLinear) Word8, Metadatas)
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.
Array S Ix2 (Pixel (BaseModel cs) e) -> Array S Ix2 (Pixel cs e)
fromImageBaseModel) (m (Image S RGB Word8, Metadatas)
 -> m (Image S (SRGB 'NonLinear) Word8, Metadatas))
-> (ByteString -> m (Image S RGB Word8, Metadatas))
-> ByteString
-> m (Image S (SRGB 'NonLinear) Word8, Metadatas)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TGA -> ByteString -> m (Image S RGB Word8, Metadata TGA)
forall f arr (m :: * -> *).
(Readable f arr, MonadThrow m) =>
f -> ByteString -> m (arr, Metadata f)
decodeWithMetadataM TGA
f

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

-- | Decode a Tga Image
decodeTGA :: (ColorModel cs e, MonadThrow m) => TGA -> B.ByteString -> m (Image S cs e)
decodeTGA :: TGA -> ByteString -> m (Image S cs e)
decodeTGA TGA
f ByteString
bs = TGA -> Either String DynamicImage -> m (Image S cs e)
forall (m :: * -> *) cs e f.
(MonadThrow m, ColorModel cs e, FileFormat f) =>
f -> Either String DynamicImage -> m (Image S cs e)
convertWith TGA
f (ByteString -> Either String DynamicImage
JP.decodeTga ByteString
bs)

-- | Decode a Tga Image
decodeWithMetadataTGA ::
     (ColorModel cs e, MonadThrow m) => TGA -> B.ByteString -> m (Image S cs e, JP.Metadatas)
decodeWithMetadataTGA :: TGA -> ByteString -> m (Image S cs e, Metadatas)
decodeWithMetadataTGA TGA
f ByteString
bs = TGA
-> Either String (DynamicImage, Metadata TGA)
-> m (Image S cs e, Metadata TGA)
forall (m :: * -> *) cs e f.
(MonadThrow m, ColorModel cs e, FileFormat f) =>
f
-> Either String (DynamicImage, Metadata f)
-> m (Image S cs e, Metadata f)
convertWithMetadata TGA
f (ByteString -> Either String (DynamicImage, Metadatas)
JP.decodeTgaWithMetadata ByteString
bs)


-- | Decode a Tga Image
decodeAutoTGA ::
     (Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m)
  => Auto TGA
  -> B.ByteString
  -> m (Image r cs e)
decodeAutoTGA :: Auto TGA -> ByteString -> m (Image r cs e)
decodeAutoTGA Auto TGA
f ByteString
bs = Auto TGA -> Either String DynamicImage -> m (Image r cs e)
forall (m :: * -> *) r cs e i f.
(MonadThrow m, Mutable r Ix2 (Pixel cs e), ColorSpace cs i e) =>
Auto f -> Either String DynamicImage -> m (Image r cs e)
convertAutoWith Auto TGA
f (ByteString -> Either String DynamicImage
JP.decodeTga ByteString
bs)

-- | Decode a Tga Image
decodeAutoWithMetadataTGA ::
     (Mutable r Ix2 (Pixel cs e), ColorSpace cs i e, MonadThrow m)
  => Auto TGA
  -> B.ByteString
  -> m (Image r cs e, JP.Metadatas)
decodeAutoWithMetadataTGA :: Auto TGA -> ByteString -> m (Image r cs e, Metadatas)
decodeAutoWithMetadataTGA Auto TGA
f ByteString
bs = Auto TGA
-> Either String (DynamicImage, Metadata TGA)
-> m (Image r cs e, Metadata TGA)
forall (m :: * -> *) r cs e i f.
(MonadThrow m, Mutable r Ix2 (Pixel cs e), ColorSpace cs i e) =>
Auto f
-> Either String (DynamicImage, Metadata f)
-> m (Image r cs e, Metadata f)
convertAutoWithMetadata Auto TGA
f (ByteString -> Either String (DynamicImage, Metadatas)
JP.decodeTgaWithMetadata ByteString
bs)


instance (Mutable r Ix2 (Pixel cs e), ColorSpace cs i e) =>
         Readable (Auto TGA) (Image r cs e) where
  decodeWithMetadataM :: Auto TGA -> ByteString -> m (Image r cs e, Metadata (Auto TGA))
decodeWithMetadataM = Auto TGA -> ByteString -> m (Image r cs e, Metadata (Auto 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, Metadatas)
decodeAutoWithMetadataTGA

encodeTGA ::
     forall cs e m. (ColorModel cs e, MonadThrow m)
  => TGA
  -> Image S cs e
  -> m BL.ByteString
encodeTGA :: TGA -> Image S cs e -> m ByteString
encodeTGA TGA
f Image S cs e
img = TGA -> Proxy (Image S cs e) -> Maybe ByteString -> m ByteString
forall f r cs e b (m :: * -> *).
(ColorModel cs e, FileFormat f, Typeable r, MonadThrow m) =>
f -> Proxy (Image r cs e) -> Maybe b -> m b
fromMaybeEncode TGA
f (Proxy (Image S cs e)
forall k (t :: k). Proxy t
Proxy :: Proxy (Image S cs e)) Maybe ByteString
encoded
  where
    encoded :: Maybe ByteString
encoded
      | Just 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) = TGA -> WriteOptions TGA -> Image S cs e -> Maybe ByteString
forall f arr (m :: * -> *).
(Writable f arr, MonadThrow m) =>
f -> WriteOptions f -> arr -> m ByteString
encodeM TGA
TGA WriteOptions TGA
forall a. Default a => a
def Image S cs e
img
      | Just e :~: Word8
Refl <- Maybe (e :~: Word8)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (e :~: Word8) = do
        [Maybe ByteString] -> Maybe ByteString
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
          [ Image Word8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image Word8 -> ByteString)
-> Maybe (Image Word8) -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Image S cs Word8 -> Maybe (Image Word8)
forall cs.
(Typeable cs, Source S Ix2 (Pixel cs Word8)) =>
Image S cs Word8 -> Maybe (Image Word8)
maybeJPImageY8 Image S cs e
Image S cs Word8
img
          , Image PixelRGB8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image PixelRGB8 -> ByteString)
-> Maybe (Image PixelRGB8) -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Image S cs Word8 -> Maybe (Image PixelRGB8)
forall cs.
(Typeable cs, Source S Ix2 (Pixel cs Word8)) =>
Image S cs Word8 -> Maybe (Image PixelRGB8)
maybeJPImageRGB8 Image S cs e
Image S cs Word8
img
          , do cs :~: Alpha (Opaque cs)
Refl <- Maybe (cs :~: Alpha (Opaque cs))
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (cs :~: Alpha (Opaque cs))
               Image PixelRGBA8 -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image PixelRGBA8 -> ByteString)
-> Maybe (Image PixelRGBA8) -> Maybe ByteString
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Image S (Alpha (Opaque cs)) Word8 -> Maybe (Image PixelRGBA8)
forall cs.
(Typeable cs, Source S Ix2 (Pixel (Alpha cs) Word8)) =>
Image S (Alpha cs) Word8 -> Maybe (Image PixelRGBA8)
maybeJPImageRGBA8 Image S cs e
Image S (Alpha (Opaque cs)) Word8
img
          ]
      | Bool
otherwise = Maybe ByteString
forall a. Maybe a
Nothing

encodeAutoTGA ::
     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
  -> BL.ByteString
encodeAutoTGA :: Auto TGA -> Image r cs e -> ByteString
encodeAutoTGA Auto TGA
_ Image r cs e
img =
  ByteString -> Maybe ByteString -> ByteString
forall a. a -> Maybe a -> a
fromMaybe ((Array D Ix2 (Pixel RGB Word8) -> Image PixelRGB8)
-> (Pixel cs e -> Pixel RGB Word8) -> Image r cs e -> ByteString
forall px ix a b.
(TgaSaveable px, Source r ix a) =>
(Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D Ix2 (Pixel RGB Word8) -> Image PixelRGB8
forall r.
Source r Ix2 (Pixel RGB Word8) =>
Image r RGB Word8 -> Image PixelRGB8
toJPImageRGB8 Pixel cs e -> Pixel RGB Word8
forall cs i e. ColorSpace cs i e => Pixel cs e -> Pixel RGB Word8
toSRGB8 Image r cs e
img) (Maybe ByteString -> ByteString) -> Maybe ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$
  [Maybe ByteString] -> Maybe ByteString
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
    [ do BaseModel cs :~: X
Refl <- Maybe (BaseModel cs :~: X)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (BaseModel cs :~: CM.X)
         [Maybe ByteString] -> Maybe ByteString
forall (t :: * -> *) (m :: * -> *) a.
(Foldable t, MonadPlus m) =>
t (m a) -> m a
msum
           [ do e :~: Bit
Refl <- Maybe (e :~: Bit)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (e :~: Bit)
                ByteString -> Maybe ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ (Array D Ix2 (Pixel X Word8) -> Image Word8)
-> (Pixel cs e -> Pixel X Word8) -> Image r cs e -> ByteString
forall px ix a b.
(TgaSaveable px, Source r ix a) =>
(Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D Ix2 (Pixel X Word8) -> Image Word8
forall r.
Source r Ix2 (Pixel X Word8) =>
Image r X Word8 -> Image Word8
toJPImageY8 (Pixel X e -> Pixel X Word8
forall cs e. ColorModel cs e => Pixel cs e -> Pixel cs Word8
toPixel8 (Pixel X e -> Pixel X Word8)
-> (Pixel cs e -> Pixel X e) -> Pixel cs e -> Pixel X Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel cs e -> Pixel X e
forall k cs (i :: k) e.
ColorSpace cs i e =>
Pixel cs e -> Pixel (BaseModel cs) e
toPixelBaseModel) Image r cs e
img
           , do e :~: Word8
Refl <- Maybe (e :~: Word8)
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (e :~: Word8)
                ByteString -> Maybe ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ (Array D Ix2 (Pixel X Word8) -> Image Word8)
-> (Pixel cs Word8 -> Pixel X Word8)
-> Array r Ix2 (Pixel cs Word8)
-> ByteString
forall px ix a b.
(TgaSaveable px, Source r ix a) =>
(Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D Ix2 (Pixel X Word8) -> Image Word8
forall r.
Source r Ix2 (Pixel X Word8) =>
Image r X Word8 -> Image Word8
toJPImageY8 Pixel cs Word8 -> Pixel X Word8
forall k cs (i :: k) e.
ColorSpace cs i e =>
Pixel cs e -> Pixel (BaseModel cs) e
toPixelBaseModel Image r cs e
Array r Ix2 (Pixel cs Word8)
img
           , ByteString -> Maybe ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ (Array D Ix2 (Pixel X Word8) -> Image Word8)
-> (Pixel cs e -> Pixel X Word8) -> Image r cs e -> ByteString
forall px ix a b.
(TgaSaveable px, Source r ix a) =>
(Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D Ix2 (Pixel X Word8) -> Image Word8
forall r.
Source r Ix2 (Pixel X Word8) =>
Image r X Word8 -> Image Word8
toJPImageY8 (Pixel X e -> Pixel X Word8
forall cs e. ColorModel cs e => Pixel cs e -> Pixel cs Word8
toPixel8 (Pixel X e -> Pixel X Word8)
-> (Pixel cs e -> Pixel X e) -> Pixel cs e -> Pixel X Word8
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel cs e -> Pixel X e
forall k cs (i :: k) e.
ColorSpace cs i e =>
Pixel cs e -> Pixel (BaseModel cs) e
toPixelBaseModel) Image r cs e
img
           ]
    , do cs :~: Alpha (Opaque cs)
Refl <- Maybe (cs :~: Alpha (Opaque cs))
forall k (a :: k) (b :: k).
(Typeable a, Typeable b) =>
Maybe (a :~: b)
eqT :: Maybe (cs :~: Alpha (Opaque cs))
         ByteString -> Maybe ByteString
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ByteString -> Maybe ByteString) -> ByteString -> Maybe ByteString
forall a b. (a -> b) -> a -> b
$ (Array D Ix2 (Pixel (Alpha RGB) Word8) -> Image PixelRGBA8)
-> (Pixel cs e -> Pixel (Alpha RGB) Word8)
-> Image r cs e
-> ByteString
forall px ix a b.
(TgaSaveable px, Source r ix a) =>
(Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D Ix2 (Pixel (Alpha RGB) Word8) -> Image PixelRGBA8
forall r.
Source r Ix2 (Pixel (Alpha RGB) Word8) =>
Image r (Alpha RGB) Word8 -> Image PixelRGBA8
toJPImageRGBA8 Pixel cs e -> Pixel (Alpha RGB) Word8
forall cs i e.
ColorSpace cs i e =>
Pixel cs e -> Pixel (Alpha RGB) Word8
toSRGBA8 Image r cs e
img
    ]
  where
    toTga ::
         (JP.TgaSaveable px, Source r ix a)
      => (Array D ix b -> JP.Image px)
      -> (a -> b)
      -> Array r ix a
      -> BL.ByteString
    toTga :: (Array D ix b -> Image px)
-> (a -> b) -> Array r ix a -> ByteString
toTga Array D ix b -> Image px
toJP a -> b
adjustPixel = Image px -> ByteString
forall px. TgaSaveable px => Image px -> ByteString
JP.encodeTga (Image px -> ByteString)
-> (Array r ix a -> Image px) -> Array r ix a -> ByteString
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Array D ix b -> Image px
toJP (Array D ix b -> Image px)
-> (Array r ix a -> Array D ix b) -> Array r ix a -> Image px
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> Array r ix a -> Array D ix b
forall r ix e' e.
Source r ix e' =>
(e' -> e) -> Array r ix e' -> Array D ix e
A.map a -> b
adjustPixel