{-# LINE 1 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
{-# language CPP #-}
{-# language OverloadedLists #-}


{-# LINE 5 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
{-# OPTIONS_HADDOCK hide #-}

{-# LINE 7 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

module OpenCV.Internal.ImgCodecs
    ( ImreadMode(..)
    , marshalImreadMode
    , JpegParams(..)
    , defaultJpegParams
    , marshalJpegParams
    , PngStrategy(..)
    , marshalPngStrategy
    , PngParams(..)
    , defaultPngParams
    , marshalPngParams
    , OutputFormat(..)
    , marshalOutputFormat
    ) where

import "base" Data.Int
import "base" Data.Word
import "base" Foreign.C.Types
import "base" Foreign.Marshal.Utils ( fromBool )
import qualified "vector" Data.Vector.Storable as VS









data ImreadMode
   = ImreadUnchanged
   | ImreadGrayscale
   | ImreadColor
   | ImreadAnyDepth
   | ImreadAnyColor
   | ImreadLoadGdal
     deriving (Show)

c'IMREAD_UNCHANGED = -1
c'IMREAD_UNCHANGED :: (Num a) => a

{-# LINE 47 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMREAD_GRAYSCALE = 0
c'IMREAD_GRAYSCALE :: (Num a) => a

{-# LINE 48 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMREAD_COLOR = 1
c'IMREAD_COLOR :: (Num a) => a

{-# LINE 49 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMREAD_ANYDEPTH = 2
c'IMREAD_ANYDEPTH :: (Num a) => a

{-# LINE 50 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMREAD_ANYCOLOR = 4
c'IMREAD_ANYCOLOR :: (Num a) => a

{-# LINE 51 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMREAD_LOAD_GDAL = 8
c'IMREAD_LOAD_GDAL :: (Num a) => a

{-# LINE 52 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

marshalImreadMode :: ImreadMode -> Int32
marshalImreadMode = \case
    ImreadUnchanged -> c'IMREAD_UNCHANGED
    ImreadGrayscale -> c'IMREAD_GRAYSCALE
    ImreadColor     -> c'IMREAD_COLOR
    ImreadAnyDepth  -> c'IMREAD_ANYDEPTH
    ImreadAnyColor  -> c'IMREAD_ANYCOLOR
    ImreadLoadGdal  -> c'IMREAD_LOAD_GDAL

data JpegParams =
     JpegParams
     { jpegParamQuality         :: Int -- ^ \[0..100\]
     , jpegParamProgressive     :: Bool
     , jpegParamOptimize        :: Bool
     , jpegParamRestartInterval :: Word16
     , jpegParamLumaQuality     :: Int
     , jpegParamChromaQuality   :: Int
     } deriving Show

defaultJpegParams :: JpegParams
defaultJpegParams =
    JpegParams
    { jpegParamQuality         = 95
    , jpegParamProgressive     = False
    , jpegParamOptimize        = False
    , jpegParamRestartInterval = 0
    , jpegParamLumaQuality     = -1
    , jpegParamChromaQuality   = -1
    }

c'IMWRITE_JPEG_QUALITY = 1
c'IMWRITE_JPEG_QUALITY :: (Num a) => a

{-# LINE 84 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_JPEG_PROGRESSIVE = 2
c'IMWRITE_JPEG_PROGRESSIVE :: (Num a) => a

{-# LINE 85 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_JPEG_OPTIMIZE = 3
c'IMWRITE_JPEG_OPTIMIZE :: (Num a) => a

{-# LINE 86 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_JPEG_RST_INTERVAL = 4
c'IMWRITE_JPEG_RST_INTERVAL :: (Num a) => a

{-# LINE 87 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_JPEG_LUMA_QUALITY = 5
c'IMWRITE_JPEG_LUMA_QUALITY :: (Num a) => a

{-# LINE 88 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_JPEG_CHROMA_QUALITY = 6
c'IMWRITE_JPEG_CHROMA_QUALITY :: (Num a) => a

{-# LINE 89 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

marshalJpegParams :: JpegParams -> VS.Vector CInt
marshalJpegParams params =
    [ c'IMWRITE_JPEG_QUALITY       , fromIntegral $ jpegParamQuality         params
    , c'IMWRITE_JPEG_PROGRESSIVE   , fromBool     $ jpegParamProgressive     params
    , c'IMWRITE_JPEG_OPTIMIZE      , fromBool     $ jpegParamOptimize        params
    , c'IMWRITE_JPEG_RST_INTERVAL  , fromIntegral $ jpegParamRestartInterval params
    , c'IMWRITE_JPEG_LUMA_QUALITY  , fromIntegral $ jpegParamLumaQuality     params
    , c'IMWRITE_JPEG_CHROMA_QUALITY, fromIntegral $ jpegParamChromaQuality   params
    ]

data PngStrategy
   = PngStrategyDefault
   | PngStrategyFiltered
   | PngStrategyHuffmanOnly
   | PngStrategyRLE
   | PngStrategyFixed
     deriving Show

c'IMWRITE_PNG_STRATEGY_DEFAULT = 0
c'IMWRITE_PNG_STRATEGY_DEFAULT :: (Num a) => a

{-# LINE 109 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_STRATEGY_FILTERED = 1
c'IMWRITE_PNG_STRATEGY_FILTERED :: (Num a) => a

{-# LINE 110 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY = 2
c'IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY :: (Num a) => a

{-# LINE 111 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_STRATEGY_RLE = 3
c'IMWRITE_PNG_STRATEGY_RLE :: (Num a) => a

{-# LINE 112 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_STRATEGY_FIXED = 4
c'IMWRITE_PNG_STRATEGY_FIXED :: (Num a) => a

{-# LINE 113 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

marshalPngStrategy :: PngStrategy -> CInt
marshalPngStrategy = \case
   PngStrategyDefault     -> c'IMWRITE_PNG_STRATEGY_DEFAULT
   PngStrategyFiltered    -> c'IMWRITE_PNG_STRATEGY_FILTERED
   PngStrategyHuffmanOnly -> c'IMWRITE_PNG_STRATEGY_HUFFMAN_ONLY
   PngStrategyRLE         -> c'IMWRITE_PNG_STRATEGY_RLE
   PngStrategyFixed       -> c'IMWRITE_PNG_STRATEGY_FIXED

data PngParams =
     PngParams
     { pngParamCompression :: Int
     , pngParamStrategy    :: PngStrategy
     , pngParamBinaryLevel :: Bool
     } deriving Show

defaultPngParams :: PngParams
defaultPngParams =
    PngParams
    { pngParamCompression = 3
    , pngParamStrategy    = PngStrategyDefault
    , pngParamBinaryLevel = False
    }

c'IMWRITE_PNG_COMPRESSION = 16
c'IMWRITE_PNG_COMPRESSION :: (Num a) => a

{-# LINE 138 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_STRATEGY = 17
c'IMWRITE_PNG_STRATEGY :: (Num a) => a

{-# LINE 139 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_PNG_BILEVEL = 18
c'IMWRITE_PNG_BILEVEL :: (Num a) => a

{-# LINE 140 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

marshalPngParams :: PngParams -> VS.Vector CInt
marshalPngParams params =
    [ c'IMWRITE_PNG_COMPRESSION, fromIntegral        $ pngParamCompression params
    , c'IMWRITE_PNG_STRATEGY   , marshalPngStrategy $ pngParamStrategy    params
    , c'IMWRITE_PNG_BILEVEL    , fromBool            $ pngParamBinaryLevel params
    ]

data OutputFormat
   = OutputBmp
   | OutputExr
   | OutputHdr Bool -- ^ Compression (run length encoding)
   | OutputJpeg JpegParams
   | OutputJpeg2000
   | OutputPng PngParams
   | OutputPxm Bool -- ^ Binary
   | OutputSunras
   | OutputTiff
   | OutputWebP Int -- ^ Quality [1..100], > 100 == lossless
     deriving Show

c'IMWRITE_PXM_BINARY = 32
c'IMWRITE_PXM_BINARY :: (Num a) => a

{-# LINE 162 "src/OpenCV/Internal/ImgCodecs.hsc" #-}
c'IMWRITE_WEBP_QUALITY = 64
c'IMWRITE_WEBP_QUALITY :: (Num a) => a

{-# LINE 163 "src/OpenCV/Internal/ImgCodecs.hsc" #-}

marshalOutputFormat :: OutputFormat -> (String, VS.Vector CInt)
marshalOutputFormat = \case
    OutputBmp          -> (".bmp" , [])
    OutputExr          -> (".exr" , [])
    OutputHdr comp     -> (".hdr" , [fromBool comp])
    OutputJpeg params  -> (".jpeg", marshalJpegParams params)
    OutputJpeg2000     -> (".jp2" , [])
    OutputPng params   -> (".png" , marshalPngParams params)
    OutputPxm binary   -> (".pxm" , [c'IMWRITE_PXM_BINARY, fromBool binary])
    OutputSunras       -> (".sr"  , [])
    OutputTiff         -> (".tiff", [])
    OutputWebP quality -> (".webp", [c'IMWRITE_WEBP_QUALITY, fromIntegral quality])