{-# LANGUAGE CPP #-}
module Codec.Picture.Jpg.Internal.Metadata ( extractMetadatas, encodeMetadatas ) where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative( pure )
import Data.Monoid( mempty )
import Data.Word( Word )
#endif

import Data.Word( Word16 )
import Data.Maybe( fromMaybe )
import qualified Codec.Picture.Metadata as Met
import Codec.Picture.Metadata( Metadatas )
import Codec.Picture.Jpg.Internal.Types

scalerOfUnit :: JFifUnit -> Met.Keys Word -> Word16 -> Metadatas -> Metadatas
scalerOfUnit :: JFifUnit -> Keys Word -> Word16 -> Metadatas -> Metadatas
scalerOfUnit JFifUnit
unit Keys Word
k Word16
v = case JFifUnit
unit of
  JFifUnit
JFifUnitUnknown -> Metadatas -> Metadatas
forall a. a -> a
id
  JFifUnit
JFifPixelsPerInch -> Keys Word -> Word -> Metadatas -> Metadatas
forall a.
(Show a, NFData a) =>
Keys a -> a -> Metadatas -> Metadatas
Met.insert Keys Word
k (Word16 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
v)
  JFifUnit
JFifPixelsPerCentimeter ->
    Keys Word -> Word -> Metadatas -> Metadatas
forall a.
(Show a, NFData a) =>
Keys a -> a -> Metadatas -> Metadatas
Met.insert Keys Word
k (Word -> Word
Met.dotsPerCentiMeterToDotPerInch (Word -> Word) -> Word -> Word
forall a b. (a -> b) -> a -> b
$ Word16 -> Word
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word16
v)

extractMetadatas :: JpgJFIFApp0 -> Metadatas
extractMetadatas :: JpgJFIFApp0 -> Metadatas
extractMetadatas JpgJFIFApp0
jfif = 
    Keys Word -> Word16 -> Metadatas -> Metadatas
inserter Keys Word
Met.DpiX (JpgJFIFApp0 -> Word16
_jfifDpiX JpgJFIFApp0
jfif)
        (Metadatas -> Metadatas) -> Metadatas -> Metadatas
forall a b. (a -> b) -> a -> b
$ Keys Word -> Word16 -> Metadatas -> Metadatas
inserter Keys Word
Met.DpiY (JpgJFIFApp0 -> Word16
_jfifDpiY JpgJFIFApp0
jfif) Metadatas
forall a. Monoid a => a
mempty
  where
    inserter :: Keys Word -> Word16 -> Metadatas -> Metadatas
inserter = JFifUnit -> Keys Word -> Word16 -> Metadatas -> Metadatas
scalerOfUnit (JFifUnit -> Keys Word -> Word16 -> Metadatas -> Metadatas)
-> JFifUnit -> Keys Word -> Word16 -> Metadatas -> Metadatas
forall a b. (a -> b) -> a -> b
$ JpgJFIFApp0 -> JFifUnit
_jfifUnit JpgJFIFApp0
jfif


encodeMetadatas :: Metadatas -> [JpgFrame]
encodeMetadatas :: Metadatas -> [JpgFrame]
encodeMetadatas Metadatas
metas = [JpgFrame] -> Maybe [JpgFrame] -> [JpgFrame]
forall a. a -> Maybe a -> a
fromMaybe [] (Maybe [JpgFrame] -> [JpgFrame]) -> Maybe [JpgFrame] -> [JpgFrame]
forall a b. (a -> b) -> a -> b
$ do
  Word
dpiX <- Keys Word -> Metadatas -> Maybe Word
forall a. Keys a -> Metadatas -> Maybe a
Met.lookup Keys Word
Met.DpiX Metadatas
metas
  Word
dpiY <- Keys Word -> Metadatas -> Maybe Word
forall a. Keys a -> Metadatas -> Maybe a
Met.lookup Keys Word
Met.DpiY Metadatas
metas
  [JpgFrame] -> Maybe [JpgFrame]
forall (f :: * -> *) a. Applicative f => a -> f a
pure ([JpgFrame] -> Maybe [JpgFrame])
-> (JpgJFIFApp0 -> [JpgFrame]) -> JpgJFIFApp0 -> Maybe [JpgFrame]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JpgFrame -> [JpgFrame]
forall (f :: * -> *) a. Applicative f => a -> f a
pure (JpgFrame -> [JpgFrame])
-> (JpgJFIFApp0 -> JpgFrame) -> JpgJFIFApp0 -> [JpgFrame]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. JpgJFIFApp0 -> JpgFrame
JpgJFIF (JpgJFIFApp0 -> Maybe [JpgFrame])
-> JpgJFIFApp0 -> Maybe [JpgFrame]
forall a b. (a -> b) -> a -> b
$ JpgJFIFApp0 :: JFifUnit -> Word16 -> Word16 -> Maybe Int -> JpgJFIFApp0
JpgJFIFApp0
    { _jfifUnit :: JFifUnit
_jfifUnit      = JFifUnit
JFifPixelsPerInch
    , _jfifDpiX :: Word16
_jfifDpiX      = Word -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
dpiX
    , _jfifDpiY :: Word16
_jfifDpiY      = Word -> Word16
forall a b. (Integral a, Num b) => a -> b
fromIntegral Word
dpiY
    , _jfifThumbnail :: Maybe Int
_jfifThumbnail = Maybe Int
forall a. Maybe a
Nothing
    }