/
Types.hs
595 lines (514 loc) · 19.8 KB
/
Types.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE CPP #-}
module Codec.Picture.Jpg.Types( MutableMacroBlock
, createEmptyMutableMacroBlock
, printMacroBlock
, printPureMacroBlock
, DcCoefficient
, JpgImage( .. )
, JpgComponent( .. )
, JpgFrameHeader( .. )
, JpgFrame( .. )
, JpgFrameKind( .. )
, JpgScanHeader( .. )
, JpgQuantTableSpec( .. )
, JpgHuffmanTableSpec( .. )
, JpgImageKind( .. )
, JpgScanSpecification( .. )
, JpgColorSpace( .. )
, calculateSize
, dctBlockSize
) where
#if !MIN_VERSION_base(4,8,0)
import Control.Applicative( (<*>))
#endif
import Control.Applicative( (<$>) )
import Control.Monad( when, replicateM, forM, forM_, unless )
import Control.Monad.ST( ST )
import Data.Bits( (.|.), (.&.), unsafeShiftL, unsafeShiftR )
import Foreign.Storable ( Storable )
import Data.Vector.Unboxed( (!) )
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Storable as VS
import qualified Data.Vector.Storable.Mutable as M
import qualified Data.ByteString as B
import qualified Data.ByteString.Lazy as L
import Data.Int( Int16 )
import Data.Word(Word8, Word16 )
import Data.Binary( Binary(..) )
import Data.Binary.Get( Get
, getWord8
, getWord16be
, getByteString
, skip
, bytesRead
)
import Data.Binary.Put( Put
, putWord8
, putWord16be
, putLazyByteString
)
import Codec.Picture.InternalHelper
import Codec.Picture.Jpg.DefaultTable
{-import Debug.Trace-}
import Text.Printf
-- | Type only used to make clear what kind of integer we are carrying
-- Might be transformed into newtype in the future
type DcCoefficient = Int16
-- | Macroblock that can be transformed.
type MutableMacroBlock s a = M.STVector s a
data JpgFrameKind =
JpgBaselineDCTHuffman
| JpgExtendedSequentialDCTHuffman
| JpgProgressiveDCTHuffman
| JpgLosslessHuffman
| JpgDifferentialSequentialDCTHuffman
| JpgDifferentialProgressiveDCTHuffman
| JpgDifferentialLosslessHuffman
| JpgExtendedSequentialArithmetic
| JpgProgressiveDCTArithmetic
| JpgLosslessArithmetic
| JpgDifferentialSequentialDCTArithmetic
| JpgDifferentialProgressiveDCTArithmetic
| JpgDifferentialLosslessArithmetic
| JpgQuantizationTable
| JpgHuffmanTableMarker
| JpgStartOfScan
| JpgEndOfImage
| JpgAppSegment Word8
| JpgExtensionSegment Word8
| JpgRestartInterval
| JpgRestartIntervalEnd Word8
deriving (Eq, Show)
data JpgFrame =
JpgAppFrame !Word8 B.ByteString
| JpgAdobeAPP14 !JpgAdobeApp14
| JpgExtension !Word8 B.ByteString
| JpgQuantTable ![JpgQuantTableSpec]
| JpgHuffmanTable ![(JpgHuffmanTableSpec, HuffmanPackedTree)]
| JpgScanBlob !JpgScanHeader !L.ByteString
| JpgScans !JpgFrameKind !JpgFrameHeader
| JpgIntervalRestart !Word16
deriving Show
data JpgColorSpace
= JpgColorSpaceYCbCr
| JpgColorSpaceYCC
| JpgColorSpaceY
| JpgColorSpaceYA
| JpgColorSpaceYCCA
| JpgColorSpaceYCCK
| JpgColorSpaceCMYK
| JpgColorSpaceRGB
| JpgColorSpaceRGBA
deriving Show
data JpgAdobeApp14 = JpgAdobeApp14
{ _adobeDctVersion :: !Word8
, _adobeFlag0 :: !Word16
, _adobeFlag1 :: !Word16
, _adobeTransform :: !Word8
}
deriving Show
instance Binary JpgAdobeApp14 where
get = JpgAdobeApp14
<$> getWord8 <*> getWord16be <*> getWord16be <*> getWord8
put (JpgAdobeApp14 v f0 f1 t) = do
putWord8 v
putWord16be f0
putWord16be f1
putWord8 t
data JpgFrameHeader = JpgFrameHeader
{ jpgFrameHeaderLength :: !Word16
, jpgSamplePrecision :: !Word8
, jpgHeight :: !Word16
, jpgWidth :: !Word16
, jpgImageComponentCount :: !Word8
, jpgComponents :: ![JpgComponent]
}
deriving Show
instance SizeCalculable JpgFrameHeader where
calculateSize hdr = 2 + 1 + 2 + 2 + 1
+ sum [calculateSize c | c <- jpgComponents hdr]
data JpgComponent = JpgComponent
{ componentIdentifier :: !Word8
-- | Stored with 4 bits
, horizontalSamplingFactor :: !Word8
-- | Stored with 4 bits
, verticalSamplingFactor :: !Word8
, quantizationTableDest :: !Word8
}
deriving Show
instance SizeCalculable JpgComponent where
calculateSize _ = 3
data JpgImage = JpgImage { jpgFrame :: [JpgFrame] }
deriving Show
data JpgScanSpecification = JpgScanSpecification
{ componentSelector :: !Word8
-- | Encoded as 4 bits
, dcEntropyCodingTable :: !Word8
-- | Encoded as 4 bits
, acEntropyCodingTable :: !Word8
}
deriving Show
instance SizeCalculable JpgScanSpecification where
calculateSize _ = 2
data JpgScanHeader = JpgScanHeader
{ scanLength :: !Word16
, scanComponentCount :: !Word8
, scans :: [JpgScanSpecification]
-- | (begin, end)
, spectralSelection :: (Word8, Word8)
-- | Encoded as 4 bits
, successiveApproxHigh :: !Word8
-- | Encoded as 4 bits
, successiveApproxLow :: !Word8
}
deriving Show
instance SizeCalculable JpgScanHeader where
calculateSize hdr = 2 + 1
+ sum [calculateSize c | c <- scans hdr]
+ 2
+ 1
data JpgQuantTableSpec = JpgQuantTableSpec
{ -- | Stored on 4 bits
quantPrecision :: !Word8
-- | Stored on 4 bits
, quantDestination :: !Word8
, quantTable :: MacroBlock Int16
}
deriving Show
class SizeCalculable a where
calculateSize :: a -> Int
-- | Type introduced only to avoid some typeclass overlapping
-- problem
newtype TableList a = TableList [a]
instance (SizeCalculable a, Binary a) => Binary (TableList a) where
put (TableList lst) = do
putWord16be . fromIntegral $ sum [calculateSize table | table <- lst] + 2
mapM_ put lst
get = TableList <$> (getWord16be >>= \s -> innerParse (fromIntegral s - 2))
where innerParse :: Int -> Get [a]
innerParse 0 = return []
innerParse size = do
onStart <- fromIntegral <$> bytesRead
table <- get
onEnd <- fromIntegral <$> bytesRead
(table :) <$> innerParse (size - (onEnd - onStart))
instance SizeCalculable JpgQuantTableSpec where
calculateSize table =
1 + (fromIntegral (quantPrecision table) + 1) * 64
instance Binary JpgQuantTableSpec where
put table = do
let precision = quantPrecision table
put4BitsOfEach precision (quantDestination table)
forM_ (VS.toList $ quantTable table) $ \coeff ->
if precision == 0 then putWord8 $ fromIntegral coeff
else putWord16be $ fromIntegral coeff
get = do
(precision, dest) <- get4BitOfEach
coeffs <- replicateM 64 $ if precision == 0
then fromIntegral <$> getWord8
else fromIntegral <$> getWord16be
return JpgQuantTableSpec
{ quantPrecision = precision
, quantDestination = dest
, quantTable = VS.fromListN 64 coeffs
}
data JpgHuffmanTableSpec = JpgHuffmanTableSpec
{ -- | 0 : DC, 1 : AC, stored on 4 bits
huffmanTableClass :: !DctComponent
-- | Stored on 4 bits
, huffmanTableDest :: !Word8
, huffSizes :: !(VU.Vector Word8)
, huffCodes :: !(V.Vector (VU.Vector Word8))
}
deriving Show
instance SizeCalculable JpgHuffmanTableSpec where
calculateSize table = 1 + 16 + sum [fromIntegral e | e <- VU.toList $ huffSizes table]
instance Binary JpgHuffmanTableSpec where
put table = do
let classVal = if huffmanTableClass table == DcComponent
then 0 else 1
put4BitsOfEach classVal $ huffmanTableDest table
mapM_ put . VU.toList $ huffSizes table
forM_ [0 .. 15] $ \i ->
when (huffSizes table ! i /= 0)
(let elements = VU.toList $ huffCodes table V.! i
in mapM_ put elements)
get = do
(huffClass, huffDest) <- get4BitOfEach
sizes <- replicateM 16 getWord8
codes <- forM sizes $ \s ->
VU.replicateM (fromIntegral s) getWord8
return JpgHuffmanTableSpec
{ huffmanTableClass =
if huffClass == 0 then DcComponent else AcComponent
, huffmanTableDest = huffDest
, huffSizes = VU.fromListN 16 sizes
, huffCodes = V.fromListN 16 codes
}
instance Binary JpgImage where
put (JpgImage { jpgFrame = frames }) =
putWord8 0xFF >> putWord8 0xD8 >> mapM_ putFrame frames
>> putWord8 0xFF >> putWord8 0xD9
get = do
let startOfImageMarker = 0xD8
-- endOfImageMarker = 0xD9
checkMarker commonMarkerFirstByte startOfImageMarker
eatUntilCode
frames <- parseFrames
{-checkMarker commonMarkerFirstByte endOfImageMarker-}
return JpgImage { jpgFrame = frames }
eatUntilCode :: Get ()
eatUntilCode = do
code <- getWord8
unless (code == 0xFF) eatUntilCode
takeCurrentFrame :: Get B.ByteString
takeCurrentFrame = do
size <- getWord16be
getByteString (fromIntegral size - 2)
putFrame :: JpgFrame -> Put
putFrame (JpgAdobeAPP14 _adobe) = return ()
putFrame (JpgAppFrame appCode str) =
put (JpgAppSegment appCode) >> putWord16be (fromIntegral $ B.length str) >> put str
putFrame (JpgExtension appCode str) =
put (JpgExtensionSegment appCode) >> putWord16be (fromIntegral $ B.length str) >> put str
putFrame (JpgQuantTable tables) =
put JpgQuantizationTable >> put (TableList tables)
putFrame (JpgHuffmanTable tables) =
put JpgHuffmanTableMarker >> put (TableList $ map fst tables)
putFrame (JpgIntervalRestart size) =
put JpgRestartInterval >> put (RestartInterval size)
putFrame (JpgScanBlob hdr blob) =
put JpgStartOfScan >> put hdr >> putLazyByteString blob
putFrame (JpgScans kind hdr) =
put kind >> put hdr
--------------------------------------------------
---- Serialization instances
--------------------------------------------------
commonMarkerFirstByte :: Word8
commonMarkerFirstByte = 0xFF
checkMarker :: Word8 -> Word8 -> Get ()
checkMarker b1 b2 = do
rb1 <- getWord8
rb2 <- getWord8
when (rb1 /= b1 || rb2 /= b2)
(fail "Invalid marker used")
extractScanContent :: L.ByteString -> (L.ByteString, L.ByteString)
extractScanContent str = aux 0
where maxi = fromIntegral $ L.length str - 1
aux n | n >= maxi = (str, L.empty)
| v == 0xFF && vNext /= 0 && not isReset = L.splitAt n str
| otherwise = aux (n + 1)
where v = str `L.index` n
vNext = str `L.index` (n + 1)
isReset = 0xD0 <= vNext && vNext <= 0xD7
parseAdobe14 :: B.ByteString -> [JpgFrame] -> [JpgFrame]
parseAdobe14 str lst = go where
go = case runGetStrict get str of
Left _err -> lst
Right app14 -> JpgAdobeAPP14 app14 : lst
parseFrames :: Get [JpgFrame]
parseFrames = do
kind <- get
let parseNextFrame = do
word <- getWord8
when (word /= 0xFF) $ do
readedData <- bytesRead
fail $ "Invalid Frame marker (" ++ show word
++ ", bytes read : " ++ show readedData ++ ")"
parseFrames
case kind of
JpgEndOfImage -> return []
JpgAppSegment 14 ->
parseAdobe14 <$> takeCurrentFrame <*> parseNextFrame
JpgAppSegment c ->
(\frm lst -> JpgAppFrame c frm : lst) <$> takeCurrentFrame <*> parseNextFrame
JpgExtensionSegment c ->
(\frm lst -> JpgExtension c frm : lst) <$> takeCurrentFrame <*> parseNextFrame
JpgQuantizationTable ->
(\(TableList quants) lst -> JpgQuantTable quants : lst) <$> get <*> parseNextFrame
JpgRestartInterval ->
(\(RestartInterval i) lst -> JpgIntervalRestart i : lst) <$> get <*> parseNextFrame
JpgHuffmanTableMarker ->
(\(TableList huffTables) lst ->
JpgHuffmanTable [(t, packHuffmanTree . buildPackedHuffmanTree $ huffCodes t) | t <- huffTables] : lst)
<$> get <*> parseNextFrame
JpgStartOfScan ->
(\frm imgData ->
let (d, other) = extractScanContent imgData
in
case runGet parseFrames (L.drop 1 other) of
Left _ -> [JpgScanBlob frm d]
Right lst -> JpgScanBlob frm d : lst
) <$> get <*> getRemainingLazyBytes
_ -> (\hdr lst -> JpgScans kind hdr : lst) <$> get <*> parseNextFrame
buildPackedHuffmanTree :: V.Vector (VU.Vector Word8) -> HuffmanTree
buildPackedHuffmanTree = buildHuffmanTree . map VU.toList . V.toList
secondStartOfFrameByteOfKind :: JpgFrameKind -> Word8
secondStartOfFrameByteOfKind = aux
where
aux JpgBaselineDCTHuffman = 0xC0
aux JpgExtendedSequentialDCTHuffman = 0xC1
aux JpgProgressiveDCTHuffman = 0xC2
aux JpgLosslessHuffman = 0xC3
aux JpgDifferentialSequentialDCTHuffman = 0xC5
aux JpgDifferentialProgressiveDCTHuffman = 0xC6
aux JpgDifferentialLosslessHuffman = 0xC7
aux JpgExtendedSequentialArithmetic = 0xC9
aux JpgProgressiveDCTArithmetic = 0xCA
aux JpgLosslessArithmetic = 0xCB
aux JpgHuffmanTableMarker = 0xC4
aux JpgDifferentialSequentialDCTArithmetic = 0xCD
aux JpgDifferentialProgressiveDCTArithmetic = 0xCE
aux JpgDifferentialLosslessArithmetic = 0xCF
aux JpgEndOfImage = 0xD9
aux JpgQuantizationTable = 0xDB
aux JpgStartOfScan = 0xDA
aux JpgRestartInterval = 0xDD
aux (JpgRestartIntervalEnd v) = v
aux (JpgAppSegment a) = a
aux (JpgExtensionSegment a) = a
data JpgImageKind = BaseLineDCT | ProgressiveDCT
instance Binary JpgFrameKind where
put v = putWord8 0xFF >> put (secondStartOfFrameByteOfKind v)
get = do
-- no lookahead :(
{-word <- getWord8-}
word2 <- getWord8
return $ case word2 of
0xC0 -> JpgBaselineDCTHuffman
0xC1 -> JpgExtendedSequentialDCTHuffman
0xC2 -> JpgProgressiveDCTHuffman
0xC3 -> JpgLosslessHuffman
0xC4 -> JpgHuffmanTableMarker
0xC5 -> JpgDifferentialSequentialDCTHuffman
0xC6 -> JpgDifferentialProgressiveDCTHuffman
0xC7 -> JpgDifferentialLosslessHuffman
0xC9 -> JpgExtendedSequentialArithmetic
0xCA -> JpgProgressiveDCTArithmetic
0xCB -> JpgLosslessArithmetic
0xCD -> JpgDifferentialSequentialDCTArithmetic
0xCE -> JpgDifferentialProgressiveDCTArithmetic
0xCF -> JpgDifferentialLosslessArithmetic
0xD9 -> JpgEndOfImage
0xDA -> JpgStartOfScan
0xDB -> JpgQuantizationTable
0xDD -> JpgRestartInterval
a | a >= 0xF0 -> JpgExtensionSegment a
| a >= 0xE0 -> JpgAppSegment a
| a >= 0xD0 && a <= 0xD7 -> JpgRestartIntervalEnd a
| otherwise -> error ("Invalid frame marker (" ++ show a ++ ")")
put4BitsOfEach :: Word8 -> Word8 -> Put
put4BitsOfEach a b = put $ (a `unsafeShiftL` 4) .|. b
get4BitOfEach :: Get (Word8, Word8)
get4BitOfEach = do
val <- get
return ((val `unsafeShiftR` 4) .&. 0xF, val .&. 0xF)
newtype RestartInterval = RestartInterval Word16
instance Binary RestartInterval where
put (RestartInterval i) = putWord16be 4 >> putWord16be i
get = do
size <- getWord16be
when (size /= 4) (fail "Invalid jpeg restart interval size")
RestartInterval <$> getWord16be
instance Binary JpgComponent where
get = do
ident <- getWord8
(horiz, vert) <- get4BitOfEach
quantTableIndex <- getWord8
return JpgComponent
{ componentIdentifier = ident
, horizontalSamplingFactor = horiz
, verticalSamplingFactor = vert
, quantizationTableDest = quantTableIndex
}
put v = do
put $ componentIdentifier v
put4BitsOfEach (horizontalSamplingFactor v) $ verticalSamplingFactor v
put $ quantizationTableDest v
instance Binary JpgFrameHeader where
get = do
beginOffset <- fromIntegral <$> bytesRead
frmHLength <- getWord16be
samplePrec <- getWord8
h <- getWord16be
w <- getWord16be
compCount <- getWord8
components <- replicateM (fromIntegral compCount) get
endOffset <- fromIntegral <$> bytesRead
when (beginOffset - endOffset < fromIntegral frmHLength)
(skip $ fromIntegral frmHLength - (endOffset - beginOffset))
return JpgFrameHeader
{ jpgFrameHeaderLength = frmHLength
, jpgSamplePrecision = samplePrec
, jpgHeight = h
, jpgWidth = w
, jpgImageComponentCount = compCount
, jpgComponents = components
}
put v = do
putWord16be $ jpgFrameHeaderLength v
putWord8 $ jpgSamplePrecision v
putWord16be $ jpgHeight v
putWord16be $ jpgWidth v
putWord8 $ jpgImageComponentCount v
mapM_ put $ jpgComponents v
instance Binary JpgScanSpecification where
put v = do
put $ componentSelector v
put4BitsOfEach (dcEntropyCodingTable v) $ acEntropyCodingTable v
get = do
compSel <- get
(dc, ac) <- get4BitOfEach
return JpgScanSpecification {
componentSelector = compSel
, dcEntropyCodingTable = dc
, acEntropyCodingTable = ac
}
instance Binary JpgScanHeader where
get = do
thisScanLength <- getWord16be
compCount <- getWord8
comp <- replicateM (fromIntegral compCount) get
specBeg <- get
specEnd <- get
(approxHigh, approxLow) <- get4BitOfEach
return JpgScanHeader {
scanLength = thisScanLength,
scanComponentCount = compCount,
scans = comp,
spectralSelection = (specBeg, specEnd),
successiveApproxHigh = approxHigh,
successiveApproxLow = approxLow
}
put v = do
putWord16be $ scanLength v
putWord8 $ scanComponentCount v
mapM_ put $ scans v
putWord8 . fst $ spectralSelection v
putWord8 . snd $ spectralSelection v
put4BitsOfEach (successiveApproxHigh v) $ successiveApproxLow v
{-# INLINE createEmptyMutableMacroBlock #-}
-- | Create a new macroblock with the good array size
createEmptyMutableMacroBlock :: (Storable a, Num a) => ST s (MutableMacroBlock s a)
createEmptyMutableMacroBlock = M.replicate 64 0
printMacroBlock :: (Storable a, PrintfArg a)
=> MutableMacroBlock s a -> ST s String
printMacroBlock block = pLn 0
where pLn 64 = return "===============================\n"
pLn i = do
v <- block `M.unsafeRead` i
vn <- pLn (i+1)
return $ printf (if i `mod` 8 == 0 then "\n%5d " else "%5d ") v ++ vn
printPureMacroBlock :: (Storable a, PrintfArg a) => MacroBlock a -> String
printPureMacroBlock block = pLn 0
where pLn 64 = "===============================\n"
pLn i = str ++ pLn (i + 1)
where str | i `mod` 8 == 0 = printf "\n%5d " v
| otherwise = printf "%5d" v
v = block VS.! i
{-# INLINE dctBlockSize #-}
dctBlockSize :: Num a => a
dctBlockSize = 8