{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE RecordWildCards #-}

module Codec.QRCode.Data.MQRImage
  ( MQRImage1(..)
  , MQRImage2(..)
  , MQRImage3(..)
  , new
  , unsafeConvert
  , clone
  , unsafeFreeze
  ) where

import           Codec.QRCode.Base

import           Control.Monad.Primitive      (PrimMonad, PrimState)
import qualified Data.Vector.Unboxed          as UV
import qualified Data.Vector.Unboxed.Mutable  as MUV

import           Codec.QRCode.Code.Data
import           Codec.QRCode.Data.ErrorLevel
import           Codec.QRCode.Data.QRImage
import           Codec.QRCode.Data.Version

data MQRImage1 s
  = MQRImage1
    { MQRImage1 s -> Int
mqrImage1Size       :: !Int
    , MQRImage1 s -> MVector s Bool
mqrImage1Data       :: !(MUV.MVector s Bool)
    , MQRImage1 s -> MVector s Bool
mqrImage1Fixed      :: !(MUV.MVector s Bool)
    , MQRImage1 s -> Version
mqrImage1Version    :: !Version
    , MQRImage1 s -> ErrorLevel
mqrImage1ErrorLevel :: !ErrorLevel
    }

data MQRImage2 s
  = MQRImage2
    { MQRImage2 s -> Int
mqrImage2Size       :: !Int
    , MQRImage2 s -> MVector s Bool
mqrImage2Data       :: !(MUV.MVector s Bool)
    , MQRImage2 s -> Vector Bool
mqrImage2Fixed      :: !(UV.Vector Bool)
    , MQRImage2 s -> Version
mqrImage2Version    :: !Version
    , MQRImage2 s -> ErrorLevel
mqrImage2ErrorLevel :: !ErrorLevel
    }

data MQRImage3 s
  = MQRImage3
    { MQRImage3 s -> Int
mqrImage3Size       :: !Int
    , MQRImage3 s -> MVector s Bool
mqrImage3Data       :: !(MUV.MVector s Bool)
    , MQRImage3 s -> Vector Bool
mqrImage3Fixed      :: !(UV.Vector Bool)
    , MQRImage3 s -> Version
mqrImage3Version    :: !Version
    , MQRImage3 s -> ErrorLevel
mqrImage3ErrorLevel :: !ErrorLevel
    }

new :: PrimMonad m => Version -> ErrorLevel -> m (MQRImage1 (PrimState m))
new :: Version -> ErrorLevel -> m (MQRImage1 (PrimState m))
new Version
v ErrorLevel
e = do
  let
    size :: Int
size = Version -> Int
qrSize Version
v
  MVector (PrimState m) Bool
img <- Int -> m (MVector (PrimState m) Bool)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (MVector (PrimState m) a)
MUV.new (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
size)
  MVector (PrimState m) Bool -> Bool -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> a -> m ()
MUV.set MVector (PrimState m) Bool
img Bool
False
  MVector (PrimState m) Bool
fix <- Int -> m (MVector (PrimState m) Bool)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
Int -> m (MVector (PrimState m) a)
MUV.new (Int
size Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
size)
  MVector (PrimState m) Bool -> Bool -> m ()
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> a -> m ()
MUV.set MVector (PrimState m) Bool
fix Bool
False
  MQRImage1 (PrimState m) -> m (MQRImage1 (PrimState m))
forall (m :: * -> *) a. Monad m => a -> m a
return
    MQRImage1 :: forall s.
Int
-> MVector s Bool
-> MVector s Bool
-> Version
-> ErrorLevel
-> MQRImage1 s
MQRImage1
      { mqrImage1Size :: Int
mqrImage1Size = Int
size
      , mqrImage1Data :: MVector (PrimState m) Bool
mqrImage1Data = MVector (PrimState m) Bool
img
      , mqrImage1Fixed :: MVector (PrimState m) Bool
mqrImage1Fixed = MVector (PrimState m) Bool
fix
      , mqrImage1Version :: Version
mqrImage1Version = Version
v
      , mqrImage1ErrorLevel :: ErrorLevel
mqrImage1ErrorLevel = ErrorLevel
e
      }

unsafeConvert :: PrimMonad m => MQRImage1 (PrimState m) -> m (MQRImage2 (PrimState m))
unsafeConvert :: MQRImage1 (PrimState m) -> m (MQRImage2 (PrimState m))
unsafeConvert MQRImage1{Int
MVector (PrimState m) Bool
ErrorLevel
Version
mqrImage1ErrorLevel :: ErrorLevel
mqrImage1Version :: Version
mqrImage1Fixed :: MVector (PrimState m) Bool
mqrImage1Data :: MVector (PrimState m) Bool
mqrImage1Size :: Int
mqrImage1ErrorLevel :: forall s. MQRImage1 s -> ErrorLevel
mqrImage1Version :: forall s. MQRImage1 s -> Version
mqrImage1Fixed :: forall s. MQRImage1 s -> MVector s Bool
mqrImage1Data :: forall s. MQRImage1 s -> MVector s Bool
mqrImage1Size :: forall s. MQRImage1 s -> Int
..} = do
  Vector Bool
fix <- MVector (PrimState m) Bool -> m (Vector Bool)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
UV.unsafeFreeze MVector (PrimState m) Bool
mqrImage1Fixed
  MQRImage2 (PrimState m) -> m (MQRImage2 (PrimState m))
forall (m :: * -> *) a. Monad m => a -> m a
return
    MQRImage2 :: forall s.
Int
-> MVector s Bool
-> Vector Bool
-> Version
-> ErrorLevel
-> MQRImage2 s
MQRImage2
      { mqrImage2Size :: Int
mqrImage2Size = Int
mqrImage1Size
      , mqrImage2Data :: MVector (PrimState m) Bool
mqrImage2Data = MVector (PrimState m) Bool
mqrImage1Data
      , mqrImage2Fixed :: Vector Bool
mqrImage2Fixed = Vector Bool
fix
      , mqrImage2Version :: Version
mqrImage2Version = Version
mqrImage1Version
      , mqrImage2ErrorLevel :: ErrorLevel
mqrImage2ErrorLevel = ErrorLevel
mqrImage1ErrorLevel
      }

clone :: PrimMonad m => MQRImage2 (PrimState m) -> m (MQRImage3 (PrimState m))
clone :: MQRImage2 (PrimState m) -> m (MQRImage3 (PrimState m))
clone MQRImage2{Int
MVector (PrimState m) Bool
Vector Bool
ErrorLevel
Version
mqrImage2ErrorLevel :: ErrorLevel
mqrImage2Version :: Version
mqrImage2Fixed :: Vector Bool
mqrImage2Data :: MVector (PrimState m) Bool
mqrImage2Size :: Int
mqrImage2ErrorLevel :: forall s. MQRImage2 s -> ErrorLevel
mqrImage2Version :: forall s. MQRImage2 s -> Version
mqrImage2Fixed :: forall s. MQRImage2 s -> Vector Bool
mqrImage2Data :: forall s. MQRImage2 s -> MVector s Bool
mqrImage2Size :: forall s. MQRImage2 s -> Int
..} = do
  MVector (PrimState m) Bool
img <- MVector (PrimState m) Bool -> m (MVector (PrimState m) Bool)
forall (m :: * -> *) a.
(PrimMonad m, Unbox a) =>
MVector (PrimState m) a -> m (MVector (PrimState m) a)
MUV.clone MVector (PrimState m) Bool
mqrImage2Data
  MQRImage3 (PrimState m) -> m (MQRImage3 (PrimState m))
forall (m :: * -> *) a. Monad m => a -> m a
return
    MQRImage3 :: forall s.
Int
-> MVector s Bool
-> Vector Bool
-> Version
-> ErrorLevel
-> MQRImage3 s
MQRImage3
      { mqrImage3Size :: Int
mqrImage3Size = Int
mqrImage2Size
      , mqrImage3Data :: MVector (PrimState m) Bool
mqrImage3Data = MVector (PrimState m) Bool
img
      , mqrImage3Fixed :: Vector Bool
mqrImage3Fixed = Vector Bool
mqrImage2Fixed
      , mqrImage3Version :: Version
mqrImage3Version = Version
mqrImage2Version
      , mqrImage3ErrorLevel :: ErrorLevel
mqrImage3ErrorLevel = ErrorLevel
mqrImage2ErrorLevel
      }

unsafeFreeze :: PrimMonad m => MQRImage3 (PrimState m) -> m QRImage
unsafeFreeze :: MQRImage3 (PrimState m) -> m QRImage
unsafeFreeze MQRImage3{Int
MVector (PrimState m) Bool
Vector Bool
ErrorLevel
Version
mqrImage3ErrorLevel :: ErrorLevel
mqrImage3Version :: Version
mqrImage3Fixed :: Vector Bool
mqrImage3Data :: MVector (PrimState m) Bool
mqrImage3Size :: Int
mqrImage3ErrorLevel :: forall s. MQRImage3 s -> ErrorLevel
mqrImage3Version :: forall s. MQRImage3 s -> Version
mqrImage3Fixed :: forall s. MQRImage3 s -> Vector Bool
mqrImage3Data :: forall s. MQRImage3 s -> MVector s Bool
mqrImage3Size :: forall s. MQRImage3 s -> Int
..} = do
  Vector Bool
img <- MVector (PrimState m) Bool -> m (Vector Bool)
forall a (m :: * -> *).
(Unbox a, PrimMonad m) =>
MVector (PrimState m) a -> m (Vector a)
UV.unsafeFreeze MVector (PrimState m) Bool
mqrImage3Data
  QRImage -> m QRImage
forall (m :: * -> *) a. Monad m => a -> m a
return
    QRImage :: Int -> ErrorLevel -> Int -> Vector Bool -> QRImage
QRImage
      { qrVersion :: Int
qrVersion = Version -> Int
unVersion Version
mqrImage3Version
      , qrErrorLevel :: ErrorLevel
qrErrorLevel = ErrorLevel
mqrImage3ErrorLevel
      , qrImageSize :: Int
qrImageSize = Int
mqrImage3Size
      , qrImageData :: Vector Bool
qrImageData = Vector Bool
img
      }