{-# LANGUAGE NoImplicitPrelude #-}
module Codec.QRCode.Code.Intermediate
( toIntermediate
, fromIntermediate
) where
import Codec.QRCode.Base
import Codec.QRCode.Code.Data
import Codec.QRCode.Code.Image
import Codec.QRCode.Code.Mask
import qualified Codec.QRCode.Data.ByteStreamBuilder as BSB
import Codec.QRCode.Data.Mask
import qualified Codec.QRCode.Data.MQRImage as MI
import Codec.QRCode.Data.QRCodeOptions
import Codec.QRCode.Data.QRImage
import Codec.QRCode.Data.QRIntermediate.Internal
import Codec.QRCode.Data.QRSegment.Internal
import Codec.QRCode.Data.Result
toIntermediate :: QRCodeOptions -> QRSegment -> Result QRIntermediate
{-# INLINE toIntermediate #-}
toIntermediate = calcVersionAndErrorLevel
fromIntermediate :: QRIntermediate -> QRImage
{-# INLINE fromIntermediate #-}
fromIntermediate = generateQRImage . appendErrorCorrection . appendEndAndPadding
generateQRImage :: QRInternal [Word8] -> QRImage
generateQRImage (v, e, bs, mmask) = runST $ do
img1 <- MI.new v e
drawFunctionPatterns img1
img2 <- MI.unsafeConvert img1
drawCodeWords img2 (BSB.toBitStream bs)
case mmask of
Just m -> do
img3 <- MI.clone img2
applyMask img3 m
MI.unsafeFreeze img3
Nothing -> do
rs <- forM [Mask0 .. Mask7] $ \m -> do
img3 <- MI.clone img2
applyMask img3 m
qrimg <- MI.unsafeFreeze img3
return (getPenaltyScore qrimg, qrimg)
return $ snd $ head $ sortOn fst rs