{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE InstanceSigs #-}
{-# LANGUAGE CPP #-}
module Codec.Picture.Tiff( decodeTiff
, decodeTiffWithMetadata
, decodeTiffWithPaletteAndMetadata
, TiffSaveable
, encodeTiff
, writeTiff
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative( (<$>), (<*>), pure )
import Data.Monoid( mempty )
#endif
import Control.Arrow( first )
import Control.Monad( when, foldM_, unless, forM_ )
import Control.Monad.ST( ST, runST )
import Control.Monad.Writer.Strict( execWriter, tell, Writer )
import Data.Int( Int8 )
import Data.Word( Word8, Word16, Word32 )
import Data.Bits( (.&.), (.|.), unsafeShiftL, unsafeShiftR )
import Data.Binary.Get( Get )
import Data.Binary.Put( runPut )
import qualified Data.Vector as V
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as M
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as Lb
import qualified Data.ByteString.Unsafe as BU
import Foreign.Storable( sizeOf )
import Codec.Picture.Metadata.Exif
import Codec.Picture.Metadata( Metadatas )
import Codec.Picture.InternalHelper
import Codec.Picture.BitWriter
import Codec.Picture.Types
import Codec.Picture.Gif.Internal.LZW
import Codec.Picture.Tiff.Internal.Types
import Codec.Picture.Tiff.Internal.Metadata
import Codec.Picture.VectorByteConversion( toByteString )
data TiffInfo = TiffInfo
{ :: TiffHeader
, TiffInfo -> Pixel32
tiffWidth :: Word32
, TiffInfo -> Pixel32
tiffHeight :: Word32
, TiffInfo -> TiffColorspace
tiffColorspace :: TiffColorspace
, TiffInfo -> Pixel32
tiffSampleCount :: Word32
, TiffInfo -> Pixel32
tiffRowPerStrip :: Word32
, TiffInfo -> TiffPlanarConfiguration
tiffPlaneConfiguration :: TiffPlanarConfiguration
, TiffInfo -> [TiffSampleFormat]
tiffSampleFormat :: [TiffSampleFormat]
, TiffInfo -> Vector Pixel32
tiffBitsPerSample :: V.Vector Word32
, TiffInfo -> TiffCompression
tiffCompression :: TiffCompression
, TiffInfo -> Vector Pixel32
tiffStripSize :: V.Vector Word32
, TiffInfo -> Vector Pixel32
tiffOffsets :: V.Vector Word32
, TiffInfo -> Maybe (Image PixelRGB16)
tiffPalette :: Maybe (Image PixelRGB16)
, TiffInfo -> Vector Pixel32
tiffYCbCrSubsampling :: V.Vector Word32
, :: Maybe ExtraSample
, TiffInfo -> Predictor
tiffPredictor :: Predictor
, TiffInfo -> Metadatas
tiffMetadatas :: Metadatas
}
unLong :: String -> ExifData -> Get (V.Vector Word32)
unLong :: String -> ExifData -> Get (Vector Pixel32)
unLong String
_ (ExifLong Pixel32
v) = Vector Pixel32 -> Get (Vector Pixel32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector Pixel32 -> Get (Vector Pixel32))
-> Vector Pixel32 -> Get (Vector Pixel32)
forall a b. (a -> b) -> a -> b
$ Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
v
unLong String
_ (ExifShort Pixel16
v) = Vector Pixel32 -> Get (Vector Pixel32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector Pixel32 -> Get (Vector Pixel32))
-> Vector Pixel32 -> Get (Vector Pixel32)
forall a b. (a -> b) -> a -> b
$ Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton (Pixel16 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel16
v)
unLong String
_ (ExifShorts Vector Pixel16
v) = Vector Pixel32 -> Get (Vector Pixel32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Vector Pixel32 -> Get (Vector Pixel32))
-> Vector Pixel32 -> Get (Vector Pixel32)
forall a b. (a -> b) -> a -> b
$ (Pixel16 -> Pixel32) -> Vector Pixel16 -> Vector Pixel32
forall a b. (a -> b) -> Vector a -> Vector b
V.map Pixel16 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Vector Pixel16
v
unLong String
_ (ExifLongs Vector Pixel32
v) = Vector Pixel32 -> Get (Vector Pixel32)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Vector Pixel32
v
unLong String
errMessage ExifData
_ = String -> Get (Vector Pixel32)
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errMessage
findIFD :: String -> ExifTag -> [ImageFileDirectory]
-> Get ImageFileDirectory
findIFD :: String -> ExifTag -> [ImageFileDirectory] -> Get ImageFileDirectory
findIFD String
errorMessage ExifTag
tag [ImageFileDirectory]
lst =
case [ImageFileDirectory
v | ImageFileDirectory
v <- [ImageFileDirectory]
lst, ImageFileDirectory -> ExifTag
ifdIdentifier ImageFileDirectory
v ExifTag -> ExifTag -> Bool
forall a. Eq a => a -> a -> Bool
== ExifTag
tag] of
[] -> String -> Get ImageFileDirectory
forall (m :: * -> *) a. MonadFail m => String -> m a
fail String
errorMessage
(ImageFileDirectory
x:[ImageFileDirectory]
_) -> ImageFileDirectory -> Get ImageFileDirectory
forall (f :: * -> *) a. Applicative f => a -> f a
pure ImageFileDirectory
x
findPalette :: [ImageFileDirectory] -> Get (Maybe (Image PixelRGB16))
findPalette :: [ImageFileDirectory] -> Get (Maybe (Image PixelRGB16))
findPalette [ImageFileDirectory]
ifds =
case [ImageFileDirectory
v | ImageFileDirectory
v <- [ImageFileDirectory]
ifds, ImageFileDirectory -> ExifTag
ifdIdentifier ImageFileDirectory
v ExifTag -> ExifTag -> Bool
forall a. Eq a => a -> a -> Bool
== ExifTag
TagColorMap] of
(ImageFileDirectory { ifdExtended :: ImageFileDirectory -> ExifData
ifdExtended = ExifShorts Vector Pixel16
vec }:[ImageFileDirectory]
_) ->
Maybe (Image PixelRGB16) -> Get (Maybe (Image PixelRGB16))
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Maybe (Image PixelRGB16) -> Get (Maybe (Image PixelRGB16)))
-> (Vector Pixel16 -> Maybe (Image PixelRGB16))
-> Vector Pixel16
-> Get (Maybe (Image PixelRGB16))
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB16 -> Maybe (Image PixelRGB16)
forall a. a -> Maybe a
Just (Image PixelRGB16 -> Maybe (Image PixelRGB16))
-> (Vector Pixel16 -> Image PixelRGB16)
-> Vector Pixel16
-> Maybe (Image PixelRGB16)
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Int
-> Int
-> Vector (PixelBaseComponent PixelRGB16)
-> Image PixelRGB16
forall a. Int -> Int -> Vector (PixelBaseComponent a) -> Image a
Image Int
pixelCount Int
1 (Vector Pixel16 -> Get (Maybe (Image PixelRGB16)))
-> Vector Pixel16 -> Get (Maybe (Image PixelRGB16))
forall a b. (a -> b) -> a -> b
$ Int -> (Int -> Pixel16) -> Vector Pixel16
forall a. Storable a => Int -> (Int -> a) -> Vector a
VS.generate (Vector Pixel16 -> Int
forall a. Vector a -> Int
V.length Vector Pixel16
vec) Int -> Pixel16
axx
where pixelCount :: Int
pixelCount = Vector Pixel16 -> Int
forall a. Vector a -> Int
V.length Vector Pixel16
vec Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
3
axx :: Int -> Pixel16
axx Int
v = Vector Pixel16
vec Vector Pixel16 -> Int -> Pixel16
forall a. Vector a -> Int -> a
`V.unsafeIndex` (Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
color Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
pixelCount)
where (Int
idx, Int
color) = Int
v Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`divMod` Int
3
[ImageFileDirectory]
_ -> Maybe (Image PixelRGB16) -> Get (Maybe (Image PixelRGB16))
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe (Image PixelRGB16)
forall a. Maybe a
Nothing
findIFDData :: String -> ExifTag -> [ImageFileDirectory] -> Get Word32
findIFDData :: String -> ExifTag -> [ImageFileDirectory] -> Get Pixel32
findIFDData String
msg ExifTag
tag [ImageFileDirectory]
lst = ImageFileDirectory -> Pixel32
ifdOffset (ImageFileDirectory -> Pixel32)
-> Get ImageFileDirectory -> Get Pixel32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> ExifTag -> [ImageFileDirectory] -> Get ImageFileDirectory
findIFD String
msg ExifTag
tag [ImageFileDirectory]
lst
findIFDDefaultData :: Word32 -> ExifTag -> [ImageFileDirectory] -> Get Word32
findIFDDefaultData :: Pixel32 -> ExifTag -> [ImageFileDirectory] -> Get Pixel32
findIFDDefaultData Pixel32
d ExifTag
tag [ImageFileDirectory]
lst =
case [ImageFileDirectory
v | ImageFileDirectory
v <- [ImageFileDirectory]
lst, ImageFileDirectory -> ExifTag
ifdIdentifier ImageFileDirectory
v ExifTag -> ExifTag -> Bool
forall a. Eq a => a -> a -> Bool
== ExifTag
tag] of
[] -> Pixel32 -> Get Pixel32
forall (f :: * -> *) a. Applicative f => a -> f a
pure Pixel32
d
(ImageFileDirectory
x:[ImageFileDirectory]
_) -> Pixel32 -> Get Pixel32
forall (f :: * -> *) a. Applicative f => a -> f a
pure (Pixel32 -> Get Pixel32) -> Pixel32 -> Get Pixel32
forall a b. (a -> b) -> a -> b
$ ImageFileDirectory -> Pixel32
ifdOffset ImageFileDirectory
x
findIFDExt :: String -> ExifTag -> [ImageFileDirectory] -> Get ExifData
findIFDExt :: String -> ExifTag -> [ImageFileDirectory] -> Get ExifData
findIFDExt String
msg ExifTag
tag [ImageFileDirectory]
lst = do
ImageFileDirectory
val <- String -> ExifTag -> [ImageFileDirectory] -> Get ImageFileDirectory
findIFD String
msg ExifTag
tag [ImageFileDirectory]
lst
case ImageFileDirectory
val of
ImageFileDirectory
{ ifdCount :: ImageFileDirectory -> Pixel32
ifdCount = Pixel32
1, ifdOffset :: ImageFileDirectory -> Pixel32
ifdOffset = Pixel32
ofs, ifdType :: ImageFileDirectory -> IfdType
ifdType = IfdType
TypeShort } ->
ExifData -> Get ExifData
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExifData -> Get ExifData)
-> (Pixel16 -> ExifData) -> Pixel16 -> Get ExifData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Pixel16 -> ExifData
ExifShorts (Vector Pixel16 -> ExifData)
-> (Pixel16 -> Vector Pixel16) -> Pixel16 -> ExifData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel16 -> Vector Pixel16
forall a. a -> Vector a
V.singleton (Pixel16 -> Get ExifData) -> Pixel16 -> Get ExifData
forall a b. (a -> b) -> a -> b
$ Pixel32 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
ofs
ImageFileDirectory
{ ifdCount :: ImageFileDirectory -> Pixel32
ifdCount = Pixel32
1, ifdOffset :: ImageFileDirectory -> Pixel32
ifdOffset = Pixel32
ofs, ifdType :: ImageFileDirectory -> IfdType
ifdType = IfdType
TypeLong } ->
ExifData -> Get ExifData
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ExifData -> Get ExifData)
-> (Pixel32 -> ExifData) -> Pixel32 -> Get ExifData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Vector Pixel32 -> ExifData
ExifLongs (Vector Pixel32 -> ExifData)
-> (Pixel32 -> Vector Pixel32) -> Pixel32 -> ExifData
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton (Pixel32 -> Get ExifData) -> Pixel32 -> Get ExifData
forall a b. (a -> b) -> a -> b
$ Pixel32 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
ofs
ImageFileDirectory { ifdExtended :: ImageFileDirectory -> ExifData
ifdExtended = ExifData
v } -> ExifData -> Get ExifData
forall (f :: * -> *) a. Applicative f => a -> f a
pure ExifData
v
findIFDExtDefaultData :: [Word32] -> ExifTag -> [ImageFileDirectory]
-> Get [Word32]
findIFDExtDefaultData :: [Pixel32] -> ExifTag -> [ImageFileDirectory] -> Get [Pixel32]
findIFDExtDefaultData [Pixel32]
d ExifTag
tag [ImageFileDirectory]
lst =
case [ImageFileDirectory
v | ImageFileDirectory
v <- [ImageFileDirectory]
lst, ImageFileDirectory -> ExifTag
ifdIdentifier ImageFileDirectory
v ExifTag -> ExifTag -> Bool
forall a. Eq a => a -> a -> Bool
== ExifTag
tag] of
[] -> [Pixel32] -> Get [Pixel32]
forall (f :: * -> *) a. Applicative f => a -> f a
pure [Pixel32]
d
(ImageFileDirectory { ifdExtended :: ImageFileDirectory -> ExifData
ifdExtended = ExifData
ExifNone }:[ImageFileDirectory]
_) -> [Pixel32] -> Get [Pixel32]
forall (m :: * -> *) a. Monad m => a -> m a
return [Pixel32]
d
(ImageFileDirectory
x:[ImageFileDirectory]
_) -> Vector Pixel32 -> [Pixel32]
forall a. Vector a -> [a]
V.toList (Vector Pixel32 -> [Pixel32])
-> Get (Vector Pixel32) -> Get [Pixel32]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> ExifData -> Get (Vector Pixel32)
unLong String
errorMessage (ImageFileDirectory -> ExifData
ifdExtended ImageFileDirectory
x)
where errorMessage :: String
errorMessage =
String
"Can't parse tag " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExifTag -> String
forall a. Show a => a -> String
show ExifTag
tag String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
" " String -> String -> String
forall a. [a] -> [a] -> [a]
++ ExifData -> String
forall a. Show a => a -> String
show (ImageFileDirectory -> ExifData
ifdExtended ImageFileDirectory
x)
copyByteString :: B.ByteString -> M.STVector s Word8 -> Int -> Int -> (Word32, Word32)
-> ST s Int
copyByteString :: forall s.
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
copyByteString ByteString
str STVector s Pixel8
vec Int
stride Int
startWrite (Pixel32
from, Pixel32
count) = Int -> Int -> ST s Int
inner Int
startWrite Int
fromi
where fromi :: Int
fromi = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
from
maxi :: Int
maxi = Int
fromi Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
count
inner :: Int -> Int -> ST s Int
inner Int
writeIdx Int
i | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxi = Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
writeIdx
inner Int
writeIdx Int
i = do
let v :: Pixel8
v = ByteString
str ByteString -> Int -> Pixel8
`BU.unsafeIndex` Int
i
(STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
vec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` Int
writeIdx) Pixel8
v
Int -> Int -> ST s Int
inner (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) (Int -> ST s Int) -> Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
unpackPackBit :: B.ByteString -> M.STVector s Word8 -> Int -> Int
-> (Word32, Word32)
-> ST s Int
unpackPackBit :: forall s.
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
unpackPackBit ByteString
str STVector s Pixel8
outVec Int
stride Int
writeIndex (Pixel32
offset, Pixel32
size) = Int -> Int -> ST s Int
loop Int
fromi Int
writeIndex
where fromi :: Int
fromi = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
offset
maxi :: Int
maxi = Int
fromi Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size
replicateByte :: Int -> Pixel8 -> Int -> ST s Int
replicateByte Int
writeIdx Pixel8
_ Int
0 = Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
writeIdx
replicateByte Int
writeIdx Pixel8
v Int
count = do
(STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` Int
writeIdx) Pixel8
v
Int -> Pixel8 -> Int -> ST s Int
replicateByte (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) Pixel8
v (Int -> ST s Int) -> Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int
count Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1
loop :: Int -> Int -> ST s Int
loop Int
i Int
writeIdx | Int
i Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxi = Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
writeIdx
loop Int
i Int
writeIdx = ST s Int
choice
where v :: Int8
v = Pixel8 -> Int8
forall a b. (Integral a, Num b) => a -> b
fromIntegral (ByteString
str ByteString -> Int -> Pixel8
`B.index` Int
i) :: Int8
choice :: ST s Int
choice
| Int8
0 Int8 -> Int8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int8
v =
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall s.
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
copyByteString ByteString
str STVector s Pixel8
outVec Int
stride Int
writeIdx
(Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1, Int8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
v Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
+ Pixel32
1)
ST s Int -> (Int -> ST s Int) -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Int -> ST s Int
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
v)
| -Int8
127 Int8 -> Int8 -> Bool
forall a. Ord a => a -> a -> Bool
<= Int8
v = do
let nextByte :: Pixel8
nextByte = ByteString
str ByteString -> Int -> Pixel8
`B.index` (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
count :: Int
count = Int -> Int
forall a. Num a => a -> a
negate (Int8 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int8
v) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 :: Int
Int -> Pixel8 -> Int -> ST s Int
replicateByte Int
writeIdx Pixel8
nextByte Int
count
ST s Int -> (Int -> ST s Int) -> ST s Int
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Int -> Int -> ST s Int
loop (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
| Bool
otherwise = Int -> Int -> ST s Int
loop Int
writeIdx (Int -> ST s Int) -> Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
uncompressAt :: TiffCompression
-> B.ByteString -> M.STVector s Word8 -> Int -> Int -> (Word32, Word32)
-> ST s Int
uncompressAt :: forall s.
TiffCompression
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
uncompressAt TiffCompression
CompressionNone = ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall s.
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
copyByteString
uncompressAt TiffCompression
CompressionPackBit = ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall s.
ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
unpackPackBit
uncompressAt TiffCompression
CompressionLZW = \ByteString
str STVector s Pixel8
outVec Int
_stride Int
writeIndex (Pixel32
offset, Pixel32
size) -> do
let toDecode :: ByteString
toDecode = Int -> ByteString -> ByteString
B.take (Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size) (ByteString -> ByteString) -> ByteString -> ByteString
forall a b. (a -> b) -> a -> b
$ Int -> ByteString -> ByteString
B.drop (Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
offset) ByteString
str
BoolReader s () -> ST s ()
forall s a. BoolReader s a -> ST s a
runBoolReader (BoolReader s () -> ST s ()) -> BoolReader s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ ByteString -> STVector s Pixel8 -> Int -> BoolReader s ()
forall s. ByteString -> STVector s Pixel8 -> Int -> BoolReader s ()
decodeLzwTiff ByteString
toDecode STVector s Pixel8
outVec Int
writeIndex
Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return Int
0
uncompressAt TiffCompression
_ = String
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall a. HasCallStack => String -> a
error String
"Unhandled compression"
class Unpackable a where
type StorageType a :: *
outAlloc :: a -> Int -> ST s (M.STVector s (StorageType a))
allocTempBuffer :: a -> M.STVector s (StorageType a) -> Int
-> ST s (M.STVector s Word8)
offsetStride :: a -> Int -> Int -> (Int, Int)
mergeBackTempBuffer :: a
-> Endianness
-> M.STVector s Word8
-> Int
-> Int
-> Word32
-> Int
-> M.STVector s (StorageType a)
-> ST s ()
instance Unpackable Word8 where
type StorageType Word8 = Word8
offsetStride :: Pixel8 -> Int -> Int -> (Int, Int)
offsetStride Pixel8
_ Int
i Int
stride = (Int
i, Int
stride)
allocTempBuffer :: forall s.
Pixel8
-> STVector s (StorageType Pixel8)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pixel8
_ STVector s (StorageType Pixel8)
buff Int
_ = STVector s Pixel8 -> ST s (STVector s Pixel8)
forall (f :: * -> *) a. Applicative f => a -> f a
pure STVector s Pixel8
STVector s (StorageType Pixel8)
buff
mergeBackTempBuffer :: forall s.
Pixel8
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pixel8)
-> ST s ()
mergeBackTempBuffer Pixel8
_ Endianness
_ STVector s Pixel8
_ Int
_ Int
_ Pixel32
_ Int
_ STVector s (StorageType Pixel8)
_ = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
outAlloc :: forall s. Pixel8 -> Int -> ST s (STVector s (StorageType Pixel8))
outAlloc Pixel8
_ Int
count = Int -> Pixel8 -> ST s (MVector (PrimState (ST s)) Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> a -> m (MVector (PrimState m) a)
M.replicate Int
count Pixel8
0
instance Unpackable Word16 where
type StorageType Word16 = Word16
offsetStride :: Pixel16 -> Int -> Int -> (Int, Int)
offsetStride Pixel16
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. Pixel16 -> Int -> ST s (STVector s (StorageType Pixel16))
outAlloc Pixel16
_ = Int -> ST s (MVector s (StorageType Pixel16))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
allocTempBuffer :: forall s.
Pixel16
-> STVector s (StorageType Pixel16)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pixel16
_ STVector s (StorageType Pixel16)
_ Int
s = Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new (Int -> ST s (MVector (PrimState (ST s)) Pixel8))
-> Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall a b. (a -> b) -> a -> b
$ Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2
mergeBackTempBuffer :: forall s.
Pixel16
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pixel16)
-> ST s ()
mergeBackTempBuffer Pixel16
_ Endianness
EndianLittle STVector s Pixel8
tempVec Int
_ Int
index Pixel32
size Int
stride STVector s (StorageType Pixel16)
outVec =
Int -> Int -> ST s ()
looperLe Int
index Int
0
where looperLe :: Int -> Int -> ST s ()
looperLe Int
_ Int
readIndex | Int
readIndex Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
looperLe Int
writeIndex Int
readIndex = do
Pixel8
v1 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIndex
Pixel8
v2 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
let finalValue :: Pixel16
finalValue =
(Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v2 Pixel16 -> Int -> Pixel16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8) Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.|. Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1
(STVector s (StorageType Pixel16)
MVector (PrimState (ST s)) Pixel16
outVec MVector (PrimState (ST s)) Pixel16 -> Int -> Pixel16 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIndex) Pixel16
finalValue
Int -> Int -> ST s ()
looperLe (Int
writeIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
mergeBackTempBuffer Pixel16
_ Endianness
EndianBig STVector s Pixel8
tempVec Int
_ Int
index Pixel32
size Int
stride STVector s (StorageType Pixel16)
outVec =
Int -> Int -> ST s ()
looperBe Int
index Int
0
where looperBe :: Int -> Int -> ST s ()
looperBe Int
_ Int
readIndex | Int
readIndex Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
looperBe Int
writeIndex Int
readIndex = do
Pixel8
v1 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIndex
Pixel8
v2 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
let finalValue :: Pixel16
finalValue =
(Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1 Pixel16 -> Int -> Pixel16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8) Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.|. Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v2
(STVector s (StorageType Pixel16)
MVector (PrimState (ST s)) Pixel16
outVec MVector (PrimState (ST s)) Pixel16 -> Int -> Pixel16 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIndex) Pixel16
finalValue
Int -> Int -> ST s ()
looperBe (Int
writeIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
instance Unpackable Word32 where
type StorageType Word32 = Word32
offsetStride :: Pixel32 -> Int -> Int -> (Int, Int)
offsetStride Pixel32
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. Pixel32 -> Int -> ST s (STVector s (StorageType Pixel32))
outAlloc Pixel32
_ = Int -> ST s (MVector s (StorageType Pixel32))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
allocTempBuffer :: forall s.
Pixel32
-> STVector s (StorageType Pixel32)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pixel32
_ STVector s (StorageType Pixel32)
_ Int
s = Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new (Int -> ST s (MVector (PrimState (ST s)) Pixel8))
-> Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall a b. (a -> b) -> a -> b
$ Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4
mergeBackTempBuffer :: forall s.
Pixel32
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pixel32)
-> ST s ()
mergeBackTempBuffer Pixel32
_ Endianness
EndianLittle STVector s Pixel8
tempVec Int
_ Int
index Pixel32
size Int
stride STVector s (StorageType Pixel32)
outVec =
Int -> Int -> ST s ()
looperLe Int
index Int
0
where looperLe :: Int -> Int -> ST s ()
looperLe Int
_ Int
readIndex | Int
readIndex Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
looperLe Int
writeIndex Int
readIndex = do
Pixel8
v1 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIndex
Pixel8
v2 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Pixel8
v3 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
Pixel8
v4 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
let finalValue :: Pixel32
finalValue =
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v4 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
24) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v3 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v2 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1
(STVector s (StorageType Pixel32)
MVector (PrimState (ST s)) Pixel32
outVec MVector (PrimState (ST s)) Pixel32 -> Int -> Pixel32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIndex) Pixel32
finalValue
Int -> Int -> ST s ()
looperLe (Int
writeIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)
mergeBackTempBuffer Pixel32
_ Endianness
EndianBig STVector s Pixel8
tempVec Int
_ Int
index Pixel32
size Int
stride STVector s (StorageType Pixel32)
outVec =
Int -> Int -> ST s ()
looperBe Int
index Int
0
where looperBe :: Int -> Int -> ST s ()
looperBe Int
_ Int
readIndex | Int
readIndex Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
looperBe Int
writeIndex Int
readIndex = do
Pixel8
v1 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIndex
Pixel8
v2 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
Pixel8
v3 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
Pixel8
v4 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
let finalValue :: Pixel32
finalValue =
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
24) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v2 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
(Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v3 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8) Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|.
Pixel8 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v4
(STVector s (StorageType Pixel32)
MVector (PrimState (ST s)) Pixel32
outVec MVector (PrimState (ST s)) Pixel32 -> Int -> Pixel32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIndex) Pixel32
finalValue
Int -> Int -> ST s ()
looperBe (Int
writeIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride) (Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4)
instance Unpackable Float where
type StorageType Float = Float
offsetStride :: PixelF -> Int -> Int -> (Int, Int)
offsetStride PixelF
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. PixelF -> Int -> ST s (STVector s (StorageType PixelF))
outAlloc PixelF
_ = Int -> ST s (MVector s (StorageType PixelF))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
allocTempBuffer :: forall s.
PixelF
-> STVector s (StorageType PixelF)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer PixelF
_ STVector s (StorageType PixelF)
_ Int
s = Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new (Int -> ST s (MVector (PrimState (ST s)) Pixel8))
-> Int -> ST s (MVector (PrimState (ST s)) Pixel8)
forall a b. (a -> b) -> a -> b
$ Int
s Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
4
mergeBackTempBuffer :: forall s. Float
-> Endianness
-> M.STVector s Word8
-> Int
-> Int
-> Word32
-> Int
-> M.STVector s (StorageType Float)
-> ST s ()
mergeBackTempBuffer :: forall s.
PixelF
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType PixelF)
-> ST s ()
mergeBackTempBuffer PixelF
_ Endianness
endianness STVector s Pixel8
tempVec Int
lineSize Int
index Pixel32
size Int
stride STVector s (StorageType PixelF)
outVec =
let outVecWord32 :: M.STVector s Word32
outVecWord32 :: STVector s Pixel32
outVecWord32 = MVector s PixelF -> STVector s Pixel32
forall a b s.
(Storable a, Storable b) =>
MVector s a -> MVector s b
M.unsafeCast MVector s PixelF
STVector s (StorageType PixelF)
outVec
in Pixel32
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pixel32)
-> ST s ()
forall a s.
Unpackable a =>
a
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType a)
-> ST s ()
mergeBackTempBuffer (Pixel32
0 :: Word32)
Endianness
endianness
STVector s Pixel8
tempVec
Int
lineSize
Int
index
Pixel32
size
Int
stride
STVector s Pixel32
STVector s (StorageType Pixel32)
outVecWord32
data Pack4 = Pack4
instance Unpackable Pack4 where
type StorageType Pack4 = Word8
allocTempBuffer :: forall s.
Pack4
-> STVector s (StorageType Pack4)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pack4
_ STVector s (StorageType Pack4)
_ = Int -> ST s (MVector s Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
offsetStride :: Pack4 -> Int -> Int -> (Int, Int)
offsetStride Pack4
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. Pack4 -> Int -> ST s (STVector s (StorageType Pack4))
outAlloc Pack4
_ = Int -> ST s (MVector s (StorageType Pack4))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
mergeBackTempBuffer :: forall s.
Pack4
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pack4)
-> ST s ()
mergeBackTempBuffer Pack4
_ Endianness
_ STVector s Pixel8
tempVec Int
lineSize Int
index Pixel32
size Int
stride STVector s (StorageType Pack4)
outVec =
Int -> Int -> Int -> ST s ()
inner Int
0 Int
index Int
pxCount
where pxCount :: Int
pxCount = Int
lineSize Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
stride
maxWrite :: Int
maxWrite = STVector s Pixel8 -> Int
forall a s. Storable a => MVector s a -> Int
M.length STVector s Pixel8
STVector s (StorageType Pack4)
outVec
inner :: Int -> Int -> Int -> ST s ()
inner Int
readIdx Int
writeIdx Int
_
| Int
readIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size Bool -> Bool -> Bool
|| Int
writeIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxWrite = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
inner Int
readIdx Int
writeIdx Int
line
| Int
line Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Int -> Int -> Int -> ST s ()
inner Int
readIdx (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) Int
pxCount
inner Int
readIdx Int
writeIdx Int
line = do
Pixel8
v <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIdx
let high :: Pixel8
high = (Pixel8
v Pixel8 -> Int -> Pixel8
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
4) Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0xF
low :: Pixel8
low = Pixel8
v Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0xF
(STVector s (StorageType Pack4)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIdx) Pixel8
high
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxWrite) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
(STVector s (StorageType Pack4)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride)) Pixel8
low
Int -> Int -> Int -> ST s ()
inner (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) (Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
data Pack2 = Pack2
instance Unpackable Pack2 where
type StorageType Pack2 = Word8
allocTempBuffer :: forall s.
Pack2
-> STVector s (StorageType Pack2)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pack2
_ STVector s (StorageType Pack2)
_ = Int -> ST s (MVector s Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
offsetStride :: Pack2 -> Int -> Int -> (Int, Int)
offsetStride Pack2
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. Pack2 -> Int -> ST s (STVector s (StorageType Pack2))
outAlloc Pack2
_ = Int -> ST s (MVector s (StorageType Pack2))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
mergeBackTempBuffer :: forall s.
Pack2
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pack2)
-> ST s ()
mergeBackTempBuffer Pack2
_ Endianness
_ STVector s Pixel8
tempVec Int
lineSize Int
index Pixel32
size Int
stride STVector s (StorageType Pack2)
outVec =
Int -> Int -> Int -> ST s ()
inner Int
0 Int
index Int
pxCount
where pxCount :: Int
pxCount = Int
lineSize Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
stride
maxWrite :: Int
maxWrite = STVector s Pixel8 -> Int
forall a s. Storable a => MVector s a -> Int
M.length STVector s Pixel8
STVector s (StorageType Pack2)
outVec
inner :: Int -> Int -> Int -> ST s ()
inner Int
readIdx Int
writeIdx Int
_
| Int
readIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size Bool -> Bool -> Bool
|| Int
writeIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxWrite = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
inner Int
readIdx Int
writeIdx Int
line
| Int
line Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Int -> Int -> Int -> ST s ()
inner Int
readIdx (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) Int
pxCount
inner Int
readIdx Int
writeIdx Int
line = do
Pixel8
v <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIdx
let v0 :: Pixel8
v0 = (Pixel8
v Pixel8 -> Int -> Pixel8
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
6) Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0x3
v1 :: Pixel8
v1 = (Pixel8
v Pixel8 -> Int -> Pixel8
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
4) Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0x3
v2 :: Pixel8
v2 = (Pixel8
v Pixel8 -> Int -> Pixel8
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
2) Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0x3
v3 :: Pixel8
v3 = Pixel8
v Pixel8 -> Pixel8 -> Pixel8
forall a. Bits a => a -> a -> a
.&. Pixel8
0x3
(STVector s (StorageType Pack2)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIdx) Pixel8
v0
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxWrite) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
(STVector s (StorageType Pack2)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride)) Pixel8
v1
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxWrite) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
(STVector s (StorageType Pack2)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
2)) Pixel8
v2
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxWrite) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
(STVector s (StorageType Pack2)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
3)) Pixel8
v3
Int -> Int -> Int -> ST s ()
inner (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1) (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
4 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) (Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4)
data Pack12 = Pack12
instance Unpackable Pack12 where
type StorageType Pack12 = Word16
allocTempBuffer :: forall s.
Pack12
-> STVector s (StorageType Pack12)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer Pack12
_ STVector s (StorageType Pack12)
_ = Int -> ST s (MVector s Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
offsetStride :: Pack12 -> Int -> Int -> (Int, Int)
offsetStride Pack12
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s. Pack12 -> Int -> ST s (STVector s (StorageType Pack12))
outAlloc Pack12
_ = Int -> ST s (MVector s (StorageType Pack12))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
mergeBackTempBuffer :: forall s.
Pack12
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType Pack12)
-> ST s ()
mergeBackTempBuffer Pack12
_ Endianness
_ STVector s Pixel8
tempVec Int
lineSize Int
index Pixel32
size Int
stride STVector s (StorageType Pack12)
outVec =
Int -> Int -> Int -> ST s ()
inner Int
0 Int
index Int
pxCount
where pxCount :: Int
pxCount = Int
lineSize Int -> Int -> Int
forall a. Integral a => a -> a -> a
`div` Int
stride
maxWrite :: Int
maxWrite = MVector s Pixel16 -> Int
forall a s. Storable a => MVector s a -> Int
M.length MVector s Pixel16
STVector s (StorageType Pack12)
outVec
inner :: Int -> Int -> Int -> ST s ()
inner Int
readIdx Int
writeIdx Int
_
| Int
readIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size Bool -> Bool -> Bool
|| Int
writeIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Int
maxWrite = () -> ST s ()
forall (f :: * -> *) a. Applicative f => a -> f a
pure ()
inner Int
readIdx Int
writeIdx Int
line
| Int
line Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
<= Int
0 = Int -> Int -> Int -> ST s ()
inner Int
readIdx (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) Int
pxCount
inner Int
readIdx Int
writeIdx Int
line = do
Pixel8
v0 <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIdx
Pixel8
v1 <- if Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size
then STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
else Pixel8 -> ST s Pixel8
forall (f :: * -> *) a. Applicative f => a -> f a
pure Pixel8
0
Pixel8
v2 <- if Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size
then STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)
else Pixel8 -> ST s Pixel8
forall (f :: * -> *) a. Applicative f => a -> f a
pure Pixel8
0
let high0 :: Pixel16
high0 = Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v0 Pixel16 -> Int -> Pixel16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
4
low0 :: Pixel16
low0 = (Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1 Pixel16 -> Int -> Pixel16
forall a. Bits a => a -> Int -> a
`unsafeShiftR` Int
4) Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.&. Pixel16
0xF
p0 :: Pixel16
p0 = Pixel16
high0 Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.|. Pixel16
low0
high1 :: Pixel16
high1 = (Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v1 Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.&. Pixel16
0xF) Pixel16 -> Int -> Pixel16
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
8
low1 :: Pixel16
low1 = Pixel8 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel8
v2
p1 :: Pixel16
p1 = Pixel16
high1 Pixel16 -> Pixel16 -> Pixel16
forall a. Bits a => a -> a -> a
.|. Pixel16
low1
(STVector s (StorageType Pack12)
MVector (PrimState (ST s)) Pixel16
outVec MVector (PrimState (ST s)) Pixel16 -> Int -> Pixel16 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIdx) Pixel16
p0
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
< Int
maxWrite) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$
(STVector s (StorageType Pack12)
MVector (PrimState (ST s)) Pixel16
outVec MVector (PrimState (ST s)) Pixel16 -> Int -> Pixel16 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
stride)) Pixel16
p1
Int -> Int -> Int -> ST s ()
inner (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3) (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2 Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
stride) (Int
line Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
2)
data YCbCrSubsampling = YCbCrSubsampling
{ YCbCrSubsampling -> Int
ycbcrWidth :: !Int
, YCbCrSubsampling -> Int
ycbcrHeight :: !Int
, YCbCrSubsampling -> Int
ycbcrImageWidth :: !Int
, YCbCrSubsampling -> Int
ycbcrStripHeight :: !Int
}
instance Unpackable YCbCrSubsampling where
type StorageType YCbCrSubsampling = Word8
offsetStride :: YCbCrSubsampling -> Int -> Int -> (Int, Int)
offsetStride YCbCrSubsampling
_ Int
_ Int
_ = (Int
0, Int
1)
outAlloc :: forall s.
YCbCrSubsampling
-> Int -> ST s (STVector s (StorageType YCbCrSubsampling))
outAlloc YCbCrSubsampling
_ = Int -> ST s (MVector s (StorageType YCbCrSubsampling))
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
allocTempBuffer :: forall s.
YCbCrSubsampling
-> STVector s (StorageType YCbCrSubsampling)
-> Int
-> ST s (STVector s Pixel8)
allocTempBuffer YCbCrSubsampling
_ STVector s (StorageType YCbCrSubsampling)
_ = Int -> ST s (MVector s Pixel8)
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
Int -> m (MVector (PrimState m) a)
M.new
mergeBackTempBuffer :: forall s.
YCbCrSubsampling
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType YCbCrSubsampling)
-> ST s ()
mergeBackTempBuffer YCbCrSubsampling
subSampling Endianness
_ STVector s Pixel8
tempVec Int
_ Int
index Pixel32
size Int
_ STVector s (StorageType YCbCrSubsampling)
outVec =
(Int -> (Int, Int) -> ST s Int) -> Int -> [(Int, Int)] -> ST s ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ Int -> (Int, Int) -> ST s Int
unpacker Int
0 [(Int
bx, Int
by) | Int
by <- [Int
0, Int
h .. Int
lineCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]
, Int
bx <- [Int
0, Int
w .. Int
imgWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
where w :: Int
w = YCbCrSubsampling -> Int
ycbcrWidth YCbCrSubsampling
subSampling
h :: Int
h = YCbCrSubsampling -> Int
ycbcrHeight YCbCrSubsampling
subSampling
imgWidth :: Int
imgWidth = YCbCrSubsampling -> Int
ycbcrImageWidth YCbCrSubsampling
subSampling
lineCount :: Int
lineCount = YCbCrSubsampling -> Int
ycbcrStripHeight YCbCrSubsampling
subSampling
lumaCount :: Int
lumaCount = Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
h
blockSize :: Int
blockSize = Int
lumaCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2
maxOut :: Int
maxOut = STVector s Pixel8 -> Int
forall a s. Storable a => MVector s a -> Int
M.length STVector s Pixel8
STVector s (StorageType YCbCrSubsampling)
outVec
unpacker :: Int -> (Int, Int) -> ST s Int
unpacker Int
readIdx (Int, Int)
_ | Int
readIdx Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
>= Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
3 = Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
readIdx
unpacker Int
readIdx (Int
bx, Int
by) = do
Pixel8
cb <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lumaCount)
Pixel8
cr <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` (Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
lumaCount Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)
let pixelIndices :: [Int]
pixelIndices =
[Int
index Int -> Int -> Int
forall a. Num a => a -> a -> a
+ ((Int
by Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
y) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
imgWidth Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
bx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x) Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
3 | Int
y <- [Int
0 .. Int
h Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1], Int
x <- [Int
0 .. Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]]
writer :: Int -> Int -> ST s Int
writer Int
readIndex Int
writeIdx | Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3 Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
maxOut = Int -> ST s Int
forall (f :: * -> *) a. Applicative f => a -> f a
pure Int
readIndex
writer Int
readIndex Int
writeIdx = do
Pixel8
y <- STVector s Pixel8
MVector (PrimState (ST s)) Pixel8
tempVec MVector (PrimState (ST s)) Pixel8 -> Int -> ST s Pixel8
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.read` Int
readIndex
(STVector s (StorageType YCbCrSubsampling)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` Int
writeIdx) Pixel8
y
(STVector s (StorageType YCbCrSubsampling)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1)) Pixel8
cb
(STVector s (StorageType YCbCrSubsampling)
MVector (PrimState (ST s)) Pixel8
outVec MVector (PrimState (ST s)) Pixel8 -> Int -> Pixel8 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.write` (Int
writeIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
2)) Pixel8
cr
Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ST s Int) -> Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int
readIndex Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
(Int -> Int -> ST s Int) -> Int -> [Int] -> ST s ()
forall (t :: * -> *) (m :: * -> *) b a.
(Foldable t, Monad m) =>
(b -> a -> m b) -> b -> t a -> m ()
foldM_ Int -> Int -> ST s Int
writer Int
readIdx [Int]
pixelIndices
Int -> ST s Int
forall (m :: * -> *) a. Monad m => a -> m a
return (Int -> ST s Int) -> Int -> ST s Int
forall a b. (a -> b) -> a -> b
$ Int
readIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
blockSize
gatherStrips :: ( Unpackable comp
, Pixel pixel
, StorageType comp ~ PixelBaseComponent pixel
)
=> comp -> B.ByteString -> TiffInfo -> Image pixel
gatherStrips :: forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips comp
comp ByteString
str TiffInfo
nfo = (forall s. ST s (Image pixel)) -> Image pixel
forall a. (forall s. ST s a) -> a
runST ((forall s. ST s (Image pixel)) -> Image pixel)
-> (forall s. ST s (Image pixel)) -> Image pixel
forall a b. (a -> b) -> a -> b
$ do
let width :: Int
width = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffWidth TiffInfo
nfo
height :: Int
height = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffHeight TiffInfo
nfo
sampleCount :: Int
sampleCount = if TiffInfo -> Pixel32
tiffSampleCount TiffInfo
nfo Pixel32 -> Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
/= Pixel32
0
then Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffSampleCount TiffInfo
nfo
else Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length (Vector Pixel32 -> Int) -> Vector Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffBitsPerSample TiffInfo
nfo
rowPerStrip :: Int
rowPerStrip = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffRowPerStrip TiffInfo
nfo
endianness :: Endianness
endianness = TiffHeader -> Endianness
hdrEndianness (TiffHeader -> Endianness) -> TiffHeader -> Endianness
forall a b. (a -> b) -> a -> b
$ TiffInfo -> TiffHeader
tiffHeader TiffInfo
nfo
stripCount :: Int
stripCount = Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length (Vector Pixel32 -> Int) -> Vector Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffOffsets TiffInfo
nfo
compression :: TiffCompression
compression = TiffInfo -> TiffCompression
tiffCompression TiffInfo
nfo
MVector s (PixelBaseComponent pixel)
outVec <- comp -> Int -> ST s (STVector s (StorageType comp))
forall a s.
Unpackable a =>
a -> Int -> ST s (STVector s (StorageType a))
outAlloc comp
comp (Int -> ST s (STVector s (StorageType comp)))
-> Int -> ST s (STVector s (StorageType comp))
forall a b. (a -> b) -> a -> b
$ Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
height Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount
STVector s Pixel8
tempVec <- comp
-> STVector s (StorageType comp) -> Int -> ST s (STVector s Pixel8)
forall a s.
Unpackable a =>
a -> STVector s (StorageType a) -> Int -> ST s (STVector s Pixel8)
allocTempBuffer comp
comp MVector s (PixelBaseComponent pixel)
STVector s (StorageType comp)
outVec
(Int
rowPerStrip Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount)
let mutableImage :: MutableImage s pixel
mutableImage = MutableImage :: forall s a.
Int -> Int -> STVector s (PixelBaseComponent a) -> MutableImage s a
MutableImage
{ mutableImageWidth :: Int
mutableImageWidth = Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
width
, mutableImageHeight :: Int
mutableImageHeight = Int -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
height
, mutableImageData :: MVector s (PixelBaseComponent pixel)
mutableImageData = MVector s (PixelBaseComponent pixel)
outVec
}
case TiffInfo -> TiffPlanarConfiguration
tiffPlaneConfiguration TiffInfo
nfo of
TiffPlanarConfiguration
PlanarConfigContig -> ((Int, Int, Pixel32, Pixel32) -> ST s ())
-> Vector (Int, Int, Pixel32, Pixel32) -> ST s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Vector a -> m ()
V.mapM_ (Int, Int, Pixel32, Pixel32) -> ST s ()
unpacker Vector (Int, Int, Pixel32, Pixel32)
sizes
where unpacker :: (Int, Int, Pixel32, Pixel32) -> ST s ()
unpacker (Int
idx, Int
stripSampleCount, Pixel32
offset, Pixel32
packedSize) = do
let (Int
writeIdx, Int
tempStride) = comp -> Int -> Int -> (Int, Int)
forall a. Unpackable a => a -> Int -> Int -> (Int, Int)
offsetStride comp
comp Int
idx Int
1
Int
_ <- TiffCompression
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall s.
TiffCompression
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
uncompressAt TiffCompression
compression ByteString
str STVector s Pixel8
tempVec Int
tempStride
Int
writeIdx (Pixel32
offset, Pixel32
packedSize)
let typ :: M.MVector s a -> a
typ :: forall s a. MVector s a -> a
typ = a -> MVector s a -> a
forall a b. a -> b -> a
const a
forall a. HasCallStack => a
undefined
sampleSize :: Int
sampleSize = PixelBaseComponent pixel -> Int
forall a. Storable a => a -> Int
sizeOf (MVector s (PixelBaseComponent pixel) -> PixelBaseComponent pixel
forall s a. MVector s a -> a
typ MVector s (PixelBaseComponent pixel)
outVec)
comp
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType comp)
-> ST s ()
forall a s.
Unpackable a =>
a
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType a)
-> ST s ()
mergeBackTempBuffer comp
comp Endianness
endianness STVector s Pixel8
tempVec (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount)
Int
idx (Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Int
stripSampleCount Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleSize) Int
1 MVector s (PixelBaseComponent pixel)
STVector s (StorageType comp)
outVec
fullStripSampleCount :: Int
fullStripSampleCount = Int
rowPerStrip Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount
startWriteOffset :: Vector Int
startWriteOffset = Int -> (Int -> Int) -> Vector Int
forall a. Int -> (Int -> a) -> Vector a
V.generate Int
stripCount (Int
fullStripSampleCount Int -> Int -> Int
forall a. Num a => a -> a -> a
*)
stripSampleCounts :: Vector Int
stripSampleCounts = (Int -> Int) -> Vector Int -> Vector Int
forall a b. (a -> b) -> Vector a -> Vector b
V.map Int -> Int
strip Vector Int
startWriteOffset
where
strip :: Int -> Int
strip Int
start = Int -> Int -> Int
forall a. Ord a => a -> a -> a
min Int
fullStripSampleCount (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
height Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
start)
sizes :: Vector (Int, Int, Pixel32, Pixel32)
sizes = Vector Int
-> Vector Int
-> Vector Pixel32
-> Vector Pixel32
-> Vector (Int, Int, Pixel32, Pixel32)
forall a b c d.
Vector a -> Vector b -> Vector c -> Vector d -> Vector (a, b, c, d)
V.zip4 Vector Int
startWriteOffset Vector Int
stripSampleCounts
(TiffInfo -> Vector Pixel32
tiffOffsets TiffInfo
nfo) (TiffInfo -> Vector Pixel32
tiffStripSize TiffInfo
nfo)
TiffPlanarConfiguration
PlanarConfigSeparate -> ((Int, Pixel32, Pixel32) -> ST s ())
-> Vector (Int, Pixel32, Pixel32) -> ST s ()
forall (m :: * -> *) a b. Monad m => (a -> m b) -> Vector a -> m ()
V.mapM_ (Int, Pixel32, Pixel32) -> ST s ()
unpacker Vector (Int, Pixel32, Pixel32)
sizes
where unpacker :: (Int, Pixel32, Pixel32) -> ST s ()
unpacker (Int
idx, Pixel32
offset, Pixel32
size) = do
let (Int
writeIdx, Int
tempStride) = comp -> Int -> Int -> (Int, Int)
forall a. Unpackable a => a -> Int -> Int -> (Int, Int)
offsetStride comp
comp Int
idx Int
stride
Int
_ <- TiffCompression
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
forall s.
TiffCompression
-> ByteString
-> STVector s Pixel8
-> Int
-> Int
-> (Pixel32, Pixel32)
-> ST s Int
uncompressAt TiffCompression
compression ByteString
str STVector s Pixel8
tempVec Int
tempStride
Int
writeIdx (Pixel32
offset, Pixel32
size)
comp
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType comp)
-> ST s ()
forall a s.
Unpackable a =>
a
-> Endianness
-> STVector s Pixel8
-> Int
-> Int
-> Pixel32
-> Int
-> STVector s (StorageType a)
-> ST s ()
mergeBackTempBuffer comp
comp Endianness
endianness STVector s Pixel8
tempVec (Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
sampleCount)
Int
idx Pixel32
size Int
stride MVector s (PixelBaseComponent pixel)
STVector s (StorageType comp)
outVec
stride :: Int
stride = Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length (Vector Pixel32 -> Int) -> Vector Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffOffsets TiffInfo
nfo
idxVector :: Vector Int
idxVector = Int -> Int -> Vector Int
forall a. Num a => a -> Int -> Vector a
V.enumFromN Int
0 Int
stride
sizes :: Vector (Int, Pixel32, Pixel32)
sizes = Vector Int
-> Vector Pixel32
-> Vector Pixel32
-> Vector (Int, Pixel32, Pixel32)
forall a b c. Vector a -> Vector b -> Vector c -> Vector (a, b, c)
V.zip3 Vector Int
idxVector (TiffInfo -> Vector Pixel32
tiffOffsets TiffInfo
nfo) (TiffInfo -> Vector Pixel32
tiffStripSize TiffInfo
nfo)
Bool -> ST s () -> ST s ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (TiffInfo -> Predictor
tiffPredictor TiffInfo
nfo Predictor -> Predictor -> Bool
forall a. Eq a => a -> a -> Bool
== Predictor
PredictorHorizontalDifferencing) (ST s () -> ST s ()) -> ST s () -> ST s ()
forall a b. (a -> b) -> a -> b
$ do
let f :: p -> a -> a -> a
f p
_ a
c1 a
c2 = a
c1 a -> a -> a
forall a. Num a => a -> a -> a
+ a
c2
[Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
0 .. Int
height Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int
y ->
[Int] -> (Int -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [Int
1 .. Int
width Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1] ((Int -> ST s ()) -> ST s ()) -> (Int -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \Int
x -> do
pixel
p <- MutableImage (PrimState (ST s)) pixel -> Int -> Int -> ST s pixel
forall a (m :: * -> *).
(Pixel a, PrimMonad m) =>
MutableImage (PrimState m) a -> Int -> Int -> m a
readPixel MutableImage s pixel
MutableImage (PrimState (ST s)) pixel
mutableImage (Int
x Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1) Int
y
pixel
q <- MutableImage (PrimState (ST s)) pixel -> Int -> Int -> ST s pixel
forall a (m :: * -> *).
(Pixel a, PrimMonad m) =>
MutableImage (PrimState m) a -> Int -> Int -> m a
readPixel MutableImage s pixel
MutableImage (PrimState (ST s)) pixel
mutableImage Int
x Int
y
MutableImage (PrimState (ST s)) pixel
-> Int -> Int -> pixel -> ST s ()
forall a (m :: * -> *).
(Pixel a, PrimMonad m) =>
MutableImage (PrimState m) a -> Int -> Int -> a -> m ()
writePixel MutableImage s pixel
MutableImage (PrimState (ST s)) pixel
mutableImage Int
x Int
y (pixel -> ST s ()) -> pixel -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Int
-> PixelBaseComponent pixel
-> PixelBaseComponent pixel
-> PixelBaseComponent pixel)
-> pixel -> pixel -> pixel
forall a.
Pixel a =>
(Int
-> PixelBaseComponent a
-> PixelBaseComponent a
-> PixelBaseComponent a)
-> a -> a -> a
mixWith Int
-> PixelBaseComponent pixel
-> PixelBaseComponent pixel
-> PixelBaseComponent pixel
forall {a} {p}. Num a => p -> a -> a -> a
f pixel
p pixel
q
MutableImage (PrimState (ST s)) pixel -> ST s (Image pixel)
forall a (m :: * -> *).
(Storable (PixelBaseComponent a), PrimMonad m) =>
MutableImage (PrimState m) a -> m (Image a)
unsafeFreezeImage MutableImage s pixel
MutableImage (PrimState (ST s)) pixel
mutableImage
ifdSingleLong :: ExifTag -> Word32 -> Writer [ImageFileDirectory] ()
ifdSingleLong :: ExifTag -> Pixel32 -> Writer [ImageFileDirectory] ()
ifdSingleLong ExifTag
tag = ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiLong ExifTag
tag (Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> (Pixel32 -> Vector Pixel32)
-> Pixel32
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton
ifdSingleShort :: Endianness -> ExifTag -> Word16
-> Writer [ImageFileDirectory] ()
ifdSingleShort :: Endianness -> ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdSingleShort Endianness
endian ExifTag
tag = Endianness
-> ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiShort Endianness
endian ExifTag
tag (Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> (Pixel16 -> Vector Pixel32)
-> Pixel16
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton (Pixel32 -> Vector Pixel32)
-> (Pixel16 -> Pixel32) -> Pixel16 -> Vector Pixel32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Pixel16 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral
ifdMultiLong :: ExifTag -> V.Vector Word32 -> Writer [ImageFileDirectory] ()
ifdMultiLong :: ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiLong ExifTag
tag Vector Pixel32
v = [ImageFileDirectory] -> Writer [ImageFileDirectory] ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell ([ImageFileDirectory] -> Writer [ImageFileDirectory] ())
-> (ImageFileDirectory -> [ImageFileDirectory])
-> ImageFileDirectory
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImageFileDirectory -> [ImageFileDirectory]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ImageFileDirectory -> Writer [ImageFileDirectory] ())
-> ImageFileDirectory -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ ImageFileDirectory :: ExifTag
-> IfdType -> Pixel32 -> Pixel32 -> ExifData -> ImageFileDirectory
ImageFileDirectory
{ ifdIdentifier :: ExifTag
ifdIdentifier = ExifTag
tag
, ifdType :: IfdType
ifdType = IfdType
TypeLong
, ifdCount :: Pixel32
ifdCount = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length Vector Pixel32
v
, ifdOffset :: Pixel32
ifdOffset = Pixel32
offset
, ifdExtended :: ExifData
ifdExtended = ExifData
extended
}
where (Pixel32
offset, ExifData
extended)
| Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length Vector Pixel32
v Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
1 = (Pixel32
0, Vector Pixel32 -> ExifData
ExifLongs Vector Pixel32
v)
| Bool
otherwise = (Vector Pixel32 -> Pixel32
forall a. Vector a -> a
V.head Vector Pixel32
v, ExifData
ExifNone)
ifdMultiShort :: Endianness -> ExifTag -> V.Vector Word32
-> Writer [ImageFileDirectory] ()
ifdMultiShort :: Endianness
-> ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiShort Endianness
endian ExifTag
tag Vector Pixel32
v = [ImageFileDirectory] -> Writer [ImageFileDirectory] ()
forall w (m :: * -> *). MonadWriter w m => w -> m ()
tell ([ImageFileDirectory] -> Writer [ImageFileDirectory] ())
-> (ImageFileDirectory -> [ImageFileDirectory])
-> ImageFileDirectory
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ImageFileDirectory -> [ImageFileDirectory]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (ImageFileDirectory -> Writer [ImageFileDirectory] ())
-> ImageFileDirectory -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ ImageFileDirectory :: ExifTag
-> IfdType -> Pixel32 -> Pixel32 -> ExifData -> ImageFileDirectory
ImageFileDirectory
{ ifdIdentifier :: ExifTag
ifdIdentifier = ExifTag
tag
, ifdType :: IfdType
ifdType = IfdType
TypeShort
, ifdCount :: Pixel32
ifdCount = Pixel32
size
, ifdOffset :: Pixel32
ifdOffset = Pixel32
offset
, ifdExtended :: ExifData
ifdExtended = ExifData
extended
}
where size :: Pixel32
size = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Vector Pixel32 -> Int
forall a. Vector a -> Int
V.length Vector Pixel32
v
(Pixel32
offset, ExifData
extended)
| Pixel32
size Pixel32 -> Pixel32 -> Bool
forall a. Ord a => a -> a -> Bool
> Pixel32
2 = (Pixel32
0, Vector Pixel16 -> ExifData
ExifShorts (Vector Pixel16 -> ExifData) -> Vector Pixel16 -> ExifData
forall a b. (a -> b) -> a -> b
$ (Pixel32 -> Pixel16) -> Vector Pixel32 -> Vector Pixel16
forall a b. (a -> b) -> Vector a -> Vector b
V.map Pixel32 -> Pixel16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Vector Pixel32
v)
| Pixel32
size Pixel32 -> Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32
2 =
let v1 :: Pixel32
v1 = Pixel32 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Pixel32) -> Pixel32 -> Pixel32
forall a b. (a -> b) -> a -> b
$ Vector Pixel32 -> Pixel32
forall a. Vector a -> a
V.head Vector Pixel32
v
v2 :: Pixel32
v2 = Pixel32 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Pixel32) -> Pixel32 -> Pixel32
forall a b. (a -> b) -> a -> b
$ Vector Pixel32
v Vector Pixel32 -> Int -> Pixel32
forall a. Vector a -> Int -> a
`V.unsafeIndex` Int
1
in
case Endianness
endian of
Endianness
EndianLittle -> (Pixel32
v2 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16 Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|. Pixel32
v1, ExifData
ExifNone)
Endianness
EndianBig -> (Pixel32
v1 Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16 Pixel32 -> Pixel32 -> Pixel32
forall a. Bits a => a -> a -> a
.|. Pixel32
v2, ExifData
ExifNone)
| Bool
otherwise = case Endianness
endian of
Endianness
EndianLittle -> (Vector Pixel32 -> Pixel32
forall a. Vector a -> a
V.head Vector Pixel32
v, ExifData
ExifNone)
Endianness
EndianBig -> (Vector Pixel32 -> Pixel32
forall a. Vector a -> a
V.head Vector Pixel32
v Pixel32 -> Int -> Pixel32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
16, ExifData
ExifNone)
instance BinaryParam B.ByteString TiffInfo where
putP :: ByteString -> TiffInfo -> Put
putP ByteString
rawData TiffInfo
nfo = ByteString -> (TiffHeader, [[ImageFileDirectory]]) -> Put
forall a b. BinaryParam a b => a -> b -> Put
putP ByteString
rawData (TiffInfo -> TiffHeader
tiffHeader TiffInfo
nfo, [[ImageFileDirectory]
list]) where
endianness :: Endianness
endianness = TiffHeader -> Endianness
hdrEndianness (TiffHeader -> Endianness) -> TiffHeader -> Endianness
forall a b. (a -> b) -> a -> b
$ TiffInfo -> TiffHeader
tiffHeader TiffInfo
nfo
ifdShort :: ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdShort = Endianness -> ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdSingleShort Endianness
endianness
ifdShorts :: ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdShorts = Endianness
-> ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiShort Endianness
endianness
list :: [ImageFileDirectory]
list = Writer [ImageFileDirectory] () -> [ImageFileDirectory]
forall w a. Writer w a -> w
execWriter (Writer [ImageFileDirectory] () -> [ImageFileDirectory])
-> Writer [ImageFileDirectory] () -> [ImageFileDirectory]
forall a b. (a -> b) -> a -> b
$ do
ExifTag -> Pixel32 -> Writer [ImageFileDirectory] ()
ifdSingleLong ExifTag
TagImageWidth (Pixel32 -> Writer [ImageFileDirectory] ())
-> Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffWidth TiffInfo
nfo
ExifTag -> Pixel32 -> Writer [ImageFileDirectory] ()
ifdSingleLong ExifTag
TagImageLength (Pixel32 -> Writer [ImageFileDirectory] ())
-> Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffHeight TiffInfo
nfo
ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdShorts ExifTag
TagBitsPerSample (Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> Vector Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffBitsPerSample TiffInfo
nfo
ExifTag -> Pixel32 -> Writer [ImageFileDirectory] ()
ifdSingleLong ExifTag
TagSamplesPerPixel (Pixel32 -> Writer [ImageFileDirectory] ())
-> Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffSampleCount TiffInfo
nfo
ExifTag -> Pixel32 -> Writer [ImageFileDirectory] ()
ifdSingleLong ExifTag
TagRowPerStrip (Pixel32 -> Writer [ImageFileDirectory] ())
-> Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffRowPerStrip TiffInfo
nfo
ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdShort ExifTag
TagPhotometricInterpretation
(Pixel16 -> Writer [ImageFileDirectory] ())
-> (TiffColorspace -> Pixel16)
-> TiffColorspace
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TiffColorspace -> Pixel16
packPhotometricInterpretation
(TiffColorspace -> Writer [ImageFileDirectory] ())
-> TiffColorspace -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> TiffColorspace
tiffColorspace TiffInfo
nfo
ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdShort ExifTag
TagPlanarConfiguration
(Pixel16 -> Writer [ImageFileDirectory] ())
-> (TiffPlanarConfiguration -> Pixel16)
-> TiffPlanarConfiguration
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TiffPlanarConfiguration -> Pixel16
constantToPlaneConfiguration (TiffPlanarConfiguration -> Writer [ImageFileDirectory] ())
-> TiffPlanarConfiguration -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> TiffPlanarConfiguration
tiffPlaneConfiguration TiffInfo
nfo
ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiLong ExifTag
TagSampleFormat
(Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> ([TiffSampleFormat] -> Vector Pixel32)
-> [TiffSampleFormat]
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList
([Pixel32] -> Vector Pixel32)
-> ([TiffSampleFormat] -> [Pixel32])
-> [TiffSampleFormat]
-> Vector Pixel32
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (TiffSampleFormat -> Pixel32) -> [TiffSampleFormat] -> [Pixel32]
forall a b. (a -> b) -> [a] -> [b]
map TiffSampleFormat -> Pixel32
packSampleFormat
([TiffSampleFormat] -> Writer [ImageFileDirectory] ())
-> [TiffSampleFormat] -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> [TiffSampleFormat]
tiffSampleFormat TiffInfo
nfo
ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdShort ExifTag
TagCompression (Pixel16 -> Writer [ImageFileDirectory] ())
-> (TiffCompression -> Pixel16)
-> TiffCompression
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. TiffCompression -> Pixel16
packCompression
(TiffCompression -> Writer [ImageFileDirectory] ())
-> TiffCompression -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> TiffCompression
tiffCompression TiffInfo
nfo
ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiLong ExifTag
TagStripOffsets (Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> Vector Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffOffsets TiffInfo
nfo
ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdMultiLong ExifTag
TagStripByteCounts (Vector Pixel32 -> Writer [ImageFileDirectory] ())
-> Vector Pixel32 -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffStripSize TiffInfo
nfo
Writer [ImageFileDirectory] ()
-> (ExtraSample -> Writer [ImageFileDirectory] ())
-> Maybe ExtraSample
-> Writer [ImageFileDirectory] ()
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (() -> Writer [ImageFileDirectory] ()
forall (m :: * -> *) a. Monad m => a -> m a
return ())
(ExifTag -> Pixel16 -> Writer [ImageFileDirectory] ()
ifdShort ExifTag
TagExtraSample (Pixel16 -> Writer [ImageFileDirectory] ())
-> (ExtraSample -> Pixel16)
-> ExtraSample
-> Writer [ImageFileDirectory] ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ExtraSample -> Pixel16
codeOfExtraSample)
(Maybe ExtraSample -> Writer [ImageFileDirectory] ())
-> Maybe ExtraSample -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Maybe ExtraSample
tiffExtraSample TiffInfo
nfo
let subSampling :: Vector Pixel32
subSampling = TiffInfo -> Vector Pixel32
tiffYCbCrSubsampling TiffInfo
nfo
Bool
-> Writer [ImageFileDirectory] () -> Writer [ImageFileDirectory] ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (Vector Pixel32 -> Bool
forall a. Vector a -> Bool
V.null Vector Pixel32
subSampling) (Writer [ImageFileDirectory] () -> Writer [ImageFileDirectory] ())
-> Writer [ImageFileDirectory] () -> Writer [ImageFileDirectory] ()
forall a b. (a -> b) -> a -> b
$
ExifTag -> Vector Pixel32 -> Writer [ImageFileDirectory] ()
ifdShorts ExifTag
TagYCbCrSubsampling Vector Pixel32
subSampling
getP :: ByteString -> Get TiffInfo
getP ByteString
rawData = do
(TiffHeader
hdr, [[ImageFileDirectory]]
cleanedFull :: [[ImageFileDirectory]]) <- ByteString -> Get (TiffHeader, [[ImageFileDirectory]])
forall a b. BinaryParam a b => a -> Get b
getP ByteString
rawData
let cleaned :: [ImageFileDirectory]
cleaned = [[ImageFileDirectory]] -> [ImageFileDirectory]
forall (t :: * -> *) a. Foldable t => t [a] -> [a]
concat [[ImageFileDirectory]]
cleanedFull
dataFind :: String -> ExifTag -> Get Pixel32
dataFind String
str ExifTag
tag = String -> ExifTag -> [ImageFileDirectory] -> Get Pixel32
findIFDData String
str ExifTag
tag [ImageFileDirectory]
cleaned
dataDefault :: Pixel32 -> ExifTag -> Get Pixel32
dataDefault Pixel32
def ExifTag
tag = Pixel32 -> ExifTag -> [ImageFileDirectory] -> Get Pixel32
findIFDDefaultData Pixel32
def ExifTag
tag [ImageFileDirectory]
cleaned
extFind :: String -> ExifTag -> Get ExifData
extFind String
str ExifTag
tag = String -> ExifTag -> [ImageFileDirectory] -> Get ExifData
findIFDExt String
str ExifTag
tag [ImageFileDirectory]
cleaned
extDefault :: [Pixel32] -> ExifTag -> Get [Pixel32]
extDefault [Pixel32]
def ExifTag
tag = [Pixel32] -> ExifTag -> [ImageFileDirectory] -> Get [Pixel32]
findIFDExtDefaultData [Pixel32]
def ExifTag
tag [ImageFileDirectory]
cleaned
TiffHeader
-> Pixel32
-> Pixel32
-> TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo
TiffInfo TiffHeader
hdr
(Pixel32
-> Pixel32
-> TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get Pixel32
-> Get
(Pixel32
-> TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> String -> ExifTag -> Get Pixel32
dataFind String
"Can't find width" ExifTag
TagImageWidth
Get
(Pixel32
-> TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get Pixel32
-> Get
(TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> String -> ExifTag -> Get Pixel32
dataFind String
"Can't find height" ExifTag
TagImageLength
Get
(TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get TiffColorspace
-> Get
(Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> ExifTag -> Get Pixel32
dataFind String
"Can't find color space" ExifTag
TagPhotometricInterpretation
Get Pixel32
-> (Pixel32 -> Get TiffColorspace) -> Get TiffColorspace
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pixel32 -> Get TiffColorspace
unpackPhotometricInterpretation)
Get
(Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get Pixel32
-> Get
(Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> String -> ExifTag -> Get Pixel32
dataFind String
"Can't find sample per pixel" ExifTag
TagSamplesPerPixel
Get
(Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get Pixel32
-> Get
(TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> String -> ExifTag -> Get Pixel32
dataFind String
"Can't find row per strip" ExifTag
TagRowPerStrip
Get
(TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get TiffPlanarConfiguration
-> Get
([TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Pixel32 -> ExifTag -> Get Pixel32
dataDefault Pixel32
1 ExifTag
TagPlanarConfiguration
Get Pixel32
-> (Pixel32 -> Get TiffPlanarConfiguration)
-> Get TiffPlanarConfiguration
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pixel32 -> Get TiffPlanarConfiguration
planarConfgOfConstant)
Get
([TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get [TiffSampleFormat]
-> Get
(Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([Pixel32] -> ExifTag -> Get [Pixel32]
extDefault [Pixel32
1] ExifTag
TagSampleFormat
Get [Pixel32]
-> ([Pixel32] -> Get [TiffSampleFormat]) -> Get [TiffSampleFormat]
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= (Pixel32 -> Get TiffSampleFormat)
-> [Pixel32] -> Get [TiffSampleFormat]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Pixel32 -> Get TiffSampleFormat
unpackSampleFormat)
Get
(Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get (Vector Pixel32)
-> Get
(TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> ExifTag -> Get ExifData
extFind String
"Can't find bit per sample" ExifTag
TagBitsPerSample
Get ExifData
-> (ExifData -> Get (Vector Pixel32)) -> Get (Vector Pixel32)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> ExifData -> Get (Vector Pixel32)
unLong String
"Can't find bit depth")
Get
(TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get TiffCompression
-> Get
(Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> ExifTag -> Get Pixel32
dataFind String
"Can't find Compression" ExifTag
TagCompression
Get Pixel32
-> (Pixel32 -> Get TiffCompression) -> Get TiffCompression
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pixel32 -> Get TiffCompression
unPackCompression)
Get
(Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get (Vector Pixel32)
-> Get
(Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> ExifTag -> Get ExifData
extFind String
"Can't find byte counts" ExifTag
TagStripByteCounts
Get ExifData
-> (ExifData -> Get (Vector Pixel32)) -> Get (Vector Pixel32)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> ExifData -> Get (Vector Pixel32)
unLong String
"Can't find bit per sample")
Get
(Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get (Vector Pixel32)
-> Get
(Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (String -> ExifTag -> Get ExifData
extFind String
"Strip offsets missing" ExifTag
TagStripOffsets
Get ExifData
-> (ExifData -> Get (Vector Pixel32)) -> Get (Vector Pixel32)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= String -> ExifData -> Get (Vector Pixel32)
unLong String
"Can't find strip offsets")
Get
(Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo)
-> Get (Maybe (Image PixelRGB16))
-> Get
(Vector Pixel32
-> Maybe ExtraSample -> Predictor -> Metadatas -> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> [ImageFileDirectory] -> Get (Maybe (Image PixelRGB16))
findPalette [ImageFileDirectory]
cleaned
Get
(Vector Pixel32
-> Maybe ExtraSample -> Predictor -> Metadatas -> TiffInfo)
-> Get (Vector Pixel32)
-> Get (Maybe ExtraSample -> Predictor -> Metadatas -> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> ([Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList ([Pixel32] -> Vector Pixel32)
-> Get [Pixel32] -> Get (Vector Pixel32)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [Pixel32] -> ExifTag -> Get [Pixel32]
extDefault [Pixel32
2, Pixel32
2] ExifTag
TagYCbCrSubsampling)
Get (Maybe ExtraSample -> Predictor -> Metadatas -> TiffInfo)
-> Get (Maybe ExtraSample)
-> Get (Predictor -> Metadatas -> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Maybe ExtraSample -> Get (Maybe ExtraSample)
forall (f :: * -> *) a. Applicative f => a -> f a
pure Maybe ExtraSample
forall a. Maybe a
Nothing
Get (Predictor -> Metadatas -> TiffInfo)
-> Get Predictor -> Get (Metadatas -> TiffInfo)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> (Pixel32 -> ExifTag -> Get Pixel32
dataDefault Pixel32
1 ExifTag
TagPredictor
Get Pixel32 -> (Pixel32 -> Get Predictor) -> Get Predictor
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= Pixel32 -> Get Predictor
predictorOfConstant)
Get (Metadatas -> TiffInfo) -> Get Metadatas -> Get TiffInfo
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Metadatas -> Get Metadatas
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([ImageFileDirectory] -> Metadatas
extractTiffMetadata [ImageFileDirectory]
cleaned)
palette16Of :: Image PixelRGB16 -> Palette' PixelRGB16
palette16Of :: Image PixelRGB16 -> Palette' PixelRGB16
palette16Of Image PixelRGB16
p = Palette' :: forall px. Int -> Vector (PixelBaseComponent px) -> Palette' px
Palette'
{ _paletteSize :: Int
_paletteSize = Image PixelRGB16 -> Int
forall a. Image a -> Int
imageWidth Image PixelRGB16
p
, _paletteData :: Vector (PixelBaseComponent PixelRGB16)
_paletteData = Image PixelRGB16 -> Vector (PixelBaseComponent PixelRGB16)
forall a. Image a -> Vector (PixelBaseComponent a)
imageData Image PixelRGB16
p
}
unpack :: B.ByteString -> TiffInfo -> Either String PalettedImage
unpack :: ByteString -> TiffInfo -> Either String PalettedImage
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffRowPerStrip :: TiffInfo -> Pixel32
tiffRowPerStrip = Pixel32
0 } =
ByteString -> TiffInfo -> Either String PalettedImage
unpack ByteString
file (TiffInfo -> Either String PalettedImage)
-> TiffInfo -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ TiffInfo
nfo { tiffRowPerStrip :: Pixel32
tiffRowPerStrip = TiffInfo -> Pixel32
tiffHeight TiffInfo
nfo }
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffPaleted
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format
, tiffPalette :: TiffInfo -> Maybe (Image PixelRGB16)
tiffPalette = Just Image PixelRGB16
p
}
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
8 Bool -> Bool -> Bool
&& [TiffSampleFormat]
format [TiffSampleFormat] -> [TiffSampleFormat] -> Bool
forall a. Eq a => a -> a -> Bool
== [TiffSampleFormat
TiffSampleUint] =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Palette' PixelRGB16 -> PalettedImage)
-> Palette' PixelRGB16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> Palette' PixelRGB16 -> PalettedImage
PalettedRGB16 (Pixel8 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo) (Palette' PixelRGB16 -> Either String PalettedImage)
-> Palette' PixelRGB16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Image PixelRGB16 -> Palette' PixelRGB16
palette16Of Image PixelRGB16
p
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
4 Bool -> Bool -> Bool
&& [TiffSampleFormat]
format [TiffSampleFormat] -> [TiffSampleFormat] -> Bool
forall a. Eq a => a -> a -> Bool
== [TiffSampleFormat
TiffSampleUint] =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Palette' PixelRGB16 -> PalettedImage)
-> Palette' PixelRGB16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> Palette' PixelRGB16 -> PalettedImage
PalettedRGB16 (Pack4 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack4
Pack4 ByteString
file TiffInfo
nfo) (Palette' PixelRGB16 -> Either String PalettedImage)
-> Palette' PixelRGB16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Image PixelRGB16 -> Palette' PixelRGB16
palette16Of Image PixelRGB16
p
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
2 Bool -> Bool -> Bool
&& [TiffSampleFormat]
format [TiffSampleFormat] -> [TiffSampleFormat] -> Bool
forall a. Eq a => a -> a -> Bool
== [TiffSampleFormat
TiffSampleUint] =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Palette' PixelRGB16 -> PalettedImage)
-> Palette' PixelRGB16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> Palette' PixelRGB16 -> PalettedImage
PalettedRGB16 (Pack2 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack2
Pack2 ByteString
file TiffInfo
nfo) (Palette' PixelRGB16 -> Either String PalettedImage)
-> Palette' PixelRGB16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Image PixelRGB16 -> Palette' PixelRGB16
palette16Of Image PixelRGB16
p
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffCMYK
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format }
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8, Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelCMYK8 -> PalettedImage)
-> Image PixelCMYK8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelCMYK8 -> DynamicImage)
-> Image PixelCMYK8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelCMYK8 -> DynamicImage
ImageCMYK8 (Image PixelCMYK8 -> Either String PalettedImage)
-> Image PixelCMYK8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image PixelCMYK8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
16, Pixel32
16, Pixel32
16, Pixel32
16] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelCMYK16 -> PalettedImage)
-> Image PixelCMYK16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelCMYK16 -> DynamicImage)
-> Image PixelCMYK16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelCMYK16 -> DynamicImage
ImageCMYK16 (Image PixelCMYK16 -> Either String PalettedImage)
-> Image PixelCMYK16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel16 -> ByteString -> TiffInfo -> Image PixelCMYK16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel16
0 :: Word16) ByteString
file TiffInfo
nfo
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffMonochromeWhite0 } = do
PalettedImage
img <- ByteString -> TiffInfo -> Either String PalettedImage
unpack ByteString
file (TiffInfo
nfo { tiffColorspace :: TiffColorspace
tiffColorspace = TiffColorspace
TiffMonochrome })
case PalettedImage
img of
TrueColorImage (ImageY8 Image Pixel8
i) -> PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel8 -> PalettedImage)
-> Image Pixel8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel8 -> DynamicImage) -> Image Pixel8 -> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> DynamicImage
ImageY8 (Image Pixel8 -> Either String PalettedImage)
-> Image Pixel8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ (Pixel8 -> Pixel8) -> Image Pixel8 -> Image Pixel8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap (Pixel8
forall a. Bounded a => a
maxBound Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
-) Image Pixel8
i
TrueColorImage (ImageY16 Image Pixel16
i) -> PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel16 -> PalettedImage)
-> Image Pixel16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel16 -> DynamicImage)
-> Image Pixel16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel16 -> DynamicImage
ImageY16 (Image Pixel16 -> Either String PalettedImage)
-> Image Pixel16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ (Pixel16 -> Pixel16) -> Image Pixel16 -> Image Pixel16
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap (Pixel16
forall a. Bounded a => a
maxBound Pixel16 -> Pixel16 -> Pixel16
forall a. Num a => a -> a -> a
-) Image Pixel16
i
TrueColorImage (ImageYA8 Image PixelYA8
i) -> let negative :: PixelYA8 -> PixelYA8
negative (PixelYA8 Pixel8
y Pixel8
a) = Pixel8 -> Pixel8 -> PixelYA8
PixelYA8 (Pixel8
forall a. Bounded a => a
maxBound Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
- Pixel8
y) Pixel8
a
in PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA8 -> PalettedImage)
-> Image PixelYA8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA8 -> DynamicImage)
-> Image PixelYA8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA8 -> DynamicImage
ImageYA8 (Image PixelYA8 -> Either String PalettedImage)
-> Image PixelYA8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ (PixelYA8 -> PixelYA8) -> Image PixelYA8 -> Image PixelYA8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap PixelYA8 -> PixelYA8
negative Image PixelYA8
i
TrueColorImage (ImageYA16 Image PixelYA16
i) -> let negative :: PixelYA16 -> PixelYA16
negative (PixelYA16 Pixel16
y Pixel16
a) = Pixel16 -> Pixel16 -> PixelYA16
PixelYA16 (Pixel16
forall a. Bounded a => a
maxBound Pixel16 -> Pixel16 -> Pixel16
forall a. Num a => a -> a -> a
- Pixel16
y) Pixel16
a
in PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA16 -> PalettedImage)
-> Image PixelYA16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA16 -> DynamicImage)
-> Image PixelYA16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA16 -> DynamicImage
ImageYA16 (Image PixelYA16 -> Either String PalettedImage)
-> Image PixelYA16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ (PixelYA16 -> PixelYA16) -> Image PixelYA16 -> Image PixelYA16
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap PixelYA16 -> PixelYA16
negative Image PixelYA16
i
PalettedImage
_ -> String -> Either String PalettedImage
forall a b. a -> Either a b
Left String
"Unsupported color type used with colorspace MonochromeWhite0"
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffMonochrome
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format }
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
2 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel8 -> PalettedImage)
-> Image Pixel8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel8 -> DynamicImage) -> Image Pixel8 -> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> DynamicImage
ImageY8 (Image Pixel8 -> DynamicImage)
-> (Image Pixel8 -> Image Pixel8) -> Image Pixel8 -> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pixel8 -> Pixel8) -> Image Pixel8 -> Image Pixel8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent Pixel8 -> PixelBaseComponent Pixel8)
-> Pixel8 -> Pixel8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x55 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image Pixel8 -> Either String PalettedImage)
-> Image Pixel8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack2 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack2
Pack2 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
4 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel8 -> PalettedImage)
-> Image Pixel8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel8 -> DynamicImage) -> Image Pixel8 -> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> DynamicImage
ImageY8 (Image Pixel8 -> DynamicImage)
-> (Image Pixel8 -> Image Pixel8) -> Image Pixel8 -> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pixel8 -> Pixel8) -> Image Pixel8 -> Image Pixel8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent Pixel8 -> PixelBaseComponent Pixel8)
-> Pixel8 -> Pixel8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x11 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image Pixel8 -> Either String PalettedImage)
-> Image Pixel8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack4 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack4
Pack4 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
8 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel8 -> PalettedImage)
-> Image Pixel8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel8 -> DynamicImage) -> Image Pixel8 -> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel8 -> DynamicImage
ImageY8 (Image Pixel8 -> Either String PalettedImage)
-> Image Pixel8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image Pixel8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
12 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel16 -> PalettedImage)
-> Image Pixel16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel16 -> DynamicImage)
-> Image Pixel16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel16 -> DynamicImage
ImageY16 (Image Pixel16 -> DynamicImage)
-> (Image Pixel16 -> Image Pixel16)
-> Image Pixel16
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (Pixel16 -> Pixel16) -> Image Pixel16 -> Image Pixel16
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent Pixel16 -> PixelBaseComponent Pixel16)
-> Pixel16 -> Pixel16
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap PixelBaseComponent Pixel16 -> PixelBaseComponent Pixel16
forall {a}. (Num a, Bits a) => a -> a
expand12to16) (Image Pixel16 -> Either String PalettedImage)
-> Image Pixel16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack12 -> ByteString -> TiffInfo -> Image Pixel16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack12
Pack12 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
16 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image Pixel16 -> PalettedImage)
-> Image Pixel16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image Pixel16 -> DynamicImage)
-> Image Pixel16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image Pixel16 -> DynamicImage
ImageY16 (Image Pixel16 -> Either String PalettedImage)
-> Image Pixel16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel16 -> ByteString -> TiffInfo -> Image Pixel16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel16
0 :: Word16) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
32 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
let img :: Image Pixel32
img = Pixel32 -> ByteString -> TiffInfo -> Image Pixel32
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel32
0 :: Word32) ByteString
file TiffInfo
nfo :: Image Pixel32
in PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> PalettedImage -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage) -> DynamicImage -> PalettedImage
forall a b. (a -> b) -> a -> b
$ Image Pixel32 -> DynamicImage
ImageY32 (Image Pixel32 -> DynamicImage) -> Image Pixel32 -> DynamicImage
forall a b. (a -> b) -> a -> b
$ Image Pixel32
img
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
32 Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleFloat TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
let img :: Image PixelF
img = PixelF -> ByteString -> TiffInfo -> Image PixelF
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (PixelF
0 :: Float) ByteString
file TiffInfo
nfo :: Image PixelF
in PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> PalettedImage -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage) -> DynamicImage -> PalettedImage
forall a b. (a -> b) -> a -> b
$ Image PixelF -> DynamicImage
ImageYF (Image PixelF -> DynamicImage) -> Image PixelF -> DynamicImage
forall a b. (a -> b) -> a -> b
$ Image PixelF
img
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
64 = String -> Either String PalettedImage
forall a b. a -> Either a b
Left String
"Failure to unpack TIFF file, 64-bit samples unsupported."
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
2, Pixel32
2] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA8 -> PalettedImage)
-> Image PixelYA8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA8 -> DynamicImage)
-> Image PixelYA8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA8 -> DynamicImage
ImageYA8 (Image PixelYA8 -> DynamicImage)
-> (Image PixelYA8 -> Image PixelYA8)
-> Image PixelYA8
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PixelYA8 -> PixelYA8) -> Image PixelYA8 -> Image PixelYA8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent PixelYA8 -> PixelBaseComponent PixelYA8)
-> PixelYA8 -> PixelYA8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x55 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image PixelYA8 -> Either String PalettedImage)
-> Image PixelYA8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack2 -> ByteString -> TiffInfo -> Image PixelYA8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack2
Pack2 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
4, Pixel32
4] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA8 -> PalettedImage)
-> Image PixelYA8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA8 -> DynamicImage)
-> Image PixelYA8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA8 -> DynamicImage
ImageYA8 (Image PixelYA8 -> DynamicImage)
-> (Image PixelYA8 -> Image PixelYA8)
-> Image PixelYA8
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PixelYA8 -> PixelYA8) -> Image PixelYA8 -> Image PixelYA8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent PixelYA8 -> PixelBaseComponent PixelYA8)
-> PixelYA8 -> PixelYA8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x11 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image PixelYA8 -> Either String PalettedImage)
-> Image PixelYA8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack4 -> ByteString -> TiffInfo -> Image PixelYA8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack4
Pack4 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA8 -> PalettedImage)
-> Image PixelYA8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA8 -> DynamicImage)
-> Image PixelYA8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA8 -> DynamicImage
ImageYA8 (Image PixelYA8 -> Either String PalettedImage)
-> Image PixelYA8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image PixelYA8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
12, Pixel32
12] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA16 -> PalettedImage)
-> Image PixelYA16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA16 -> DynamicImage)
-> Image PixelYA16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA16 -> DynamicImage
ImageYA16 (Image PixelYA16 -> DynamicImage)
-> (Image PixelYA16 -> Image PixelYA16)
-> Image PixelYA16
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PixelYA16 -> PixelYA16) -> Image PixelYA16 -> Image PixelYA16
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent PixelYA16 -> PixelBaseComponent PixelYA16)
-> PixelYA16 -> PixelYA16
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap PixelBaseComponent PixelYA16 -> PixelBaseComponent PixelYA16
forall {a}. (Num a, Bits a) => a -> a
expand12to16) (Image PixelYA16 -> Either String PalettedImage)
-> Image PixelYA16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack12 -> ByteString -> TiffInfo -> Image PixelYA16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack12
Pack12 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
16, Pixel32
16] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYA16 -> PalettedImage)
-> Image PixelYA16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYA16 -> DynamicImage)
-> Image PixelYA16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYA16 -> DynamicImage
ImageYA16 (Image PixelYA16 -> Either String PalettedImage)
-> Image PixelYA16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel16 -> ByteString -> TiffInfo -> Image PixelYA16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel16
0 :: Word16) ByteString
file TiffInfo
nfo
where
expand12to16 :: a -> a
expand12to16 a
x = a
x a -> Int -> a
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
4 a -> a -> a
forall a. Num a => a -> a -> a
+ a
x a -> Int -> a
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
12 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
4)
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffYCbCr
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffPlaneConfiguration :: TiffInfo -> TiffPlanarConfiguration
tiffPlaneConfiguration = TiffPlanarConfiguration
PlanarConfigContig
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format }
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelYCbCr8 -> PalettedImage)
-> Image PixelYCbCr8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelYCbCr8 -> DynamicImage)
-> Image PixelYCbCr8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelYCbCr8 -> DynamicImage
ImageYCbCr8 (Image PixelYCbCr8 -> Either String PalettedImage)
-> Image PixelYCbCr8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ YCbCrSubsampling -> ByteString -> TiffInfo -> Image PixelYCbCr8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips YCbCrSubsampling
cbcrConf ByteString
file TiffInfo
nfo
where defaulting :: p -> p
defaulting p
0 = p
2
defaulting p
n = p
n
w :: Pixel32
w = Pixel32 -> Pixel32
forall {p}. (Eq p, Num p) => p -> p
defaulting (Pixel32 -> Pixel32) -> Pixel32 -> Pixel32
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffYCbCrSubsampling TiffInfo
nfo Vector Pixel32 -> Int -> Pixel32
forall a. Vector a -> Int -> a
V.! Int
0
h :: Pixel32
h = Pixel32 -> Pixel32
forall {p}. (Eq p, Num p) => p -> p
defaulting (Pixel32 -> Pixel32) -> Pixel32 -> Pixel32
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Vector Pixel32
tiffYCbCrSubsampling TiffInfo
nfo Vector Pixel32 -> Int -> Pixel32
forall a. Vector a -> Int -> a
V.! Int
1
cbcrConf :: YCbCrSubsampling
cbcrConf = YCbCrSubsampling :: Int -> Int -> Int -> Int -> YCbCrSubsampling
YCbCrSubsampling
{ ycbcrWidth :: Int
ycbcrWidth = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
w
, ycbcrHeight :: Int
ycbcrHeight = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
h
, ycbcrImageWidth :: Int
ycbcrImageWidth = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffWidth TiffInfo
nfo
, ycbcrStripHeight :: Int
ycbcrStripHeight = Pixel32 -> Int
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Pixel32 -> Int) -> Pixel32 -> Int
forall a b. (a -> b) -> a -> b
$ TiffInfo -> Pixel32
tiffRowPerStrip TiffInfo
nfo
}
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffRGB
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format }
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
2, Pixel32
2, Pixel32
2] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGB8 -> PalettedImage)
-> Image PixelRGB8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGB8 -> DynamicImage)
-> Image PixelRGB8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB8 -> DynamicImage
ImageRGB8 (Image PixelRGB8 -> DynamicImage)
-> (Image PixelRGB8 -> Image PixelRGB8)
-> Image PixelRGB8
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PixelRGB8 -> PixelRGB8) -> Image PixelRGB8 -> Image PixelRGB8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent PixelRGB8 -> PixelBaseComponent PixelRGB8)
-> PixelRGB8 -> PixelRGB8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x55 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image PixelRGB8 -> Either String PalettedImage)
-> Image PixelRGB8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack2 -> ByteString -> TiffInfo -> Image PixelRGB8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack2
Pack2 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
4, Pixel32
4, Pixel32
4] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGB8 -> PalettedImage)
-> Image PixelRGB8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGB8 -> DynamicImage)
-> Image PixelRGB8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB8 -> DynamicImage
ImageRGB8 (Image PixelRGB8 -> DynamicImage)
-> (Image PixelRGB8 -> Image PixelRGB8)
-> Image PixelRGB8
-> DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (PixelRGB8 -> PixelRGB8) -> Image PixelRGB8 -> Image PixelRGB8
forall a b. (Pixel a, Pixel b) => (a -> b) -> Image a -> Image b
pixelMap ((PixelBaseComponent PixelRGB8 -> PixelBaseComponent PixelRGB8)
-> PixelRGB8 -> PixelRGB8
forall a.
Pixel a =>
(PixelBaseComponent a -> PixelBaseComponent a) -> a -> a
colorMap (Pixel8
0x11 Pixel8 -> Pixel8 -> Pixel8
forall a. Num a => a -> a -> a
*)) (Image PixelRGB8 -> Either String PalettedImage)
-> Image PixelRGB8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pack4 -> ByteString -> TiffInfo -> Image PixelRGB8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips Pack4
Pack4 ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGB8 -> PalettedImage)
-> Image PixelRGB8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGB8 -> DynamicImage)
-> Image PixelRGB8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB8 -> DynamicImage
ImageRGB8 (Image PixelRGB8 -> Either String PalettedImage)
-> Image PixelRGB8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image PixelRGB8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8, Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGBA8 -> PalettedImage)
-> Image PixelRGBA8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGBA8 -> DynamicImage)
-> Image PixelRGBA8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGBA8 -> DynamicImage
ImageRGBA8 (Image PixelRGBA8 -> Either String PalettedImage)
-> Image PixelRGBA8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image PixelRGBA8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
16, Pixel32
16, Pixel32
16] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGB16 -> PalettedImage)
-> Image PixelRGB16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGB16 -> DynamicImage)
-> Image PixelRGB16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB16 -> DynamicImage
ImageRGB16 (Image PixelRGB16 -> Either String PalettedImage)
-> Image PixelRGB16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel16 -> ByteString -> TiffInfo -> Image PixelRGB16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel16
0 :: Word16) ByteString
file TiffInfo
nfo
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
16, Pixel32
16, Pixel32
16, Pixel32
16] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGBA16 -> PalettedImage)
-> Image PixelRGBA16
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGBA16 -> DynamicImage)
-> Image PixelRGBA16
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGBA16 -> DynamicImage
ImageRGBA16 (Image PixelRGBA16 -> Either String PalettedImage)
-> Image PixelRGBA16 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel16 -> ByteString -> TiffInfo -> Image PixelRGBA16
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel16
0 :: Word16) ByteString
file TiffInfo
nfo
unpack ByteString
file nfo :: TiffInfo
nfo@TiffInfo { tiffColorspace :: TiffInfo -> TiffColorspace
tiffColorspace = TiffColorspace
TiffMonochrome
, tiffBitsPerSample :: TiffInfo -> Vector Pixel32
tiffBitsPerSample = Vector Pixel32
lst
, tiffSampleFormat :: TiffInfo -> [TiffSampleFormat]
tiffSampleFormat = [TiffSampleFormat]
format }
| Vector Pixel32
lst Vector Pixel32 -> Vector Pixel32 -> Bool
forall a. Eq a => a -> a -> Bool
== [Pixel32] -> Vector Pixel32
forall a. [a] -> Vector a
V.fromList [Pixel32
8, Pixel32
8, Pixel32
8] Bool -> Bool -> Bool
&& (TiffSampleFormat -> Bool) -> [TiffSampleFormat] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (TiffSampleFormat
TiffSampleUint TiffSampleFormat -> TiffSampleFormat -> Bool
forall a. Eq a => a -> a -> Bool
==) [TiffSampleFormat]
format =
PalettedImage -> Either String PalettedImage
forall (f :: * -> *) a. Applicative f => a -> f a
pure (PalettedImage -> Either String PalettedImage)
-> (Image PixelRGB8 -> PalettedImage)
-> Image PixelRGB8
-> Either String PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynamicImage -> PalettedImage
TrueColorImage (DynamicImage -> PalettedImage)
-> (Image PixelRGB8 -> DynamicImage)
-> Image PixelRGB8
-> PalettedImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Image PixelRGB8 -> DynamicImage
ImageRGB8 (Image PixelRGB8 -> Either String PalettedImage)
-> Image PixelRGB8 -> Either String PalettedImage
forall a b. (a -> b) -> a -> b
$ Pixel8 -> ByteString -> TiffInfo -> Image PixelRGB8
forall comp pixel.
(Unpackable comp, Pixel pixel,
StorageType comp ~ PixelBaseComponent pixel) =>
comp -> ByteString -> TiffInfo -> Image pixel
gatherStrips (Pixel8
0 :: Word8) ByteString
file TiffInfo
nfo
unpack ByteString
_ TiffInfo
_ = String -> Either String PalettedImage
forall a b. a -> Either a b
Left String
"Failure to unpack TIFF file"
decodeTiff :: B.ByteString -> Either String DynamicImage
decodeTiff :: ByteString -> Either String DynamicImage
decodeTiff = ((DynamicImage, Metadatas) -> DynamicImage)
-> Either String (DynamicImage, Metadatas)
-> Either String DynamicImage
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (DynamicImage, Metadatas) -> DynamicImage
forall a b. (a, b) -> a
fst (Either String (DynamicImage, Metadatas)
-> Either String DynamicImage)
-> (ByteString -> Either String (DynamicImage, Metadatas))
-> ByteString
-> Either String DynamicImage
forall b c a. (b -> c) -> (a -> b) -> a -> c
. ByteString -> Either String (DynamicImage, Metadatas)
decodeTiffWithMetadata
decodeTiffWithMetadata :: B.ByteString -> Either String (DynamicImage, Metadatas)
decodeTiffWithMetadata :: ByteString -> Either String (DynamicImage, Metadatas)
decodeTiffWithMetadata ByteString
str = (PalettedImage -> DynamicImage)
-> (PalettedImage, Metadatas) -> (DynamicImage, Metadatas)
forall (a :: * -> * -> *) b c d.
Arrow a =>
a b c -> a (b, d) (c, d)
first PalettedImage -> DynamicImage
palettedToTrueColor ((PalettedImage, Metadatas) -> (DynamicImage, Metadatas))
-> Either String (PalettedImage, Metadatas)
-> Either String (DynamicImage, Metadatas)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> Either String (PalettedImage, Metadatas)
decodeTiffWithPaletteAndMetadata ByteString
str
decodeTiffWithPaletteAndMetadata :: B.ByteString -> Either String (PalettedImage, Metadatas)
decodeTiffWithPaletteAndMetadata :: ByteString -> Either String (PalettedImage, Metadatas)
decodeTiffWithPaletteAndMetadata ByteString
file = Get TiffInfo -> ByteString -> Either String TiffInfo
forall a. Get a -> ByteString -> Either String a
runGetStrict (ByteString -> Get TiffInfo
forall a b. BinaryParam a b => a -> Get b
getP ByteString
file) ByteString
file Either String TiffInfo
-> (TiffInfo -> Either String (PalettedImage, Metadatas))
-> Either String (PalettedImage, Metadatas)
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= TiffInfo -> Either String (PalettedImage, Metadatas)
go
where
go :: TiffInfo -> Either String (PalettedImage, Metadatas)
go TiffInfo
tinfo = (, TiffInfo -> Metadatas
tiffMetadatas TiffInfo
tinfo) (PalettedImage -> (PalettedImage, Metadatas))
-> Either String PalettedImage
-> Either String (PalettedImage, Metadatas)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> ByteString -> TiffInfo -> Either String PalettedImage
unpack ByteString
file TiffInfo
tinfo
class (Pixel px) => TiffSaveable px where
colorSpaceOfPixel :: px -> TiffColorspace
:: px -> Maybe ExtraSample
extraSampleCodeOfPixel px
_ = Maybe ExtraSample
forall a. Maybe a
Nothing
subSamplingInfo :: px -> V.Vector Word32
subSamplingInfo px
_ = Vector Pixel32
forall a. Vector a
V.empty
sampleFormat :: px -> [TiffSampleFormat]
sampleFormat px
_ = [TiffSampleFormat
TiffSampleUint]
instance TiffSaveable Pixel8 where
colorSpaceOfPixel :: Pixel8 -> TiffColorspace
colorSpaceOfPixel Pixel8
_ = TiffColorspace
TiffMonochrome
instance TiffSaveable Pixel16 where
colorSpaceOfPixel :: Pixel16 -> TiffColorspace
colorSpaceOfPixel Pixel16
_ = TiffColorspace
TiffMonochrome
instance TiffSaveable Pixel32 where
colorSpaceOfPixel :: Pixel32 -> TiffColorspace
colorSpaceOfPixel Pixel32
_ = TiffColorspace
TiffMonochrome
instance TiffSaveable PixelF where
colorSpaceOfPixel :: PixelF -> TiffColorspace
colorSpaceOfPixel PixelF
_ = TiffColorspace
TiffMonochrome
sampleFormat :: PixelF -> [TiffSampleFormat]
sampleFormat PixelF
_ = [TiffSampleFormat
TiffSampleFloat]
instance TiffSaveable PixelYA8 where
colorSpaceOfPixel :: PixelYA8 -> TiffColorspace
colorSpaceOfPixel PixelYA8
_ = TiffColorspace
TiffMonochrome
extraSampleCodeOfPixel :: PixelYA8 -> Maybe ExtraSample
extraSampleCodeOfPixel PixelYA8
_ = ExtraSample -> Maybe ExtraSample
forall a. a -> Maybe a
Just ExtraSample
ExtraSampleUnassociatedAlpha
instance TiffSaveable PixelYA16 where
colorSpaceOfPixel :: PixelYA16 -> TiffColorspace
colorSpaceOfPixel PixelYA16
_ = TiffColorspace
TiffMonochrome
extraSampleCodeOfPixel :: PixelYA16 -> Maybe ExtraSample
extraSampleCodeOfPixel PixelYA16
_ = ExtraSample -> Maybe ExtraSample
forall a. a -> Maybe a
Just ExtraSample
ExtraSampleUnassociatedAlpha
instance TiffSaveable PixelCMYK8 where
colorSpaceOfPixel :: PixelCMYK8 -> TiffColorspace
colorSpaceOfPixel PixelCMYK8
_ = TiffColorspace
TiffCMYK
instance TiffSaveable PixelCMYK16 where
colorSpaceOfPixel :: PixelCMYK16 -> TiffColorspace
colorSpaceOfPixel PixelCMYK16
_ = TiffColorspace
TiffCMYK
instance TiffSaveable PixelRGB8 where
colorSpaceOfPixel :: PixelRGB8 -> TiffColorspace
colorSpaceOfPixel PixelRGB8
_ = TiffColorspace
TiffRGB
instance TiffSaveable PixelRGB16 where
colorSpaceOfPixel :: PixelRGB16 -> TiffColorspace
colorSpaceOfPixel PixelRGB16
_ = TiffColorspace
TiffRGB
instance TiffSaveable PixelRGBA8 where
colorSpaceOfPixel :: PixelRGBA8 -> TiffColorspace
colorSpaceOfPixel PixelRGBA8
_ = TiffColorspace
TiffRGB
extraSampleCodeOfPixel :: PixelRGBA8 -> Maybe ExtraSample
extraSampleCodeOfPixel PixelRGBA8
_ = ExtraSample -> Maybe ExtraSample
forall a. a -> Maybe a
Just ExtraSample
ExtraSampleUnassociatedAlpha
instance TiffSaveable PixelRGBA16 where
colorSpaceOfPixel :: PixelRGBA16 -> TiffColorspace
colorSpaceOfPixel PixelRGBA16
_ = TiffColorspace
TiffRGB
extraSampleCodeOfPixel :: PixelRGBA16 -> Maybe ExtraSample
extraSampleCodeOfPixel PixelRGBA16
_ = ExtraSample -> Maybe ExtraSample
forall a. a -> Maybe a
Just ExtraSample
ExtraSampleUnassociatedAlpha
instance TiffSaveable PixelYCbCr8 where
colorSpaceOfPixel :: PixelYCbCr8 -> TiffColorspace
colorSpaceOfPixel PixelYCbCr8
_ = TiffColorspace
TiffYCbCr
subSamplingInfo :: PixelYCbCr8 -> Vector Pixel32
subSamplingInfo PixelYCbCr8
_ = Int -> [Pixel32] -> Vector Pixel32
forall a. Int -> [a] -> Vector a
V.fromListN Int
2 [Pixel32
1, Pixel32
1]
encodeTiff :: forall px. (TiffSaveable px) => Image px -> Lb.ByteString
encodeTiff :: forall px. TiffSaveable px => Image px -> ByteString
encodeTiff Image px
img = Put -> ByteString
runPut (Put -> ByteString) -> Put -> ByteString
forall a b. (a -> b) -> a -> b
$ ByteString -> TiffInfo -> Put
forall a b. BinaryParam a b => a -> b -> Put
putP ByteString
rawPixelData TiffInfo
hdr
where intSampleCount :: Int
intSampleCount = px -> Int
forall a. Pixel a => a -> Int
componentCount (px
forall a. HasCallStack => a
undefined :: px)
sampleCount :: Pixel32
sampleCount = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
intSampleCount
sampleType :: PixelBaseComponent px
sampleType = PixelBaseComponent px
forall a. HasCallStack => a
undefined :: PixelBaseComponent px
pixelData :: Vector (PixelBaseComponent px)
pixelData = Image px -> Vector (PixelBaseComponent px)
forall a. Image a -> Vector (PixelBaseComponent a)
imageData Image px
img
rawPixelData :: ByteString
rawPixelData = Vector (PixelBaseComponent px) -> ByteString
forall a. Storable a => Vector a -> ByteString
toByteString Vector (PixelBaseComponent px)
pixelData
width :: Pixel32
width = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Image px -> Int
forall a. Image a -> Int
imageWidth Image px
img
height :: Pixel32
height = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Image px -> Int
forall a. Image a -> Int
imageHeight Image px
img
intSampleSize :: Int
intSampleSize = PixelBaseComponent px -> Int
forall a. Storable a => a -> Int
sizeOf PixelBaseComponent px
sampleType
sampleSize :: Pixel32
sampleSize = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
intSampleSize
bitPerSample :: Pixel32
bitPerSample = Pixel32
sampleSize Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
* Pixel32
8
imageSize :: Pixel32
imageSize = Pixel32
width Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
* Pixel32
height Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
* Pixel32
sampleCount Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
* Pixel32
sampleSize
headerSize :: Pixel32
headerSize = Pixel32
8
hdr :: TiffInfo
hdr = TiffInfo :: TiffHeader
-> Pixel32
-> Pixel32
-> TiffColorspace
-> Pixel32
-> Pixel32
-> TiffPlanarConfiguration
-> [TiffSampleFormat]
-> Vector Pixel32
-> TiffCompression
-> Vector Pixel32
-> Vector Pixel32
-> Maybe (Image PixelRGB16)
-> Vector Pixel32
-> Maybe ExtraSample
-> Predictor
-> Metadatas
-> TiffInfo
TiffInfo
{ tiffHeader :: TiffHeader
tiffHeader = TiffHeader :: Endianness -> Pixel32 -> TiffHeader
TiffHeader
{ hdrEndianness :: Endianness
hdrEndianness = Endianness
EndianLittle
, hdrOffset :: Pixel32
hdrOffset = Pixel32
headerSize Pixel32 -> Pixel32 -> Pixel32
forall a. Num a => a -> a -> a
+ Pixel32
imageSize
}
, tiffWidth :: Pixel32
tiffWidth = Pixel32
width
, tiffHeight :: Pixel32
tiffHeight = Pixel32
height
, tiffColorspace :: TiffColorspace
tiffColorspace = px -> TiffColorspace
forall px. TiffSaveable px => px -> TiffColorspace
colorSpaceOfPixel (px
forall a. HasCallStack => a
undefined :: px)
, tiffSampleCount :: Pixel32
tiffSampleCount = Pixel32 -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral Pixel32
sampleCount
, tiffRowPerStrip :: Pixel32
tiffRowPerStrip = Int -> Pixel32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int -> Pixel32) -> Int -> Pixel32
forall a b. (a -> b) -> a -> b
$ Image px -> Int
forall a. Image a -> Int
imageHeight Image px
img
, tiffPlaneConfiguration :: TiffPlanarConfiguration
tiffPlaneConfiguration = TiffPlanarConfiguration
PlanarConfigContig
, tiffSampleFormat :: [TiffSampleFormat]
tiffSampleFormat = px -> [TiffSampleFormat]
forall px. TiffSaveable px => px -> [TiffSampleFormat]
sampleFormat (px
forall a. HasCallStack => a
undefined :: px)
, tiffBitsPerSample :: Vector Pixel32
tiffBitsPerSample = Int -> Pixel32 -> Vector Pixel32
forall a. Int -> a -> Vector a
V.replicate Int
intSampleCount Pixel32
bitPerSample
, tiffCompression :: TiffCompression
tiffCompression = TiffCompression
CompressionNone
, tiffStripSize :: Vector Pixel32
tiffStripSize = Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
imageSize
, tiffOffsets :: Vector Pixel32
tiffOffsets = Pixel32 -> Vector Pixel32
forall a. a -> Vector a
V.singleton Pixel32
headerSize
, tiffPalette :: Maybe (Image PixelRGB16)
tiffPalette = Maybe (Image PixelRGB16)
forall a. Maybe a
Nothing
, tiffYCbCrSubsampling :: Vector Pixel32
tiffYCbCrSubsampling = px -> Vector Pixel32
forall px. TiffSaveable px => px -> Vector Pixel32
subSamplingInfo (px
forall a. HasCallStack => a
undefined :: px)
, tiffExtraSample :: Maybe ExtraSample
tiffExtraSample = px -> Maybe ExtraSample
forall px. TiffSaveable px => px -> Maybe ExtraSample
extraSampleCodeOfPixel (px
forall a. HasCallStack => a
undefined :: px)
, tiffPredictor :: Predictor
tiffPredictor = Predictor
PredictorNone
, tiffMetadatas :: Metadatas
tiffMetadatas = Metadatas
forall a. Monoid a => a
mempty
}
writeTiff :: (TiffSaveable pixel) => FilePath -> Image pixel -> IO ()
writeTiff :: forall pixel. TiffSaveable pixel => String -> Image pixel -> IO ()
writeTiff String
path Image pixel
img = String -> ByteString -> IO ()
Lb.writeFile String
path (ByteString -> IO ()) -> ByteString -> IO ()
forall a b. (a -> b) -> a -> b
$ Image pixel -> ByteString
forall px. TiffSaveable px => Image px -> ByteString
encodeTiff Image pixel
img
{-# ANN module "HLint: ignore Reduce duplication" #-}