Skip to content

Commit

Permalink
Tiny jpeg cleanups/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Twinside committed Sep 4, 2016
1 parent c5d23dd commit b2ddfd8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 28 deletions.
22 changes: 12 additions & 10 deletions saferizer.ps1
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
ls -r | match "\.hs$" | % {
sed -i -e 's:\.unsafeWrite:.write:g' $_.fullname
sed -i -e 's:\.unsafeRead:.read:g' $_.fullname
sed -i -e 's:`V.unsafeIndex`:V.!:g' $_.fullname
sed -i -e 's:`VS\.unsafeIndex`:VS.!:g' $_.fullname
sed -i -e 's:BU.unsafeIndex:B.index:g' $_.fullname
sed -i -e 's:V.unsafeIndex:(V.!):g' $_.fullname
sed -i -e 's:VS.unsafeIndex:(VS.!):g' $_.fullname
sed -i -e 's:unsafeIndex:!:g' $_.fullname
sed -i -e 's:unsafeFreeze:freeze:g' $_.fullname
$sed = "C:\MinGW\msys\1.0\bin\sed.exe"

ls -r src | match "\.hs$" | % {
& $sed -i -e 's:\.unsafeWrite:.write:g' $_.fullname;
& $sed -i -e 's:\.unsafeRead:.read:g' $_.fullname;
& $sed -i -e 's:`V.unsafeIndex`:V.!:g' $_.fullname;
& $sed -i -e 's:`VS\.unsafeIndex`:VS.!:g' $_.fullname;
& $sed -i -e 's:BU.unsafeIndex:B.index:g' $_.fullname;
& $sed -i -e 's:V.unsafeIndex:(V.!):g' $_.fullname;
& $sed -i -e 's:VS.unsafeIndex:(VS.!):g' $_.fullname;
& $sed -i -e 's:unsafeIndex:!:g' $_.fullname;
& $sed -i -e 's:unsafeFreeze:freeze:g' $_.fullname;
}
4 changes: 2 additions & 2 deletions src/Codec/Picture/Jpg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -423,8 +423,8 @@ decodeImage frame quants lst outImage = do
maxiW = maximum [fst $ subSampling c | (c,_) <- params]
maxiH = maximum [snd $ subSampling c | (c,_) <- params]

imageBlockWidth = (imgWidth + 7) `div` 8
imageBlockHeight = (imgHeight + 7) `div` 8
imageBlockWidth = toBlockSize imgWidth
imageBlockHeight = toBlockSize imgHeight

imageMcuWidth = (imageBlockWidth + (maxiW - 1)) `div` maxiW
imageMcuHeight = (imageBlockHeight + (maxiH - 1)) `div` maxiH
Expand Down
12 changes: 11 additions & 1 deletion src/Codec/Picture/Jpg/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ module Codec.Picture.Jpg.Common
, rasterMap
, decodeMacroBlock
, decodeRestartInterval
, toBlockSize
) where

#if !MIN_VERSION_base(4,8,0)
Expand Down Expand Up @@ -53,13 +54,22 @@ data JpgUnpackerParameter = JpgUnpackerParameter
, coefficientRange :: !(Int, Int)
, successiveApprox :: !(Int, Int)
, readerIndex :: {-# UNPACK #-} !Int
-- | When in progressive mode, we can have many
-- color in a scan are only one. The indices changes
-- on this fact, when mixed, there is whole
-- MCU for all color components, spanning multiple
-- block lines. With only one color component we use
-- the normal raster order.
, indiceVector :: {-# UNPACK #-} !Int
, blockIndex :: {-# UNPACK #-} !Int
, blockMcuX :: {-# UNPACK #-} !Int
, blockMcuY :: {-# UNPACK #-} !Int
}
deriving Show

toBlockSize :: Int -> Int
toBlockSize v = (v + 7) `div` 8

decodeRestartInterval :: BoolReader s Int32
decodeRestartInterval = return (-1) {- do
bits <- replicateM 8 getNextBitJpg
Expand Down Expand Up @@ -195,9 +205,9 @@ pixelClamp n = fromIntegral . min 255 $ max 0 n
unpackMacroBlock :: Int -- ^ Component count
-> Int -- ^ Width coefficient
-> Int -- ^ Height coefficient
-> Int -- ^ Component index
-> Int -- ^ x
-> Int -- ^ y
-> Int -- ^ Component index
-> MutableImage s PixelYCbCr8
-> MutableMacroBlock s Int16
-> ST s ()
Expand Down
29 changes: 14 additions & 15 deletions src/Codec/Picture/Jpg/Progressive.hs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,9 @@ createMcuLineIndices param imgWidth mcuWidth =
V.fromList $ VS.fromList <$> [indexSolo, indexMulti]
where compW = fromIntegral $ horizontalSamplingFactor param
compH = fromIntegral $ verticalSamplingFactor param
imageBlockSize = (imgWidth + 7) `div` 8

indexSolo =
[y * mcuWidth * compW + x
| y <- [0 .. compH - 1], x <- [0 .. imageBlockSize - 1]]
imageBlockSize = toBlockSize imgWidth

indexSolo = take (imageBlockSize * compH) [0 ..]
indexMulti =
[(mcu + y * mcuWidth) * compW + x
| mcu <- [0 .. mcuWidth - 1]
Expand Down Expand Up @@ -195,13 +192,13 @@ prepareUnpacker lst = do
selection _ _ = decodeRefineAc

data ComponentData s = ComponentData
{ componentIndices :: V.Vector (VS.Vector Int)
, componentBlocks :: V.Vector (MutableMacroBlock s Int16)
, componentId :: !Int
, componentBlockCount :: !Int
}
{ componentIndices :: V.Vector (VS.Vector Int)
, componentBlocks :: V.Vector (MutableMacroBlock s Int16)
, componentId :: !Int
, componentBlockCount :: !Int
}

-- | Iteration from to n in monadic context, without data
-- | Iteration from 0 to n in monadic context, without data
-- keeping.
lineMap :: (Monad m) => Int -> (Int -> m ()) -> m ()
{-# INLINE lineMap #-}
Expand Down Expand Up @@ -259,6 +256,8 @@ progressiveUnpack (maxiW, maxiH) frame quants lst = do
let componentNumber = componentIndex unpackParam
writeIndex <- writeIndices `MS.read` componentNumber
let componentData = allBlocks !! componentNumber
-- We get back the correct block indices for the number of component
-- in the current scope (precalculated)
indexVector =
componentIndices componentData ! indiceVector unpackParam
maxIndexLength = VS.length indexVector
Expand Down Expand Up @@ -305,14 +304,14 @@ progressiveUnpack (maxiW, maxiH) frame quants lst = do
imgWidth = fromIntegral $ jpgWidth frame
imgHeight = fromIntegral $ jpgHeight frame

imageBlockWidth = (imgWidth + 7) `div` 8
imageBlockHeight = (imgHeight + 7) `div` 8
imageBlockWidth = toBlockSize imgWidth
imageBlockHeight = toBlockSize imgHeight

imageMcuWidth = (imageBlockWidth + (maxiW - 1)) `div` maxiW
imageMcuWidth = (imageBlockWidth + (maxiW - 1)) `div` maxiW
imageMcuHeight = (imageBlockHeight + (maxiH - 1)) `div` maxiH

allocateWorkingBlocks (ix, comp) = do
let blockCount = hSample * vSample * imageMcuWidth
let blockCount = hSample * vSample * imageMcuWidth * 2
blocks <- V.replicateM blockCount createEmptyMutableMacroBlock
return ComponentData
{ componentBlocks = blocks
Expand Down

0 comments on commit b2ddfd8

Please sign in to comment.