/src/skia/third_party/externals/dng_sdk/source/dng_lossless_jpeg.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /*****************************************************************************/ |
2 | | // Copyright 2006-2007 Adobe Systems Incorporated |
3 | | // All Rights Reserved. |
4 | | // |
5 | | // NOTICE: Adobe permits you to use, modify, and distribute this file in |
6 | | // accordance with the terms of the Adobe license agreement accompanying it. |
7 | | /*****************************************************************************/ |
8 | | |
9 | | /* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_lossless_jpeg.cpp#2 $ */ |
10 | | /* $DateTime: 2012/06/01 07:28:57 $ */ |
11 | | /* $Change: 832715 $ */ |
12 | | /* $Author: tknoll $ */ |
13 | | |
14 | | /*****************************************************************************/ |
15 | | |
16 | | // Lossless JPEG code adapted from: |
17 | | |
18 | | /* Copyright (C) 1991, 1992, Thomas G. Lane. |
19 | | * Part of the Independent JPEG Group's software. |
20 | | * See the file Copyright for more details. |
21 | | * |
22 | | * Copyright (c) 1993 Brian C. Smith, The Regents of the University |
23 | | * of California |
24 | | * All rights reserved. |
25 | | * |
26 | | * Copyright (c) 1994 Kongji Huang and Brian C. Smith. |
27 | | * Cornell University |
28 | | * All rights reserved. |
29 | | * |
30 | | * Permission to use, copy, modify, and distribute this software and its |
31 | | * documentation for any purpose, without fee, and without written agreement is |
32 | | * hereby granted, provided that the above copyright notice and the following |
33 | | * two paragraphs appear in all copies of this software. |
34 | | * |
35 | | * IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR |
36 | | * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT |
37 | | * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL |
38 | | * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
39 | | * |
40 | | * CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, |
41 | | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY |
42 | | * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS |
43 | | * ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO |
44 | | * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. |
45 | | */ |
46 | | |
47 | | /*****************************************************************************/ |
48 | | |
49 | | #include "dng_lossless_jpeg.h" |
50 | | |
51 | | #include "dng_assertions.h" |
52 | | #include "dng_exceptions.h" |
53 | | #include "dng_memory.h" |
54 | | #include "dng_stream.h" |
55 | | #include "dng_tag_codes.h" |
56 | | |
57 | | /*****************************************************************************/ |
58 | | |
59 | | // This module contains routines that should be as fast as possible, even |
60 | | // at the expense of slight code size increases. |
61 | | |
62 | | #include "dng_fast_module.h" |
63 | | |
64 | | /*****************************************************************************/ |
65 | | |
66 | | // The qSupportCanon_sRAW stuff not actually required for DNG support, but |
67 | | // only included to allow this code to be used on Canon sRAW files. |
68 | | |
69 | | #ifndef qSupportCanon_sRAW |
70 | | #define qSupportCanon_sRAW 1 |
71 | | #endif |
72 | | |
73 | | // The qSupportHasselblad_3FR stuff not actually required for DNG support, but |
74 | | // only included to allow this code to be used on Hasselblad 3FR files. |
75 | | |
76 | | #ifndef qSupportHasselblad_3FR |
77 | | #define qSupportHasselblad_3FR 1 |
78 | | #endif |
79 | | |
80 | | /*****************************************************************************/ |
81 | | |
82 | | /* |
83 | | * One of the following structures is created for each huffman coding |
84 | | * table. We use the same structure for encoding and decoding, so there |
85 | | * may be some extra fields for encoding that aren't used in the decoding |
86 | | * and vice-versa. |
87 | | */ |
88 | | |
89 | | struct HuffmanTable |
90 | | { |
91 | | |
92 | | /* |
93 | | * These two fields directly represent the contents of a JPEG DHT |
94 | | * marker |
95 | | */ |
96 | | uint8 bits[17]; |
97 | | uint8 huffval[256]; |
98 | | |
99 | | /* |
100 | | * The remaining fields are computed from the above to allow more |
101 | | * efficient coding and decoding. These fields should be considered |
102 | | * private to the Huffman compression & decompression modules. |
103 | | */ |
104 | | |
105 | | uint16 mincode[17]; |
106 | | int32 maxcode[18]; |
107 | | int16 valptr[17]; |
108 | | int32 numbits[256]; |
109 | | int32 value[256]; |
110 | | |
111 | | uint16 ehufco[256]; |
112 | | int8 ehufsi[256]; |
113 | | |
114 | | }; |
115 | | |
116 | | /*****************************************************************************/ |
117 | | |
118 | | // Computes the derived fields in the Huffman table structure. |
119 | | |
120 | | static void FixHuffTbl (HuffmanTable *htbl) |
121 | 0 | { |
122 | | |
123 | 0 | int32 l; |
124 | 0 | int32 i; |
125 | | |
126 | 0 | const uint32 bitMask [] = |
127 | 0 | { |
128 | 0 | 0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff, |
129 | 0 | 0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff, |
130 | 0 | 0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff, |
131 | 0 | 0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff, |
132 | 0 | 0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff, |
133 | 0 | 0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff, |
134 | 0 | 0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f, |
135 | 0 | 0x0000000f, 0x00000007, 0x00000003, 0x00000001 |
136 | 0 | }; |
137 | | |
138 | | // Figure C.1: make table of Huffman code length for each symbol |
139 | | // Note that this is in code-length order. |
140 | |
|
141 | 0 | int8 huffsize [257]; |
142 | | |
143 | 0 | int32 p = 0; |
144 | | |
145 | 0 | for (l = 1; l <= 16; l++) |
146 | 0 | { |
147 | | |
148 | 0 | for (i = 1; i <= (int32) htbl->bits [l]; i++) |
149 | 0 | huffsize [p++] = (int8) l; |
150 | |
|
151 | 0 | } |
152 | | |
153 | 0 | huffsize [p] = 0; |
154 | | |
155 | 0 | int32 lastp = p; |
156 | | |
157 | | // Figure C.2: generate the codes themselves |
158 | | // Note that this is in code-length order. |
159 | |
|
160 | 0 | uint16 huffcode [257]; |
161 | | |
162 | 0 | uint16 code = 0; |
163 | | |
164 | 0 | int32 si = huffsize [0]; |
165 | | |
166 | 0 | p = 0; |
167 | | |
168 | 0 | while (huffsize [p]) |
169 | 0 | { |
170 | | |
171 | 0 | while (((int32) huffsize [p]) == si) |
172 | 0 | { |
173 | 0 | huffcode [p++] = code; |
174 | 0 | code++; |
175 | 0 | } |
176 | | |
177 | 0 | code <<= 1; |
178 | | |
179 | 0 | si++; |
180 | | |
181 | 0 | } |
182 | | |
183 | | // Figure C.3: generate encoding tables |
184 | | // These are code and size indexed by symbol value |
185 | | // Set any codeless symbols to have code length 0; this allows |
186 | | // EmitBits to detect any attempt to emit such symbols. |
187 | |
|
188 | 0 | memset (htbl->ehufsi, 0, sizeof (htbl->ehufsi)); |
189 | |
|
190 | 0 | for (p = 0; p < lastp; p++) |
191 | 0 | { |
192 | | |
193 | 0 | htbl->ehufco [htbl->huffval [p]] = huffcode [p]; |
194 | 0 | htbl->ehufsi [htbl->huffval [p]] = huffsize [p]; |
195 | | |
196 | 0 | } |
197 | | |
198 | | // Figure F.15: generate decoding tables |
199 | | |
200 | 0 | p = 0; |
201 | | |
202 | 0 | for (l = 1; l <= 16; l++) |
203 | 0 | { |
204 | | |
205 | 0 | if (htbl->bits [l]) |
206 | 0 | { |
207 | | |
208 | 0 | htbl->valptr [l] = (int16) p; |
209 | 0 | htbl->mincode [l] = huffcode [p]; |
210 | | |
211 | 0 | p += htbl->bits [l]; |
212 | | |
213 | 0 | htbl->maxcode [l] = huffcode [p - 1]; |
214 | | |
215 | 0 | } |
216 | | |
217 | 0 | else |
218 | 0 | { |
219 | 0 | htbl->maxcode [l] = -1; |
220 | 0 | } |
221 | |
|
222 | 0 | } |
223 | | |
224 | | // We put in this value to ensure HuffDecode terminates. |
225 | |
|
226 | 0 | htbl->maxcode[17] = 0xFFFFFL; |
227 | | |
228 | | // Build the numbits, value lookup tables. |
229 | | // These table allow us to gather 8 bits from the bits stream, |
230 | | // and immediately lookup the size and value of the huffman codes. |
231 | | // If size is zero, it means that more than 8 bits are in the huffman |
232 | | // code (this happens about 3-4% of the time). |
233 | |
|
234 | 0 | memset (htbl->numbits, 0, sizeof (htbl->numbits)); |
235 | | |
236 | 0 | for (p = 0; p < lastp; p++) |
237 | 0 | { |
238 | | |
239 | 0 | int32 size = huffsize [p]; |
240 | | |
241 | 0 | if (size <= 8) |
242 | 0 | { |
243 | | |
244 | 0 | int32 value = htbl->huffval [p]; |
245 | | |
246 | 0 | code = huffcode [p]; |
247 | | |
248 | 0 | int32 ll = code << (8 -size); |
249 | | |
250 | 0 | int32 ul = (size < 8 ? ll | bitMask [24 + size] |
251 | 0 | : ll); |
252 | 0 | if (ul >= static_cast<int32>(sizeof(htbl->numbits) / sizeof(htbl->numbits[0])) || |
253 | 0 | ul >= static_cast<int32>(sizeof(htbl->value) / sizeof(htbl->value[0]))) |
254 | 0 | { |
255 | 0 | ThrowBadFormat (); |
256 | 0 | } |
257 | | |
258 | 0 | for (i = ll; i <= ul; i++) |
259 | 0 | { |
260 | 0 | htbl->numbits [i] = size; |
261 | 0 | htbl->value [i] = value; |
262 | 0 | } |
263 | | |
264 | 0 | } |
265 | |
|
266 | 0 | } |
267 | |
|
268 | 0 | } |
269 | | |
270 | | /*****************************************************************************/ |
271 | | |
272 | | /* |
273 | | * The following structure stores basic information about one component. |
274 | | */ |
275 | | |
276 | | struct JpegComponentInfo |
277 | | { |
278 | | |
279 | | /* |
280 | | * These values are fixed over the whole image. |
281 | | * They are read from the SOF marker. |
282 | | */ |
283 | | int16 componentId; /* identifier for this component (0..255) */ |
284 | | int16 componentIndex; /* its index in SOF or cPtr->compInfo[] */ |
285 | | |
286 | | /* |
287 | | * Downsampling is not normally used in lossless JPEG, although |
288 | | * it is permitted by the JPEG standard (DIS). We set all sampling |
289 | | * factors to 1 in this program. |
290 | | */ |
291 | | int16 hSampFactor; /* horizontal sampling factor */ |
292 | | int16 vSampFactor; /* vertical sampling factor */ |
293 | | |
294 | | /* |
295 | | * Huffman table selector (0..3). The value may vary |
296 | | * between scans. It is read from the SOS marker. |
297 | | */ |
298 | | int16 dcTblNo; |
299 | | |
300 | | }; |
301 | | |
302 | | /* |
303 | | * One of the following structures is used to pass around the |
304 | | * decompression information. |
305 | | */ |
306 | | |
307 | | struct DecompressInfo |
308 | | { |
309 | | |
310 | | /* |
311 | | * Image width, height, and image data precision (bits/sample) |
312 | | * These fields are set by ReadFileHeader or ReadScanHeader |
313 | | */ |
314 | | int32 imageWidth; |
315 | | int32 imageHeight; |
316 | | int32 dataPrecision; |
317 | | |
318 | | /* |
319 | | * compInfo[i] describes component that appears i'th in SOF |
320 | | * numComponents is the # of color components in JPEG image. |
321 | | */ |
322 | | JpegComponentInfo *compInfo; |
323 | | int16 numComponents; |
324 | | |
325 | | /* |
326 | | * *curCompInfo[i] describes component that appears i'th in SOS. |
327 | | * compsInScan is the # of color components in current scan. |
328 | | */ |
329 | | JpegComponentInfo *curCompInfo[4]; |
330 | | int16 compsInScan; |
331 | | |
332 | | /* |
333 | | * MCUmembership[i] indexes the i'th component of MCU into the |
334 | | * curCompInfo array. |
335 | | */ |
336 | | int16 MCUmembership[10]; |
337 | | |
338 | | /* |
339 | | * ptrs to Huffman coding tables, or NULL if not defined |
340 | | */ |
341 | | HuffmanTable *dcHuffTblPtrs[4]; |
342 | | |
343 | | /* |
344 | | * prediction selection value (PSV) and point transform parameter (Pt) |
345 | | */ |
346 | | int32 Ss; |
347 | | int32 Pt; |
348 | | |
349 | | /* |
350 | | * In lossless JPEG, restart interval shall be an integer |
351 | | * multiple of the number of MCU in a MCU row. |
352 | | */ |
353 | | int32 restartInterval;/* MCUs per restart interval, 0 = no restart */ |
354 | | int32 restartInRows; /*if > 0, MCU rows per restart interval; 0 = no restart*/ |
355 | | |
356 | | /* |
357 | | * these fields are private data for the entropy decoder |
358 | | */ |
359 | | int32 restartRowsToGo; /* MCUs rows left in this restart interval */ |
360 | | int16 nextRestartNum; /* # of next RSTn marker (0..7) */ |
361 | | |
362 | | }; |
363 | | |
364 | | /*****************************************************************************/ |
365 | | |
366 | | // An MCU (minimum coding unit) is an array of samples. |
367 | | |
368 | | typedef uint16 ComponentType; // the type of image components |
369 | | |
370 | | typedef ComponentType *MCU; // MCU - array of samples |
371 | | |
372 | | /*****************************************************************************/ |
373 | | |
374 | | class dng_lossless_decoder |
375 | | { |
376 | | |
377 | | private: |
378 | | |
379 | | dng_stream *fStream; // Input data. |
380 | | |
381 | | dng_spooler *fSpooler; // Output data. |
382 | | |
383 | | bool fBug16; // Decode data with the "16-bit" bug. |
384 | | |
385 | | dng_memory_data huffmanBuffer [4]; |
386 | | |
387 | | dng_memory_data compInfoBuffer; |
388 | | |
389 | | DecompressInfo info; |
390 | | |
391 | | dng_memory_data mcuBuffer1; |
392 | | dng_memory_data mcuBuffer2; |
393 | | dng_memory_data mcuBuffer3; |
394 | | dng_memory_data mcuBuffer4; |
395 | | |
396 | | MCU *mcuROW1; |
397 | | MCU *mcuROW2; |
398 | | |
399 | | uint64 getBuffer; // current bit-extraction buffer |
400 | | int32 bitsLeft; // # of unused bits in it |
401 | | |
402 | | #if qSupportHasselblad_3FR |
403 | | bool fHasselblad3FR; |
404 | | #endif |
405 | | |
406 | | public: |
407 | | |
408 | | dng_lossless_decoder (dng_stream *stream, |
409 | | dng_spooler *spooler, |
410 | | bool bug16); |
411 | | |
412 | | void StartRead (uint32 &imageWidth, |
413 | | uint32 &imageHeight, |
414 | | uint32 &imageChannels); |
415 | | |
416 | | void FinishRead (); |
417 | | |
418 | | private: |
419 | | |
420 | | uint8 GetJpegChar () |
421 | 0 | { |
422 | 0 | return fStream->Get_uint8 (); |
423 | 0 | } |
424 | | |
425 | | void UnGetJpegChar () |
426 | 0 | { |
427 | 0 | fStream->SetReadPosition (fStream->Position () - 1); |
428 | 0 | } |
429 | | |
430 | | uint16 Get2bytes (); |
431 | | |
432 | | void SkipVariable (); |
433 | | |
434 | | void GetDht (); |
435 | | |
436 | | void GetDri (); |
437 | | |
438 | | void GetApp0 (); |
439 | | |
440 | | void GetSof (int32 code); |
441 | | |
442 | | void GetSos (); |
443 | | |
444 | | void GetSoi (); |
445 | | |
446 | | int32 NextMarker (); |
447 | | |
448 | | JpegMarker ProcessTables (); |
449 | | |
450 | | void ReadFileHeader (); |
451 | | |
452 | | int32 ReadScanHeader (); |
453 | | |
454 | | void DecoderStructInit (); |
455 | | |
456 | | void HuffDecoderInit (); |
457 | | |
458 | | void ProcessRestart (); |
459 | | |
460 | | int32 QuickPredict (int32 col, |
461 | | int32 curComp, |
462 | | MCU *curRowBuf, |
463 | | MCU *prevRowBuf); |
464 | | |
465 | | void FillBitBuffer (int32 nbits); |
466 | | |
467 | | int32 show_bits8 (); |
468 | | |
469 | | void flush_bits (int32 nbits); |
470 | | |
471 | | int32 get_bits (int32 nbits); |
472 | | |
473 | | int32 get_bit (); |
474 | | |
475 | | int32 HuffDecode (HuffmanTable *htbl); |
476 | | |
477 | | void HuffExtend (int32 &x, int32 s); |
478 | | |
479 | | void PmPutRow (MCU *buf, |
480 | | int32 numComp, |
481 | | int32 numCol, |
482 | | int32 row); |
483 | | |
484 | | void DecodeFirstRow (MCU *curRowBuf); |
485 | | |
486 | | void DecodeImage (); |
487 | | |
488 | | // Hidden copy constructor and assignment operator. |
489 | | |
490 | | dng_lossless_decoder (const dng_lossless_decoder &decoder); |
491 | | |
492 | | dng_lossless_decoder & operator= (const dng_lossless_decoder &decoder); |
493 | | |
494 | | }; |
495 | | |
496 | | /*****************************************************************************/ |
497 | | |
498 | | dng_lossless_decoder::dng_lossless_decoder (dng_stream *stream, |
499 | | dng_spooler *spooler, |
500 | | bool bug16) |
501 | | |
502 | | : fStream (stream ) |
503 | | , fSpooler (spooler) |
504 | | , fBug16 (bug16 ) |
505 | | |
506 | | , compInfoBuffer () |
507 | | , info () |
508 | | , mcuBuffer1 () |
509 | | , mcuBuffer2 () |
510 | | , mcuBuffer3 () |
511 | | , mcuBuffer4 () |
512 | | , mcuROW1 (NULL) |
513 | | , mcuROW2 (NULL) |
514 | | , getBuffer (0) |
515 | | , bitsLeft (0) |
516 | | |
517 | | #if qSupportHasselblad_3FR |
518 | | , fHasselblad3FR (false) |
519 | | #endif |
520 | | |
521 | 0 | { |
522 | | |
523 | 0 | memset (&info, 0, sizeof (info)); |
524 | | |
525 | 0 | } |
526 | | |
527 | | /*****************************************************************************/ |
528 | | |
529 | | uint16 dng_lossless_decoder::Get2bytes () |
530 | 0 | { |
531 | | |
532 | 0 | uint16 a = GetJpegChar (); |
533 | | |
534 | 0 | return (uint16) ((a << 8) + GetJpegChar ()); |
535 | | |
536 | 0 | } |
537 | | |
538 | | /*****************************************************************************/ |
539 | | |
540 | | /* |
541 | | *-------------------------------------------------------------- |
542 | | * |
543 | | * SkipVariable -- |
544 | | * |
545 | | * Skip over an unknown or uninteresting variable-length marker |
546 | | * |
547 | | * Results: |
548 | | * None. |
549 | | * |
550 | | * Side effects: |
551 | | * Bitstream is parsed over marker. |
552 | | * |
553 | | * |
554 | | *-------------------------------------------------------------- |
555 | | */ |
556 | | |
557 | | void dng_lossless_decoder::SkipVariable () |
558 | 0 | { |
559 | | |
560 | 0 | uint32 length = Get2bytes () - 2; |
561 | | |
562 | 0 | fStream->Skip (length); |
563 | |
|
564 | 0 | } |
565 | | |
566 | | /*****************************************************************************/ |
567 | | |
568 | | /* |
569 | | *-------------------------------------------------------------- |
570 | | * |
571 | | * GetDht -- |
572 | | * |
573 | | * Process a DHT marker |
574 | | * |
575 | | * Results: |
576 | | * None |
577 | | * |
578 | | * Side effects: |
579 | | * A huffman table is read. |
580 | | * Exits on error. |
581 | | * |
582 | | *-------------------------------------------------------------- |
583 | | */ |
584 | | |
585 | | void dng_lossless_decoder::GetDht () |
586 | 0 | { |
587 | | |
588 | 0 | int32 length = Get2bytes () - 2; |
589 | | |
590 | 0 | while (length > 0) |
591 | 0 | { |
592 | |
|
593 | 0 | int32 index = GetJpegChar (); |
594 | | |
595 | 0 | if (index < 0 || index >= 4) |
596 | 0 | { |
597 | 0 | ThrowBadFormat (); |
598 | 0 | } |
599 | |
|
600 | 0 | HuffmanTable *&htblptr = info.dcHuffTblPtrs [index]; |
601 | |
|
602 | 0 | if (htblptr == NULL) |
603 | 0 | { |
604 | | |
605 | 0 | huffmanBuffer [index] . Allocate (sizeof (HuffmanTable)); |
606 | | |
607 | 0 | htblptr = (HuffmanTable *) huffmanBuffer [index] . Buffer (); |
608 | | |
609 | 0 | } |
610 | |
|
611 | 0 | htblptr->bits [0] = 0; |
612 | | |
613 | 0 | int32 count = 0; |
614 | | |
615 | 0 | for (int32 i = 1; i <= 16; i++) |
616 | 0 | { |
617 | | |
618 | 0 | htblptr->bits [i] = GetJpegChar (); |
619 | | |
620 | 0 | count += htblptr->bits [i]; |
621 | | |
622 | 0 | } |
623 | |
|
624 | 0 | if (count > 256) |
625 | 0 | { |
626 | 0 | ThrowBadFormat (); |
627 | 0 | } |
628 | |
|
629 | 0 | for (int32 j = 0; j < count; j++) |
630 | 0 | { |
631 | | |
632 | 0 | htblptr->huffval [j] = GetJpegChar (); |
633 | | |
634 | 0 | } |
635 | |
|
636 | 0 | length -= 1 + 16 + count; |
637 | |
|
638 | 0 | } |
639 | | |
640 | 0 | } |
641 | | |
642 | | /*****************************************************************************/ |
643 | | |
644 | | /* |
645 | | *-------------------------------------------------------------- |
646 | | * |
647 | | * GetDri -- |
648 | | * |
649 | | * Process a DRI marker |
650 | | * |
651 | | * Results: |
652 | | * None |
653 | | * |
654 | | * Side effects: |
655 | | * Exits on error. |
656 | | * Bitstream is parsed. |
657 | | * |
658 | | *-------------------------------------------------------------- |
659 | | */ |
660 | | |
661 | | void dng_lossless_decoder::GetDri () |
662 | 0 | { |
663 | | |
664 | 0 | if (Get2bytes () != 4) |
665 | 0 | { |
666 | 0 | ThrowBadFormat (); |
667 | 0 | } |
668 | | |
669 | 0 | info.restartInterval = Get2bytes (); |
670 | |
|
671 | 0 | } |
672 | | |
673 | | /*****************************************************************************/ |
674 | | |
675 | | /* |
676 | | *-------------------------------------------------------------- |
677 | | * |
678 | | * GetApp0 -- |
679 | | * |
680 | | * Process an APP0 marker. |
681 | | * |
682 | | * Results: |
683 | | * None |
684 | | * |
685 | | * Side effects: |
686 | | * Bitstream is parsed |
687 | | * |
688 | | *-------------------------------------------------------------- |
689 | | */ |
690 | | |
691 | | void dng_lossless_decoder::GetApp0 () |
692 | 0 | { |
693 | |
|
694 | 0 | SkipVariable (); |
695 | | |
696 | 0 | } |
697 | | |
698 | | /*****************************************************************************/ |
699 | | |
700 | | /* |
701 | | *-------------------------------------------------------------- |
702 | | * |
703 | | * GetSof -- |
704 | | * |
705 | | * Process a SOFn marker |
706 | | * |
707 | | * Results: |
708 | | * None. |
709 | | * |
710 | | * Side effects: |
711 | | * Bitstream is parsed |
712 | | * Exits on error |
713 | | * info structure is filled in |
714 | | * |
715 | | *-------------------------------------------------------------- |
716 | | */ |
717 | | |
718 | | void dng_lossless_decoder::GetSof (int32 /*code*/) |
719 | 0 | { |
720 | | |
721 | 0 | int32 length = Get2bytes (); |
722 | |
|
723 | 0 | info.dataPrecision = GetJpegChar (); |
724 | 0 | info.imageHeight = Get2bytes (); |
725 | 0 | info.imageWidth = Get2bytes (); |
726 | 0 | info.numComponents = GetJpegChar (); |
727 | | |
728 | | // We don't support files in which the image height is initially |
729 | | // specified as 0 and is later redefined by DNL. As long as we |
730 | | // have to check that, might as well have a general sanity check. |
731 | | |
732 | 0 | if ((info.imageHeight <= 0) || |
733 | 0 | (info.imageWidth <= 0) || |
734 | 0 | (info.numComponents <= 0)) |
735 | 0 | { |
736 | 0 | ThrowBadFormat (); |
737 | 0 | } |
738 | | |
739 | | // Lossless JPEG specifies data precision to be from 2 to 16 bits/sample. |
740 | |
|
741 | 0 | const int32 MinPrecisionBits = 2; |
742 | 0 | const int32 MaxPrecisionBits = 16; |
743 | |
|
744 | 0 | if ((info.dataPrecision < MinPrecisionBits) || |
745 | 0 | (info.dataPrecision > MaxPrecisionBits)) |
746 | 0 | { |
747 | 0 | ThrowBadFormat (); |
748 | 0 | } |
749 | | |
750 | | // Check length of tag. |
751 | |
|
752 | 0 | if (length != (info.numComponents * 3 + 8)) |
753 | 0 | { |
754 | 0 | ThrowBadFormat (); |
755 | 0 | } |
756 | | |
757 | | // Allocate per component info. |
758 | | |
759 | | // We can cast info.numComponents to a uint32 because the check above |
760 | | // guarantees that it cannot be negative. |
761 | 0 | compInfoBuffer.Allocate (static_cast<uint32> (info.numComponents), |
762 | 0 | sizeof (JpegComponentInfo)); |
763 | | |
764 | 0 | info.compInfo = (JpegComponentInfo *) compInfoBuffer.Buffer (); |
765 | | |
766 | | // Read in the per compent info. |
767 | |
|
768 | 0 | for (int32 ci = 0; ci < info.numComponents; ci++) |
769 | 0 | { |
770 | | |
771 | 0 | JpegComponentInfo *compptr = &info.compInfo [ci]; |
772 | | |
773 | 0 | compptr->componentIndex = (int16) ci; |
774 | | |
775 | 0 | compptr->componentId = GetJpegChar (); |
776 | | |
777 | 0 | int32 c = GetJpegChar (); |
778 | | |
779 | 0 | compptr->hSampFactor = (int16) ((c >> 4) & 15); |
780 | 0 | compptr->vSampFactor = (int16) ((c ) & 15); |
781 | | |
782 | 0 | (void) GetJpegChar (); /* skip Tq */ |
783 | | |
784 | 0 | } |
785 | |
|
786 | 0 | } |
787 | | |
788 | | /*****************************************************************************/ |
789 | | |
790 | | /* |
791 | | *-------------------------------------------------------------- |
792 | | * |
793 | | * GetSos -- |
794 | | * |
795 | | * Process a SOS marker |
796 | | * |
797 | | * Results: |
798 | | * None. |
799 | | * |
800 | | * Side effects: |
801 | | * Bitstream is parsed. |
802 | | * Exits on error. |
803 | | * |
804 | | *-------------------------------------------------------------- |
805 | | */ |
806 | | |
807 | | void dng_lossless_decoder::GetSos () |
808 | 0 | { |
809 | | |
810 | 0 | int32 length = Get2bytes (); |
811 | | |
812 | | // Get the number of image components. |
813 | |
|
814 | 0 | int32 n = GetJpegChar (); |
815 | 0 | info.compsInScan = (int16) n; |
816 | | |
817 | | // Check length. |
818 | | |
819 | 0 | length -= 3; |
820 | |
|
821 | 0 | if (length != (n * 2 + 3) || n < 1 || n > 4) |
822 | 0 | { |
823 | 0 | ThrowBadFormat (); |
824 | 0 | } |
825 | | |
826 | | // Find index and huffman table for each component. |
827 | |
|
828 | 0 | for (int32 i = 0; i < n; i++) |
829 | 0 | { |
830 | | |
831 | 0 | int32 cc = GetJpegChar (); |
832 | 0 | int32 c = GetJpegChar (); |
833 | | |
834 | 0 | int32 ci; |
835 | | |
836 | 0 | for (ci = 0; ci < info.numComponents; ci++) |
837 | 0 | { |
838 | | |
839 | 0 | if (cc == info.compInfo[ci].componentId) |
840 | 0 | { |
841 | 0 | break; |
842 | 0 | } |
843 | | |
844 | 0 | } |
845 | |
|
846 | 0 | if (ci >= info.numComponents) |
847 | 0 | { |
848 | 0 | ThrowBadFormat (); |
849 | 0 | } |
850 | |
|
851 | 0 | JpegComponentInfo *compptr = &info.compInfo [ci]; |
852 | | |
853 | 0 | info.curCompInfo [i] = compptr; |
854 | | |
855 | 0 | compptr->dcTblNo = (int16) ((c >> 4) & 15); |
856 | | |
857 | 0 | } |
858 | | |
859 | | // Get the PSV, skip Se, and get the point transform parameter. |
860 | |
|
861 | 0 | info.Ss = GetJpegChar (); |
862 | | |
863 | 0 | (void) GetJpegChar (); |
864 | | |
865 | 0 | info.Pt = GetJpegChar () & 0x0F; |
866 | | |
867 | 0 | } |
868 | | |
869 | | /*****************************************************************************/ |
870 | | |
871 | | /* |
872 | | *-------------------------------------------------------------- |
873 | | * |
874 | | * GetSoi -- |
875 | | * |
876 | | * Process an SOI marker |
877 | | * |
878 | | * Results: |
879 | | * None. |
880 | | * |
881 | | * Side effects: |
882 | | * Bitstream is parsed. |
883 | | * Exits on error. |
884 | | * |
885 | | *-------------------------------------------------------------- |
886 | | */ |
887 | | |
888 | | void dng_lossless_decoder::GetSoi () |
889 | 0 | { |
890 | | |
891 | | // Reset all parameters that are defined to be reset by SOI |
892 | | |
893 | 0 | info.restartInterval = 0; |
894 | | |
895 | 0 | } |
896 | | |
897 | | /*****************************************************************************/ |
898 | | |
899 | | /* |
900 | | *-------------------------------------------------------------- |
901 | | * |
902 | | * NextMarker -- |
903 | | * |
904 | | * Find the next JPEG marker Note that the output might not |
905 | | * be a valid marker code but it will never be 0 or FF |
906 | | * |
907 | | * Results: |
908 | | * The marker found. |
909 | | * |
910 | | * Side effects: |
911 | | * Bitstream is parsed. |
912 | | * |
913 | | *-------------------------------------------------------------- |
914 | | */ |
915 | | |
916 | | int32 dng_lossless_decoder::NextMarker () |
917 | 0 | { |
918 | | |
919 | 0 | int32 c; |
920 | |
|
921 | 0 | do |
922 | 0 | { |
923 | | |
924 | | // skip any non-FF bytes |
925 | | |
926 | 0 | do |
927 | 0 | { |
928 | 0 | c = GetJpegChar (); |
929 | 0 | } |
930 | 0 | while (c != 0xFF); |
931 | | |
932 | | // skip any duplicate FFs, since extra FFs are legal |
933 | | |
934 | 0 | do |
935 | 0 | { |
936 | 0 | c = GetJpegChar(); |
937 | 0 | } |
938 | 0 | while (c == 0xFF); |
939 | | |
940 | 0 | } |
941 | 0 | while (c == 0); // repeat if it was a stuffed FF/00 |
942 | |
|
943 | 0 | return c; |
944 | | |
945 | 0 | } |
946 | | |
947 | | /*****************************************************************************/ |
948 | | |
949 | | /* |
950 | | *-------------------------------------------------------------- |
951 | | * |
952 | | * ProcessTables -- |
953 | | * |
954 | | * Scan and process JPEG markers that can appear in any order |
955 | | * Return when an SOI, EOI, SOFn, or SOS is found |
956 | | * |
957 | | * Results: |
958 | | * The marker found. |
959 | | * |
960 | | * Side effects: |
961 | | * Bitstream is parsed. |
962 | | * |
963 | | *-------------------------------------------------------------- |
964 | | */ |
965 | | |
966 | | JpegMarker dng_lossless_decoder::ProcessTables () |
967 | 0 | { |
968 | | |
969 | 0 | while (true) |
970 | 0 | { |
971 | | |
972 | 0 | int32 c = NextMarker (); |
973 | | |
974 | 0 | switch (c) |
975 | 0 | { |
976 | | |
977 | 0 | case M_SOF0: |
978 | 0 | case M_SOF1: |
979 | 0 | case M_SOF2: |
980 | 0 | case M_SOF3: |
981 | 0 | case M_SOF5: |
982 | 0 | case M_SOF6: |
983 | 0 | case M_SOF7: |
984 | 0 | case M_JPG: |
985 | 0 | case M_SOF9: |
986 | 0 | case M_SOF10: |
987 | 0 | case M_SOF11: |
988 | 0 | case M_SOF13: |
989 | 0 | case M_SOF14: |
990 | 0 | case M_SOF15: |
991 | 0 | case M_SOI: |
992 | 0 | case M_EOI: |
993 | 0 | case M_SOS: |
994 | 0 | return (JpegMarker) c; |
995 | |
|
996 | 0 | case M_DHT: |
997 | 0 | GetDht (); |
998 | 0 | break; |
999 | |
|
1000 | 0 | case M_DQT: |
1001 | 0 | break; |
1002 | |
|
1003 | 0 | case M_DRI: |
1004 | 0 | GetDri (); |
1005 | 0 | break; |
1006 | |
|
1007 | 0 | case M_APP0: |
1008 | 0 | GetApp0 (); |
1009 | 0 | break; |
1010 | |
|
1011 | 0 | case M_RST0: // these are all parameterless |
1012 | 0 | case M_RST1: |
1013 | 0 | case M_RST2: |
1014 | 0 | case M_RST3: |
1015 | 0 | case M_RST4: |
1016 | 0 | case M_RST5: |
1017 | 0 | case M_RST6: |
1018 | 0 | case M_RST7: |
1019 | 0 | case M_TEM: |
1020 | 0 | break; |
1021 | |
|
1022 | 0 | default: // must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn |
1023 | 0 | SkipVariable (); |
1024 | 0 | break; |
1025 | | |
1026 | 0 | } |
1027 | | |
1028 | 0 | } |
1029 | |
|
1030 | 0 | return M_ERROR; |
1031 | 0 | } |
1032 | | |
1033 | | /*****************************************************************************/ |
1034 | | |
1035 | | /* |
1036 | | *-------------------------------------------------------------- |
1037 | | * |
1038 | | * ReadFileHeader -- |
1039 | | * |
1040 | | * Initialize and read the stream header (everything through |
1041 | | * the SOF marker). |
1042 | | * |
1043 | | * Results: |
1044 | | * None |
1045 | | * |
1046 | | * Side effects: |
1047 | | * Exit on error. |
1048 | | * |
1049 | | *-------------------------------------------------------------- |
1050 | | */ |
1051 | | |
1052 | | void dng_lossless_decoder::ReadFileHeader () |
1053 | 0 | { |
1054 | | |
1055 | | // Demand an SOI marker at the start of the stream --- otherwise it's |
1056 | | // probably not a JPEG stream at all. |
1057 | |
|
1058 | 0 | int32 c = GetJpegChar (); |
1059 | 0 | int32 c2 = GetJpegChar (); |
1060 | | |
1061 | 0 | if ((c != 0xFF) || (c2 != M_SOI)) |
1062 | 0 | { |
1063 | 0 | ThrowBadFormat (); |
1064 | 0 | } |
1065 | | |
1066 | | // OK, process SOI |
1067 | |
|
1068 | 0 | GetSoi (); |
1069 | | |
1070 | | // Process markers until SOF |
1071 | |
|
1072 | 0 | c = ProcessTables (); |
1073 | |
|
1074 | 0 | switch (c) |
1075 | 0 | { |
1076 | | |
1077 | 0 | case M_SOF0: |
1078 | 0 | case M_SOF1: |
1079 | 0 | case M_SOF3: |
1080 | 0 | GetSof (c); |
1081 | 0 | break; |
1082 | |
|
1083 | 0 | default: |
1084 | 0 | ThrowBadFormat (); |
1085 | 0 | break; |
1086 | | |
1087 | 0 | } |
1088 | |
|
1089 | 0 | } |
1090 | | |
1091 | | /*****************************************************************************/ |
1092 | | |
1093 | | /* |
1094 | | *-------------------------------------------------------------- |
1095 | | * |
1096 | | * ReadScanHeader -- |
1097 | | * |
1098 | | * Read the start of a scan (everything through the SOS marker). |
1099 | | * |
1100 | | * Results: |
1101 | | * 1 if find SOS, 0 if find EOI |
1102 | | * |
1103 | | * Side effects: |
1104 | | * Bitstream is parsed, may exit on errors. |
1105 | | * |
1106 | | *-------------------------------------------------------------- |
1107 | | */ |
1108 | | |
1109 | | int32 dng_lossless_decoder::ReadScanHeader () |
1110 | 0 | { |
1111 | | |
1112 | | // Process markers until SOS or EOI |
1113 | |
|
1114 | 0 | int32 c = ProcessTables (); |
1115 | |
|
1116 | 0 | switch (c) |
1117 | 0 | { |
1118 | | |
1119 | 0 | case M_SOS: |
1120 | 0 | GetSos (); |
1121 | 0 | return 1; |
1122 | | |
1123 | 0 | case M_EOI: |
1124 | 0 | return 0; |
1125 | | |
1126 | 0 | default: |
1127 | 0 | ThrowBadFormat (); |
1128 | 0 | break; |
1129 | | |
1130 | 0 | } |
1131 | | |
1132 | 0 | return 0; |
1133 | | |
1134 | 0 | } |
1135 | | |
1136 | | /*****************************************************************************/ |
1137 | | |
1138 | | /* |
1139 | | *-------------------------------------------------------------- |
1140 | | * |
1141 | | * DecoderStructInit -- |
1142 | | * |
1143 | | * Initalize the rest of the fields in the decompression |
1144 | | * structure. |
1145 | | * |
1146 | | * Results: |
1147 | | * None. |
1148 | | * |
1149 | | * Side effects: |
1150 | | * None. |
1151 | | * |
1152 | | *-------------------------------------------------------------- |
1153 | | */ |
1154 | | |
1155 | | void dng_lossless_decoder::DecoderStructInit () |
1156 | 0 | { |
1157 | | |
1158 | 0 | int32 ci; |
1159 | | |
1160 | 0 | #if qSupportCanon_sRAW |
1161 | | |
1162 | 0 | bool canon_sRAW = (info.numComponents == 3) && |
1163 | 0 | (info.compInfo [0].hSampFactor == 2) && |
1164 | 0 | (info.compInfo [1].hSampFactor == 1) && |
1165 | 0 | (info.compInfo [2].hSampFactor == 1) && |
1166 | 0 | (info.compInfo [0].vSampFactor == 1) && |
1167 | 0 | (info.compInfo [1].vSampFactor == 1) && |
1168 | 0 | (info.compInfo [2].vSampFactor == 1) && |
1169 | 0 | (info.dataPrecision == 15) && |
1170 | 0 | (info.Ss == 1) && |
1171 | 0 | ((info.imageWidth & 1) == 0); |
1172 | | |
1173 | 0 | bool canon_sRAW2 = (info.numComponents == 3) && |
1174 | 0 | (info.compInfo [0].hSampFactor == 2) && |
1175 | 0 | (info.compInfo [1].hSampFactor == 1) && |
1176 | 0 | (info.compInfo [2].hSampFactor == 1) && |
1177 | 0 | (info.compInfo [0].vSampFactor == 2) && |
1178 | 0 | (info.compInfo [1].vSampFactor == 1) && |
1179 | 0 | (info.compInfo [2].vSampFactor == 1) && |
1180 | 0 | (info.dataPrecision == 15) && |
1181 | 0 | (info.Ss == 1) && |
1182 | 0 | ((info.imageWidth & 1) == 0) && |
1183 | 0 | ((info.imageHeight & 1) == 0); |
1184 | | |
1185 | 0 | if (!canon_sRAW && !canon_sRAW2) |
1186 | | |
1187 | 0 | #endif |
1188 | | |
1189 | 0 | { |
1190 | | |
1191 | | // Check sampling factor validity. |
1192 | |
|
1193 | 0 | for (ci = 0; ci < info.numComponents; ci++) |
1194 | 0 | { |
1195 | | |
1196 | 0 | JpegComponentInfo *compPtr = &info.compInfo [ci]; |
1197 | | |
1198 | 0 | if (compPtr->hSampFactor != 1 || |
1199 | 0 | compPtr->vSampFactor != 1) |
1200 | 0 | { |
1201 | 0 | ThrowBadFormat (); |
1202 | 0 | } |
1203 | | |
1204 | 0 | } |
1205 | | |
1206 | 0 | } |
1207 | | |
1208 | | // Prepare array describing MCU composition. |
1209 | |
|
1210 | 0 | if (info.compsInScan < 0 || info.compsInScan > 4) |
1211 | 0 | { |
1212 | 0 | ThrowBadFormat (); |
1213 | 0 | } |
1214 | |
|
1215 | 0 | for (ci = 0; ci < info.compsInScan; ci++) |
1216 | 0 | { |
1217 | 0 | info.MCUmembership [ci] = (int16) ci; |
1218 | 0 | } |
1219 | | |
1220 | | // Initialize mucROW1 and mcuROW2 which buffer two rows of |
1221 | | // pixels for predictor calculation. |
1222 | | |
1223 | | // This multiplication cannot overflow because info.compsInScan is |
1224 | | // guaranteed to be between 0 and 4 inclusive (see checks above). |
1225 | 0 | int32 mcuSize = info.compsInScan * (uint32) sizeof (ComponentType); |
1226 | | |
1227 | 0 | mcuBuffer1.Allocate (info.imageWidth, sizeof (MCU)); |
1228 | 0 | mcuBuffer2.Allocate (info.imageWidth, sizeof (MCU)); |
1229 | | |
1230 | 0 | mcuROW1 = (MCU *) mcuBuffer1.Buffer (); |
1231 | 0 | mcuROW2 = (MCU *) mcuBuffer2.Buffer (); |
1232 | | |
1233 | 0 | mcuBuffer3.Allocate (info.imageWidth, mcuSize); |
1234 | 0 | mcuBuffer4.Allocate (info.imageWidth, mcuSize); |
1235 | | |
1236 | 0 | mcuROW1 [0] = (ComponentType *) mcuBuffer3.Buffer (); |
1237 | 0 | mcuROW2 [0] = (ComponentType *) mcuBuffer4.Buffer (); |
1238 | | |
1239 | 0 | for (int32 j = 1; j < info.imageWidth; j++) |
1240 | 0 | { |
1241 | | |
1242 | 0 | mcuROW1 [j] = mcuROW1 [j - 1] + info.compsInScan; |
1243 | 0 | mcuROW2 [j] = mcuROW2 [j - 1] + info.compsInScan; |
1244 | | |
1245 | 0 | } |
1246 | | |
1247 | 0 | } |
1248 | | |
1249 | | /*****************************************************************************/ |
1250 | | |
1251 | | /* |
1252 | | *-------------------------------------------------------------- |
1253 | | * |
1254 | | * HuffDecoderInit -- |
1255 | | * |
1256 | | * Initialize for a Huffman-compressed scan. |
1257 | | * This is invoked after reading the SOS marker. |
1258 | | * |
1259 | | * Results: |
1260 | | * None |
1261 | | * |
1262 | | * Side effects: |
1263 | | * None. |
1264 | | * |
1265 | | *-------------------------------------------------------------- |
1266 | | */ |
1267 | | |
1268 | | void dng_lossless_decoder::HuffDecoderInit () |
1269 | 0 | { |
1270 | | |
1271 | | // Initialize bit parser state |
1272 | | |
1273 | 0 | getBuffer = 0; |
1274 | 0 | bitsLeft = 0; |
1275 | | |
1276 | | // Prepare Huffman tables. |
1277 | |
|
1278 | 0 | for (int16 ci = 0; ci < info.compsInScan; ci++) |
1279 | 0 | { |
1280 | | |
1281 | 0 | JpegComponentInfo *compptr = info.curCompInfo [ci]; |
1282 | | |
1283 | | // Make sure requested tables are present |
1284 | | |
1285 | 0 | if (compptr->dcTblNo < 0 || compptr->dcTblNo > 3) |
1286 | 0 | { |
1287 | 0 | ThrowBadFormat (); |
1288 | 0 | } |
1289 | |
|
1290 | 0 | if (info.dcHuffTblPtrs [compptr->dcTblNo] == NULL) |
1291 | 0 | { |
1292 | 0 | ThrowBadFormat (); |
1293 | 0 | } |
1294 | | |
1295 | | // Compute derived values for Huffman tables. |
1296 | | // We may do this more than once for same table, but it's not a |
1297 | | // big deal |
1298 | |
|
1299 | 0 | FixHuffTbl (info.dcHuffTblPtrs [compptr->dcTblNo]); |
1300 | |
|
1301 | 0 | } |
1302 | | |
1303 | | // Initialize restart stuff |
1304 | |
|
1305 | 0 | info.restartInRows = info.restartInterval / info.imageWidth; |
1306 | 0 | info.restartRowsToGo = info.restartInRows; |
1307 | 0 | info.nextRestartNum = 0; |
1308 | | |
1309 | 0 | } |
1310 | | |
1311 | | /*****************************************************************************/ |
1312 | | |
1313 | | /* |
1314 | | *-------------------------------------------------------------- |
1315 | | * |
1316 | | * ProcessRestart -- |
1317 | | * |
1318 | | * Check for a restart marker & resynchronize decoder. |
1319 | | * |
1320 | | * Results: |
1321 | | * None. |
1322 | | * |
1323 | | * Side effects: |
1324 | | * BitStream is parsed, bit buffer is reset, etc. |
1325 | | * |
1326 | | *-------------------------------------------------------------- |
1327 | | */ |
1328 | | |
1329 | | void dng_lossless_decoder::ProcessRestart () |
1330 | 0 | { |
1331 | | |
1332 | | // Throw away and unused odd bits in the bit buffer. |
1333 | | |
1334 | 0 | fStream->SetReadPosition (fStream->Position () - bitsLeft / 8); |
1335 | | |
1336 | 0 | bitsLeft = 0; |
1337 | 0 | getBuffer = 0; |
1338 | | |
1339 | | // Scan for next JPEG marker |
1340 | |
|
1341 | 0 | int32 c; |
1342 | |
|
1343 | 0 | do |
1344 | 0 | { |
1345 | | |
1346 | | // skip any non-FF bytes |
1347 | | |
1348 | 0 | do |
1349 | 0 | { |
1350 | 0 | c = GetJpegChar (); |
1351 | 0 | } |
1352 | 0 | while (c != 0xFF); |
1353 | | |
1354 | | // skip any duplicate FFs |
1355 | | |
1356 | 0 | do |
1357 | 0 | { |
1358 | 0 | c = GetJpegChar (); |
1359 | 0 | } |
1360 | 0 | while (c == 0xFF); |
1361 | | |
1362 | 0 | } |
1363 | 0 | while (c == 0); // repeat if it was a stuffed FF/00 |
1364 | | |
1365 | | // Verify correct restart code. |
1366 | |
|
1367 | 0 | if (c != (M_RST0 + info.nextRestartNum)) |
1368 | 0 | { |
1369 | 0 | ThrowBadFormat (); |
1370 | 0 | } |
1371 | | |
1372 | | // Update restart state. |
1373 | |
|
1374 | 0 | info.restartRowsToGo = info.restartInRows; |
1375 | 0 | info.nextRestartNum = (info.nextRestartNum + 1) & 7; |
1376 | | |
1377 | 0 | } |
1378 | | |
1379 | | /*****************************************************************************/ |
1380 | | |
1381 | | /* |
1382 | | *-------------------------------------------------------------- |
1383 | | * |
1384 | | * QuickPredict -- |
1385 | | * |
1386 | | * Calculate the predictor for sample curRowBuf[col][curComp]. |
1387 | | * It does not handle the special cases at image edges, such |
1388 | | * as first row and first column of a scan. We put the special |
1389 | | * case checkings outside so that the computations in main |
1390 | | * loop can be simpler. This has enhenced the performance |
1391 | | * significantly. |
1392 | | * |
1393 | | * Results: |
1394 | | * predictor is passed out. |
1395 | | * |
1396 | | * Side effects: |
1397 | | * None. |
1398 | | * |
1399 | | *-------------------------------------------------------------- |
1400 | | */ |
1401 | | |
1402 | | inline int32 dng_lossless_decoder::QuickPredict (int32 col, |
1403 | | int32 curComp, |
1404 | | MCU *curRowBuf, |
1405 | | MCU *prevRowBuf) |
1406 | 0 | { |
1407 | | |
1408 | 0 | int32 diag = prevRowBuf [col - 1] [curComp]; |
1409 | 0 | int32 upper = prevRowBuf [col ] [curComp]; |
1410 | 0 | int32 left = curRowBuf [col - 1] [curComp]; |
1411 | |
|
1412 | 0 | switch (info.Ss) |
1413 | 0 | { |
1414 | | |
1415 | 0 | case 0: |
1416 | 0 | return 0; |
1417 | | |
1418 | 0 | case 1: |
1419 | 0 | return left; |
1420 | | |
1421 | 0 | case 2: |
1422 | 0 | return upper; |
1423 | | |
1424 | 0 | case 3: |
1425 | 0 | return diag; |
1426 | | |
1427 | 0 | case 4: |
1428 | 0 | return left + upper - diag; |
1429 | | |
1430 | 0 | case 5: |
1431 | 0 | return left + ((upper - diag) >> 1); |
1432 | | |
1433 | 0 | case 6: |
1434 | 0 | return upper + ((left - diag) >> 1); |
1435 | | |
1436 | 0 | case 7: |
1437 | 0 | return (left + upper) >> 1; |
1438 | | |
1439 | 0 | default: |
1440 | 0 | { |
1441 | 0 | ThrowBadFormat (); |
1442 | 0 | return 0; |
1443 | 0 | } |
1444 | | |
1445 | 0 | } |
1446 | | |
1447 | 0 | } |
1448 | | |
1449 | | /*****************************************************************************/ |
1450 | | |
1451 | | /* |
1452 | | *-------------------------------------------------------------- |
1453 | | * |
1454 | | * FillBitBuffer -- |
1455 | | * |
1456 | | * Load up the bit buffer with at least nbits |
1457 | | * Process any stuffed bytes at this time. |
1458 | | * |
1459 | | * Results: |
1460 | | * None |
1461 | | * |
1462 | | * Side effects: |
1463 | | * The bitwise global variables are updated. |
1464 | | * |
1465 | | *-------------------------------------------------------------- |
1466 | | */ |
1467 | | |
1468 | | inline void dng_lossless_decoder::FillBitBuffer (int32 nbits) |
1469 | 0 | { |
1470 | | |
1471 | 0 | const int32 kMinGetBits = sizeof (uint32) * 8 - 7; |
1472 | | |
1473 | 0 | #if qSupportHasselblad_3FR |
1474 | | |
1475 | 0 | if (fHasselblad3FR) |
1476 | 0 | { |
1477 | | |
1478 | 0 | while (bitsLeft < kMinGetBits) |
1479 | 0 | { |
1480 | | |
1481 | 0 | int32 c0 = GetJpegChar (); |
1482 | 0 | int32 c1 = GetJpegChar (); |
1483 | 0 | int32 c2 = GetJpegChar (); |
1484 | 0 | int32 c3 = GetJpegChar (); |
1485 | | |
1486 | 0 | getBuffer = (getBuffer << 8) | c3; |
1487 | 0 | getBuffer = (getBuffer << 8) | c2; |
1488 | 0 | getBuffer = (getBuffer << 8) | c1; |
1489 | 0 | getBuffer = (getBuffer << 8) | c0; |
1490 | | |
1491 | 0 | bitsLeft += 32; |
1492 | | |
1493 | 0 | } |
1494 | | |
1495 | 0 | return; |
1496 | | |
1497 | 0 | } |
1498 | | |
1499 | 0 | #endif |
1500 | | |
1501 | 0 | while (bitsLeft < kMinGetBits) |
1502 | 0 | { |
1503 | | |
1504 | 0 | int32 c = GetJpegChar (); |
1505 | | |
1506 | | // If it's 0xFF, check and discard stuffed zero byte |
1507 | |
|
1508 | 0 | if (c == 0xFF) |
1509 | 0 | { |
1510 | | |
1511 | 0 | int32 c2 = GetJpegChar (); |
1512 | | |
1513 | 0 | if (c2 != 0) |
1514 | 0 | { |
1515 | | |
1516 | | // Oops, it's actually a marker indicating end of |
1517 | | // compressed data. Better put it back for use later. |
1518 | |
|
1519 | 0 | UnGetJpegChar (); |
1520 | 0 | UnGetJpegChar (); |
1521 | | |
1522 | | // There should be enough bits still left in the data |
1523 | | // segment; if so, just break out of the while loop. |
1524 | |
|
1525 | 0 | if (bitsLeft >= nbits) |
1526 | 0 | break; |
1527 | | |
1528 | | // Uh-oh. Corrupted data: stuff zeroes into the data |
1529 | | // stream, since this sometimes occurs when we are on the |
1530 | | // last show_bits8 during decoding of the Huffman |
1531 | | // segment. |
1532 | | |
1533 | 0 | c = 0; |
1534 | | |
1535 | 0 | } |
1536 | | |
1537 | 0 | } |
1538 | | |
1539 | 0 | getBuffer = (getBuffer << 8) | c; |
1540 | | |
1541 | 0 | bitsLeft += 8; |
1542 | | |
1543 | 0 | } |
1544 | | |
1545 | 0 | } |
1546 | | |
1547 | | /*****************************************************************************/ |
1548 | | |
1549 | | inline int32 dng_lossless_decoder::show_bits8 () |
1550 | 0 | { |
1551 | | |
1552 | 0 | if (bitsLeft < 8) |
1553 | 0 | FillBitBuffer (8); |
1554 | | |
1555 | 0 | return (int32) ((getBuffer >> (bitsLeft - 8)) & 0xff); |
1556 | | |
1557 | 0 | } |
1558 | | |
1559 | | /*****************************************************************************/ |
1560 | | |
1561 | | inline void dng_lossless_decoder::flush_bits (int32 nbits) |
1562 | 0 | { |
1563 | | |
1564 | 0 | bitsLeft -= nbits; |
1565 | | |
1566 | 0 | } |
1567 | | |
1568 | | /*****************************************************************************/ |
1569 | | |
1570 | | inline int32 dng_lossless_decoder::get_bits (int32 nbits) |
1571 | 0 | { |
1572 | | |
1573 | 0 | if (nbits > 16) |
1574 | 0 | { |
1575 | 0 | ThrowBadFormat (); |
1576 | 0 | } |
1577 | | |
1578 | 0 | if (bitsLeft < nbits) |
1579 | 0 | FillBitBuffer (nbits); |
1580 | | |
1581 | 0 | return (int32) ((getBuffer >> (bitsLeft -= nbits)) & (0x0FFFF >> (16 - nbits))); |
1582 | | |
1583 | 0 | } |
1584 | | |
1585 | | /*****************************************************************************/ |
1586 | | |
1587 | | inline int32 dng_lossless_decoder::get_bit () |
1588 | 0 | { |
1589 | | |
1590 | 0 | if (!bitsLeft) |
1591 | 0 | FillBitBuffer (1); |
1592 | | |
1593 | 0 | return (int32) ((getBuffer >> (--bitsLeft)) & 1); |
1594 | | |
1595 | 0 | } |
1596 | | |
1597 | | /*****************************************************************************/ |
1598 | | |
1599 | | /* |
1600 | | *-------------------------------------------------------------- |
1601 | | * |
1602 | | * HuffDecode -- |
1603 | | * |
1604 | | * Taken from Figure F.16: extract next coded symbol from |
1605 | | * input stream. This should becode a macro. |
1606 | | * |
1607 | | * Results: |
1608 | | * Next coded symbol |
1609 | | * |
1610 | | * Side effects: |
1611 | | * Bitstream is parsed. |
1612 | | * |
1613 | | *-------------------------------------------------------------- |
1614 | | */ |
1615 | | |
1616 | | inline int32 dng_lossless_decoder::HuffDecode (HuffmanTable *htbl) |
1617 | 0 | { |
1618 | | |
1619 | | // If the huffman code is less than 8 bits, we can use the fast |
1620 | | // table lookup to get its value. It's more than 8 bits about |
1621 | | // 3-4% of the time. |
1622 | |
|
1623 | 0 | int32 code = show_bits8 (); |
1624 | | |
1625 | 0 | if (htbl->numbits [code]) |
1626 | 0 | { |
1627 | | |
1628 | 0 | flush_bits (htbl->numbits [code]); |
1629 | | |
1630 | 0 | return htbl->value [code]; |
1631 | | |
1632 | 0 | } |
1633 | | |
1634 | 0 | else |
1635 | 0 | { |
1636 | | |
1637 | 0 | flush_bits (8); |
1638 | | |
1639 | 0 | int32 l = 8; |
1640 | | |
1641 | 0 | while (code > htbl->maxcode [l]) |
1642 | 0 | { |
1643 | 0 | code = (code << 1) | get_bit (); |
1644 | 0 | l++; |
1645 | 0 | } |
1646 | | |
1647 | | // With garbage input we may reach the sentinel value l = 17. |
1648 | |
|
1649 | 0 | if (l > 16) |
1650 | 0 | { |
1651 | 0 | return 0; // fake a zero as the safest result |
1652 | 0 | } |
1653 | 0 | else |
1654 | 0 | { |
1655 | 0 | return htbl->huffval [htbl->valptr [l] + |
1656 | 0 | ((int32) (code - htbl->mincode [l]))]; |
1657 | 0 | } |
1658 | | |
1659 | 0 | } |
1660 | | |
1661 | 0 | } |
1662 | | |
1663 | | /*****************************************************************************/ |
1664 | | |
1665 | | /* |
1666 | | *-------------------------------------------------------------- |
1667 | | * |
1668 | | * HuffExtend -- |
1669 | | * |
1670 | | * Code and table for Figure F.12: extend sign bit |
1671 | | * |
1672 | | * Results: |
1673 | | * The extended value. |
1674 | | * |
1675 | | * Side effects: |
1676 | | * None. |
1677 | | * |
1678 | | *-------------------------------------------------------------- |
1679 | | */ |
1680 | | |
1681 | | inline void dng_lossless_decoder::HuffExtend (int32 &x, int32 s) |
1682 | 0 | { |
1683 | |
|
1684 | 0 | if (x < (0x08000 >> (16 - s))) |
1685 | 0 | { |
1686 | 0 | x += -(1 << s) + 1; |
1687 | 0 | } |
1688 | |
|
1689 | 0 | } |
1690 | | |
1691 | | /*****************************************************************************/ |
1692 | | |
1693 | | // Called from DecodeImage () to write one row. |
1694 | | |
1695 | | void dng_lossless_decoder::PmPutRow (MCU *buf, |
1696 | | int32 numComp, |
1697 | | int32 numCol, |
1698 | | int32 /* row */) |
1699 | 0 | { |
1700 | | |
1701 | 0 | uint16 *sPtr = &buf [0] [0]; |
1702 | | |
1703 | 0 | uint32 pixels = numCol * numComp; |
1704 | | |
1705 | 0 | fSpooler->Spool (sPtr, pixels * (uint32) sizeof (uint16)); |
1706 | | |
1707 | 0 | } |
1708 | | |
1709 | | /*****************************************************************************/ |
1710 | | |
1711 | | /* |
1712 | | *-------------------------------------------------------------- |
1713 | | * |
1714 | | * DecodeFirstRow -- |
1715 | | * |
1716 | | * Decode the first raster line of samples at the start of |
1717 | | * the scan and at the beginning of each restart interval. |
1718 | | * This includes modifying the component value so the real |
1719 | | * value, not the difference is returned. |
1720 | | * |
1721 | | * Results: |
1722 | | * None. |
1723 | | * |
1724 | | * Side effects: |
1725 | | * Bitstream is parsed. |
1726 | | * |
1727 | | *-------------------------------------------------------------- |
1728 | | */ |
1729 | | |
1730 | | void dng_lossless_decoder::DecodeFirstRow (MCU *curRowBuf) |
1731 | 0 | { |
1732 | | |
1733 | 0 | int32 compsInScan = info.compsInScan; |
1734 | | |
1735 | | // Process the first column in the row. |
1736 | |
|
1737 | 0 | for (int32 curComp = 0; curComp < compsInScan; curComp++) |
1738 | 0 | { |
1739 | | |
1740 | 0 | int32 ci = info.MCUmembership [curComp]; |
1741 | | |
1742 | 0 | JpegComponentInfo *compptr = info.curCompInfo [ci]; |
1743 | | |
1744 | 0 | HuffmanTable *dctbl = info.dcHuffTblPtrs [compptr->dcTblNo]; |
1745 | | |
1746 | | // Section F.2.2.1: decode the difference |
1747 | |
|
1748 | 0 | int32 d = 0; |
1749 | | |
1750 | 0 | int32 s = HuffDecode (dctbl); |
1751 | | |
1752 | 0 | if (s) |
1753 | 0 | { |
1754 | | |
1755 | 0 | if (s == 16 && !fBug16) |
1756 | 0 | { |
1757 | 0 | d = -32768; |
1758 | 0 | } |
1759 | | |
1760 | 0 | else |
1761 | 0 | { |
1762 | 0 | d = get_bits (s); |
1763 | 0 | HuffExtend (d, s); |
1764 | 0 | } |
1765 | |
|
1766 | 0 | } |
1767 | | |
1768 | | // Add the predictor to the difference. |
1769 | |
|
1770 | 0 | int32 Pr = info.dataPrecision; |
1771 | 0 | int32 Pt = info.Pt; |
1772 | | |
1773 | 0 | curRowBuf [0] [curComp] = (ComponentType) (d + (1 << (Pr-Pt-1))); |
1774 | | |
1775 | 0 | } |
1776 | | |
1777 | | // Process the rest of the row. |
1778 | | |
1779 | 0 | int32 numCOL = info.imageWidth; |
1780 | | |
1781 | 0 | for (int32 col = 1; col < numCOL; col++) |
1782 | 0 | { |
1783 | |
|
1784 | 0 | for (int32 curComp = 0; curComp < compsInScan; curComp++) |
1785 | 0 | { |
1786 | | |
1787 | 0 | int32 ci = info.MCUmembership [curComp]; |
1788 | | |
1789 | 0 | JpegComponentInfo *compptr = info.curCompInfo [ci]; |
1790 | | |
1791 | 0 | HuffmanTable *dctbl = info.dcHuffTblPtrs [compptr->dcTblNo]; |
1792 | | |
1793 | | // Section F.2.2.1: decode the difference |
1794 | |
|
1795 | 0 | int32 d = 0; |
1796 | | |
1797 | 0 | int32 s = HuffDecode (dctbl); |
1798 | | |
1799 | 0 | if (s) |
1800 | 0 | { |
1801 | |
|
1802 | 0 | if (s == 16 && !fBug16) |
1803 | 0 | { |
1804 | 0 | d = -32768; |
1805 | 0 | } |
1806 | | |
1807 | 0 | else |
1808 | 0 | { |
1809 | 0 | d = get_bits (s); |
1810 | 0 | HuffExtend (d, s); |
1811 | 0 | } |
1812 | |
|
1813 | 0 | } |
1814 | | |
1815 | | // Add the predictor to the difference. |
1816 | |
|
1817 | 0 | curRowBuf [col] [curComp] = (ComponentType) (d + curRowBuf [col-1] [curComp]); |
1818 | | |
1819 | 0 | } |
1820 | | |
1821 | 0 | } |
1822 | | |
1823 | | // Update the restart counter |
1824 | |
|
1825 | 0 | if (info.restartInRows) |
1826 | 0 | { |
1827 | 0 | info.restartRowsToGo--; |
1828 | 0 | } |
1829 | | |
1830 | 0 | } |
1831 | | |
1832 | | /*****************************************************************************/ |
1833 | | |
1834 | | /* |
1835 | | *-------------------------------------------------------------- |
1836 | | * |
1837 | | * DecodeImage -- |
1838 | | * |
1839 | | * Decode the input stream. This includes modifying |
1840 | | * the component value so the real value, not the |
1841 | | * difference is returned. |
1842 | | * |
1843 | | * Results: |
1844 | | * None. |
1845 | | * |
1846 | | * Side effects: |
1847 | | * Bitstream is parsed. |
1848 | | * |
1849 | | *-------------------------------------------------------------- |
1850 | | */ |
1851 | | |
1852 | | void dng_lossless_decoder::DecodeImage () |
1853 | 0 | { |
1854 | | |
1855 | 0 | #define swap(type,a,b) {type c; c=(a); (a)=(b); (b)=c;} |
1856 | |
|
1857 | 0 | int32 numCOL = info.imageWidth; |
1858 | 0 | int32 numROW = info.imageHeight; |
1859 | 0 | int32 compsInScan = info.compsInScan; |
1860 | | |
1861 | | // Precompute the decoding table for each table. |
1862 | | |
1863 | 0 | HuffmanTable *ht [4]; |
1864 | | |
1865 | 0 | for (int32 curComp = 0; curComp < compsInScan; curComp++) |
1866 | 0 | { |
1867 | | |
1868 | 0 | int32 ci = info.MCUmembership [curComp]; |
1869 | | |
1870 | 0 | JpegComponentInfo *compptr = info.curCompInfo [ci]; |
1871 | | |
1872 | 0 | ht [curComp] = info.dcHuffTblPtrs [compptr->dcTblNo]; |
1873 | |
|
1874 | 0 | } |
1875 | | |
1876 | 0 | MCU *prevRowBuf = mcuROW1; |
1877 | 0 | MCU *curRowBuf = mcuROW2; |
1878 | | |
1879 | 0 | #if qSupportCanon_sRAW |
1880 | | |
1881 | | // Canon sRAW support |
1882 | | |
1883 | 0 | if (info.compInfo [0].hSampFactor == 2 && |
1884 | 0 | info.compInfo [0].vSampFactor == 1) |
1885 | 0 | { |
1886 | | |
1887 | 0 | for (int32 row = 0; row < numROW; row++) |
1888 | 0 | { |
1889 | | |
1890 | | // Initialize predictors. |
1891 | | |
1892 | 0 | int32 p0; |
1893 | 0 | int32 p1; |
1894 | 0 | int32 p2; |
1895 | | |
1896 | 0 | if (row == 0) |
1897 | 0 | { |
1898 | 0 | p0 = 1 << 14; |
1899 | 0 | p1 = 1 << 14; |
1900 | 0 | p2 = 1 << 14; |
1901 | 0 | } |
1902 | | |
1903 | 0 | else |
1904 | 0 | { |
1905 | 0 | p0 = prevRowBuf [0] [0]; |
1906 | 0 | p1 = prevRowBuf [0] [1]; |
1907 | 0 | p2 = prevRowBuf [0] [2]; |
1908 | 0 | } |
1909 | | |
1910 | 0 | for (int32 col = 0; col < numCOL; col += 2) |
1911 | 0 | { |
1912 | | |
1913 | | // Read first luminance component. |
1914 | | |
1915 | 0 | { |
1916 | | |
1917 | 0 | int32 d = 0; |
1918 | | |
1919 | 0 | int32 s = HuffDecode (ht [0]); |
1920 | | |
1921 | 0 | if (s) |
1922 | 0 | { |
1923 | |
|
1924 | 0 | if (s == 16) |
1925 | 0 | { |
1926 | 0 | d = -32768; |
1927 | 0 | } |
1928 | | |
1929 | 0 | else |
1930 | 0 | { |
1931 | 0 | d = get_bits (s); |
1932 | 0 | HuffExtend (d, s); |
1933 | 0 | } |
1934 | |
|
1935 | 0 | } |
1936 | | |
1937 | 0 | p0 += d; |
1938 | | |
1939 | 0 | curRowBuf [col] [0] = (ComponentType) p0; |
1940 | | |
1941 | 0 | } |
1942 | | |
1943 | | // Read second luminance component. |
1944 | | |
1945 | 0 | { |
1946 | | |
1947 | 0 | int32 d = 0; |
1948 | | |
1949 | 0 | int32 s = HuffDecode (ht [0]); |
1950 | | |
1951 | 0 | if (s) |
1952 | 0 | { |
1953 | |
|
1954 | 0 | if (s == 16) |
1955 | 0 | { |
1956 | 0 | d = -32768; |
1957 | 0 | } |
1958 | | |
1959 | 0 | else |
1960 | 0 | { |
1961 | 0 | d = get_bits (s); |
1962 | 0 | HuffExtend (d, s); |
1963 | 0 | } |
1964 | |
|
1965 | 0 | } |
1966 | | |
1967 | 0 | p0 += d; |
1968 | | |
1969 | 0 | curRowBuf [col + 1] [0] = (ComponentType) p0; |
1970 | | |
1971 | 0 | } |
1972 | | |
1973 | | // Read first chroma component. |
1974 | | |
1975 | 0 | { |
1976 | | |
1977 | 0 | int32 d = 0; |
1978 | | |
1979 | 0 | int32 s = HuffDecode (ht [1]); |
1980 | | |
1981 | 0 | if (s) |
1982 | 0 | { |
1983 | |
|
1984 | 0 | if (s == 16) |
1985 | 0 | { |
1986 | 0 | d = -32768; |
1987 | 0 | } |
1988 | | |
1989 | 0 | else |
1990 | 0 | { |
1991 | 0 | d = get_bits (s); |
1992 | 0 | HuffExtend (d, s); |
1993 | 0 | } |
1994 | |
|
1995 | 0 | } |
1996 | | |
1997 | 0 | p1 += d; |
1998 | | |
1999 | 0 | curRowBuf [col ] [1] = (ComponentType) p1; |
2000 | 0 | curRowBuf [col + 1] [1] = (ComponentType) p1; |
2001 | | |
2002 | 0 | } |
2003 | | |
2004 | | // Read second chroma component. |
2005 | | |
2006 | 0 | { |
2007 | | |
2008 | 0 | int32 d = 0; |
2009 | | |
2010 | 0 | int32 s = HuffDecode (ht [2]); |
2011 | | |
2012 | 0 | if (s) |
2013 | 0 | { |
2014 | |
|
2015 | 0 | if (s == 16) |
2016 | 0 | { |
2017 | 0 | d = -32768; |
2018 | 0 | } |
2019 | | |
2020 | 0 | else |
2021 | 0 | { |
2022 | 0 | d = get_bits (s); |
2023 | 0 | HuffExtend (d, s); |
2024 | 0 | } |
2025 | |
|
2026 | 0 | } |
2027 | | |
2028 | 0 | p2 += d; |
2029 | | |
2030 | 0 | curRowBuf [col ] [2] = (ComponentType) p2; |
2031 | 0 | curRowBuf [col + 1] [2] = (ComponentType) p2; |
2032 | | |
2033 | 0 | } |
2034 | | |
2035 | 0 | } |
2036 | | |
2037 | 0 | PmPutRow (curRowBuf, compsInScan, numCOL, row); |
2038 | |
|
2039 | 0 | swap (MCU *, prevRowBuf, curRowBuf); |
2040 | | |
2041 | 0 | } |
2042 | | |
2043 | 0 | return; |
2044 | | |
2045 | 0 | } |
2046 | | |
2047 | 0 | if (info.compInfo [0].hSampFactor == 2 && |
2048 | 0 | info.compInfo [0].vSampFactor == 2) |
2049 | 0 | { |
2050 | | |
2051 | 0 | for (int32 row = 0; row < numROW; row += 2) |
2052 | 0 | { |
2053 | | |
2054 | | // Initialize predictors. |
2055 | | |
2056 | 0 | int32 p0; |
2057 | 0 | int32 p1; |
2058 | 0 | int32 p2; |
2059 | | |
2060 | 0 | if (row == 0) |
2061 | 0 | { |
2062 | 0 | p0 = 1 << 14; |
2063 | 0 | p1 = 1 << 14; |
2064 | 0 | p2 = 1 << 14; |
2065 | 0 | } |
2066 | | |
2067 | 0 | else |
2068 | 0 | { |
2069 | 0 | p0 = prevRowBuf [0] [0]; |
2070 | 0 | p1 = prevRowBuf [0] [1]; |
2071 | 0 | p2 = prevRowBuf [0] [2]; |
2072 | 0 | } |
2073 | | |
2074 | 0 | for (int32 col = 0; col < numCOL; col += 2) |
2075 | 0 | { |
2076 | | |
2077 | | // Read first luminance component. |
2078 | | |
2079 | 0 | { |
2080 | | |
2081 | 0 | int32 d = 0; |
2082 | | |
2083 | 0 | int32 s = HuffDecode (ht [0]); |
2084 | | |
2085 | 0 | if (s) |
2086 | 0 | { |
2087 | |
|
2088 | 0 | if (s == 16) |
2089 | 0 | { |
2090 | 0 | d = -32768; |
2091 | 0 | } |
2092 | | |
2093 | 0 | else |
2094 | 0 | { |
2095 | 0 | d = get_bits (s); |
2096 | 0 | HuffExtend (d, s); |
2097 | 0 | } |
2098 | |
|
2099 | 0 | } |
2100 | | |
2101 | 0 | p0 += d; |
2102 | | |
2103 | 0 | prevRowBuf [col] [0] = (ComponentType) p0; |
2104 | | |
2105 | 0 | } |
2106 | | |
2107 | | // Read second luminance component. |
2108 | | |
2109 | 0 | { |
2110 | | |
2111 | 0 | int32 d = 0; |
2112 | | |
2113 | 0 | int32 s = HuffDecode (ht [0]); |
2114 | | |
2115 | 0 | if (s) |
2116 | 0 | { |
2117 | |
|
2118 | 0 | if (s == 16) |
2119 | 0 | { |
2120 | 0 | d = -32768; |
2121 | 0 | } |
2122 | | |
2123 | 0 | else |
2124 | 0 | { |
2125 | 0 | d = get_bits (s); |
2126 | 0 | HuffExtend (d, s); |
2127 | 0 | } |
2128 | |
|
2129 | 0 | } |
2130 | | |
2131 | 0 | p0 += d; |
2132 | | |
2133 | 0 | prevRowBuf [col + 1] [0] = (ComponentType) p0; |
2134 | | |
2135 | 0 | } |
2136 | | |
2137 | | // Read third luminance component. |
2138 | | |
2139 | 0 | { |
2140 | | |
2141 | 0 | int32 d = 0; |
2142 | | |
2143 | 0 | int32 s = HuffDecode (ht [0]); |
2144 | | |
2145 | 0 | if (s) |
2146 | 0 | { |
2147 | |
|
2148 | 0 | if (s == 16) |
2149 | 0 | { |
2150 | 0 | d = -32768; |
2151 | 0 | } |
2152 | | |
2153 | 0 | else |
2154 | 0 | { |
2155 | 0 | d = get_bits (s); |
2156 | 0 | HuffExtend (d, s); |
2157 | 0 | } |
2158 | |
|
2159 | 0 | } |
2160 | | |
2161 | 0 | p0 += d; |
2162 | | |
2163 | 0 | curRowBuf [col] [0] = (ComponentType) p0; |
2164 | | |
2165 | 0 | } |
2166 | | |
2167 | | // Read fourth luminance component. |
2168 | | |
2169 | 0 | { |
2170 | | |
2171 | 0 | int32 d = 0; |
2172 | | |
2173 | 0 | int32 s = HuffDecode (ht [0]); |
2174 | | |
2175 | 0 | if (s) |
2176 | 0 | { |
2177 | |
|
2178 | 0 | if (s == 16) |
2179 | 0 | { |
2180 | 0 | d = -32768; |
2181 | 0 | } |
2182 | | |
2183 | 0 | else |
2184 | 0 | { |
2185 | 0 | d = get_bits (s); |
2186 | 0 | HuffExtend (d, s); |
2187 | 0 | } |
2188 | |
|
2189 | 0 | } |
2190 | | |
2191 | 0 | p0 += d; |
2192 | | |
2193 | 0 | curRowBuf [col + 1] [0] = (ComponentType) p0; |
2194 | | |
2195 | 0 | } |
2196 | | |
2197 | | // Read first chroma component. |
2198 | | |
2199 | 0 | { |
2200 | | |
2201 | 0 | int32 d = 0; |
2202 | | |
2203 | 0 | int32 s = HuffDecode (ht [1]); |
2204 | | |
2205 | 0 | if (s) |
2206 | 0 | { |
2207 | |
|
2208 | 0 | if (s == 16) |
2209 | 0 | { |
2210 | 0 | d = -32768; |
2211 | 0 | } |
2212 | | |
2213 | 0 | else |
2214 | 0 | { |
2215 | 0 | d = get_bits (s); |
2216 | 0 | HuffExtend (d, s); |
2217 | 0 | } |
2218 | |
|
2219 | 0 | } |
2220 | | |
2221 | 0 | p1 += d; |
2222 | | |
2223 | 0 | prevRowBuf [col ] [1] = (ComponentType) p1; |
2224 | 0 | prevRowBuf [col + 1] [1] = (ComponentType) p1; |
2225 | |
|
2226 | 0 | curRowBuf [col ] [1] = (ComponentType) p1; |
2227 | 0 | curRowBuf [col + 1] [1] = (ComponentType) p1; |
2228 | | |
2229 | 0 | } |
2230 | | |
2231 | | // Read second chroma component. |
2232 | | |
2233 | 0 | { |
2234 | | |
2235 | 0 | int32 d = 0; |
2236 | | |
2237 | 0 | int32 s = HuffDecode (ht [2]); |
2238 | | |
2239 | 0 | if (s) |
2240 | 0 | { |
2241 | |
|
2242 | 0 | if (s == 16) |
2243 | 0 | { |
2244 | 0 | d = -32768; |
2245 | 0 | } |
2246 | | |
2247 | 0 | else |
2248 | 0 | { |
2249 | 0 | d = get_bits (s); |
2250 | 0 | HuffExtend (d, s); |
2251 | 0 | } |
2252 | |
|
2253 | 0 | } |
2254 | | |
2255 | 0 | p2 += d; |
2256 | | |
2257 | 0 | prevRowBuf [col ] [2] = (ComponentType) p2; |
2258 | 0 | prevRowBuf [col + 1] [2] = (ComponentType) p2; |
2259 | | |
2260 | 0 | curRowBuf [col ] [2] = (ComponentType) p2; |
2261 | 0 | curRowBuf [col + 1] [2] = (ComponentType) p2; |
2262 | | |
2263 | 0 | } |
2264 | | |
2265 | 0 | } |
2266 | | |
2267 | 0 | PmPutRow (prevRowBuf, compsInScan, numCOL, row); |
2268 | 0 | PmPutRow (curRowBuf, compsInScan, numCOL, row); |
2269 | |
|
2270 | 0 | } |
2271 | | |
2272 | 0 | return; |
2273 | | |
2274 | 0 | } |
2275 | | |
2276 | 0 | #endif |
2277 | | |
2278 | 0 | #if qSupportHasselblad_3FR |
2279 | | |
2280 | 0 | if (info.Ss == 8) |
2281 | 0 | { |
2282 | | |
2283 | 0 | fHasselblad3FR = true; |
2284 | | |
2285 | 0 | for (int32 row = 0; row < numROW; row++) |
2286 | 0 | { |
2287 | | |
2288 | 0 | int32 p0 = 32768; |
2289 | 0 | int32 p1 = 32768; |
2290 | | |
2291 | 0 | for (int32 col = 0; col < numCOL; col += 2) |
2292 | 0 | { |
2293 | | |
2294 | 0 | int32 s0 = HuffDecode (ht [0]); |
2295 | 0 | int32 s1 = HuffDecode (ht [0]); |
2296 | | |
2297 | 0 | if (s0) |
2298 | 0 | { |
2299 | 0 | int32 d = get_bits (s0); |
2300 | 0 | if (s0 == 16) |
2301 | 0 | { |
2302 | 0 | d = -32768; |
2303 | 0 | } |
2304 | 0 | else |
2305 | 0 | { |
2306 | 0 | HuffExtend (d, s0); |
2307 | 0 | } |
2308 | 0 | p0 += d; |
2309 | 0 | } |
2310 | |
|
2311 | 0 | if (s1) |
2312 | 0 | { |
2313 | 0 | int32 d = get_bits (s1); |
2314 | 0 | if (s1 == 16) |
2315 | 0 | { |
2316 | 0 | d = -32768; |
2317 | 0 | } |
2318 | 0 | else |
2319 | 0 | { |
2320 | 0 | HuffExtend (d, s1); |
2321 | 0 | } |
2322 | 0 | p1 += d; |
2323 | 0 | } |
2324 | |
|
2325 | 0 | curRowBuf [col ] [0] = (ComponentType) p0; |
2326 | 0 | curRowBuf [col + 1] [0] = (ComponentType) p1; |
2327 | | |
2328 | 0 | } |
2329 | | |
2330 | 0 | PmPutRow (curRowBuf, compsInScan, numCOL, row); |
2331 | |
|
2332 | 0 | } |
2333 | |
|
2334 | 0 | return; |
2335 | | |
2336 | 0 | } |
2337 | | |
2338 | 0 | #endif |
2339 | | |
2340 | | // Decode the first row of image. Output the row and |
2341 | | // turn this row into a previous row for later predictor |
2342 | | // calculation. |
2343 | | |
2344 | 0 | DecodeFirstRow (mcuROW1); |
2345 | | |
2346 | 0 | PmPutRow (mcuROW1, compsInScan, numCOL, 0); |
2347 | | |
2348 | | // Process each row. |
2349 | |
|
2350 | 0 | for (int32 row = 1; row < numROW; row++) |
2351 | 0 | { |
2352 | | |
2353 | | // Account for restart interval, process restart marker if needed. |
2354 | |
|
2355 | 0 | if (info.restartInRows) |
2356 | 0 | { |
2357 | | |
2358 | 0 | if (info.restartRowsToGo == 0) |
2359 | 0 | { |
2360 | | |
2361 | 0 | ProcessRestart (); |
2362 | | |
2363 | | // Reset predictors at restart. |
2364 | | |
2365 | 0 | DecodeFirstRow (curRowBuf); |
2366 | | |
2367 | 0 | PmPutRow (curRowBuf, compsInScan, numCOL, row); |
2368 | | |
2369 | 0 | swap (MCU *, prevRowBuf, curRowBuf); |
2370 | | |
2371 | 0 | continue; |
2372 | | |
2373 | 0 | } |
2374 | | |
2375 | 0 | info.restartRowsToGo--; |
2376 | | |
2377 | 0 | } |
2378 | | |
2379 | | // The upper neighbors are predictors for the first column. |
2380 | |
|
2381 | 0 | for (int32 curComp = 0; curComp < compsInScan; curComp++) |
2382 | 0 | { |
2383 | | |
2384 | | // Section F.2.2.1: decode the difference |
2385 | |
|
2386 | 0 | int32 d = 0; |
2387 | | |
2388 | 0 | int32 s = HuffDecode (ht [curComp]); |
2389 | | |
2390 | 0 | if (s) |
2391 | 0 | { |
2392 | |
|
2393 | 0 | if (s == 16 && !fBug16) |
2394 | 0 | { |
2395 | 0 | d = -32768; |
2396 | 0 | } |
2397 | | |
2398 | 0 | else |
2399 | 0 | { |
2400 | 0 | d = get_bits (s); |
2401 | 0 | HuffExtend (d, s); |
2402 | 0 | } |
2403 | |
|
2404 | 0 | } |
2405 | | |
2406 | | // First column of row above is predictor for first column. |
2407 | |
|
2408 | 0 | curRowBuf [0] [curComp] = (ComponentType) (d + prevRowBuf [0] [curComp]); |
2409 | | |
2410 | 0 | } |
2411 | | |
2412 | | // For the rest of the column on this row, predictor |
2413 | | // calculations are based on PSV. |
2414 | |
|
2415 | 0 | if (compsInScan == 2 && info.Ss == 1) |
2416 | 0 | { |
2417 | | |
2418 | | // This is the combination used by both the Canon and Kodak raw formats. |
2419 | | // Unrolling the general case logic results in a significant speed increase. |
2420 | | |
2421 | 0 | uint16 *dPtr = &curRowBuf [1] [0]; |
2422 | | |
2423 | 0 | int32 prev0 = dPtr [-2]; |
2424 | 0 | int32 prev1 = dPtr [-1]; |
2425 | | |
2426 | 0 | for (int32 col = 1; col < numCOL; col++) |
2427 | 0 | { |
2428 | | |
2429 | 0 | int32 s = HuffDecode (ht [0]); |
2430 | | |
2431 | 0 | if (s) |
2432 | 0 | { |
2433 | | |
2434 | 0 | int32 d; |
2435 | |
|
2436 | 0 | if (s == 16 && !fBug16) |
2437 | 0 | { |
2438 | 0 | d = -32768; |
2439 | 0 | } |
2440 | | |
2441 | 0 | else |
2442 | 0 | { |
2443 | 0 | d = get_bits (s); |
2444 | 0 | HuffExtend (d, s); |
2445 | 0 | } |
2446 | |
|
2447 | 0 | prev0 += d; |
2448 | | |
2449 | 0 | } |
2450 | | |
2451 | 0 | s = HuffDecode (ht [1]); |
2452 | | |
2453 | 0 | if (s) |
2454 | 0 | { |
2455 | | |
2456 | 0 | int32 d; |
2457 | |
|
2458 | 0 | if (s == 16 && !fBug16) |
2459 | 0 | { |
2460 | 0 | d = -32768; |
2461 | 0 | } |
2462 | | |
2463 | 0 | else |
2464 | 0 | { |
2465 | 0 | d = get_bits (s); |
2466 | 0 | HuffExtend (d, s); |
2467 | 0 | } |
2468 | |
|
2469 | 0 | prev1 += d; |
2470 | | |
2471 | 0 | } |
2472 | | |
2473 | 0 | dPtr [0] = (uint16) prev0; |
2474 | 0 | dPtr [1] = (uint16) prev1; |
2475 | | |
2476 | 0 | dPtr += 2; |
2477 | | |
2478 | 0 | } |
2479 | | |
2480 | 0 | } |
2481 | | |
2482 | 0 | else |
2483 | 0 | { |
2484 | | |
2485 | 0 | for (int32 col = 1; col < numCOL; col++) |
2486 | 0 | { |
2487 | | |
2488 | 0 | for (int32 curComp = 0; curComp < compsInScan; curComp++) |
2489 | 0 | { |
2490 | | |
2491 | | // Section F.2.2.1: decode the difference |
2492 | |
|
2493 | 0 | int32 d = 0; |
2494 | | |
2495 | 0 | int32 s = HuffDecode (ht [curComp]); |
2496 | | |
2497 | 0 | if (s) |
2498 | 0 | { |
2499 | | |
2500 | 0 | if (s == 16 && !fBug16) |
2501 | 0 | { |
2502 | 0 | d = -32768; |
2503 | 0 | } |
2504 | | |
2505 | 0 | else |
2506 | 0 | { |
2507 | 0 | d = get_bits (s); |
2508 | 0 | HuffExtend (d, s); |
2509 | 0 | } |
2510 | |
|
2511 | 0 | } |
2512 | | |
2513 | | // Predict the pixel value. |
2514 | | |
2515 | 0 | int32 predictor = QuickPredict (col, |
2516 | 0 | curComp, |
2517 | 0 | curRowBuf, |
2518 | 0 | prevRowBuf); |
2519 | | |
2520 | | // Save the difference. |
2521 | |
|
2522 | 0 | curRowBuf [col] [curComp] = (ComponentType) (d + predictor); |
2523 | | |
2524 | 0 | } |
2525 | | |
2526 | 0 | } |
2527 | |
|
2528 | 0 | } |
2529 | |
|
2530 | 0 | PmPutRow (curRowBuf, compsInScan, numCOL, row); |
2531 | | |
2532 | 0 | swap (MCU *, prevRowBuf, curRowBuf); |
2533 | | |
2534 | 0 | } |
2535 | | |
2536 | 0 | #undef swap |
2537 | | |
2538 | 0 | } |
2539 | | |
2540 | | /*****************************************************************************/ |
2541 | | |
2542 | | void dng_lossless_decoder::StartRead (uint32 &imageWidth, |
2543 | | uint32 &imageHeight, |
2544 | | uint32 &imageChannels) |
2545 | 0 | { |
2546 | | |
2547 | 0 | ReadFileHeader (); |
2548 | 0 | ReadScanHeader (); |
2549 | 0 | DecoderStructInit (); |
2550 | 0 | HuffDecoderInit (); |
2551 | | |
2552 | 0 | imageWidth = info.imageWidth; |
2553 | 0 | imageHeight = info.imageHeight; |
2554 | 0 | imageChannels = info.compsInScan; |
2555 | | |
2556 | 0 | } |
2557 | | |
2558 | | /*****************************************************************************/ |
2559 | | |
2560 | | void dng_lossless_decoder::FinishRead () |
2561 | 0 | { |
2562 | | |
2563 | 0 | DecodeImage (); |
2564 | | |
2565 | 0 | } |
2566 | | |
2567 | | /*****************************************************************************/ |
2568 | | |
2569 | | void DecodeLosslessJPEG (dng_stream &stream, |
2570 | | dng_spooler &spooler, |
2571 | | uint32 minDecodedSize, |
2572 | | uint32 maxDecodedSize, |
2573 | | bool bug16) |
2574 | 0 | { |
2575 | | |
2576 | 0 | dng_lossless_decoder decoder (&stream, |
2577 | 0 | &spooler, |
2578 | 0 | bug16); |
2579 | | |
2580 | 0 | uint32 imageWidth; |
2581 | 0 | uint32 imageHeight; |
2582 | 0 | uint32 imageChannels; |
2583 | | |
2584 | 0 | decoder.StartRead (imageWidth, |
2585 | 0 | imageHeight, |
2586 | 0 | imageChannels); |
2587 | | |
2588 | 0 | uint32 decodedSize = imageWidth * |
2589 | 0 | imageHeight * |
2590 | 0 | imageChannels * |
2591 | 0 | (uint32) sizeof (uint16); |
2592 | | |
2593 | 0 | if (decodedSize < minDecodedSize || |
2594 | 0 | decodedSize > maxDecodedSize) |
2595 | 0 | { |
2596 | 0 | ThrowBadFormat (); |
2597 | 0 | } |
2598 | | |
2599 | 0 | decoder.FinishRead (); |
2600 | | |
2601 | 0 | } |
2602 | | |
2603 | | /*****************************************************************************/ |
2604 | | |
2605 | | class dng_lossless_encoder |
2606 | | { |
2607 | | |
2608 | | private: |
2609 | | |
2610 | | const uint16 *fSrcData; |
2611 | | |
2612 | | uint32 fSrcRows; |
2613 | | uint32 fSrcCols; |
2614 | | uint32 fSrcChannels; |
2615 | | uint32 fSrcBitDepth; |
2616 | | |
2617 | | int32 fSrcRowStep; |
2618 | | int32 fSrcColStep; |
2619 | | |
2620 | | dng_stream &fStream; |
2621 | | |
2622 | | HuffmanTable huffTable [4]; |
2623 | | |
2624 | | uint32 freqCount [4] [257]; |
2625 | | |
2626 | | // Current bit-accumulation buffer |
2627 | | |
2628 | | int32 huffPutBuffer; |
2629 | | int32 huffPutBits; |
2630 | | |
2631 | | // Lookup table for number of bits in an 8 bit value. |
2632 | | |
2633 | | int numBitsTable [256]; |
2634 | | |
2635 | | public: |
2636 | | |
2637 | | dng_lossless_encoder (const uint16 *srcData, |
2638 | | uint32 srcRows, |
2639 | | uint32 srcCols, |
2640 | | uint32 srcChannels, |
2641 | | uint32 srcBitDepth, |
2642 | | int32 srcRowStep, |
2643 | | int32 srcColStep, |
2644 | | dng_stream &stream); |
2645 | | |
2646 | | void Encode (); |
2647 | | |
2648 | | private: |
2649 | | |
2650 | | void EmitByte (uint8 value); |
2651 | | |
2652 | | void EmitBits (int code, int size); |
2653 | | |
2654 | | void FlushBits (); |
2655 | | |
2656 | | void CountOneDiff (int diff, uint32 *countTable); |
2657 | | |
2658 | | void EncodeOneDiff (int diff, HuffmanTable *dctbl); |
2659 | | |
2660 | | void FreqCountSet (); |
2661 | | |
2662 | | void HuffEncode (); |
2663 | | |
2664 | | void GenHuffCoding (HuffmanTable *htbl, uint32 *freq); |
2665 | | |
2666 | | void HuffOptimize (); |
2667 | | |
2668 | | void EmitMarker (JpegMarker mark); |
2669 | | |
2670 | | void Emit2bytes (int value); |
2671 | | |
2672 | | void EmitDht (int index); |
2673 | | |
2674 | | void EmitSof (JpegMarker code); |
2675 | | |
2676 | | void EmitSos (); |
2677 | | |
2678 | | void WriteFileHeader (); |
2679 | | |
2680 | | void WriteScanHeader (); |
2681 | | |
2682 | | void WriteFileTrailer (); |
2683 | | |
2684 | | }; |
2685 | | |
2686 | | /*****************************************************************************/ |
2687 | | |
2688 | | dng_lossless_encoder::dng_lossless_encoder (const uint16 *srcData, |
2689 | | uint32 srcRows, |
2690 | | uint32 srcCols, |
2691 | | uint32 srcChannels, |
2692 | | uint32 srcBitDepth, |
2693 | | int32 srcRowStep, |
2694 | | int32 srcColStep, |
2695 | | dng_stream &stream) |
2696 | | |
2697 | | : fSrcData (srcData ) |
2698 | | , fSrcRows (srcRows ) |
2699 | | , fSrcCols (srcCols ) |
2700 | | , fSrcChannels (srcChannels) |
2701 | | , fSrcBitDepth (srcBitDepth) |
2702 | | , fSrcRowStep (srcRowStep ) |
2703 | | , fSrcColStep (srcColStep ) |
2704 | | , fStream (stream ) |
2705 | | |
2706 | | , huffPutBuffer (0) |
2707 | | , huffPutBits (0) |
2708 | | |
2709 | 0 | { |
2710 | | |
2711 | | // Initialize number of bits lookup table. |
2712 | | |
2713 | 0 | numBitsTable [0] = 0; |
2714 | | |
2715 | 0 | for (int i = 1; i < 256; i++) |
2716 | 0 | { |
2717 | | |
2718 | 0 | int temp = i; |
2719 | 0 | int nbits = 1; |
2720 | | |
2721 | 0 | while (temp >>= 1) |
2722 | 0 | { |
2723 | 0 | nbits++; |
2724 | 0 | } |
2725 | | |
2726 | 0 | numBitsTable [i] = nbits; |
2727 | | |
2728 | 0 | } |
2729 | | |
2730 | 0 | } |
2731 | | |
2732 | | /*****************************************************************************/ |
2733 | | |
2734 | | inline void dng_lossless_encoder::EmitByte (uint8 value) |
2735 | 0 | { |
2736 | | |
2737 | 0 | fStream.Put_uint8 (value); |
2738 | | |
2739 | 0 | } |
2740 | | |
2741 | | /*****************************************************************************/ |
2742 | | |
2743 | | /* |
2744 | | *-------------------------------------------------------------- |
2745 | | * |
2746 | | * EmitBits -- |
2747 | | * |
2748 | | * Code for outputting bits to the file |
2749 | | * |
2750 | | * Only the right 24 bits of huffPutBuffer are used; the valid |
2751 | | * bits are left-justified in this part. At most 16 bits can be |
2752 | | * passed to EmitBits in one call, and we never retain more than 7 |
2753 | | * bits in huffPutBuffer between calls, so 24 bits are |
2754 | | * sufficient. |
2755 | | * |
2756 | | * Results: |
2757 | | * None. |
2758 | | * |
2759 | | * Side effects: |
2760 | | * huffPutBuffer and huffPutBits are updated. |
2761 | | * |
2762 | | *-------------------------------------------------------------- |
2763 | | */ |
2764 | | |
2765 | | inline void dng_lossless_encoder::EmitBits (int code, int size) |
2766 | 0 | { |
2767 | | |
2768 | 0 | DNG_ASSERT (size != 0, "Bad Huffman table entry"); |
2769 | |
|
2770 | 0 | int putBits = size; |
2771 | 0 | int putBuffer = code; |
2772 | | |
2773 | 0 | putBits += huffPutBits; |
2774 | | |
2775 | 0 | putBuffer <<= 24 - putBits; |
2776 | 0 | putBuffer |= huffPutBuffer; |
2777 | |
|
2778 | 0 | while (putBits >= 8) |
2779 | 0 | { |
2780 | | |
2781 | 0 | uint8 c = (uint8) (putBuffer >> 16); |
2782 | | |
2783 | | // Output whole bytes we've accumulated with byte stuffing |
2784 | |
|
2785 | 0 | EmitByte (c); |
2786 | | |
2787 | 0 | if (c == 0xFF) |
2788 | 0 | { |
2789 | 0 | EmitByte (0); |
2790 | 0 | } |
2791 | |
|
2792 | 0 | putBuffer <<= 8; |
2793 | 0 | putBits -= 8; |
2794 | | |
2795 | 0 | } |
2796 | |
|
2797 | 0 | huffPutBuffer = putBuffer; |
2798 | 0 | huffPutBits = putBits; |
2799 | | |
2800 | 0 | } |
2801 | | |
2802 | | /*****************************************************************************/ |
2803 | | |
2804 | | /* |
2805 | | *-------------------------------------------------------------- |
2806 | | * |
2807 | | * FlushBits -- |
2808 | | * |
2809 | | * Flush any remaining bits in the bit buffer. Used before emitting |
2810 | | * a marker. |
2811 | | * |
2812 | | * Results: |
2813 | | * None. |
2814 | | * |
2815 | | * Side effects: |
2816 | | * huffPutBuffer and huffPutBits are reset |
2817 | | * |
2818 | | *-------------------------------------------------------------- |
2819 | | */ |
2820 | | |
2821 | | void dng_lossless_encoder::FlushBits () |
2822 | 0 | { |
2823 | | |
2824 | | // The first call forces output of any partial bytes. |
2825 | |
|
2826 | 0 | EmitBits (0x007F, 7); |
2827 | | |
2828 | | // We can then zero the buffer. |
2829 | |
|
2830 | 0 | huffPutBuffer = 0; |
2831 | 0 | huffPutBits = 0; |
2832 | | |
2833 | 0 | } |
2834 | | |
2835 | | /*****************************************************************************/ |
2836 | | |
2837 | | /* |
2838 | | *-------------------------------------------------------------- |
2839 | | * |
2840 | | * CountOneDiff -- |
2841 | | * |
2842 | | * Count the difference value in countTable. |
2843 | | * |
2844 | | * Results: |
2845 | | * diff is counted in countTable. |
2846 | | * |
2847 | | * Side effects: |
2848 | | * None. |
2849 | | * |
2850 | | *-------------------------------------------------------------- |
2851 | | */ |
2852 | | |
2853 | | inline void dng_lossless_encoder::CountOneDiff (int diff, uint32 *countTable) |
2854 | 0 | { |
2855 | | |
2856 | | // Encode the DC coefficient difference per section F.1.2.1 |
2857 | | |
2858 | 0 | int temp = diff; |
2859 | | |
2860 | 0 | if (temp < 0) |
2861 | 0 | { |
2862 | | |
2863 | 0 | temp = -temp; |
2864 | | |
2865 | 0 | } |
2866 | | |
2867 | | // Find the number of bits needed for the magnitude of the coefficient |
2868 | |
|
2869 | 0 | int nbits = temp >= 256 ? numBitsTable [temp >> 8 ] + 8 |
2870 | 0 | : numBitsTable [temp & 0xFF]; |
2871 | | |
2872 | | // Update count for this bit length |
2873 | |
|
2874 | 0 | countTable [nbits] ++; |
2875 | | |
2876 | 0 | } |
2877 | | |
2878 | | /*****************************************************************************/ |
2879 | | |
2880 | | /* |
2881 | | *-------------------------------------------------------------- |
2882 | | * |
2883 | | * EncodeOneDiff -- |
2884 | | * |
2885 | | * Encode a single difference value. |
2886 | | * |
2887 | | * Results: |
2888 | | * None. |
2889 | | * |
2890 | | * Side effects: |
2891 | | * None. |
2892 | | * |
2893 | | *-------------------------------------------------------------- |
2894 | | */ |
2895 | | |
2896 | | inline void dng_lossless_encoder::EncodeOneDiff (int diff, HuffmanTable *dctbl) |
2897 | 0 | { |
2898 | | |
2899 | | // Encode the DC coefficient difference per section F.1.2.1 |
2900 | | |
2901 | 0 | int temp = diff; |
2902 | 0 | int temp2 = diff; |
2903 | | |
2904 | 0 | if (temp < 0) |
2905 | 0 | { |
2906 | | |
2907 | 0 | temp = -temp; |
2908 | | |
2909 | | // For a negative input, want temp2 = bitwise complement of |
2910 | | // abs (input). This code assumes we are on a two's complement |
2911 | | // machine. |
2912 | |
|
2913 | 0 | temp2--; |
2914 | | |
2915 | 0 | } |
2916 | | |
2917 | | // Find the number of bits needed for the magnitude of the coefficient |
2918 | |
|
2919 | 0 | int nbits = temp >= 256 ? numBitsTable [temp >> 8 ] + 8 |
2920 | 0 | : numBitsTable [temp & 0xFF]; |
2921 | | |
2922 | | // Emit the Huffman-coded symbol for the number of bits |
2923 | |
|
2924 | 0 | EmitBits (dctbl->ehufco [nbits], |
2925 | 0 | dctbl->ehufsi [nbits]); |
2926 | | |
2927 | | // Emit that number of bits of the value, if positive, |
2928 | | // or the complement of its magnitude, if negative. |
2929 | | |
2930 | | // If the number of bits is 16, there is only one possible difference |
2931 | | // value (-32786), so the lossless JPEG spec says not to output anything |
2932 | | // in that case. So we only need to output the diference value if |
2933 | | // the number of bits is between 1 and 15. |
2934 | |
|
2935 | 0 | if (nbits & 15) |
2936 | 0 | { |
2937 | | |
2938 | 0 | EmitBits (temp2 & (0x0FFFF >> (16 - nbits)), |
2939 | 0 | nbits); |
2940 | | |
2941 | 0 | } |
2942 | |
|
2943 | 0 | } |
2944 | | |
2945 | | /*****************************************************************************/ |
2946 | | |
2947 | | /* |
2948 | | *-------------------------------------------------------------- |
2949 | | * |
2950 | | * FreqCountSet -- |
2951 | | * |
2952 | | * Count the times each category symbol occurs in this image. |
2953 | | * |
2954 | | * Results: |
2955 | | * None. |
2956 | | * |
2957 | | * Side effects: |
2958 | | * The freqCount has counted all category |
2959 | | * symbols appeared in the image. |
2960 | | * |
2961 | | *-------------------------------------------------------------- |
2962 | | */ |
2963 | | |
2964 | | void dng_lossless_encoder::FreqCountSet () |
2965 | 0 | { |
2966 | | |
2967 | 0 | memset (freqCount, 0, sizeof (freqCount)); |
2968 | | |
2969 | 0 | DNG_ASSERT ((int32)fSrcRows >= 0, "dng_lossless_encoder::FreqCountSet: fSrcRpws too large."); |
2970 | |
|
2971 | 0 | for (int32 row = 0; row < (int32)fSrcRows; row++) |
2972 | 0 | { |
2973 | | |
2974 | 0 | const uint16 *sPtr = fSrcData + row * fSrcRowStep; |
2975 | | |
2976 | | // Initialize predictors for this row. |
2977 | | |
2978 | 0 | int32 predictor [4]; |
2979 | | |
2980 | 0 | for (int32 channel = 0; channel < (int32)fSrcChannels; channel++) |
2981 | 0 | { |
2982 | | |
2983 | 0 | if (row == 0) |
2984 | 0 | predictor [channel] = 1 << (fSrcBitDepth - 1); |
2985 | | |
2986 | 0 | else |
2987 | 0 | predictor [channel] = sPtr [channel - fSrcRowStep]; |
2988 | | |
2989 | 0 | } |
2990 | | |
2991 | | // Unroll most common case of two channels |
2992 | | |
2993 | 0 | if (fSrcChannels == 2) |
2994 | 0 | { |
2995 | | |
2996 | 0 | int32 pred0 = predictor [0]; |
2997 | 0 | int32 pred1 = predictor [1]; |
2998 | | |
2999 | 0 | uint32 srcCols = fSrcCols; |
3000 | 0 | int32 srcColStep = fSrcColStep; |
3001 | | |
3002 | 0 | for (uint32 col = 0; col < srcCols; col++) |
3003 | 0 | { |
3004 | | |
3005 | 0 | int32 pixel0 = sPtr [0]; |
3006 | 0 | int32 pixel1 = sPtr [1]; |
3007 | | |
3008 | 0 | int16 diff0 = (int16) (pixel0 - pred0); |
3009 | 0 | int16 diff1 = (int16) (pixel1 - pred1); |
3010 | | |
3011 | 0 | CountOneDiff (diff0, freqCount [0]); |
3012 | 0 | CountOneDiff (diff1, freqCount [1]); |
3013 | | |
3014 | 0 | pred0 = pixel0; |
3015 | 0 | pred1 = pixel1; |
3016 | | |
3017 | 0 | sPtr += srcColStep; |
3018 | | |
3019 | 0 | } |
3020 | | |
3021 | 0 | } |
3022 | | |
3023 | | // General case. |
3024 | | |
3025 | 0 | else |
3026 | 0 | { |
3027 | | |
3028 | 0 | for (uint32 col = 0; col < fSrcCols; col++) |
3029 | 0 | { |
3030 | | |
3031 | 0 | for (uint32 channel = 0; channel < fSrcChannels; channel++) |
3032 | 0 | { |
3033 | | |
3034 | 0 | int32 pixel = sPtr [channel]; |
3035 | | |
3036 | 0 | int16 diff = (int16) (pixel - predictor [channel]); |
3037 | | |
3038 | 0 | CountOneDiff (diff, freqCount [channel]); |
3039 | | |
3040 | 0 | predictor [channel] = pixel; |
3041 | | |
3042 | 0 | } |
3043 | | |
3044 | 0 | sPtr += fSrcColStep; |
3045 | | |
3046 | 0 | } |
3047 | | |
3048 | 0 | } |
3049 | | |
3050 | 0 | } |
3051 | |
|
3052 | 0 | } |
3053 | | |
3054 | | /*****************************************************************************/ |
3055 | | |
3056 | | /* |
3057 | | *-------------------------------------------------------------- |
3058 | | * |
3059 | | * HuffEncode -- |
3060 | | * |
3061 | | * Encode and output Huffman-compressed image data. |
3062 | | * |
3063 | | * Results: |
3064 | | * None. |
3065 | | * |
3066 | | * Side effects: |
3067 | | * None. |
3068 | | * |
3069 | | *-------------------------------------------------------------- |
3070 | | */ |
3071 | | |
3072 | | void dng_lossless_encoder::HuffEncode () |
3073 | 0 | { |
3074 | | |
3075 | 0 | DNG_ASSERT ((int32)fSrcRows >= 0, "dng_lossless_encoder::HuffEncode: fSrcRows too large."); |
3076 | |
|
3077 | 0 | for (int32 row = 0; row < (int32)fSrcRows; row++) |
3078 | 0 | { |
3079 | | |
3080 | 0 | const uint16 *sPtr = fSrcData + row * fSrcRowStep; |
3081 | | |
3082 | | // Initialize predictors for this row. |
3083 | | |
3084 | 0 | int32 predictor [4]; |
3085 | | |
3086 | 0 | for (int32 channel = 0; channel < (int32)fSrcChannels; channel++) |
3087 | 0 | { |
3088 | | |
3089 | 0 | if (row == 0) |
3090 | 0 | predictor [channel] = 1 << (fSrcBitDepth - 1); |
3091 | | |
3092 | 0 | else |
3093 | 0 | predictor [channel] = sPtr [channel - fSrcRowStep]; |
3094 | | |
3095 | 0 | } |
3096 | | |
3097 | | // Unroll most common case of two channels |
3098 | | |
3099 | 0 | if (fSrcChannels == 2) |
3100 | 0 | { |
3101 | | |
3102 | 0 | int32 pred0 = predictor [0]; |
3103 | 0 | int32 pred1 = predictor [1]; |
3104 | | |
3105 | 0 | uint32 srcCols = fSrcCols; |
3106 | 0 | int32 srcColStep = fSrcColStep; |
3107 | | |
3108 | 0 | for (uint32 col = 0; col < srcCols; col++) |
3109 | 0 | { |
3110 | | |
3111 | 0 | int32 pixel0 = sPtr [0]; |
3112 | 0 | int32 pixel1 = sPtr [1]; |
3113 | | |
3114 | 0 | int16 diff0 = (int16) (pixel0 - pred0); |
3115 | 0 | int16 diff1 = (int16) (pixel1 - pred1); |
3116 | | |
3117 | 0 | EncodeOneDiff (diff0, &huffTable [0]); |
3118 | 0 | EncodeOneDiff (diff1, &huffTable [1]); |
3119 | | |
3120 | 0 | pred0 = pixel0; |
3121 | 0 | pred1 = pixel1; |
3122 | | |
3123 | 0 | sPtr += srcColStep; |
3124 | | |
3125 | 0 | } |
3126 | | |
3127 | 0 | } |
3128 | | |
3129 | | // General case. |
3130 | | |
3131 | 0 | else |
3132 | 0 | { |
3133 | | |
3134 | 0 | for (uint32 col = 0; col < fSrcCols; col++) |
3135 | 0 | { |
3136 | | |
3137 | 0 | for (uint32 channel = 0; channel < fSrcChannels; channel++) |
3138 | 0 | { |
3139 | | |
3140 | 0 | int32 pixel = sPtr [channel]; |
3141 | | |
3142 | 0 | int16 diff = (int16) (pixel - predictor [channel]); |
3143 | | |
3144 | 0 | EncodeOneDiff (diff, &huffTable [channel]); |
3145 | | |
3146 | 0 | predictor [channel] = pixel; |
3147 | | |
3148 | 0 | } |
3149 | | |
3150 | 0 | sPtr += fSrcColStep; |
3151 | | |
3152 | 0 | } |
3153 | | |
3154 | 0 | } |
3155 | | |
3156 | 0 | } |
3157 | | |
3158 | 0 | FlushBits (); |
3159 | | |
3160 | 0 | } |
3161 | | |
3162 | | /*****************************************************************************/ |
3163 | | |
3164 | | /* |
3165 | | *-------------------------------------------------------------- |
3166 | | * |
3167 | | * GenHuffCoding -- |
3168 | | * |
3169 | | * Generate the optimal coding for the given counts. |
3170 | | * This algorithm is explained in section K.2 of the |
3171 | | * JPEG standard. |
3172 | | * |
3173 | | * Results: |
3174 | | * htbl->bits and htbl->huffval are constructed. |
3175 | | * |
3176 | | * Side effects: |
3177 | | * None. |
3178 | | * |
3179 | | *-------------------------------------------------------------- |
3180 | | */ |
3181 | | |
3182 | | void dng_lossless_encoder::GenHuffCoding (HuffmanTable *htbl, uint32 *freq) |
3183 | 0 | { |
3184 | | |
3185 | 0 | int i; |
3186 | 0 | int j; |
3187 | | |
3188 | 0 | const int MAX_CLEN = 32; // assumed maximum initial code length |
3189 | | |
3190 | 0 | uint8 bits [MAX_CLEN + 1]; // bits [k] = # of symbols with code length k |
3191 | 0 | short codesize [257]; // codesize [k] = code length of symbol k |
3192 | 0 | short others [257]; // next symbol in current branch of tree |
3193 | | |
3194 | 0 | memset (bits , 0, sizeof (bits )); |
3195 | 0 | memset (codesize, 0, sizeof (codesize)); |
3196 | | |
3197 | 0 | for (i = 0; i < 257; i++) |
3198 | 0 | others [i] = -1; // init links to empty |
3199 | | |
3200 | | // Including the pseudo-symbol 256 in the Huffman procedure guarantees |
3201 | | // that no real symbol is given code-value of all ones, because 256 |
3202 | | // will be placed in the largest codeword category. |
3203 | |
|
3204 | 0 | freq [256] = 1; // make sure there is a nonzero count |
3205 | | |
3206 | | // Huffman's basic algorithm to assign optimal code lengths to symbols |
3207 | | |
3208 | 0 | while (true) |
3209 | 0 | { |
3210 | | |
3211 | | // Find the smallest nonzero frequency, set c1 = its symbol. |
3212 | | // In case of ties, take the larger symbol number. |
3213 | |
|
3214 | 0 | int c1 = -1; |
3215 | | |
3216 | 0 | uint32 v = 0xFFFFFFFF; |
3217 | | |
3218 | 0 | for (i = 0; i <= 256; i++) |
3219 | 0 | { |
3220 | | |
3221 | 0 | if (freq [i] && freq [i] <= v) |
3222 | 0 | { |
3223 | 0 | v = freq [i]; |
3224 | 0 | c1 = i; |
3225 | 0 | } |
3226 | | |
3227 | 0 | } |
3228 | | |
3229 | | // Find the next smallest nonzero frequency, set c2 = its symbol. |
3230 | | // In case of ties, take the larger symbol number. |
3231 | |
|
3232 | 0 | int c2 = -1; |
3233 | | |
3234 | 0 | v = 0xFFFFFFFF; |
3235 | | |
3236 | 0 | for (i = 0; i <= 256; i++) |
3237 | 0 | { |
3238 | | |
3239 | 0 | if (freq [i] && freq [i] <= v && i != c1) |
3240 | 0 | { |
3241 | 0 | v = freq [i]; |
3242 | 0 | c2 = i; |
3243 | 0 | } |
3244 | | |
3245 | 0 | } |
3246 | | |
3247 | | // Done if we've merged everything into one frequency. |
3248 | |
|
3249 | 0 | if (c2 < 0) |
3250 | 0 | break; |
3251 | | |
3252 | | // Else merge the two counts/trees. |
3253 | | |
3254 | 0 | freq [c1] += freq [c2]; |
3255 | 0 | freq [c2] = 0; |
3256 | | |
3257 | | // Increment the codesize of everything in c1's tree branch. |
3258 | |
|
3259 | 0 | codesize [c1] ++; |
3260 | | |
3261 | 0 | while (others [c1] >= 0) |
3262 | 0 | { |
3263 | 0 | c1 = others [c1]; |
3264 | 0 | codesize [c1] ++; |
3265 | 0 | } |
3266 | | |
3267 | | // chain c2 onto c1's tree branch |
3268 | |
|
3269 | 0 | others [c1] = (short) c2; |
3270 | | |
3271 | | // Increment the codesize of everything in c2's tree branch. |
3272 | |
|
3273 | 0 | codesize [c2] ++; |
3274 | | |
3275 | 0 | while (others [c2] >= 0) |
3276 | 0 | { |
3277 | 0 | c2 = others [c2]; |
3278 | 0 | codesize [c2] ++; |
3279 | 0 | } |
3280 | |
|
3281 | 0 | } |
3282 | | |
3283 | | // Now count the number of symbols of each code length. |
3284 | |
|
3285 | 0 | for (i = 0; i <= 256; i++) |
3286 | 0 | { |
3287 | | |
3288 | 0 | if (codesize [i]) |
3289 | 0 | { |
3290 | | |
3291 | | // The JPEG standard seems to think that this can't happen, |
3292 | | // but I'm paranoid... |
3293 | | |
3294 | 0 | if (codesize [i] > MAX_CLEN) |
3295 | 0 | { |
3296 | | |
3297 | 0 | DNG_REPORT ("Huffman code size table overflow"); |
3298 | | |
3299 | 0 | ThrowProgramError (); |
3300 | | |
3301 | 0 | } |
3302 | |
|
3303 | 0 | bits [codesize [i]]++; |
3304 | | |
3305 | 0 | } |
3306 | |
|
3307 | 0 | } |
3308 | | |
3309 | | // JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure |
3310 | | // Huffman procedure assigned any such lengths, we must adjust the coding. |
3311 | | // Here is what the JPEG spec says about how this next bit works: |
3312 | | // Since symbols are paired for the longest Huffman code, the symbols are |
3313 | | // removed from this length category two at a time. The prefix for the pair |
3314 | | // (which is one bit shorter) is allocated to one of the pair; then, |
3315 | | // skipping the BITS entry for that prefix length, a code word from the next |
3316 | | // shortest nonzero BITS entry is converted into a prefix for two code words |
3317 | | // one bit longer. |
3318 | | |
3319 | 0 | for (i = MAX_CLEN; i > 16; i--) |
3320 | 0 | { |
3321 | | |
3322 | 0 | while (bits [i] > 0) |
3323 | 0 | { |
3324 | | |
3325 | | // Kludge: I have never been able to test this logic, and there |
3326 | | // are comments on the web that this encoder has bugs with 16-bit |
3327 | | // data, so just throw an error if we get here and revert to a |
3328 | | // default table. - tknoll 12/1/03. |
3329 | | |
3330 | 0 | DNG_REPORT ("Info: Optimal huffman table bigger than 16 bits"); |
3331 | | |
3332 | 0 | ThrowProgramError (); |
3333 | | |
3334 | | // Original logic: |
3335 | | |
3336 | 0 | j = i - 2; // find length of new prefix to be used |
3337 | | |
3338 | 0 | while (bits [j] == 0) |
3339 | 0 | j--; |
3340 | | |
3341 | 0 | bits [i ] -= 2; // remove two symbols |
3342 | 0 | bits [i - 1] ++; // one goes in this length |
3343 | 0 | bits [j + 1] += 2; // two new symbols in this length |
3344 | 0 | bits [j ] --; // symbol of this length is now a prefix |
3345 | | |
3346 | 0 | } |
3347 | | |
3348 | 0 | } |
3349 | | |
3350 | | // Remove the count for the pseudo-symbol 256 from |
3351 | | // the largest codelength. |
3352 | | |
3353 | 0 | while (bits [i] == 0) // find largest codelength still in use |
3354 | 0 | i--; |
3355 | | |
3356 | 0 | bits [i] --; |
3357 | | |
3358 | | // Return final symbol counts (only for lengths 0..16). |
3359 | |
|
3360 | 0 | memcpy (htbl->bits, bits, sizeof (htbl->bits)); |
3361 | | |
3362 | | // Return a list of the symbols sorted by code length. |
3363 | | // It's not real clear to me why we don't need to consider the codelength |
3364 | | // changes made above, but the JPEG spec seems to think this works. |
3365 | | |
3366 | 0 | int p = 0; |
3367 | | |
3368 | 0 | for (i = 1; i <= MAX_CLEN; i++) |
3369 | 0 | { |
3370 | | |
3371 | 0 | for (j = 0; j <= 255; j++) |
3372 | 0 | { |
3373 | | |
3374 | 0 | if (codesize [j] == i) |
3375 | 0 | { |
3376 | 0 | htbl->huffval [p] = (uint8) j; |
3377 | 0 | p++; |
3378 | 0 | } |
3379 | |
|
3380 | 0 | } |
3381 | | |
3382 | 0 | } |
3383 | | |
3384 | 0 | } |
3385 | | |
3386 | | /*****************************************************************************/ |
3387 | | |
3388 | | /* |
3389 | | *-------------------------------------------------------------- |
3390 | | * |
3391 | | * HuffOptimize -- |
3392 | | * |
3393 | | * Find the best coding parameters for a Huffman-coded scan. |
3394 | | * When called, the scan data has already been converted to |
3395 | | * a sequence of MCU groups of source image samples, which |
3396 | | * are stored in a "big" array, mcuTable. |
3397 | | * |
3398 | | * It counts the times each category symbol occurs. Based on |
3399 | | * this counting, optimal Huffman tables are built. Then it |
3400 | | * uses this optimal Huffman table and counting table to find |
3401 | | * the best PSV. |
3402 | | * |
3403 | | * Results: |
3404 | | * Optimal Huffman tables are retured in cPtr->dcHuffTblPtrs[tbl]. |
3405 | | * Best PSV is retured in cPtr->Ss. |
3406 | | * |
3407 | | * Side effects: |
3408 | | * None. |
3409 | | * |
3410 | | *-------------------------------------------------------------- |
3411 | | */ |
3412 | | |
3413 | | void dng_lossless_encoder::HuffOptimize () |
3414 | 0 | { |
3415 | | |
3416 | | // Collect the frequency counts. |
3417 | | |
3418 | 0 | FreqCountSet (); |
3419 | | |
3420 | | // Generate Huffman encoding tables. |
3421 | | |
3422 | 0 | for (uint32 channel = 0; channel < fSrcChannels; channel++) |
3423 | 0 | { |
3424 | | |
3425 | 0 | try |
3426 | 0 | { |
3427 | | |
3428 | 0 | GenHuffCoding (&huffTable [channel], freqCount [channel]); |
3429 | | |
3430 | 0 | } |
3431 | | |
3432 | 0 | catch (...) |
3433 | 0 | { |
3434 | | |
3435 | 0 | DNG_REPORT ("Info: Reverting to default huffman table"); |
3436 | | |
3437 | 0 | for (uint32 j = 0; j <= 256; j++) |
3438 | 0 | { |
3439 | | |
3440 | 0 | freqCount [channel] [j] = (j <= 16 ? 1 : 0); |
3441 | | |
3442 | 0 | } |
3443 | | |
3444 | 0 | GenHuffCoding (&huffTable [channel], freqCount [channel]); |
3445 | | |
3446 | 0 | } |
3447 | | |
3448 | 0 | FixHuffTbl (&huffTable [channel]); |
3449 | | |
3450 | 0 | } |
3451 | | |
3452 | 0 | } |
3453 | | |
3454 | | /*****************************************************************************/ |
3455 | | |
3456 | | /* |
3457 | | *-------------------------------------------------------------- |
3458 | | * |
3459 | | * EmitMarker -- |
3460 | | * |
3461 | | * Emit a marker code into the output stream. |
3462 | | * |
3463 | | * Results: |
3464 | | * None. |
3465 | | * |
3466 | | * Side effects: |
3467 | | * None. |
3468 | | * |
3469 | | *-------------------------------------------------------------- |
3470 | | */ |
3471 | | |
3472 | | void dng_lossless_encoder::EmitMarker (JpegMarker mark) |
3473 | 0 | { |
3474 | | |
3475 | 0 | EmitByte (0xFF); |
3476 | 0 | EmitByte ((uint8) mark); |
3477 | | |
3478 | 0 | } |
3479 | | |
3480 | | /*****************************************************************************/ |
3481 | | |
3482 | | /* |
3483 | | *-------------------------------------------------------------- |
3484 | | * |
3485 | | * Emit2bytes -- |
3486 | | * |
3487 | | * Emit a 2-byte integer; these are always MSB first in JPEG |
3488 | | * files |
3489 | | * |
3490 | | * Results: |
3491 | | * None. |
3492 | | * |
3493 | | * Side effects: |
3494 | | * None. |
3495 | | * |
3496 | | *-------------------------------------------------------------- |
3497 | | */ |
3498 | | |
3499 | | void dng_lossless_encoder::Emit2bytes (int value) |
3500 | 0 | { |
3501 | | |
3502 | 0 | EmitByte ((value >> 8) & 0xFF); |
3503 | 0 | EmitByte (value & 0xFF); |
3504 | | |
3505 | 0 | } |
3506 | | |
3507 | | /*****************************************************************************/ |
3508 | | |
3509 | | /* |
3510 | | *-------------------------------------------------------------- |
3511 | | * |
3512 | | * EmitDht -- |
3513 | | * |
3514 | | * Emit a DHT marker, follwed by the huffman data. |
3515 | | * |
3516 | | * Results: |
3517 | | * None |
3518 | | * |
3519 | | * Side effects: |
3520 | | * None |
3521 | | * |
3522 | | *-------------------------------------------------------------- |
3523 | | */ |
3524 | | |
3525 | | void dng_lossless_encoder::EmitDht (int index) |
3526 | 0 | { |
3527 | | |
3528 | 0 | int i; |
3529 | | |
3530 | 0 | HuffmanTable *htbl = &huffTable [index]; |
3531 | | |
3532 | 0 | EmitMarker (M_DHT); |
3533 | |
|
3534 | 0 | int length = 0; |
3535 | | |
3536 | 0 | for (i = 1; i <= 16; i++) |
3537 | 0 | length += htbl->bits [i]; |
3538 | |
|
3539 | 0 | Emit2bytes (length + 2 + 1 + 16); |
3540 | | |
3541 | 0 | EmitByte ((uint8) index); |
3542 | |
|
3543 | 0 | for (i = 1; i <= 16; i++) |
3544 | 0 | EmitByte (htbl->bits [i]); |
3545 | |
|
3546 | 0 | for (i = 0; i < length; i++) |
3547 | 0 | EmitByte (htbl->huffval [i]); |
3548 | |
|
3549 | 0 | } |
3550 | | |
3551 | | /*****************************************************************************/ |
3552 | | |
3553 | | /* |
3554 | | *-------------------------------------------------------------- |
3555 | | * |
3556 | | * EmitSof -- |
3557 | | * |
3558 | | * Emit a SOF marker plus data. |
3559 | | * |
3560 | | * Results: |
3561 | | * None. |
3562 | | * |
3563 | | * Side effects: |
3564 | | * None. |
3565 | | * |
3566 | | *-------------------------------------------------------------- |
3567 | | */ |
3568 | | |
3569 | | void dng_lossless_encoder::EmitSof (JpegMarker code) |
3570 | 0 | { |
3571 | | |
3572 | 0 | EmitMarker (code); |
3573 | |
|
3574 | 0 | Emit2bytes (3 * fSrcChannels + 2 + 5 + 1); // length |
3575 | |
|
3576 | 0 | EmitByte ((uint8) fSrcBitDepth); |
3577 | | |
3578 | 0 | Emit2bytes (fSrcRows); |
3579 | 0 | Emit2bytes (fSrcCols); |
3580 | |
|
3581 | 0 | EmitByte ((uint8) fSrcChannels); |
3582 | |
|
3583 | 0 | for (uint32 i = 0; i < fSrcChannels; i++) |
3584 | 0 | { |
3585 | | |
3586 | 0 | EmitByte ((uint8) i); |
3587 | | |
3588 | 0 | EmitByte ((uint8) ((1 << 4) + 1)); // Not subsampled. |
3589 | | |
3590 | 0 | EmitByte (0); // Tq shall be 0 for lossless. |
3591 | | |
3592 | 0 | } |
3593 | | |
3594 | 0 | } |
3595 | | |
3596 | | /*****************************************************************************/ |
3597 | | |
3598 | | /* |
3599 | | *-------------------------------------------------------------- |
3600 | | * |
3601 | | * EmitSos -- |
3602 | | * |
3603 | | * Emit a SOS marker plus data. |
3604 | | * |
3605 | | * Results: |
3606 | | * None. |
3607 | | * |
3608 | | * Side effects: |
3609 | | * None. |
3610 | | * |
3611 | | *-------------------------------------------------------------- |
3612 | | */ |
3613 | | |
3614 | | void dng_lossless_encoder::EmitSos () |
3615 | 0 | { |
3616 | | |
3617 | 0 | EmitMarker (M_SOS); |
3618 | |
|
3619 | 0 | Emit2bytes (2 * fSrcChannels + 2 + 1 + 3); // length |
3620 | |
|
3621 | 0 | EmitByte ((uint8) fSrcChannels); // Ns |
3622 | |
|
3623 | 0 | for (uint32 i = 0; i < fSrcChannels; i++) |
3624 | 0 | { |
3625 | | |
3626 | | // Cs,Td,Ta |
3627 | | |
3628 | 0 | EmitByte ((uint8) i); |
3629 | 0 | EmitByte ((uint8) (i << 4)); |
3630 | | |
3631 | 0 | } |
3632 | |
|
3633 | 0 | EmitByte (1); // PSV - hardcoded - tknoll |
3634 | 0 | EmitByte (0); // Spectral selection end - Se |
3635 | 0 | EmitByte (0); // The point transform parameter |
3636 | | |
3637 | 0 | } |
3638 | | |
3639 | | /*****************************************************************************/ |
3640 | | |
3641 | | /* |
3642 | | *-------------------------------------------------------------- |
3643 | | * |
3644 | | * WriteFileHeader -- |
3645 | | * |
3646 | | * Write the file header. |
3647 | | * |
3648 | | * Results: |
3649 | | * None. |
3650 | | * |
3651 | | * Side effects: |
3652 | | * None. |
3653 | | * |
3654 | | *-------------------------------------------------------------- |
3655 | | */ |
3656 | | |
3657 | | void dng_lossless_encoder::WriteFileHeader () |
3658 | 0 | { |
3659 | | |
3660 | 0 | EmitMarker (M_SOI); // first the SOI |
3661 | | |
3662 | 0 | EmitSof (M_SOF3); |
3663 | | |
3664 | 0 | } |
3665 | | |
3666 | | /*****************************************************************************/ |
3667 | | |
3668 | | /* |
3669 | | *-------------------------------------------------------------- |
3670 | | * |
3671 | | * WriteScanHeader -- |
3672 | | * |
3673 | | * Write the start of a scan (everything through the SOS marker). |
3674 | | * |
3675 | | * Results: |
3676 | | * None. |
3677 | | * |
3678 | | * Side effects: |
3679 | | * None. |
3680 | | * |
3681 | | *-------------------------------------------------------------- |
3682 | | */ |
3683 | | |
3684 | | void dng_lossless_encoder::WriteScanHeader () |
3685 | 0 | { |
3686 | | |
3687 | | // Emit Huffman tables. |
3688 | | |
3689 | 0 | for (uint32 i = 0; i < fSrcChannels; i++) |
3690 | 0 | { |
3691 | | |
3692 | 0 | EmitDht (i); |
3693 | | |
3694 | 0 | } |
3695 | |
|
3696 | 0 | EmitSos (); |
3697 | | |
3698 | 0 | } |
3699 | | |
3700 | | /*****************************************************************************/ |
3701 | | |
3702 | | /* |
3703 | | *-------------------------------------------------------------- |
3704 | | * |
3705 | | * WriteFileTrailer -- |
3706 | | * |
3707 | | * Write the End of image marker at the end of a JPEG file. |
3708 | | * |
3709 | | * Results: |
3710 | | * None. |
3711 | | * |
3712 | | * Side effects: |
3713 | | * None. |
3714 | | * |
3715 | | *-------------------------------------------------------------- |
3716 | | */ |
3717 | | |
3718 | | void dng_lossless_encoder::WriteFileTrailer () |
3719 | 0 | { |
3720 | | |
3721 | 0 | EmitMarker (M_EOI); |
3722 | | |
3723 | 0 | } |
3724 | | |
3725 | | /*****************************************************************************/ |
3726 | | |
3727 | | void dng_lossless_encoder::Encode () |
3728 | 0 | { |
3729 | | |
3730 | 0 | DNG_ASSERT (fSrcChannels <= 4, "Too many components in scan"); |
3731 | | |
3732 | | // Count the times each difference category occurs. |
3733 | | // Construct the optimal Huffman table. |
3734 | | |
3735 | 0 | HuffOptimize (); |
3736 | | |
3737 | | // Write the frame and scan headers. |
3738 | |
|
3739 | 0 | WriteFileHeader (); |
3740 | | |
3741 | 0 | WriteScanHeader (); |
3742 | | |
3743 | | // Encode the image. |
3744 | | |
3745 | 0 | HuffEncode (); |
3746 | | |
3747 | | // Clean up everything. |
3748 | | |
3749 | 0 | WriteFileTrailer (); |
3750 | |
|
3751 | 0 | } |
3752 | | |
3753 | | /*****************************************************************************/ |
3754 | | |
3755 | | void EncodeLosslessJPEG (const uint16 *srcData, |
3756 | | uint32 srcRows, |
3757 | | uint32 srcCols, |
3758 | | uint32 srcChannels, |
3759 | | uint32 srcBitDepth, |
3760 | | int32 srcRowStep, |
3761 | | int32 srcColStep, |
3762 | | dng_stream &stream) |
3763 | 0 | { |
3764 | | |
3765 | 0 | dng_lossless_encoder encoder (srcData, |
3766 | 0 | srcRows, |
3767 | 0 | srcCols, |
3768 | 0 | srcChannels, |
3769 | 0 | srcBitDepth, |
3770 | 0 | srcRowStep, |
3771 | 0 | srcColStep, |
3772 | 0 | stream); |
3773 | |
|
3774 | 0 | encoder.Encode (); |
3775 | | |
3776 | 0 | } |
3777 | | |
3778 | | /*****************************************************************************/ |