{-# LANGUAGE CPP #-}
{-# LANGUAGE TypeFamilies #-}
module Codec.Picture.Jpg.Internal.FastDct( referenceDct, fastDctLibJpeg ) where

#if !MIN_VERSION_base(4,8,0)
import Control.Applicative( (<$>) )
#endif

import Data.Int( Int16, Int32 )
import Data.Bits( unsafeShiftR, unsafeShiftL )
import Control.Monad.ST( ST )

import qualified Data.Vector.Storable.Mutable as M

import Codec.Picture.Jpg.Internal.Types
import Control.Monad( forM, forM_ )

-- | Reference implementation of the DCT, directly implementing the formula

-- of ITU-81. It's slow as hell, perform to many operations, but is accurate

-- and a good reference point.

referenceDct :: MutableMacroBlock s Int32
             -> MutableMacroBlock s Int16
             -> ST s (MutableMacroBlock s Int32)
referenceDct :: forall s.
MutableMacroBlock s Int32
-> MutableMacroBlock s Int16 -> ST s (MutableMacroBlock s Int32)
referenceDct MutableMacroBlock s Int32
workData MutableMacroBlock s Int16
block = do
  [(Int, Int)] -> ((Int, Int) -> ST s ()) -> ST s ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Int
u, Int
v) | Int
u <- [Int
0 :: Int .. Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1], Int
v <- [Int
0..Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1]] (((Int, Int) -> ST s ()) -> ST s ())
-> ((Int, Int) -> ST s ()) -> ST s ()
forall a b. (a -> b) -> a -> b
$ \(Int
u,Int
v) -> do
    Float
val <- (Int, Int) -> ST s Float
at (Int
u,Int
v)
    (MutableMacroBlock s Int32
MVector (PrimState (ST s)) Int32
workData MVector (PrimState (ST s)) Int32 -> Int -> Int32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` (Int
v Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
u)) (Int32 -> ST s ()) -> (Float -> Int32) -> Float -> ST s ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Float -> Int32
forall b. Integral b => Float -> b
forall a b. (RealFrac a, Integral b) => a -> b
truncate (Float -> ST s ()) -> Float -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Float
1 Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
4) Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall {a} {a}. (Eq a, Num a, Floating a) => a -> a
c Int
u Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall {a} {a}. (Eq a, Num a, Floating a) => a -> a
c Int
v Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
val

  MutableMacroBlock s Int32 -> ST s (MutableMacroBlock s Int32)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return MutableMacroBlock s Int32
workData
 where -- at :: (Int, Int) -> ST s Float

   at :: (Int, Int) -> ST s Float
at (Int
u,Int
v) = do
     [Float]
toSum <-
        [(Int, Int)] -> ((Int, Int) -> ST s Float) -> ST s [Float]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Int
x,Int
y) | Int
x <- [Int
0..Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1], Int
y <- [Int
0..Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1 :: Int]] (((Int, Int) -> ST s Float) -> ST s [Float])
-> ((Int, Int) -> ST s Float) -> ST s [Float]
forall a b. (a -> b) -> a -> b
$ \(Int
x,Int
y) -> do
            Float
sample <- Int16 -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int16 -> Float) -> ST s Int16 -> ST s Float
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> (MutableMacroBlock s Int16
MVector (PrimState (ST s)) Int16
block MVector (PrimState (ST s)) Int16 -> Int -> ST s Int16
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.unsafeRead` (Int
y Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
x))
            Float -> ST s Float
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Float -> ST s Float) -> Float -> ST s Float
forall a b. (a -> b) -> a -> b
$ Float
sample Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float -> Float
forall a. Floating a => a -> a
cos ((Float
2 Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
x Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
1) Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
u Float -> Float -> Float
forall a. Num a => a -> a -> a
* (Float
forall a. Floating a => a
pi :: Float)Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
16) 
                            Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float -> Float
forall a. Floating a => a -> a
cos ((Float
2 Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
y Float -> Float -> Float
forall a. Num a => a -> a -> a
+ Float
1) Float -> Float -> Float
forall a. Num a => a -> a -> a
* Int -> Float
forall a b. (Integral a, Num b) => a -> b
fromIntegral Int
v Float -> Float -> Float
forall a. Num a => a -> a -> a
* Float
forall a. Floating a => a
pi Float -> Float -> Float
forall a. Fractional a => a -> a -> a
/ Float
16)
     Float -> ST s Float
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return (Float -> ST s Float) -> Float -> ST s Float
forall a b. (a -> b) -> a -> b
$ [Float] -> Float
forall a. Num a => [a] -> a
forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum [Float]
toSum

   c :: a -> a
c a
0 = a
1 a -> a -> a
forall a. Fractional a => a -> a -> a
/ a -> a
forall a. Floating a => a -> a
sqrt a
2
   c a
_ = a
1

pASS1_BITS, cONST_BITS :: Int
cONST_BITS :: Int
cONST_BITS = Int
13
pASS1_BITS :: Int
pASS1_BITS =  Int
2


fIX_0_298631336, fIX_0_390180644, fIX_0_541196100,
    fIX_0_765366865, fIX_0_899976223, fIX_1_175875602,
    fIX_1_501321110, fIX_1_847759065, fIX_1_961570560,
    fIX_2_053119869, fIX_2_562915447, fIX_3_072711026 :: Int32
fIX_0_298631336 :: Int32
fIX_0_298631336 = Int32
2446    -- FIX(0.298631336) */

fIX_0_390180644 :: Int32
fIX_0_390180644 = Int32
3196    -- FIX(0.390180644) */

fIX_0_541196100 :: Int32
fIX_0_541196100 = Int32
4433    -- FIX(0.541196100) */

fIX_0_765366865 :: Int32
fIX_0_765366865 = Int32
6270    -- FIX(0.765366865) */

fIX_0_899976223 :: Int32
fIX_0_899976223 = Int32
7373    -- FIX(0.899976223) */

fIX_1_175875602 :: Int32
fIX_1_175875602 = Int32
9633    -- FIX(1.175875602) */

fIX_1_501321110 :: Int32
fIX_1_501321110 = Int32
12299    -- FIX(1.501321110) */

fIX_1_847759065 :: Int32
fIX_1_847759065 = Int32
15137    -- FIX(1.847759065) */

fIX_1_961570560 :: Int32
fIX_1_961570560 = Int32
16069    -- FIX(1.961570560) */

fIX_2_053119869 :: Int32
fIX_2_053119869 = Int32
16819    -- FIX(2.053119869) */

fIX_2_562915447 :: Int32
fIX_2_562915447 = Int32
20995    -- FIX(2.562915447) */

fIX_3_072711026 :: Int32
fIX_3_072711026 = Int32
25172    -- FIX(3.072711026) */


cENTERJSAMPLE :: Int32
cENTERJSAMPLE :: Int32
cENTERJSAMPLE = Int32
128

-- | Fast DCT extracted from libjpeg

fastDctLibJpeg :: MutableMacroBlock s Int32
        -> MutableMacroBlock s Int16
        -> ST s (MutableMacroBlock s Int32)
fastDctLibJpeg :: forall s.
MutableMacroBlock s Int32
-> MutableMacroBlock s Int16 -> ST s (MutableMacroBlock s Int32)
fastDctLibJpeg MutableMacroBlock s Int32
workData MutableMacroBlock s Int16
sample_block = do
 MutableMacroBlock s Int32 -> Int -> ST s ()
firstPass MutableMacroBlock s Int32
workData  Int
0
 MutableMacroBlock s Int32 -> Int -> ST s ()
forall s. STVector s Int32 -> Int -> ST s ()
secondPass MutableMacroBlock s Int32
workData Int
7
 {-_ <- mutate (\_ a -> a `quot` 8) workData-}
 MutableMacroBlock s Int32 -> ST s (MutableMacroBlock s Int32)
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return MutableMacroBlock s Int32
workData
  where -- Pass 1: process rows.

        -- Note results are scaled up by sqrt(8) compared to a true DCT;

        -- furthermore, we scale the results by 2**PASS1_BITS.

        firstPass :: MutableMacroBlock s Int32 -> Int -> ST s ()
firstPass         MutableMacroBlock s Int32
_ Int
i | Int
i Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== Int
forall a. Num a => a
dctBlockSize = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        firstPass MutableMacroBlock s Int32
dataBlock Int
i = do
            let baseIdx :: Int
baseIdx = Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
forall a. Num a => a
dctBlockSize
                readAt :: Int -> ST s Int32
readAt Int
idx = Int16 -> Int32
forall a b. (Integral a, Num b) => a -> b
fromIntegral (Int16 -> Int32) -> ST s Int16 -> ST s Int32
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> MutableMacroBlock s Int16
MVector (PrimState (ST s)) Int16
sample_block MVector (PrimState (ST s)) Int16 -> Int -> ST s Int16
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.unsafeRead` (Int
baseIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
idx)
                mult :: Int32 -> Int32 -> Int32
mult = Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
(*)
                writeAt :: Int -> Int32 -> ST s ()
writeAt Int
idx = MutableMacroBlock s Int32
MVector (PrimState (ST s)) Int32
dataBlock MVector (PrimState (ST s)) Int32 -> Int -> Int32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` (Int
baseIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
idx)
                writeAtPos :: Int -> Int32 -> ST s ()
writeAtPos Int
idx Int32
n = (MutableMacroBlock s Int32
MVector (PrimState (ST s)) Int32
dataBlock MVector (PrimState (ST s)) Int32 -> Int -> Int32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` (Int
baseIdx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
idx))
                                    (Int32
n Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
cONST_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pASS1_BITS))

            Int32
blk0 <- Int -> ST s Int32
readAt Int
0
            Int32
blk1 <- Int -> ST s Int32
readAt Int
1
            Int32
blk2 <- Int -> ST s Int32
readAt Int
2
            Int32
blk3 <- Int -> ST s Int32
readAt Int
3
            Int32
blk4 <- Int -> ST s Int32
readAt Int
4
            Int32
blk5 <- Int -> ST s Int32
readAt Int
5
            Int32
blk6 <- Int -> ST s Int32
readAt Int
6
            Int32
blk7 <- Int -> ST s Int32
readAt Int
7

            let tmp0 :: Int32
tmp0 = Int32
blk0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk7
                tmp1 :: Int32
tmp1 = Int32
blk1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk6
                tmp2 :: Int32
tmp2 = Int32
blk2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk5
                tmp3 :: Int32
tmp3 = Int32
blk3 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk4

                tmp10 :: Int32
tmp10 = Int32
tmp0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3
                tmp12 :: Int32
tmp12 = Int32
tmp0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp3
                tmp11 :: Int32
tmp11 = Int32
tmp1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2
                tmp13 :: Int32
tmp13 = Int32
tmp1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp2

                tmp0' :: Int32
tmp0' = Int32
blk0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk7
                tmp1' :: Int32
tmp1' = Int32
blk1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk6
                tmp2' :: Int32
tmp2' = Int32
blk2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk5
                tmp3' :: Int32
tmp3' = Int32
blk3 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk4

            -- Stage 4 and output

            Int -> Int32 -> ST s ()
writeAt Int
0 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Int32
tmp10 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
forall a. Num a => a
dctBlockSize Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
* Int32
cENTERJSAMPLE) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
pASS1_BITS
            Int -> Int32 -> ST s ()
writeAt Int
4 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Int32
tmp10 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp11) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` Int
pASS1_BITS

            let z1 :: Int32
z1 = Int32 -> Int32 -> Int32
mult (Int32
tmp12 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13) Int32
fIX_0_541196100
                     Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
1 Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
cONST_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pASS1_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))

            Int -> Int32 -> ST s ()
writeAtPos Int
2 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
z1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32 -> Int32 -> Int32
mult Int32
tmp12 Int32
fIX_0_765366865
            Int -> Int32 -> ST s ()
writeAtPos Int
6 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
z1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32 -> Int32 -> Int32
mult Int32
tmp13 Int32
fIX_1_847759065

            let tmp10' :: Int32
tmp10' = Int32
tmp0' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3'
                tmp11' :: Int32
tmp11' = Int32
tmp1' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2'
                tmp12' :: Int32
tmp12' = Int32
tmp0' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2'
                tmp13' :: Int32
tmp13' = Int32
tmp1' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3'
                z1' :: Int32
z1' = Int32 -> Int32 -> Int32
mult (Int32
tmp12' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13') Int32
fIX_1_175875602 --  c3 */

                        -- Add fudge factor here for final descale. */

                        Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
1 Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
cONST_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
pASS1_BITSInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
                tmp0'' :: Int32
tmp0'' = Int32 -> Int32 -> Int32
mult Int32
tmp0' Int32
fIX_1_501321110
                tmp1'' :: Int32
tmp1'' = Int32 -> Int32 -> Int32
mult Int32
tmp1' Int32
fIX_3_072711026
                tmp2'' :: Int32
tmp2'' = Int32 -> Int32 -> Int32
mult Int32
tmp2' Int32
fIX_2_053119869
                tmp3'' :: Int32
tmp3'' = Int32 -> Int32 -> Int32
mult Int32
tmp3' Int32
fIX_0_298631336

                tmp10'' :: Int32
tmp10'' = Int32 -> Int32 -> Int32
mult Int32
tmp10' (- Int32
fIX_0_899976223)
                tmp11'' :: Int32
tmp11'' = Int32 -> Int32 -> Int32
mult Int32
tmp11' (- Int32
fIX_2_562915447)
                tmp12'' :: Int32
tmp12'' = Int32 -> Int32 -> Int32
mult Int32
tmp12' (- Int32
fIX_0_390180644) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
z1'
                tmp13'' :: Int32
tmp13'' = Int32 -> Int32 -> Int32
mult Int32
tmp13' (- Int32
fIX_1_961570560) Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
z1'

            Int -> Int32 -> ST s ()
writeAtPos Int
1 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp0'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp10'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp12''
            Int -> Int32 -> ST s ()
writeAtPos Int
3 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp1'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13''
            Int -> Int32 -> ST s ()
writeAtPos Int
5 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp2'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp12''
            Int -> Int32 -> ST s ()
writeAtPos Int
7 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp3'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp10'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13''

            MutableMacroBlock s Int32 -> Int -> ST s ()
firstPass MutableMacroBlock s Int32
dataBlock (Int -> ST s ()) -> Int -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1

        -- Pass 2: process columns.

        -- We remove the PASS1_BITS scaling, but leave the results scaled up

        -- by an overall factor of 8.

        secondPass :: M.STVector s Int32 -> Int -> ST s ()
        secondPass :: forall s. STVector s Int32 -> Int -> ST s ()
secondPass STVector s Int32
_     (-1) = () -> ST s ()
forall a. a -> ST s a
forall (m :: * -> *) a. Monad m => a -> m a
return ()
        secondPass STVector s Int32
block Int
i = do
            let readAt :: Int -> ST s Int32
readAt Int
idx = STVector s Int32
MVector (PrimState (ST s)) Int32
block MVector (PrimState (ST s)) Int32 -> Int -> ST s Int32
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> m a
`M.unsafeRead` ((Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
forall a. Num a => a
dctBlockSize)
                mult :: Int32 -> Int32 -> Int32
mult = Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
(*)
                writeAt :: Int -> Int32 -> ST s ()
writeAt Int
idx = STVector s Int32
MVector (PrimState (ST s)) Int32
block MVector (PrimState (ST s)) Int32 -> Int -> Int32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` (Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i))
                writeAtPos :: Int -> Int32 -> ST s ()
writeAtPos Int
idx Int32
n = (STVector s Int32
MVector (PrimState (ST s)) Int32
block MVector (PrimState (ST s)) Int32 -> Int -> Int32 -> ST s ()
forall (m :: * -> *) a.
(PrimMonad m, Storable a) =>
MVector (PrimState m) a -> Int -> a -> m ()
`M.unsafeWrite` (Int
forall a. Num a => a
dctBlockSize Int -> Int -> Int
forall a. Num a => a -> a -> a
* Int
idx Int -> Int -> Int
forall a. Num a => a -> a -> a
+ (Int
7 Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i))) (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
n Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
cONST_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
pASS1_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
            Int32
blk0 <- Int -> ST s Int32
readAt Int
0
            Int32
blk1 <- Int -> ST s Int32
readAt Int
1
            Int32
blk2 <- Int -> ST s Int32
readAt Int
2
            Int32
blk3 <- Int -> ST s Int32
readAt Int
3
            Int32
blk4 <- Int -> ST s Int32
readAt Int
4
            Int32
blk5 <- Int -> ST s Int32
readAt Int
5
            Int32
blk6 <- Int -> ST s Int32
readAt Int
6
            Int32
blk7 <- Int -> ST s Int32
readAt Int
7

            let tmp0 :: Int32
tmp0 = Int32
blk0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk7
                tmp1 :: Int32
tmp1 = Int32
blk1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk6
                tmp2 :: Int32
tmp2 = Int32
blk2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk5
                tmp3 :: Int32
tmp3 = Int32
blk3 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
blk4

                -- Add fudge factor here for final descale. */

                tmp10 :: Int32
tmp10 = Int32
tmp0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
1 Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
pASS1_BITSInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1))
                tmp12 :: Int32
tmp12 = Int32
tmp0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp3
                tmp11 :: Int32
tmp11 = Int32
tmp1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2
                tmp13 :: Int32
tmp13 = Int32
tmp1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp2

                tmp0' :: Int32
tmp0' = Int32
blk0 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk7
                tmp1' :: Int32
tmp1' = Int32
blk1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk6
                tmp2' :: Int32
tmp2' = Int32
blk2 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk5
                tmp3' :: Int32
tmp3' = Int32
blk3 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
blk4

            Int -> Int32 -> ST s ()
writeAt Int
0 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Int32
tmp10 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
pASS1_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)
            Int -> Int32 -> ST s ()
writeAt Int
4 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ (Int32
tmp10 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32
tmp11) Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftR` (Int
pASS1_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
3)

            let z1 :: Int32
z1 = Int32 -> Int32 -> Int32
mult (Int32
tmp12 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13) Int32
fIX_0_541196100
                    Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ (Int32
1 Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
cONST_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
pASS1_BITS Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))

            Int -> Int32 -> ST s ()
writeAtPos Int
2 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
z1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32 -> Int32 -> Int32
mult Int32
tmp12 Int32
fIX_0_765366865
            Int -> Int32 -> ST s ()
writeAtPos Int
6 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
z1 Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
- Int32 -> Int32 -> Int32
mult Int32
tmp13 Int32
fIX_1_847759065

            let tmp10' :: Int32
tmp10' = Int32
tmp0' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3'
                tmp11' :: Int32
tmp11' = Int32
tmp1' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2'
                tmp12' :: Int32
tmp12' = Int32
tmp0' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp2'
                tmp13' :: Int32
tmp13' = Int32
tmp1' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp3'

                z1' :: Int32
z1' = Int32 -> Int32 -> Int32
mult (Int32
tmp12' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13') Int32
fIX_1_175875602
                    -- Add fudge factor here for final descale. */

                   Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
1 Int32 -> Int -> Int32
forall a. Bits a => a -> Int -> a
`unsafeShiftL` (Int
cONST_BITSInt -> Int -> Int
forall a. Num a => a -> a -> a
+Int
pASS1_BITSInt -> Int -> Int
forall a. Num a => a -> a -> a
-Int
1);

                tmp0'' :: Int32
tmp0''  = Int32 -> Int32 -> Int32
mult Int32
tmp0'    Int32
fIX_1_501321110
                tmp1'' :: Int32
tmp1''  = Int32 -> Int32 -> Int32
mult Int32
tmp1'    Int32
fIX_3_072711026
                tmp2'' :: Int32
tmp2''  = Int32 -> Int32 -> Int32
mult Int32
tmp2'    Int32
fIX_2_053119869
                tmp3'' :: Int32
tmp3''  = Int32 -> Int32 -> Int32
mult Int32
tmp3'    Int32
fIX_0_298631336
                tmp10'' :: Int32
tmp10'' = Int32 -> Int32 -> Int32
mult Int32
tmp10' (- Int32
fIX_0_899976223)
                tmp11'' :: Int32
tmp11'' = Int32 -> Int32 -> Int32
mult Int32
tmp11' (- Int32
fIX_2_562915447)
                tmp12'' :: Int32
tmp12'' = Int32 -> Int32 -> Int32
mult Int32
tmp12' (- Int32
fIX_0_390180644)
                            Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
z1'
                tmp13'' :: Int32
tmp13'' = Int32 -> Int32 -> Int32
mult Int32
tmp13' (- Int32
fIX_1_961570560)
                            Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
z1'
            Int -> Int32 -> ST s ()
writeAtPos Int
1 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp0'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp10'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp12''
            Int -> Int32 -> ST s ()
writeAtPos Int
3 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp1'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13''
            Int -> Int32 -> ST s ()
writeAtPos Int
5 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp2'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp11'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp12''
            Int -> Int32 -> ST s ()
writeAtPos Int
7 (Int32 -> ST s ()) -> Int32 -> ST s ()
forall a b. (a -> b) -> a -> b
$ Int32
tmp3'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp10'' Int32 -> Int32 -> Int32
forall a. Num a => a -> a -> a
+ Int32
tmp13''

            STVector s Int32 -> Int -> ST s ()
forall s. STVector s Int32 -> Int -> ST s ()
secondPass STVector s Int32
block (Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)

{-# ANN module "HLint: ignore Use camelCase" #-}
{-# ANN module "HLint: ignore Reduce duplication" #-}