Coverage Report

Created: 2026-01-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dng_sdk/source/dng_lossless_jpeg.cpp
Line
Count
Source
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
152k
  {
122
  
123
152k
  int32 l;
124
152k
  int32 i;
125
  
126
152k
  const uint32 bitMask [] =
127
152k
    {
128
152k
    0xffffffff, 0x7fffffff, 0x3fffffff, 0x1fffffff,
129
152k
        0x0fffffff, 0x07ffffff, 0x03ffffff, 0x01ffffff,
130
152k
        0x00ffffff, 0x007fffff, 0x003fffff, 0x001fffff,
131
152k
        0x000fffff, 0x0007ffff, 0x0003ffff, 0x0001ffff,
132
152k
        0x0000ffff, 0x00007fff, 0x00003fff, 0x00001fff,
133
152k
        0x00000fff, 0x000007ff, 0x000003ff, 0x000001ff,
134
152k
        0x000000ff, 0x0000007f, 0x0000003f, 0x0000001f,
135
152k
        0x0000000f, 0x00000007, 0x00000003, 0x00000001
136
152k
        };
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
152k
  int8 huffsize [257];
142
  
143
152k
    int32 p = 0;
144
    
145
2.58M
  for (l = 1; l <= 16; l++)
146
2.43M
      {
147
      
148
3.95M
        for (i = 1; i <= (int32) htbl->bits [l]; i++)
149
1.51M
            huffsize [p++] = (int8) l;
150
151
2.43M
      }
152
      
153
152k
    huffsize [p] = 0;
154
    
155
152k
    int32 lastp = p;
156
157
  // Figure C.2: generate the codes themselves
158
  // Note that this is in code-length order.
159
160
152k
  uint16 huffcode [257];
161
  
162
152k
  uint16 code = 0;
163
    
164
152k
    int32 si = huffsize [0];
165
    
166
152k
    p = 0;
167
    
168
1.02M
    while (huffsize [p])
169
868k
      {
170
      
171
2.38M
        while (((int32) huffsize [p]) == si) 
172
1.51M
          {
173
1.51M
            huffcode [p++] = code;
174
1.51M
            code++;
175
1.51M
          }
176
          
177
868k
        code <<= 1;
178
        
179
868k
        si++;
180
        
181
868k
      }
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
152k
    memset (htbl->ehufsi, 0, sizeof (htbl->ehufsi));
189
190
1.66M
    for (p = 0; p < lastp; p++)
191
1.51M
      {
192
      
193
1.51M
        htbl->ehufco [htbl->huffval [p]] = huffcode [p];
194
1.51M
        htbl->ehufsi [htbl->huffval [p]] = huffsize [p];
195
        
196
1.51M
      }
197
    
198
  // Figure F.15: generate decoding tables
199
 
200
152k
  p = 0;
201
  
202
2.58M
    for (l = 1; l <= 16; l++)
203
2.43M
      {
204
      
205
2.43M
        if (htbl->bits [l])
206
780k
          {
207
          
208
780k
            htbl->valptr  [l] = (int16) p;
209
780k
            htbl->mincode [l] = huffcode [p];
210
            
211
780k
            p += htbl->bits [l];
212
            
213
780k
            htbl->maxcode [l] = huffcode [p - 1];
214
            
215
780k
          }
216
          
217
1.65M
        else 
218
1.65M
          {
219
1.65M
            htbl->maxcode [l] = -1;
220
1.65M
          }
221
222
2.43M
      }
223
224
    // We put in this value to ensure HuffDecode terminates.
225
226
152k
    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
152k
    memset (htbl->numbits, 0, sizeof (htbl->numbits));
235
    
236
1.66M
  for (p = 0; p < lastp; p++)
237
1.51M
    {
238
    
239
1.51M
        int32 size = huffsize [p];
240
        
241
1.51M
        if (size <= 8)
242
951k
          {
243
          
244
951k
            int32 value = htbl->huffval [p];
245
            
246
951k
            code = huffcode [p];
247
            
248
951k
            int32 ll = code << (8  -size);
249
            
250
951k
        int32 ul = (size < 8 ? ll | bitMask [24 + size]
251
951k
                     : ll);
252
951k
            if (ul >= static_cast<int32>(sizeof(htbl->numbits) / sizeof(htbl->numbits[0])) ||
253
951k
                ul >= static_cast<int32>(sizeof(htbl->value) / sizeof(htbl->value[0])))
254
14
                {
255
14
                ThrowBadFormat ();
256
14
                }
257
  
258
36.2M
            for (i = ll; i <= ul; i++)
259
35.3M
              {
260
35.3M
                htbl->numbits [i] = size;
261
35.3M
                htbl->value   [i] = value;
262
35.3M
              }
263
              
264
951k
      }
265
266
1.51M
    }
267
268
152k
  }
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
122M
      {
422
122M
      return fStream->Get_uint8 ();
423
122M
      }
424
      
425
    void UnGetJpegChar ()
426
74.7M
      {
427
74.7M
      fStream->SetReadPosition (fStream->Position () - 1);
428
74.7M
      }
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
3.70k
  : fStream  (stream )
503
3.70k
  , fSpooler (spooler)
504
3.70k
  , fBug16   (bug16  )
505
  
506
3.70k
  , compInfoBuffer ()
507
3.70k
  , info           ()
508
3.70k
  , mcuBuffer1     ()
509
3.70k
  , mcuBuffer2     ()
510
3.70k
  , mcuBuffer3     ()
511
3.70k
  , mcuBuffer4     ()
512
3.70k
  , mcuROW1      (NULL)
513
3.70k
  , mcuROW2      (NULL)
514
3.70k
  , getBuffer      (0)
515
3.70k
  , bitsLeft     (0)
516
  
517
  #if qSupportHasselblad_3FR
518
3.70k
  , fHasselblad3FR (false)
519
  #endif
520
  
521
3.70k
  {
522
  
523
3.70k
  memset (&info, 0, sizeof (info));
524
  
525
3.70k
  }
526
527
/*****************************************************************************/
528
529
uint16 dng_lossless_decoder::Get2bytes ()
530
290k
  {
531
  
532
290k
    uint16 a = GetJpegChar ();
533
    
534
290k
    return (uint16) ((a << 8) + GetJpegChar ());
535
    
536
290k
  }
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
186k
  {
559
  
560
186k
    uint32 length = Get2bytes () - 2;
561
    
562
186k
    fStream->Skip (length);
563
564
186k
  }
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
91.5k
  {
587
  
588
91.5k
    int32 length = Get2bytes () - 2;
589
    
590
244k
    while (length > 0)
591
152k
      {
592
593
152k
    int32 index = GetJpegChar ();
594
      
595
152k
    if (index < 0 || index >= 4)
596
45
      {
597
45
        ThrowBadFormat ();
598
45
      }
599
600
152k
    HuffmanTable *&htblptr = info.dcHuffTblPtrs [index];
601
602
152k
    if (htblptr == NULL)
603
4.39k
      {
604
      
605
4.39k
      huffmanBuffer [index] . Allocate (sizeof (HuffmanTable));
606
      
607
4.39k
        htblptr = (HuffmanTable *) huffmanBuffer [index] . Buffer ();
608
        
609
4.39k
      }
610
611
152k
    htblptr->bits [0] = 0;
612
    
613
152k
      int32 count = 0;
614
      
615
2.59M
    for (int32 i = 1; i <= 16; i++)
616
2.44M
      {
617
      
618
2.44M
        htblptr->bits [i] = GetJpegChar ();
619
        
620
2.44M
        count += htblptr->bits [i];
621
        
622
2.44M
      }
623
624
152k
    if (count > 256) 
625
60
      {
626
60
        ThrowBadFormat ();
627
60
      }
628
629
4.35M
    for (int32 j = 0; j < count; j++)
630
4.20M
      {
631
      
632
4.20M
        htblptr->huffval [j] = GetJpegChar ();
633
        
634
4.20M
        }
635
636
152k
    length -= 1 + 16 + count;
637
638
152k
      }
639
      
640
91.5k
  }
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
1.53k
  {
663
  
664
1.53k
    if (Get2bytes () != 4)
665
15
      {
666
15
    ThrowBadFormat ();
667
15
    }
668
    
669
1.53k
    info.restartInterval = Get2bytes ();
670
671
1.53k
  }
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
3.72k
  {
693
694
3.72k
  SkipVariable ();
695
  
696
3.72k
  }
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
2.55k
  {
720
  
721
2.55k
    int32 length = Get2bytes ();
722
723
2.55k
    info.dataPrecision = GetJpegChar ();
724
2.55k
    info.imageHeight   = Get2bytes   ();
725
2.55k
    info.imageWidth    = Get2bytes   ();
726
2.55k
    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
2.55k
    if ((info.imageHeight   <= 0) ||
733
2.53k
    (info.imageWidth    <= 0) || 
734
2.52k
    (info.numComponents <= 0))
735
23
    {
736
23
    ThrowBadFormat ();
737
23
      }
738
739
  // Lossless JPEG specifies data precision to be from 2 to 16 bits/sample.
740
741
2.55k
  const int32 MinPrecisionBits = 2;
742
2.55k
  const int32 MaxPrecisionBits = 16;
743
744
2.55k
    if ((info.dataPrecision < MinPrecisionBits) ||
745
2.50k
        (info.dataPrecision > MaxPrecisionBits))
746
38
        {
747
38
    ThrowBadFormat ();
748
38
      }
749
      
750
    // Check length of tag.
751
752
2.55k
    if (length != (info.numComponents * 3 + 8))
753
38
      {
754
38
    ThrowBadFormat ();
755
38
      }
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
2.55k
    compInfoBuffer.Allocate (static_cast<uint32> (info.numComponents),
762
2.55k
                             sizeof (JpegComponentInfo));
763
    
764
2.55k
    info.compInfo = (JpegComponentInfo *) compInfoBuffer.Buffer ();
765
                   
766
    // Read in the per compent info.
767
768
20.0k
    for (int32 ci = 0; ci < info.numComponents; ci++)
769
17.4k
      {
770
      
771
17.4k
      JpegComponentInfo *compptr = &info.compInfo [ci];
772
      
773
17.4k
    compptr->componentIndex = (int16) ci;
774
    
775
17.4k
    compptr->componentId = GetJpegChar ();
776
    
777
17.4k
      int32 c = GetJpegChar ();
778
      
779
17.4k
    compptr->hSampFactor = (int16) ((c >> 4) & 15);
780
17.4k
    compptr->vSampFactor = (int16) ((c     ) & 15);
781
    
782
17.4k
        (void) GetJpegChar ();   /* skip Tq */
783
        
784
17.4k
      }
785
786
2.55k
  }
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
2.18k
  {
809
  
810
2.18k
    int32 length = Get2bytes ();
811
812
    // Get the number of image components.
813
814
2.18k
    int32 n = GetJpegChar ();
815
2.18k
    info.compsInScan = (int16) n;
816
    
817
    // Check length.
818
    
819
2.18k
    length -= 3;
820
821
2.18k
    if (length != (n * 2 + 3) || n < 1 || n > 4)
822
23
      {
823
23
    ThrowBadFormat ();
824
23
    }
825
  
826
  // Find index and huffman table for each component.
827
828
8.62k
    for (int32 i = 0; i < n; i++)
829
6.43k
      {
830
      
831
6.43k
    int32 cc = GetJpegChar ();
832
6.43k
    int32 c  = GetJpegChar ();
833
    
834
6.43k
    int32 ci;
835
    
836
11.9k
    for (ci = 0; ci < info.numComponents; ci++)
837
11.8k
      {
838
      
839
11.8k
        if (cc == info.compInfo[ci].componentId)
840
6.34k
          {
841
6.34k
        break;
842
6.34k
          }
843
          
844
11.8k
        }
845
846
6.43k
    if (ci >= info.numComponents) 
847
35
      {
848
35
        ThrowBadFormat ();
849
35
      }
850
851
6.43k
      JpegComponentInfo *compptr = &info.compInfo [ci];
852
      
853
6.43k
    info.curCompInfo [i] = compptr;
854
    
855
6.43k
    compptr->dcTblNo = (int16) ((c >> 4) & 15);
856
    
857
6.43k
      }
858
859
    // Get the PSV, skip Se, and get the point transform parameter.
860
861
2.18k
    info.Ss = GetJpegChar (); 
862
    
863
2.18k
    (void) GetJpegChar ();
864
    
865
2.18k
    info.Pt = GetJpegChar () & 0x0F;
866
    
867
2.18k
  }
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
3.40k
  {
890
891
    // Reset all parameters that are defined to be reset by SOI
892
     
893
3.40k
    info.restartInterval = 0;
894
    
895
3.40k
  }
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
337k
  {
918
  
919
337k
    int32 c;
920
921
337k
    do
922
358k
      {
923
924
    // skip any non-FF bytes
925
    
926
358k
    do 
927
16.0M
      {
928
16.0M
        c = GetJpegChar ();
929
16.0M
      }
930
16.0M
    while (c != 0xFF);
931
    
932
    // skip any duplicate FFs, since extra FFs are legal
933
    
934
358k
    do 
935
2.09M
      {
936
2.09M
      c = GetJpegChar();
937
2.09M
      } 
938
2.09M
    while (c == 0xFF);
939
    
940
358k
    }
941
358k
  while (c == 0);    // repeat if it was a stuffed FF/00
942
943
337k
    return c;
944
    
945
337k
  }
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
5.73k
  {
968
  
969
337k
    while (true)
970
337k
      {
971
    
972
337k
    int32 c = NextMarker ();
973
  
974
337k
    switch (c)
975
337k
      {
976
      
977
421
      case M_SOF0:
978
445
      case M_SOF1:
979
449
      case M_SOF2:
980
2.57k
      case M_SOF3:
981
2.58k
      case M_SOF5:
982
2.60k
      case M_SOF6:
983
2.60k
      case M_SOF7:
984
2.62k
      case M_JPG:
985
2.63k
      case M_SOF9:
986
2.64k
      case M_SOF10:
987
2.65k
      case M_SOF11:
988
2.67k
      case M_SOF13:
989
2.67k
      case M_SOF14:
990
2.68k
      case M_SOF15:
991
2.70k
      case M_SOI:
992
2.76k
      case M_EOI:
993
4.95k
      case M_SOS:
994
4.95k
          return (JpegMarker) c;
995
996
91.5k
      case M_DHT:
997
91.5k
          GetDht ();
998
91.5k
          break;
999
1000
3.41k
      case M_DQT:
1001
3.41k
          break;
1002
1003
1.53k
      case M_DRI:
1004
1.53k
          GetDri ();
1005
1.53k
          break;
1006
1007
3.72k
      case M_APP0:
1008
3.72k
          GetApp0 ();
1009
3.72k
          break;
1010
1011
3.59k
      case M_RST0:  // these are all parameterless
1012
7.52k
      case M_RST1:
1013
17.6k
      case M_RST2:
1014
23.0k
      case M_RST3:
1015
26.6k
      case M_RST4:
1016
30.4k
      case M_RST5:
1017
34.6k
      case M_RST6:
1018
38.2k
      case M_RST7:
1019
49.2k
      case M_TEM:
1020
49.2k
          break;
1021
1022
182k
      default:    // must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn
1023
182k
          SkipVariable ();
1024
182k
          break;
1025
          
1026
337k
      }
1027
      
1028
337k
      }
1029
1030
0
      return M_ERROR;
1031
5.73k
  }
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
3.70k
  {
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
3.70k
    int32 c  = GetJpegChar ();
1059
3.70k
    int32 c2 = GetJpegChar ();
1060
    
1061
3.70k
    if ((c != 0xFF) || (c2 != M_SOI)) 
1062
294
      {
1063
294
    ThrowBadFormat ();
1064
294
      }
1065
      
1066
    // OK, process SOI
1067
1068
3.70k
    GetSoi ();
1069
1070
    // Process markers until SOF
1071
1072
3.70k
    c = ProcessTables ();
1073
1074
3.70k
    switch (c)
1075
3.70k
      {
1076
      
1077
409
    case M_SOF0:
1078
431
    case M_SOF1:
1079
2.55k
    case M_SOF3:
1080
2.55k
      GetSof (c);
1081
2.55k
      break;
1082
1083
154
      default:
1084
154
      ThrowBadFormat ();
1085
154
      break;
1086
      
1087
3.70k
      }
1088
1089
3.70k
  }
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
2.33k
  {
1111
1112
    // Process markers until SOS or EOI
1113
1114
2.33k
    int32 c = ProcessTables ();
1115
1116
2.33k
    switch (c)
1117
2.33k
      {
1118
      
1119
2.18k
    case M_SOS:
1120
2.18k
      GetSos ();
1121
2.18k
      return 1;
1122
1123
34
      case M_EOI:
1124
34
      return 0;
1125
1126
27
      default:
1127
27
      ThrowBadFormat ();
1128
27
      break;
1129
      
1130
2.33k
      }
1131
      
1132
0
    return 0;
1133
    
1134
2.33k
  }
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
2.10k
  {
1157
  
1158
2.10k
  int32 ci;
1159
  
1160
2.10k
  #if qSupportCanon_sRAW
1161
  
1162
2.10k
  bool canon_sRAW = (info.numComponents == 3) &&
1163
2.07k
            (info.compInfo [0].hSampFactor == 2) &&
1164
571
            (info.compInfo [1].hSampFactor == 1) &&
1165
568
            (info.compInfo [2].hSampFactor == 1) &&
1166
557
            (info.compInfo [0].vSampFactor == 1) &&
1167
236
            (info.compInfo [1].vSampFactor == 1) &&
1168
226
            (info.compInfo [2].vSampFactor == 1) &&
1169
215
            (info.dataPrecision == 15) &&
1170
205
            (info.Ss == 1) &&
1171
202
            ((info.imageWidth & 1) == 0);
1172
            
1173
2.10k
  bool canon_sRAW2 = (info.numComponents == 3) &&
1174
2.07k
             (info.compInfo [0].hSampFactor == 2) &&
1175
571
             (info.compInfo [1].hSampFactor == 1) &&
1176
568
             (info.compInfo [2].hSampFactor == 1) &&
1177
557
             (info.compInfo [0].vSampFactor == 2) &&
1178
321
             (info.compInfo [1].vSampFactor == 1) &&
1179
311
             (info.compInfo [2].vSampFactor == 1) &&
1180
301
             (info.dataPrecision == 15) &&
1181
291
             (info.Ss == 1) &&
1182
288
             ((info.imageWidth  & 1) == 0) &&
1183
278
             ((info.imageHeight & 1) == 0);
1184
             
1185
2.10k
  if (!canon_sRAW && !canon_sRAW2)
1186
  
1187
1.62k
  #endif
1188
  
1189
1.62k
    {
1190
  
1191
    // Check sampling factor validity.
1192
1193
6.30k
    for (ci = 0; ci < info.numComponents; ci++)
1194
4.67k
      {
1195
      
1196
4.67k
      JpegComponentInfo *compPtr = &info.compInfo [ci];
1197
      
1198
4.67k
      if (compPtr->hSampFactor != 1 ||
1199
4.52k
        compPtr->vSampFactor != 1) 
1200
170
        {
1201
170
        ThrowBadFormat ();
1202
170
        }
1203
    
1204
4.67k
      }
1205
      
1206
1.62k
    }
1207
  
1208
    // Prepare array describing MCU composition.
1209
1210
2.10k
  if (info.compsInScan < 0 || info.compsInScan > 4)
1211
0
    {
1212
0
      ThrowBadFormat ();
1213
0
    }
1214
1215
7.85k
  for (ci = 0; ci < info.compsInScan; ci++)
1216
5.74k
    {
1217
5.74k
        info.MCUmembership [ci] = (int16) ci;
1218
5.74k
    }
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
2.10k
  int32 mcuSize = info.compsInScan * (uint32) sizeof (ComponentType);
1226
  
1227
2.10k
  mcuBuffer1.Allocate (info.imageWidth, sizeof (MCU));
1228
2.10k
  mcuBuffer2.Allocate (info.imageWidth, sizeof (MCU));
1229
  
1230
2.10k
  mcuROW1 = (MCU *) mcuBuffer1.Buffer ();
1231
2.10k
  mcuROW2 = (MCU *) mcuBuffer2.Buffer ();
1232
  
1233
2.10k
  mcuBuffer3.Allocate (info.imageWidth, mcuSize);
1234
2.10k
  mcuBuffer4.Allocate (info.imageWidth, mcuSize);
1235
  
1236
2.10k
  mcuROW1 [0] = (ComponentType *) mcuBuffer3.Buffer ();
1237
2.10k
  mcuROW2 [0] = (ComponentType *) mcuBuffer4.Buffer ();
1238
  
1239
626k
  for (int32 j = 1; j < info.imageWidth; j++)
1240
624k
    {
1241
    
1242
624k
    mcuROW1 [j] = mcuROW1 [j - 1] + info.compsInScan;
1243
624k
    mcuROW2 [j] = mcuROW2 [j - 1] + info.compsInScan;
1244
  
1245
624k
    }
1246
  
1247
2.10k
  }
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
1.93k
  {
1270
  
1271
    // Initialize bit parser state
1272
 
1273
1.93k
  getBuffer = 0;
1274
1.93k
    bitsLeft  = 0;
1275
    
1276
    // Prepare Huffman tables.
1277
1278
7.58k
    for (int16 ci = 0; ci < info.compsInScan; ci++)
1279
5.65k
      {
1280
      
1281
5.65k
    JpegComponentInfo *compptr = info.curCompInfo [ci];
1282
    
1283
    // Make sure requested tables are present
1284
    
1285
5.65k
    if (compptr->dcTblNo < 0 || compptr->dcTblNo > 3)
1286
24
      {
1287
24
      ThrowBadFormat ();
1288
24
      }
1289
1290
5.65k
    if (info.dcHuffTblPtrs [compptr->dcTblNo] == NULL) 
1291
24
      { 
1292
24
        ThrowBadFormat ();
1293
24
      }
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
5.65k
    FixHuffTbl (info.dcHuffTblPtrs [compptr->dcTblNo]);
1300
1301
5.65k
      }
1302
1303
    // Initialize restart stuff
1304
1305
1.93k
  info.restartInRows   = info.restartInterval / info.imageWidth;
1306
1.93k
    info.restartRowsToGo = info.restartInRows;
1307
1.93k
    info.nextRestartNum  = 0;
1308
    
1309
1.93k
  }
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
9.22k
  {
1331
  
1332
  // Throw away and unused odd bits in the bit buffer.
1333
  
1334
9.22k
  fStream->SetReadPosition (fStream->Position () - bitsLeft / 8);
1335
  
1336
9.22k
  bitsLeft  = 0;
1337
9.22k
  getBuffer = 0;
1338
  
1339
    // Scan for next JPEG marker
1340
1341
9.22k
    int32 c;
1342
1343
9.22k
    do
1344
12.0k
      {
1345
      
1346
    // skip any non-FF bytes
1347
    
1348
12.0k
    do 
1349
6.00M
      { 
1350
6.00M
        c = GetJpegChar ();
1351
6.00M
      }
1352
6.00M
    while (c != 0xFF);
1353
    
1354
    // skip any duplicate FFs
1355
    
1356
12.0k
    do
1357
497k
      {
1358
497k
      c = GetJpegChar ();
1359
497k
      }
1360
497k
    while (c == 0xFF);
1361
    
1362
12.0k
      }
1363
12.0k
    while (c == 0);    // repeat if it was a stuffed FF/00
1364
    
1365
    // Verify correct restart code.
1366
1367
9.22k
    if (c != (M_RST0 + info.nextRestartNum))
1368
103
      {
1369
103
    ThrowBadFormat ();
1370
103
      }
1371
1372
    // Update restart state.
1373
1374
9.22k
    info.restartRowsToGo = info.restartInRows;
1375
9.22k
    info.nextRestartNum  = (info.nextRestartNum + 1) & 7;
1376
    
1377
9.22k
  }
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
27.7M
  {
1407
  
1408
27.7M
    int32 diag  = prevRowBuf [col - 1] [curComp];
1409
27.7M
    int32 upper = prevRowBuf [col    ] [curComp];
1410
27.7M
    int32 left  = curRowBuf  [col - 1] [curComp];
1411
1412
27.7M
    switch (info.Ss)
1413
27.7M
      {
1414
      
1415
1.33M
    case 0:
1416
1.33M
      return 0;
1417
      
1418
23.0M
    case 1:
1419
23.0M
      return left;
1420
1421
1.28M
    case 2:
1422
1.28M
      return upper;
1423
1424
324k
    case 3:
1425
324k
      return diag;
1426
1427
45.2k
    case 4:
1428
45.2k
      return left + upper - diag;
1429
1430
200k
    case 5:
1431
200k
      return left + ((upper - diag) >> 1);
1432
1433
565k
    case 6:
1434
565k
          return upper + ((left - diag) >> 1);
1435
1436
862k
    case 7:
1437
862k
            return (left + upper) >> 1;
1438
1439
12
    default:
1440
12
      {
1441
12
      ThrowBadFormat ();
1442
12
            return 0;
1443
0
            }
1444
              
1445
27.7M
      }
1446
     
1447
27.7M
  }
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
22.5M
  {
1470
  
1471
22.5M
  const int32 kMinGetBits = sizeof (uint32) * 8 - 7;
1472
  
1473
22.5M
  #if qSupportHasselblad_3FR
1474
  
1475
22.5M
  if (fHasselblad3FR)
1476
1.41M
    {
1477
    
1478
2.83M
    while (bitsLeft < kMinGetBits)
1479
1.41M
      {
1480
      
1481
1.41M
      int32 c0 = GetJpegChar ();
1482
1.41M
      int32 c1 = GetJpegChar ();
1483
1.41M
      int32 c2 = GetJpegChar ();
1484
1.41M
      int32 c3 = GetJpegChar ();
1485
      
1486
1.41M
      getBuffer = (getBuffer << 8) | c3;
1487
1.41M
      getBuffer = (getBuffer << 8) | c2;
1488
1.41M
      getBuffer = (getBuffer << 8) | c1;
1489
1.41M
      getBuffer = (getBuffer << 8) | c0;
1490
      
1491
1.41M
      bitsLeft += 32;
1492
      
1493
1.41M
      }
1494
      
1495
1.41M
    return;
1496
    
1497
1.41M
    }
1498
  
1499
21.1M
  #endif
1500
  
1501
49.5M
    while (bitsLeft < kMinGetBits)
1502
46.8M
      {
1503
      
1504
46.8M
    int32 c = GetJpegChar ();
1505
1506
    // If it's 0xFF, check and discard stuffed zero byte
1507
1508
46.8M
    if (c == 0xFF)
1509
37.3M
      {
1510
      
1511
37.3M
      int32 c2 = GetJpegChar ();
1512
      
1513
37.3M
        if (c2 != 0)
1514
37.3M
          {
1515
1516
        // Oops, it's actually a marker indicating end of
1517
        // compressed data.  Better put it back for use later.
1518
1519
37.3M
        UnGetJpegChar ();
1520
37.3M
        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
37.3M
        if (bitsLeft >= nbits)
1526
18.4M
            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
18.9M
        c = 0;
1534
        
1535
18.9M
          }
1536
          
1537
37.3M
      }
1538
      
1539
28.3M
    getBuffer = (getBuffer << 8) | c;
1540
    
1541
28.3M
    bitsLeft += 8;
1542
    
1543
28.3M
      }
1544
 
1545
21.1M
  }
1546
1547
/*****************************************************************************/
1548
1549
inline int32 dng_lossless_decoder::show_bits8 ()
1550
63.6M
  {
1551
  
1552
63.6M
  if (bitsLeft < 8)
1553
15.8M
    FillBitBuffer (8);
1554
    
1555
63.6M
  return (int32) ((getBuffer >> (bitsLeft - 8)) & 0xff);
1556
  
1557
63.6M
  }
1558
1559
/*****************************************************************************/
1560
1561
inline void dng_lossless_decoder::flush_bits (int32 nbits)
1562
63.6M
  {
1563
  
1564
63.6M
  bitsLeft -= nbits;
1565
  
1566
63.6M
  }
1567
1568
/*****************************************************************************/
1569
1570
inline int32 dng_lossless_decoder::get_bits (int32 nbits)
1571
15.5M
  {
1572
  
1573
15.5M
  if (nbits > 16)
1574
462
    {
1575
462
    ThrowBadFormat ();
1576
462
    }
1577
  
1578
15.5M
  if (bitsLeft < nbits)
1579
3.07M
    FillBitBuffer (nbits);
1580
    
1581
15.5M
  return (int32) ((getBuffer >> (bitsLeft -= nbits)) & (0x0FFFF >> (16 - nbits)));
1582
  
1583
15.5M
  }
1584
1585
/*****************************************************************************/
1586
1587
inline int32 dng_lossless_decoder::get_bit ()
1588
52.2M
  {
1589
  
1590
52.2M
  if (!bitsLeft)
1591
3.64M
    FillBitBuffer (1);
1592
    
1593
52.2M
  return (int32) ((getBuffer >> (--bitsLeft)) & 1);
1594
  
1595
52.2M
  }
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
63.6M
  {
1618
  
1619
63.6M
  if (htbl == nullptr) {
1620
0
    ThrowBadFormat ();
1621
0
  }
1622
1623
    // If the huffman code is less than 8 bits, we can use the fast
1624
    // table lookup to get its value.  It's more than 8 bits about
1625
    // 3-4% of the time.
1626
1627
63.6M
    int32 code = show_bits8 ();
1628
    
1629
63.6M
    if (htbl->numbits [code])
1630
55.1M
      {
1631
      
1632
55.1M
    flush_bits (htbl->numbits [code]);
1633
    
1634
55.1M
    return htbl->value [code];
1635
    
1636
55.1M
      }
1637
      
1638
8.50M
    else
1639
8.50M
      {
1640
      
1641
8.50M
    flush_bits (8);
1642
    
1643
8.50M
    int32 l = 8;
1644
    
1645
60.7M
    while (code > htbl->maxcode [l]) 
1646
52.2M
      {
1647
52.2M
        code = (code << 1) | get_bit ();
1648
52.2M
        l++;
1649
52.2M
      }
1650
1651
    // With garbage input we may reach the sentinel value l = 17.
1652
1653
8.50M
    if (l > 16) 
1654
5.04M
      {
1655
5.04M
        return 0;   // fake a zero as the safest result
1656
5.04M
      }
1657
3.45M
    else
1658
3.45M
      {
1659
3.45M
        return htbl->huffval [htbl->valptr [l] +
1660
3.45M
                    ((int32) (code - htbl->mincode [l]))];
1661
3.45M
      }
1662
      
1663
8.50M
      }
1664
      
1665
63.6M
  }
1666
1667
/*****************************************************************************/
1668
1669
/*
1670
 *--------------------------------------------------------------
1671
 *
1672
 * HuffExtend --
1673
 *
1674
 *  Code and table for Figure F.12: extend sign bit
1675
 *
1676
 * Results:
1677
 *  The extended value.
1678
 *
1679
 * Side effects:
1680
 *  None.
1681
 *
1682
 *--------------------------------------------------------------
1683
 */
1684
1685
inline void dng_lossless_decoder::HuffExtend (int32 &x, int32 s)
1686
15.3M
  {
1687
1688
15.3M
  if (x < (0x08000 >> (16 - s)))
1689
14.3M
    {
1690
14.3M
    x += -(1 << s) + 1;
1691
14.3M
    }
1692
1693
15.3M
  }
1694
1695
/*****************************************************************************/
1696
1697
// Called from DecodeImage () to write one row.
1698
 
1699
void dng_lossless_decoder::PmPutRow (MCU *buf,
1700
                     int32 numComp,
1701
                     int32 numCol,
1702
                     int32 /* row */)
1703
9.81M
  {
1704
  
1705
9.81M
  uint16 *sPtr = &buf [0] [0];
1706
  
1707
9.81M
  uint32 pixels = numCol * numComp;
1708
  
1709
9.81M
  fSpooler->Spool (sPtr, pixels * (uint32) sizeof (uint16));
1710
    
1711
9.81M
  }
1712
1713
/*****************************************************************************/
1714
1715
/*
1716
 *--------------------------------------------------------------
1717
 *
1718
 * DecodeFirstRow --
1719
 *
1720
 *  Decode the first raster line of samples at the start of 
1721
 *      the scan and at the beginning of each restart interval.
1722
 *  This includes modifying the component value so the real
1723
 *      value, not the difference is returned.
1724
 *
1725
 * Results:
1726
 *  None.
1727
 *
1728
 * Side effects:
1729
 *  Bitstream is parsed.
1730
 *
1731
 *--------------------------------------------------------------
1732
 */
1733
 
1734
void dng_lossless_decoder::DecodeFirstRow (MCU *curRowBuf)
1735
10.1k
  {
1736
  
1737
10.1k
    int32 compsInScan = info.compsInScan;
1738
    
1739
    // Process the first column in the row.
1740
1741
40.3k
    for (int32 curComp = 0; curComp < compsInScan; curComp++) 
1742
30.2k
      {
1743
      
1744
30.2k
        int32 ci = info.MCUmembership [curComp];
1745
        
1746
30.2k
        JpegComponentInfo *compptr = info.curCompInfo [ci];
1747
        
1748
30.2k
        HuffmanTable *dctbl = info.dcHuffTblPtrs [compptr->dcTblNo];
1749
1750
        // Section F.2.2.1: decode the difference
1751
1752
30.2k
      int32 d = 0;
1753
  
1754
30.2k
        int32 s = HuffDecode (dctbl);
1755
        
1756
30.2k
        if (s)
1757
6.54k
          {
1758
          
1759
6.54k
          if (s == 16 && !fBug16)
1760
323
            {
1761
323
            d = -32768;
1762
323
            }
1763
          
1764
6.22k
          else
1765
6.22k
            {
1766
6.22k
        d = get_bits (s);
1767
6.22k
              HuffExtend (d, s);
1768
6.22k
              }
1769
1770
6.54k
            }
1771
1772
    // Add the predictor to the difference.
1773
1774
30.2k
      int32 Pr = info.dataPrecision;
1775
30.2k
      int32 Pt = info.Pt;
1776
    
1777
30.2k
        curRowBuf [0] [curComp] = (ComponentType) (d + (1 << (Pr-Pt-1)));
1778
        
1779
30.2k
      }
1780
      
1781
    // Process the rest of the row.
1782
    
1783
10.1k
    int32 numCOL = info.imageWidth;
1784
    
1785
78.3k
    for (int32 col = 1; col < numCOL; col++)
1786
68.2k
      {
1787
1788
273k
        for (int32 curComp = 0; curComp < compsInScan; curComp++)
1789
204k
          {
1790
          
1791
204k
            int32 ci = info.MCUmembership [curComp];
1792
            
1793
204k
            JpegComponentInfo *compptr = info.curCompInfo [ci];
1794
            
1795
204k
            HuffmanTable *dctbl = info.dcHuffTblPtrs [compptr->dcTblNo];
1796
1797
      // Section F.2.2.1: decode the difference
1798
1799
204k
        int32 d = 0;
1800
    
1801
204k
          int32 s = HuffDecode (dctbl);
1802
          
1803
204k
          if (s)
1804
26.7k
            {
1805
1806
26.7k
            if (s == 16 && !fBug16)
1807
3.85k
              {
1808
3.85k
              d = -32768;
1809
3.85k
              }
1810
            
1811
22.9k
            else
1812
22.9k
              {
1813
22.9k
          d = get_bits (s);
1814
22.9k
                HuffExtend (d, s);
1815
22.9k
                }
1816
1817
26.7k
              }
1818
              
1819
      // Add the predictor to the difference.
1820
1821
204k
            curRowBuf [col] [curComp] = (ComponentType) (d + curRowBuf [col-1] [curComp]);
1822
            
1823
204k
          }
1824
          
1825
68.2k
      }
1826
      
1827
    // Update the restart counter
1828
1829
10.1k
    if (info.restartInRows)
1830
9.29k
      {
1831
9.29k
        info.restartRowsToGo--;
1832
9.29k
      }
1833
      
1834
10.1k
  }
1835
1836
/*****************************************************************************/
1837
1838
/*
1839
 *--------------------------------------------------------------
1840
 *
1841
 * DecodeImage --
1842
 *
1843
 *      Decode the input stream. This includes modifying
1844
 *      the component value so the real value, not the
1845
 *      difference is returned.
1846
 *
1847
 * Results:
1848
 *      None.
1849
 *
1850
 * Side effects:
1851
 *      Bitstream is parsed.
1852
 *
1853
 *--------------------------------------------------------------
1854
 */
1855
 
1856
void dng_lossless_decoder::DecodeImage ()
1857
1.81k
  {
1858
  
1859
5.89M
  #define swap(type,a,b) {type c; c=(a); (a)=(b); (b)=c;}
1860
1861
1.81k
    int32 numCOL      = info.imageWidth;
1862
1.81k
    int32 numROW    = info.imageHeight;
1863
1.81k
    int32 compsInScan = info.compsInScan;
1864
    
1865
    // Precompute the decoding table for each table.
1866
    
1867
1.81k
    HuffmanTable *ht [4];
1868
    
1869
7.26k
  for (int32 curComp = 0; curComp < compsInScan; curComp++)
1870
5.44k
      {
1871
      
1872
5.44k
        int32 ci = info.MCUmembership [curComp];
1873
        
1874
5.44k
        JpegComponentInfo *compptr = info.curCompInfo [ci];
1875
        
1876
5.44k
        ht [curComp] = info.dcHuffTblPtrs [compptr->dcTblNo];
1877
1878
5.44k
      }
1879
    
1880
1.81k
  MCU *prevRowBuf = mcuROW1;
1881
1.81k
  MCU *curRowBuf  = mcuROW2;
1882
  
1883
1.81k
  #if qSupportCanon_sRAW
1884
    
1885
  // Canon sRAW support
1886
  
1887
1.81k
  if (info.compInfo [0].hSampFactor == 2 &&
1888
451
    info.compInfo [0].vSampFactor == 1)
1889
196
    {
1890
  
1891
815k
    for (int32 row = 0; row < numROW; row++)
1892
815k
      {
1893
      
1894
      // Initialize predictors.
1895
      
1896
815k
      int32 p0;
1897
815k
      int32 p1;
1898
815k
      int32 p2;
1899
      
1900
815k
      if (row == 0)
1901
196
        {
1902
196
        p0 = 1 << 14;
1903
196
        p1 = 1 << 14;
1904
196
        p2 = 1 << 14;
1905
196
        }
1906
        
1907
815k
      else
1908
815k
        {
1909
815k
        p0 = prevRowBuf [0] [0];
1910
815k
        p1 = prevRowBuf [0] [1];
1911
815k
        p2 = prevRowBuf [0] [2];
1912
815k
        }
1913
      
1914
2.42M
      for (int32 col = 0; col < numCOL; col += 2)
1915
1.61M
        {
1916
        
1917
        // Read first luminance component.
1918
        
1919
1.61M
          {
1920
        
1921
1.61M
          int32 d = 0;
1922
        
1923
1.61M
          int32 s = HuffDecode (ht [0]);
1924
          
1925
1.61M
          if (s)
1926
663k
            {
1927
1928
663k
            if (s == 16)
1929
197k
              {
1930
197k
              d = -32768;
1931
197k
              }
1932
            
1933
465k
            else
1934
465k
              {
1935
465k
              d = get_bits (s);
1936
465k
              HuffExtend (d, s);
1937
465k
              }
1938
1939
663k
            }
1940
            
1941
1.61M
          p0 += d;
1942
          
1943
1.61M
          curRowBuf [col] [0] = (ComponentType) p0;
1944
        
1945
1.61M
          }
1946
        
1947
        // Read second luminance component.
1948
        
1949
1.61M
          {
1950
        
1951
1.61M
          int32 d = 0;
1952
        
1953
1.61M
          int32 s = HuffDecode (ht [0]);
1954
          
1955
1.61M
          if (s)
1956
663k
            {
1957
1958
663k
            if (s == 16)
1959
197k
              {
1960
197k
              d = -32768;
1961
197k
              }
1962
            
1963
466k
            else
1964
466k
              {
1965
466k
              d = get_bits (s);
1966
466k
              HuffExtend (d, s);
1967
466k
              }
1968
1969
663k
            }
1970
            
1971
1.61M
          p0 += d;
1972
          
1973
1.61M
          curRowBuf [col + 1] [0] = (ComponentType) p0;
1974
        
1975
1.61M
          }
1976
        
1977
        // Read first chroma component.
1978
        
1979
1.61M
          {
1980
        
1981
1.61M
          int32 d = 0;
1982
        
1983
1.61M
          int32 s = HuffDecode (ht [1]);
1984
          
1985
1.61M
          if (s)
1986
630k
            {
1987
1988
630k
            if (s == 16)
1989
197k
              {
1990
197k
              d = -32768;
1991
197k
              }
1992
            
1993
433k
            else
1994
433k
              {
1995
433k
              d = get_bits (s);
1996
433k
              HuffExtend (d, s);
1997
433k
              }
1998
1999
630k
            }
2000
            
2001
1.61M
          p1 += d;
2002
          
2003
1.61M
          curRowBuf [col    ] [1] = (ComponentType) p1;
2004
1.61M
          curRowBuf [col + 1] [1] = (ComponentType) p1;
2005
        
2006
1.61M
          }
2007
        
2008
        // Read second chroma component.
2009
        
2010
1.61M
          {
2011
        
2012
1.61M
          int32 d = 0;
2013
        
2014
1.61M
          int32 s = HuffDecode (ht [2]);
2015
          
2016
1.61M
          if (s)
2017
631k
            {
2018
2019
631k
            if (s == 16)
2020
197k
              {
2021
197k
              d = -32768;
2022
197k
              }
2023
            
2024
433k
            else
2025
433k
              {
2026
433k
              d = get_bits (s);
2027
433k
              HuffExtend (d, s);
2028
433k
              }
2029
2030
631k
            }
2031
            
2032
1.61M
          p2 += d;
2033
          
2034
1.61M
          curRowBuf [col    ] [2] = (ComponentType) p2;
2035
1.61M
          curRowBuf [col + 1] [2] = (ComponentType) p2;
2036
        
2037
1.61M
          }
2038
                
2039
1.61M
        }
2040
      
2041
815k
      PmPutRow (curRowBuf, compsInScan, numCOL, row);
2042
2043
815k
      swap (MCU *, prevRowBuf, curRowBuf);
2044
      
2045
815k
      }
2046
      
2047
196
    return;
2048
    
2049
196
    }
2050
    
2051
1.61k
  if (info.compInfo [0].hSampFactor == 2 &&
2052
255
    info.compInfo [0].vSampFactor == 2)
2053
255
    {
2054
  
2055
1.46M
    for (int32 row = 0; row < numROW; row += 2)
2056
1.46M
      {
2057
      
2058
      // Initialize predictors.
2059
      
2060
1.46M
      int32 p0;
2061
1.46M
      int32 p1;
2062
1.46M
      int32 p2;
2063
      
2064
1.46M
      if (row == 0)
2065
255
        {
2066
255
        p0 = 1 << 14;
2067
255
        p1 = 1 << 14;
2068
255
        p2 = 1 << 14;
2069
255
        }
2070
        
2071
1.46M
      else
2072
1.46M
        {
2073
1.46M
        p0 = prevRowBuf [0] [0];
2074
1.46M
        p1 = prevRowBuf [0] [1];
2075
1.46M
        p2 = prevRowBuf [0] [2];
2076
1.46M
        }
2077
      
2078
2.98M
      for (int32 col = 0; col < numCOL; col += 2)
2079
1.51M
        {
2080
        
2081
        // Read first luminance component.
2082
        
2083
1.51M
          {
2084
        
2085
1.51M
          int32 d = 0;
2086
        
2087
1.51M
          int32 s = HuffDecode (ht [0]);
2088
          
2089
1.51M
          if (s)
2090
867k
            {
2091
2092
867k
            if (s == 16)
2093
226k
              {
2094
226k
              d = -32768;
2095
226k
              }
2096
            
2097
640k
            else
2098
640k
              {
2099
640k
              d = get_bits (s);
2100
640k
              HuffExtend (d, s);
2101
640k
              }
2102
2103
867k
            }
2104
            
2105
1.51M
          p0 += d;
2106
          
2107
1.51M
          prevRowBuf [col] [0] = (ComponentType) p0;
2108
        
2109
1.51M
          }
2110
        
2111
        // Read second luminance component.
2112
        
2113
1.51M
          {
2114
        
2115
1.51M
          int32 d = 0;
2116
        
2117
1.51M
          int32 s = HuffDecode (ht [0]);
2118
          
2119
1.51M
          if (s)
2120
866k
            {
2121
2122
866k
            if (s == 16)
2123
226k
              {
2124
226k
              d = -32768;
2125
226k
              }
2126
            
2127
640k
            else
2128
640k
              {
2129
640k
              d = get_bits (s);
2130
640k
              HuffExtend (d, s);
2131
640k
              }
2132
2133
866k
            }
2134
            
2135
1.51M
          p0 += d;
2136
          
2137
1.51M
          prevRowBuf [col + 1] [0] = (ComponentType) p0;
2138
        
2139
1.51M
          }
2140
        
2141
        // Read third luminance component.
2142
        
2143
1.51M
          {
2144
        
2145
1.51M
          int32 d = 0;
2146
        
2147
1.51M
          int32 s = HuffDecode (ht [0]);
2148
          
2149
1.51M
          if (s)
2150
867k
            {
2151
2152
867k
            if (s == 16)
2153
226k
              {
2154
226k
              d = -32768;
2155
226k
              }
2156
            
2157
641k
            else
2158
641k
              {
2159
641k
              d = get_bits (s);
2160
641k
              HuffExtend (d, s);
2161
641k
              }
2162
2163
867k
            }
2164
            
2165
1.51M
          p0 += d;
2166
          
2167
1.51M
          curRowBuf [col] [0] = (ComponentType) p0;
2168
        
2169
1.51M
          }
2170
        
2171
        // Read fourth luminance component.
2172
        
2173
1.51M
          {
2174
        
2175
1.51M
          int32 d = 0;
2176
        
2177
1.51M
          int32 s = HuffDecode (ht [0]);
2178
          
2179
1.51M
          if (s)
2180
866k
            {
2181
2182
866k
            if (s == 16)
2183
226k
              {
2184
226k
              d = -32768;
2185
226k
              }
2186
            
2187
639k
            else
2188
639k
              {
2189
639k
              d = get_bits (s);
2190
639k
              HuffExtend (d, s);
2191
639k
              }
2192
2193
866k
            }
2194
            
2195
1.51M
          p0 += d;
2196
          
2197
1.51M
          curRowBuf [col + 1] [0] = (ComponentType) p0;
2198
        
2199
1.51M
          }
2200
        
2201
        // Read first chroma component.
2202
        
2203
1.51M
          {
2204
        
2205
1.51M
          int32 d = 0;
2206
        
2207
1.51M
          int32 s = HuffDecode (ht [1]);
2208
          
2209
1.51M
          if (s)
2210
866k
            {
2211
2212
866k
            if (s == 16)
2213
225k
              {
2214
225k
              d = -32768;
2215
225k
              }
2216
            
2217
640k
            else
2218
640k
              {
2219
640k
              d = get_bits (s);
2220
640k
              HuffExtend (d, s);
2221
640k
              }
2222
2223
866k
            }
2224
            
2225
1.51M
          p1 += d;
2226
          
2227
1.51M
          prevRowBuf [col    ] [1] = (ComponentType) p1;
2228
1.51M
          prevRowBuf [col + 1] [1] = (ComponentType) p1;
2229
2230
1.51M
          curRowBuf [col    ] [1] = (ComponentType) p1;
2231
1.51M
          curRowBuf [col + 1] [1] = (ComponentType) p1;
2232
        
2233
1.51M
          }
2234
        
2235
        // Read second chroma component.
2236
        
2237
1.51M
          {
2238
        
2239
1.51M
          int32 d = 0;
2240
        
2241
1.51M
          int32 s = HuffDecode (ht [2]);
2242
          
2243
1.51M
          if (s)
2244
867k
            {
2245
2246
867k
            if (s == 16)
2247
226k
              {
2248
226k
              d = -32768;
2249
226k
              }
2250
            
2251
640k
            else
2252
640k
              {
2253
640k
              d = get_bits (s);
2254
640k
              HuffExtend (d, s);
2255
640k
              }
2256
2257
867k
            }
2258
            
2259
1.51M
          p2 += d;
2260
          
2261
1.51M
          prevRowBuf [col    ] [2] = (ComponentType) p2;
2262
1.51M
          prevRowBuf [col + 1] [2] = (ComponentType) p2;
2263
        
2264
1.51M
          curRowBuf [col    ] [2] = (ComponentType) p2;
2265
1.51M
          curRowBuf [col + 1] [2] = (ComponentType) p2;
2266
        
2267
1.51M
          }
2268
                
2269
1.51M
        }
2270
      
2271
1.46M
      PmPutRow (prevRowBuf, compsInScan, numCOL, row);
2272
1.46M
      PmPutRow (curRowBuf, compsInScan, numCOL, row);
2273
2274
1.46M
      }
2275
      
2276
255
    return;
2277
    
2278
255
    }
2279
2280
1.36k
  #endif
2281
  
2282
1.36k
  #if qSupportHasselblad_3FR
2283
  
2284
1.36k
  if (info.Ss == 8 && (numCOL & 1) == 0)
2285
325
    {
2286
    
2287
325
    fHasselblad3FR = true;
2288
    
2289
989k
    for (int32 row = 0; row < numROW; row++)
2290
988k
      {
2291
      
2292
988k
      int32 p0 = 32768;
2293
988k
      int32 p1 = 32768;
2294
      
2295
3.44M
      for (int32 col = 0; col < numCOL; col += 2)
2296
2.45M
        {
2297
        
2298
2.45M
        int32 s0 = HuffDecode (ht [0]);
2299
2.45M
        int32 s1 = HuffDecode (ht [0]);
2300
        
2301
2.45M
        if (s0)
2302
1.40M
          {
2303
1.40M
          int32 d = get_bits (s0);
2304
1.40M
          if (s0 == 16)
2305
137k
            {
2306
137k
            d = -32768;
2307
137k
            }
2308
1.27M
          else
2309
1.27M
            {
2310
1.27M
            HuffExtend (d, s0);
2311
1.27M
            }
2312
1.40M
          p0 += d;
2313
1.40M
          }
2314
2315
2.45M
        if (s1)
2316
1.43M
          {
2317
1.43M
          int32 d = get_bits (s1);
2318
1.43M
          if (s1 == 16)
2319
135k
            {
2320
135k
            d = -32768;
2321
135k
            }
2322
1.29M
          else
2323
1.29M
            {
2324
1.29M
            HuffExtend (d, s1);
2325
1.29M
            }
2326
1.43M
          p1 += d;
2327
1.43M
          }
2328
2329
2.45M
        curRowBuf [col    ] [0] = (ComponentType) p0;
2330
2.45M
        curRowBuf [col + 1] [0] = (ComponentType) p1;
2331
        
2332
2.45M
        }
2333
      
2334
988k
      PmPutRow (curRowBuf, compsInScan, numCOL, row);
2335
2336
988k
      }
2337
2338
325
    return;
2339
    
2340
325
    }
2341
  
2342
1.03k
  #endif
2343
  
2344
    // Decode the first row of image. Output the row and
2345
    // turn this row into a previous row for later predictor
2346
    // calculation.
2347
2348
1.03k
    DecodeFirstRow (mcuROW1);
2349
    
2350
1.03k
    PmPutRow (mcuROW1, compsInScan, numCOL, 0);
2351
    
2352
  // Process each row.
2353
2354
5.07M
    for (int32 row = 1; row < numROW; row++)
2355
5.07M
      {
2356
2357
        // Account for restart interval, process restart marker if needed.
2358
2359
5.07M
    if (info.restartInRows)
2360
358k
      {
2361
      
2362
358k
      if (info.restartRowsToGo == 0)
2363
9.22k
        {
2364
        
2365
9.22k
        ProcessRestart ();
2366
            
2367
                // Reset predictors at restart.
2368
                
2369
9.22k
        DecodeFirstRow (curRowBuf);
2370
        
2371
9.22k
        PmPutRow (curRowBuf, compsInScan, numCOL, row);
2372
        
2373
9.22k
        swap (MCU *, prevRowBuf, curRowBuf);
2374
        
2375
9.22k
        continue;
2376
        
2377
9.22k
              }
2378
              
2379
349k
      info.restartRowsToGo--;
2380
           
2381
349k
      }
2382
      
2383
    // The upper neighbors are predictors for the first column.
2384
2385
20.2M
        for (int32 curComp = 0; curComp < compsInScan; curComp++)
2386
15.2M
          {
2387
          
2388
          // Section F.2.2.1: decode the difference
2389
2390
15.2M
        int32 d = 0;
2391
    
2392
15.2M
          int32 s = HuffDecode (ht [curComp]);
2393
          
2394
15.2M
          if (s)
2395
3.21M
            {
2396
2397
3.21M
            if (s == 16 && !fBug16)
2398
307k
              {
2399
307k
              d = -32768;
2400
307k
              }
2401
            
2402
2.91M
            else
2403
2.91M
              {
2404
2.91M
          d = get_bits (s);
2405
2.91M
                HuffExtend (d, s);
2406
2.91M
                }
2407
2408
3.21M
              }
2409
              
2410
          // First column of row above is predictor for first column.
2411
2412
15.2M
            curRowBuf [0] [curComp] = (ComponentType) (d + prevRowBuf [0] [curComp]);
2413
            
2414
15.2M
      }
2415
2416
        // For the rest of the column on this row, predictor
2417
        // calculations are based on PSV. 
2418
2419
5.06M
      if (compsInScan == 2 && info.Ss == 1 && numCOL > 1)
2420
0
        {
2421
        
2422
        // This is the combination used by both the Canon and Kodak raw formats. 
2423
        // Unrolling the general case logic results in a significant speed increase.
2424
        
2425
0
        uint16 *dPtr = &curRowBuf [1] [0];
2426
        
2427
0
        int32 prev0 = dPtr [-2];
2428
0
        int32 prev1 = dPtr [-1];
2429
        
2430
0
      for (int32 col = 1; col < numCOL; col++)
2431
0
            {
2432
            
2433
0
            int32 s = HuffDecode (ht [0]);
2434
            
2435
0
            if (s)
2436
0
              {
2437
              
2438
0
              int32 d;
2439
2440
0
              if (s == 16 && !fBug16)
2441
0
                {
2442
0
                d = -32768;
2443
0
                }
2444
              
2445
0
              else
2446
0
                {
2447
0
            d = get_bits (s);
2448
0
                  HuffExtend (d, s);
2449
0
                  }
2450
2451
0
              prev0 += d;
2452
              
2453
0
                }
2454
                
2455
0
            s = HuffDecode (ht [1]);
2456
            
2457
0
            if (s)
2458
0
              {
2459
              
2460
0
              int32 d;
2461
2462
0
              if (s == 16 && !fBug16)
2463
0
                {
2464
0
                d = -32768;
2465
0
                }
2466
              
2467
0
              else
2468
0
                {
2469
0
            d = get_bits (s);
2470
0
                  HuffExtend (d, s);
2471
0
                  }
2472
2473
0
          prev1 += d;
2474
          
2475
0
                }
2476
            
2477
0
        dPtr [0] = (uint16) prev0;
2478
0
        dPtr [1] = (uint16) prev1;
2479
        
2480
0
        dPtr += 2;
2481
        
2482
0
            }
2483
            
2484
0
          }
2485
          
2486
5.06M
        else
2487
5.06M
          {
2488
          
2489
14.3M
      for (int32 col = 1; col < numCOL; col++)
2490
9.23M
            {
2491
            
2492
36.9M
              for (int32 curComp = 0; curComp < compsInScan; curComp++)
2493
27.7M
                {
2494
                
2495
              // Section F.2.2.1: decode the difference
2496
2497
27.7M
            int32 d = 0;
2498
        
2499
27.7M
              int32 s = HuffDecode (ht [curComp]);
2500
              
2501
27.7M
              if (s)
2502
5.03M
                {
2503
                
2504
5.03M
                if (s == 16 && !fBug16)
2505
860k
                  {
2506
860k
                  d = -32768;
2507
860k
                  }
2508
                
2509
4.17M
                else
2510
4.17M
                  {
2511
4.17M
              d = get_bits (s);
2512
4.17M
                    HuffExtend (d, s);
2513
4.17M
                    }
2514
2515
5.03M
                  }
2516
                  
2517
              // Predict the pixel value.
2518
                
2519
27.7M
                  int32 predictor = QuickPredict (col,
2520
27.7M
                                    curComp,
2521
27.7M
                                    curRowBuf,
2522
27.7M
                                    prevRowBuf);
2523
                                  
2524
                  // Save the difference.
2525
2526
27.7M
                  curRowBuf [col] [curComp] = (ComponentType) (d + predictor);
2527
                  
2528
27.7M
          }
2529
          
2530
9.23M
        }
2531
2532
5.06M
          }
2533
2534
5.06M
    PmPutRow (curRowBuf, compsInScan, numCOL, row);
2535
    
2536
5.06M
    swap (MCU *, prevRowBuf, curRowBuf);
2537
    
2538
5.06M
      }
2539
      
2540
1.03k
    #undef swap
2541
  
2542
1.03k
  }
2543
2544
/*****************************************************************************/
2545
2546
void dng_lossless_decoder::StartRead (uint32 &imageWidth,
2547
                      uint32 &imageHeight,
2548
                      uint32 &imageChannels)
2549
3.70k
  { 
2550
  
2551
3.70k
  ReadFileHeader    ();
2552
3.70k
  ReadScanHeader    ();
2553
3.70k
  DecoderStructInit ();
2554
3.70k
  HuffDecoderInit   ();
2555
  
2556
3.70k
  imageWidth    = info.imageWidth;
2557
3.70k
  imageHeight   = info.imageHeight;
2558
3.70k
  imageChannels = info.compsInScan;
2559
  
2560
3.70k
  }
2561
2562
/*****************************************************************************/
2563
2564
void dng_lossless_decoder::FinishRead ()
2565
1.81k
  {
2566
  
2567
1.81k
  DecodeImage ();
2568
    
2569
1.81k
  }
2570
2571
/*****************************************************************************/
2572
2573
void DecodeLosslessJPEG (dng_stream &stream,
2574
               dng_spooler &spooler,
2575
               uint32 minDecodedSize,
2576
               uint32 maxDecodedSize,
2577
             bool bug16)
2578
3.70k
  {
2579
  
2580
3.70k
  dng_lossless_decoder decoder (&stream,
2581
3.70k
                    &spooler,
2582
3.70k
                    bug16);
2583
  
2584
3.70k
  uint32 imageWidth;
2585
3.70k
  uint32 imageHeight;
2586
3.70k
  uint32 imageChannels;
2587
  
2588
3.70k
  decoder.StartRead (imageWidth,
2589
3.70k
             imageHeight,
2590
3.70k
             imageChannels);
2591
             
2592
3.70k
  uint32 decodedSize = imageWidth    *
2593
3.70k
             imageHeight   *
2594
3.70k
             imageChannels *
2595
3.70k
             (uint32) sizeof (uint16);
2596
             
2597
3.70k
  if (decodedSize < minDecodedSize ||
2598
1.85k
    decodedSize > maxDecodedSize)
2599
56
    {
2600
56
    ThrowBadFormat ();
2601
56
    }
2602
  
2603
3.70k
  decoder.FinishRead ();
2604
  
2605
3.70k
  }
2606
2607
/*****************************************************************************/
2608
2609
class dng_lossless_encoder
2610
  {
2611
  
2612
  private:
2613
  
2614
    const uint16 *fSrcData;
2615
    
2616
    uint32 fSrcRows;
2617
    uint32 fSrcCols;
2618
    uint32 fSrcChannels;
2619
    uint32 fSrcBitDepth;
2620
    
2621
    int32 fSrcRowStep;
2622
    int32 fSrcColStep;
2623
  
2624
    dng_stream &fStream;
2625
  
2626
    HuffmanTable huffTable [4];
2627
    
2628
    uint32 freqCount [4] [257];
2629
    
2630
    // Current bit-accumulation buffer
2631
2632
    int32 huffPutBuffer;
2633
    int32 huffPutBits;
2634
    
2635
    // Lookup table for number of bits in an 8 bit value.
2636
    
2637
    int numBitsTable [256];
2638
    
2639
  public:
2640
  
2641
    dng_lossless_encoder (const uint16 *srcData,
2642
                  uint32 srcRows,
2643
                  uint32 srcCols,
2644
                  uint32 srcChannels,
2645
                  uint32 srcBitDepth,
2646
                  int32 srcRowStep,
2647
                  int32 srcColStep,
2648
                  dng_stream &stream);
2649
    
2650
    void Encode ();
2651
    
2652
  private:
2653
  
2654
    void EmitByte (uint8 value);
2655
  
2656
    void EmitBits (int code, int size);
2657
2658
    void FlushBits ();
2659
2660
    void CountOneDiff (int diff, uint32 *countTable);
2661
2662
    void EncodeOneDiff (int diff, HuffmanTable *dctbl);
2663
    
2664
    void FreqCountSet ();
2665
2666
    void HuffEncode ();
2667
2668
    void GenHuffCoding (HuffmanTable *htbl, uint32 *freq);
2669
2670
    void HuffOptimize ();
2671
2672
    void EmitMarker (JpegMarker mark);
2673
2674
    void Emit2bytes (int value);
2675
2676
    void EmitDht (int index);
2677
2678
    void EmitSof (JpegMarker code);
2679
2680
    void EmitSos ();
2681
2682
    void WriteFileHeader ();
2683
2684
    void WriteScanHeader ();
2685
2686
    void WriteFileTrailer ();
2687
2688
  };
2689
  
2690
/*****************************************************************************/
2691
2692
dng_lossless_encoder::dng_lossless_encoder (const uint16 *srcData,
2693
                      uint32 srcRows,
2694
                      uint32 srcCols,
2695
                      uint32 srcChannels,
2696
                      uint32 srcBitDepth,
2697
                      int32 srcRowStep,
2698
                      int32 srcColStep,
2699
                      dng_stream &stream)
2700
                    
2701
83.9k
  : fSrcData     (srcData    )
2702
83.9k
  , fSrcRows     (srcRows    )
2703
83.9k
  , fSrcCols     (srcCols    )
2704
83.9k
  , fSrcChannels (srcChannels)
2705
83.9k
  , fSrcBitDepth (srcBitDepth)
2706
83.9k
  , fSrcRowStep  (srcRowStep )
2707
83.9k
  , fSrcColStep  (srcColStep )
2708
83.9k
  , fStream      (stream     )
2709
  
2710
83.9k
  , huffPutBuffer (0)
2711
83.9k
  , huffPutBits   (0)
2712
  
2713
83.9k
  {
2714
  
2715
    // Initialize number of bits lookup table.
2716
    
2717
83.9k
    numBitsTable [0] = 0;
2718
      
2719
21.4M
    for (int i = 1; i < 256; i++)
2720
21.4M
      {
2721
      
2722
21.4M
    int temp = i;
2723
21.4M
    int nbits = 1;
2724
    
2725
150M
    while (temp >>= 1)
2726
129M
      {
2727
129M
        nbits++;
2728
129M
      }
2729
      
2730
21.4M
    numBitsTable [i] = nbits;
2731
    
2732
21.4M
      }
2733
      
2734
83.9k
  }
2735
2736
/*****************************************************************************/
2737
2738
inline void dng_lossless_encoder::EmitByte (uint8 value)
2739
782M
  {
2740
  
2741
782M
  fStream.Put_uint8 (value);
2742
  
2743
782M
  }
2744
  
2745
/*****************************************************************************/
2746
2747
/*
2748
 *--------------------------------------------------------------
2749
 *
2750
 * EmitBits --
2751
 *
2752
 *  Code for outputting bits to the file
2753
 *
2754
 *  Only the right 24 bits of huffPutBuffer are used; the valid
2755
 *  bits are left-justified in this part.  At most 16 bits can be
2756
 *  passed to EmitBits in one call, and we never retain more than 7
2757
 *  bits in huffPutBuffer between calls, so 24 bits are
2758
 *  sufficient.
2759
 *
2760
 * Results:
2761
 *  None.
2762
 *
2763
 * Side effects:
2764
 *  huffPutBuffer and huffPutBits are updated.
2765
 *
2766
 *--------------------------------------------------------------
2767
 */
2768
 
2769
inline void dng_lossless_encoder::EmitBits (int code, int size)
2770
2.16G
  {
2771
  
2772
2.16G
    DNG_ASSERT (size != 0, "Bad Huffman table entry");
2773
2774
2.16G
    int putBits   = size;
2775
2.16G
  int putBuffer = code;
2776
    
2777
2.16G
    putBits += huffPutBits;
2778
    
2779
2.16G
    putBuffer <<= 24 - putBits;
2780
2.16G
    putBuffer |= huffPutBuffer;
2781
2782
2.93G
    while (putBits >= 8)
2783
771M
      {
2784
      
2785
771M
    uint8 c = (uint8) (putBuffer >> 16);
2786
2787
    // Output whole bytes we've accumulated with byte stuffing
2788
2789
771M
    EmitByte (c);
2790
    
2791
771M
    if (c == 0xFF)
2792
3.70M
      {
2793
3.70M
        EmitByte (0);
2794
3.70M
      }
2795
2796
771M
    putBuffer <<= 8;
2797
771M
    putBits -= 8;
2798
    
2799
771M
      }
2800
2801
2.16G
    huffPutBuffer = putBuffer;
2802
2.16G
    huffPutBits   = putBits;
2803
    
2804
2.16G
  }
2805
2806
/*****************************************************************************/
2807
2808
/*
2809
 *--------------------------------------------------------------
2810
 *
2811
 * FlushBits --
2812
 *
2813
 *  Flush any remaining bits in the bit buffer. Used before emitting
2814
 *  a marker.
2815
 *
2816
 * Results:
2817
 *  None.
2818
 *
2819
 * Side effects:
2820
 *  huffPutBuffer and huffPutBits are reset
2821
 *
2822
 *--------------------------------------------------------------
2823
 */
2824
2825
void dng_lossless_encoder::FlushBits ()
2826
83.9k
  {
2827
  
2828
    // The first call forces output of any partial bytes.
2829
2830
83.9k
    EmitBits (0x007F, 7);
2831
    
2832
    // We can then zero the buffer.
2833
2834
83.9k
    huffPutBuffer = 0;
2835
83.9k
    huffPutBits   = 0;
2836
    
2837
83.9k
  }
2838
2839
/*****************************************************************************/
2840
2841
/*
2842
 *--------------------------------------------------------------
2843
 *
2844
 * CountOneDiff --
2845
 *
2846
 *      Count the difference value in countTable.
2847
 *
2848
 * Results:
2849
 *      diff is counted in countTable.
2850
 *
2851
 * Side effects:
2852
 *      None. 
2853
 *
2854
 *--------------------------------------------------------------
2855
 */
2856
2857
inline void dng_lossless_encoder::CountOneDiff (int diff, uint32 *countTable)
2858
1.85G
  {
2859
  
2860
    // Encode the DC coefficient difference per section F.1.2.1
2861
     
2862
1.85G
    int temp = diff;
2863
    
2864
1.85G
    if (temp < 0)
2865
152M
      {
2866
      
2867
152M
    temp = -temp;
2868
 
2869
152M
      }
2870
2871
    // Find the number of bits needed for the magnitude of the coefficient
2872
2873
1.85G
    int nbits = temp >= 256 ? numBitsTable [temp >> 8  ] + 8
2874
1.85G
                : numBitsTable [temp & 0xFF];
2875
          
2876
    // Update count for this bit length
2877
2878
1.85G
    countTable [nbits] ++;
2879
    
2880
1.85G
  }
2881
2882
/*****************************************************************************/
2883
2884
/*
2885
 *--------------------------------------------------------------
2886
 *
2887
 * EncodeOneDiff --
2888
 *
2889
 *  Encode a single difference value.
2890
 *
2891
 * Results:
2892
 *  None.
2893
 *
2894
 * Side effects:
2895
 *  None.
2896
 *
2897
 *--------------------------------------------------------------
2898
 */
2899
2900
inline void dng_lossless_encoder::EncodeOneDiff (int diff, HuffmanTable *dctbl)
2901
1.85G
  {
2902
2903
    // Encode the DC coefficient difference per section F.1.2.1
2904
     
2905
1.85G
    int temp  = diff;
2906
1.85G
    int temp2 = diff;
2907
    
2908
1.85G
    if (temp < 0)
2909
152M
      {
2910
      
2911
152M
    temp = -temp;
2912
    
2913
    // For a negative input, want temp2 = bitwise complement of
2914
    // abs (input).  This code assumes we are on a two's complement
2915
    // machine.
2916
2917
152M
    temp2--;
2918
    
2919
152M
      }
2920
2921
    // Find the number of bits needed for the magnitude of the coefficient
2922
2923
1.85G
    int nbits = temp >= 256 ? numBitsTable [temp >> 8  ] + 8
2924
1.85G
                : numBitsTable [temp & 0xFF];
2925
2926
    // Emit the Huffman-coded symbol for the number of bits
2927
2928
1.85G
    EmitBits (dctbl->ehufco [nbits],
2929
1.85G
          dctbl->ehufsi [nbits]);
2930
2931
    // Emit that number of bits of the value, if positive,
2932
    // or the complement of its magnitude, if negative.
2933
    
2934
    // If the number of bits is 16, there is only one possible difference
2935
    // value (-32786), so the lossless JPEG spec says not to output anything
2936
    // in that case.  So we only need to output the diference value if
2937
    // the number of bits is between 1 and 15.
2938
2939
1.85G
    if (nbits & 15)
2940
309M
      {
2941
      
2942
309M
    EmitBits (temp2 & (0x0FFFF >> (16 - nbits)),
2943
309M
          nbits);
2944
    
2945
309M
    }
2946
2947
1.85G
  }
2948
2949
/*****************************************************************************/
2950
2951
/*
2952
 *--------------------------------------------------------------
2953
 *
2954
 * FreqCountSet --
2955
 *
2956
 *      Count the times each category symbol occurs in this image.
2957
 *
2958
 * Results:
2959
 *  None.
2960
 *
2961
 * Side effects:
2962
 *  The freqCount has counted all category 
2963
 *  symbols appeared in the image.        
2964
 *
2965
 *--------------------------------------------------------------
2966
 */
2967
2968
void dng_lossless_encoder::FreqCountSet ()
2969
83.9k
  {
2970
    
2971
83.9k
  memset (freqCount, 0, sizeof (freqCount));
2972
  
2973
83.9k
  DNG_ASSERT ((int32)fSrcRows >= 0, "dng_lossless_encoder::FreqCountSet: fSrcRpws too large.");
2974
2975
13.3M
    for (int32 row = 0; row < (int32)fSrcRows; row++)
2976
13.3M
      {
2977
      
2978
13.3M
    const uint16 *sPtr = fSrcData + row * fSrcRowStep;
2979
    
2980
    // Initialize predictors for this row.
2981
    
2982
13.3M
    int32 predictor [4];
2983
    
2984
29.4M
    for (int32 channel = 0; channel < (int32)fSrcChannels; channel++)
2985
16.1M
      {
2986
      
2987
16.1M
      if (row == 0)
2988
146k
        predictor [channel] = 1 << (fSrcBitDepth - 1);
2989
        
2990
16.0M
      else
2991
16.0M
        predictor [channel] = sPtr [channel - fSrcRowStep];
2992
      
2993
16.1M
      }
2994
      
2995
    // Unroll most common case of two channels
2996
    
2997
13.3M
    if (fSrcChannels == 2)
2998
192k
      {
2999
      
3000
192k
      int32 pred0 = predictor [0];
3001
192k
      int32 pred1 = predictor [1];
3002
      
3003
192k
      uint32 srcCols    = fSrcCols;
3004
192k
      int32  srcColStep = fSrcColStep;
3005
      
3006
23.8M
        for (uint32 col = 0; col < srcCols; col++)
3007
23.6M
          {
3008
          
3009
23.6M
          int32 pixel0 = sPtr [0];
3010
23.6M
        int32 pixel1 = sPtr [1];
3011
          
3012
23.6M
          int16 diff0 = (int16) (pixel0 - pred0);
3013
23.6M
          int16 diff1 = (int16) (pixel1 - pred1);
3014
          
3015
23.6M
          CountOneDiff (diff0, freqCount [0]);
3016
23.6M
          CountOneDiff (diff1, freqCount [1]);
3017
          
3018
23.6M
          pred0 = pixel0;
3019
23.6M
          pred1 = pixel1;
3020
            
3021
23.6M
          sPtr += srcColStep;
3022
            
3023
23.6M
          }
3024
      
3025
192k
      }
3026
      
3027
    // General case.
3028
      
3029
13.1M
    else
3030
13.1M
      {
3031
      
3032
1.58G
        for (uint32 col = 0; col < fSrcCols; col++)
3033
1.57G
          {
3034
          
3035
3.38G
          for (uint32 channel = 0; channel < fSrcChannels; channel++)
3036
1.80G
            {
3037
            
3038
1.80G
            int32 pixel = sPtr [channel];
3039
            
3040
1.80G
            int16 diff = (int16) (pixel - predictor [channel]);
3041
            
3042
1.80G
            CountOneDiff (diff, freqCount [channel]);
3043
            
3044
1.80G
            predictor [channel] = pixel;
3045
            
3046
1.80G
            }
3047
            
3048
1.57G
          sPtr += fSrcColStep;
3049
            
3050
1.57G
          }
3051
          
3052
13.1M
        }
3053
        
3054
13.3M
      }
3055
3056
83.9k
  }
3057
3058
/*****************************************************************************/
3059
3060
/*
3061
 *--------------------------------------------------------------
3062
 *
3063
 * HuffEncode --
3064
 *
3065
 *      Encode and output Huffman-compressed image data.
3066
 *
3067
 * Results:
3068
 *      None.
3069
 *
3070
 * Side effects:
3071
 *      None.
3072
 *
3073
 *--------------------------------------------------------------
3074
 */
3075
3076
void dng_lossless_encoder::HuffEncode ()
3077
83.9k
  {
3078
    
3079
83.9k
  DNG_ASSERT ((int32)fSrcRows >= 0, "dng_lossless_encoder::HuffEncode: fSrcRows too large.");
3080
3081
13.3M
  for (int32 row = 0; row < (int32)fSrcRows; row++)
3082
13.3M
      {
3083
      
3084
13.3M
    const uint16 *sPtr = fSrcData + row * fSrcRowStep;
3085
    
3086
    // Initialize predictors for this row.
3087
    
3088
13.3M
    int32 predictor [4];
3089
    
3090
29.4M
    for (int32 channel = 0; channel < (int32)fSrcChannels; channel++)
3091
16.1M
      {
3092
      
3093
16.1M
      if (row == 0)
3094
146k
        predictor [channel] = 1 << (fSrcBitDepth - 1);
3095
        
3096
16.0M
      else
3097
16.0M
        predictor [channel] = sPtr [channel - fSrcRowStep];
3098
      
3099
16.1M
      }
3100
      
3101
    // Unroll most common case of two channels
3102
    
3103
13.3M
    if (fSrcChannels == 2)
3104
192k
      {
3105
      
3106
192k
      int32 pred0 = predictor [0];
3107
192k
      int32 pred1 = predictor [1];
3108
      
3109
192k
      uint32 srcCols    = fSrcCols;
3110
192k
      int32  srcColStep = fSrcColStep;
3111
      
3112
23.8M
        for (uint32 col = 0; col < srcCols; col++)
3113
23.6M
          {
3114
          
3115
23.6M
          int32 pixel0 = sPtr [0];
3116
23.6M
        int32 pixel1 = sPtr [1];
3117
          
3118
23.6M
          int16 diff0 = (int16) (pixel0 - pred0);
3119
23.6M
          int16 diff1 = (int16) (pixel1 - pred1);
3120
          
3121
23.6M
          EncodeOneDiff (diff0, &huffTable [0]);
3122
23.6M
          EncodeOneDiff (diff1, &huffTable [1]);
3123
          
3124
23.6M
          pred0 = pixel0;
3125
23.6M
          pred1 = pixel1;
3126
            
3127
23.6M
          sPtr += srcColStep;
3128
            
3129
23.6M
          }
3130
      
3131
192k
      }
3132
      
3133
    // General case.
3134
      
3135
13.1M
    else
3136
13.1M
      {
3137
      
3138
1.58G
        for (uint32 col = 0; col < fSrcCols; col++)
3139
1.57G
          {
3140
          
3141
3.38G
          for (uint32 channel = 0; channel < fSrcChannels; channel++)
3142
1.80G
            {
3143
            
3144
1.80G
            int32 pixel = sPtr [channel];
3145
            
3146
1.80G
            int16 diff = (int16) (pixel - predictor [channel]);
3147
            
3148
1.80G
            EncodeOneDiff (diff, &huffTable [channel]);
3149
            
3150
1.80G
            predictor [channel] = pixel;
3151
            
3152
1.80G
            }
3153
            
3154
1.57G
          sPtr += fSrcColStep;
3155
            
3156
1.57G
          }
3157
          
3158
13.1M
        }
3159
        
3160
13.3M
      }
3161
  
3162
83.9k
    FlushBits ();
3163
    
3164
83.9k
  }
3165
3166
/*****************************************************************************/
3167
3168
/*
3169
 *--------------------------------------------------------------
3170
 *
3171
 * GenHuffCoding --
3172
 *
3173
 *  Generate the optimal coding for the given counts. 
3174
 *  This algorithm is explained in section K.2 of the
3175
 *  JPEG standard. 
3176
 *
3177
 * Results:
3178
 *      htbl->bits and htbl->huffval are constructed.
3179
 *
3180
 * Side effects:
3181
 *      None.
3182
 *
3183
 *--------------------------------------------------------------
3184
 */
3185
3186
void dng_lossless_encoder::GenHuffCoding (HuffmanTable *htbl, uint32 *freq)
3187
146k
  {
3188
  
3189
146k
  int i;
3190
146k
  int j;
3191
  
3192
146k
  const int MAX_CLEN = 32;      // assumed maximum initial code length
3193
  
3194
146k
  uint8 bits [MAX_CLEN + 1];  // bits [k] = # of symbols with code length k
3195
146k
  short codesize [257];     // codesize [k] = code length of symbol k
3196
146k
  short others   [257];     // next symbol in current branch of tree
3197
  
3198
146k
  memset (bits    , 0, sizeof (bits    ));
3199
146k
    memset (codesize, 0, sizeof (codesize));
3200
  
3201
37.8M
  for (i = 0; i < 257; i++)
3202
37.7M
    others [i] = -1;     // init links to empty
3203
3204
  // Including the pseudo-symbol 256 in the Huffman procedure guarantees
3205
  // that no real symbol is given code-value of all ones, because 256
3206
  // will be placed in the largest codeword category.
3207
3208
146k
  freq [256] = 1;         // make sure there is a nonzero count
3209
3210
  // Huffman's basic algorithm to assign optimal code lengths to symbols
3211
  
3212
1.12M
  while (true)
3213
1.12M
    {
3214
3215
    // Find the smallest nonzero frequency, set c1 = its symbol.
3216
    // In case of ties, take the larger symbol number.
3217
3218
1.12M
    int c1 = -1;
3219
    
3220
1.12M
    uint32 v = 0xFFFFFFFF;
3221
    
3222
291M
    for (i = 0; i <= 256; i++)
3223
290M
      {
3224
      
3225
290M
      if (freq [i] && freq [i] <= v)
3226
3.17M
        {
3227
3.17M
        v = freq [i];
3228
3.17M
        c1 = i;
3229
3.17M
        }
3230
  
3231
290M
      }
3232
3233
    // Find the next smallest nonzero frequency, set c2 = its symbol.
3234
    // In case of ties, take the larger symbol number.
3235
3236
1.12M
    int c2 = -1;
3237
    
3238
1.12M
    v = 0xFFFFFFFF;
3239
    
3240
291M
    for (i = 0; i <= 256; i++)
3241
290M
      {
3242
      
3243
290M
          if (freq [i] && freq [i] <= v && i != c1) 
3244
2.46M
            {
3245
2.46M
        v = freq [i];
3246
2.46M
        c2 = i;
3247
2.46M
        }
3248
        
3249
290M
      }
3250
3251
    // Done if we've merged everything into one frequency.
3252
3253
1.12M
    if (c2 < 0)
3254
146k
          break;
3255
    
3256
    // Else merge the two counts/trees.
3257
3258
982k
    freq [c1] += freq [c2];
3259
982k
    freq [c2] = 0;
3260
3261
    // Increment the codesize of everything in c1's tree branch.
3262
3263
982k
    codesize [c1] ++;
3264
    
3265
2.92M
    while (others [c1] >= 0)
3266
1.94M
      {
3267
1.94M
      c1 = others [c1];
3268
1.94M
      codesize [c1] ++;
3269
1.94M
        }
3270
    
3271
    // chain c2 onto c1's tree branch 
3272
3273
982k
    others [c1] = (short) c2;
3274
    
3275
    // Increment the codesize of everything in c2's tree branch.
3276
3277
982k
    codesize [c2] ++;
3278
    
3279
2.34M
    while (others [c2] >= 0) 
3280
1.36M
      {
3281
1.36M
      c2 = others [c2];
3282
1.36M
      codesize [c2] ++;
3283
1.36M
      }
3284
3285
982k
    }
3286
3287
  // Now count the number of symbols of each code length.
3288
3289
37.8M
  for (i = 0; i <= 256; i++)
3290
37.7M
    {
3291
    
3292
37.7M
    if (codesize [i])
3293
1.12M
      {
3294
3295
      // The JPEG standard seems to think that this can't happen,
3296
      // but I'm paranoid...
3297
      
3298
1.12M
      if (codesize [i] > MAX_CLEN)
3299
0
        {
3300
       
3301
0
            DNG_REPORT ("Huffman code size table overflow");
3302
            
3303
0
            ThrowProgramError ();
3304
            
3305
0
            }
3306
3307
1.12M
      bits [codesize [i]]++;
3308
      
3309
1.12M
      }
3310
3311
37.7M
    }
3312
3313
  // JPEG doesn't allow symbols with code lengths over 16 bits, so if the pure
3314
  // Huffman procedure assigned any such lengths, we must adjust the coding.
3315
  // Here is what the JPEG spec says about how this next bit works:
3316
  // Since symbols are paired for the longest Huffman code, the symbols are
3317
  // removed from this length category two at a time.  The prefix for the pair
3318
  // (which is one bit shorter) is allocated to one of the pair; then,
3319
  // skipping the BITS entry for that prefix length, a code word from the next
3320
  // shortest nonzero BITS entry is converted into a prefix for two code words
3321
  // one bit longer.
3322
  
3323
2.49M
  for (i = MAX_CLEN; i > 16; i--)
3324
2.34M
    {
3325
    
3326
2.34M
    while (bits [i] > 0)
3327
104
      {
3328
      
3329
      // Kludge: I have never been able to test this logic, and there
3330
      // are comments on the web that this encoder has bugs with 16-bit
3331
      // data, so just throw an error if we get here and revert to a
3332
      // default table.  - tknoll 12/1/03.
3333
      
3334
104
          DNG_REPORT ("Info: Optimal huffman table bigger than 16 bits");
3335
          
3336
104
      ThrowProgramError ();
3337
      
3338
      // Original logic:
3339
      
3340
104
      j = i - 2;    // find length of new prefix to be used
3341
      
3342
104
      while (bits [j] == 0)
3343
0
        j--;
3344
      
3345
104
      bits [i    ] -= 2;    // remove two symbols
3346
104
      bits [i - 1] ++;    // one goes in this length
3347
104
      bits [j + 1] += 2;    // two new symbols in this length
3348
104
      bits [j    ] --;    // symbol of this length is now a prefix
3349
      
3350
104
      }
3351
      
3352
2.34M
    }
3353
3354
  // Remove the count for the pseudo-symbol 256 from
3355
  // the largest codelength.
3356
  
3357
1.67M
  while (bits [i] == 0)    // find largest codelength still in use
3358
1.52M
      i--;
3359
      
3360
146k
  bits [i] --;
3361
  
3362
  // Return final symbol counts (only for lengths 0..16).
3363
3364
146k
  memcpy (htbl->bits, bits, sizeof (htbl->bits));
3365
  
3366
  // Return a list of the symbols sorted by code length. 
3367
  // It's not real clear to me why we don't need to consider the codelength
3368
  // changes made above, but the JPEG spec seems to think this works.
3369
   
3370
146k
  int p = 0;
3371
  
3372
4.84M
  for (i = 1; i <= MAX_CLEN; i++)
3373
4.69M
    {
3374
    
3375
1.20G
    for (j = 0; j <= 255; j++)
3376
1.20G
      {
3377
      
3378
1.20G
      if (codesize [j] == i)
3379
981k
        {
3380
981k
        htbl->huffval [p] = (uint8) j;
3381
981k
        p++;
3382
981k
        }
3383
3384
1.20G
        }
3385
        
3386
4.69M
      }
3387
 
3388
146k
  }
3389
3390
/*****************************************************************************/
3391
3392
/*
3393
 *--------------------------------------------------------------
3394
 *
3395
 * HuffOptimize --
3396
 *
3397
 *  Find the best coding parameters for a Huffman-coded scan.
3398
 *  When called, the scan data has already been converted to
3399
 *  a sequence of MCU groups of source image samples, which
3400
 *  are stored in a "big" array, mcuTable.
3401
 *
3402
 *  It counts the times each category symbol occurs. Based on
3403
 *  this counting, optimal Huffman tables are built. Then it
3404
 *  uses this optimal Huffman table and counting table to find
3405
 *  the best PSV. 
3406
 *
3407
 * Results:
3408
 *  Optimal Huffman tables are retured in cPtr->dcHuffTblPtrs[tbl].
3409
 *  Best PSV is retured in cPtr->Ss.
3410
 *
3411
 * Side effects:
3412
 *  None.
3413
 *
3414
 *--------------------------------------------------------------
3415
 */
3416
3417
void dng_lossless_encoder::HuffOptimize ()
3418
83.9k
  {
3419
  
3420
    // Collect the frequency counts.
3421
     
3422
83.9k
  FreqCountSet ();
3423
  
3424
  // Generate Huffman encoding tables.
3425
  
3426
230k
  for (uint32 channel = 0; channel < fSrcChannels; channel++)
3427
146k
    {
3428
    
3429
146k
    try
3430
146k
      {
3431
      
3432
146k
          GenHuffCoding (&huffTable [channel], freqCount [channel]);
3433
          
3434
146k
          }
3435
          
3436
146k
        catch (...)
3437
146k
          {
3438
          
3439
104
          DNG_REPORT ("Info: Reverting to default huffman table");
3440
          
3441
26.8k
          for (uint32 j = 0; j <= 256; j++)
3442
26.7k
            {
3443
            
3444
26.7k
            freqCount [channel] [j] = (j <= 16 ? 1 : 0);
3445
            
3446
26.7k
            }
3447
          
3448
104
          GenHuffCoding (&huffTable [channel], freqCount [channel]);
3449
          
3450
104
          }
3451
        
3452
146k
        FixHuffTbl (&huffTable [channel]);
3453
        
3454
146k
    }
3455
 
3456
83.9k
  }
3457
3458
/*****************************************************************************/
3459
3460
/*
3461
 *--------------------------------------------------------------
3462
 *
3463
 * EmitMarker --
3464
 *
3465
 *  Emit a marker code into the output stream.
3466
 *
3467
 * Results:
3468
 *  None.
3469
 *
3470
 * Side effects:
3471
 *  None.
3472
 *
3473
 *--------------------------------------------------------------
3474
 */
3475
3476
void dng_lossless_encoder::EmitMarker (JpegMarker mark)
3477
482k
  {
3478
  
3479
482k
    EmitByte (0xFF);
3480
482k
    EmitByte ((uint8) mark);
3481
    
3482
482k
  }
3483
3484
/*****************************************************************************/
3485
3486
/*
3487
 *--------------------------------------------------------------
3488
 *
3489
 * Emit2bytes --
3490
 *
3491
 *  Emit a 2-byte integer; these are always MSB first in JPEG
3492
 *  files
3493
 *
3494
 * Results:
3495
 *  None.
3496
 *
3497
 * Side effects:
3498
 *  None.
3499
 *
3500
 *--------------------------------------------------------------
3501
 */
3502
3503
void dng_lossless_encoder::Emit2bytes (int value)
3504
482k
  {
3505
  
3506
482k
    EmitByte ((value >> 8) & 0xFF);
3507
482k
    EmitByte (value & 0xFF);
3508
 
3509
482k
  }
3510
3511
/*****************************************************************************/
3512
3513
/*
3514
 *--------------------------------------------------------------
3515
 *
3516
 * EmitDht --
3517
 *
3518
 *  Emit a DHT marker, follwed by the huffman data.
3519
 *
3520
 * Results:
3521
 *  None
3522
 *
3523
 * Side effects:
3524
 *  None
3525
 *
3526
 *--------------------------------------------------------------
3527
 */
3528
 
3529
void dng_lossless_encoder::EmitDht (int index)
3530
146k
  {
3531
  
3532
146k
  int i;
3533
  
3534
146k
    HuffmanTable *htbl = &huffTable [index];
3535
    
3536
146k
  EmitMarker (M_DHT);
3537
3538
146k
    int length = 0;
3539
    
3540
2.49M
  for (i = 1; i <= 16; i++)
3541
2.34M
      length += htbl->bits [i];
3542
3543
146k
  Emit2bytes (length + 2 + 1 + 16);
3544
  
3545
146k
  EmitByte ((uint8) index);
3546
3547
2.49M
  for (i = 1; i <= 16; i++)
3548
2.34M
      EmitByte (htbl->bits [i]);
3549
3550
1.12M
  for (i = 0; i < length; i++)
3551
981k
      EmitByte (htbl->huffval [i]);
3552
3553
146k
  }
3554
3555
/*****************************************************************************/
3556
3557
/*
3558
 *--------------------------------------------------------------
3559
 *
3560
 * EmitSof --
3561
 *
3562
 *  Emit a SOF marker plus data.
3563
 *
3564
 * Results:
3565
 *  None.
3566
 *
3567
 * Side effects:
3568
 *  None.
3569
 *
3570
 *--------------------------------------------------------------
3571
 */
3572
3573
void dng_lossless_encoder::EmitSof (JpegMarker code)
3574
83.9k
  {
3575
  
3576
83.9k
    EmitMarker (code);
3577
3578
83.9k
    Emit2bytes (3 * fSrcChannels + 2 + 5 + 1);  // length
3579
3580
83.9k
    EmitByte ((uint8) fSrcBitDepth);
3581
    
3582
83.9k
    Emit2bytes (fSrcRows);
3583
83.9k
    Emit2bytes (fSrcCols);
3584
3585
83.9k
    EmitByte ((uint8) fSrcChannels);
3586
3587
230k
    for (uint32 i = 0; i < fSrcChannels; i++)
3588
146k
      {
3589
      
3590
146k
    EmitByte ((uint8) i);
3591
    
3592
146k
    EmitByte ((uint8) ((1 << 4) + 1));    // Not subsampled.
3593
           
3594
146k
        EmitByte (0);         // Tq shall be 0 for lossless.
3595
        
3596
146k
      }
3597
   
3598
83.9k
  }
3599
3600
/*****************************************************************************/
3601
3602
/*
3603
 *--------------------------------------------------------------
3604
 *
3605
 * EmitSos --
3606
 *
3607
 *  Emit a SOS marker plus data.
3608
 *
3609
 * Results:
3610
 *  None.
3611
 *
3612
 * Side effects:
3613
 *  None.
3614
 *
3615
 *--------------------------------------------------------------
3616
 */
3617
3618
void dng_lossless_encoder::EmitSos ()
3619
83.9k
  {
3620
  
3621
83.9k
    EmitMarker (M_SOS);
3622
3623
83.9k
    Emit2bytes (2 * fSrcChannels + 2 + 1 + 3);  // length
3624
3625
83.9k
    EmitByte ((uint8) fSrcChannels);      // Ns
3626
3627
230k
    for (uint32 i = 0; i < fSrcChannels; i++) 
3628
146k
      { 
3629
      
3630
      // Cs,Td,Ta
3631
      
3632
146k
    EmitByte ((uint8) i);
3633
146k
    EmitByte ((uint8) (i << 4));
3634
    
3635
146k
      }
3636
3637
83.9k
    EmitByte (1);   // PSV - hardcoded - tknoll
3638
83.9k
    EmitByte (0);     // Spectral selection end  - Se
3639
83.9k
    EmitByte (0);     // The point transform parameter 
3640
    
3641
83.9k
  }
3642
3643
/*****************************************************************************/
3644
3645
/*
3646
 *--------------------------------------------------------------
3647
 *
3648
 * WriteFileHeader --
3649
 *
3650
 *  Write the file header.
3651
 *
3652
 * Results:
3653
 *  None.
3654
 *
3655
 * Side effects:
3656
 *  None.
3657
 *
3658
 *--------------------------------------------------------------
3659
 */
3660
3661
void dng_lossless_encoder::WriteFileHeader ()
3662
83.9k
  {
3663
  
3664
83.9k
    EmitMarker (M_SOI);   // first the SOI
3665
    
3666
83.9k
    EmitSof (M_SOF3);
3667
    
3668
83.9k
  }
3669
3670
/*****************************************************************************/
3671
3672
/*
3673
 *--------------------------------------------------------------
3674
 *
3675
 * WriteScanHeader --
3676
 *
3677
 *  Write the start of a scan (everything through the SOS marker).
3678
 *
3679
 * Results:
3680
 *  None.
3681
 *
3682
 * Side effects:
3683
 *  None.
3684
 *
3685
 *--------------------------------------------------------------
3686
 */
3687
3688
void dng_lossless_encoder::WriteScanHeader ()
3689
83.9k
  {
3690
3691
    // Emit Huffman tables.
3692
    
3693
230k
    for (uint32 i = 0; i < fSrcChannels; i++)
3694
146k
      {
3695
      
3696
146k
    EmitDht (i);
3697
    
3698
146k
      }
3699
3700
83.9k
  EmitSos ();
3701
     
3702
83.9k
  }
3703
3704
/*****************************************************************************/
3705
3706
/*
3707
 *--------------------------------------------------------------
3708
 *
3709
 * WriteFileTrailer --
3710
 *
3711
 *  Write the End of image marker at the end of a JPEG file.
3712
 *
3713
 * Results:
3714
 *  None.
3715
 *
3716
 * Side effects:
3717
 *  None.
3718
 *
3719
 *--------------------------------------------------------------
3720
 */
3721
3722
void dng_lossless_encoder::WriteFileTrailer ()
3723
83.9k
  {
3724
  
3725
83.9k
    EmitMarker (M_EOI);
3726
    
3727
83.9k
  }
3728
3729
/*****************************************************************************/
3730
3731
void dng_lossless_encoder::Encode ()
3732
83.9k
  {
3733
  
3734
83.9k
  DNG_ASSERT (fSrcChannels <= 4, "Too many components in scan");
3735
    
3736
  // Count the times each difference category occurs. 
3737
  // Construct the optimal Huffman table.
3738
    
3739
83.9k
  HuffOptimize ();
3740
3741
    // Write the frame and scan headers.
3742
3743
83.9k
    WriteFileHeader (); 
3744
    
3745
83.9k
    WriteScanHeader ();
3746
3747
    // Encode the image.
3748
    
3749
83.9k
    HuffEncode ();
3750
3751
    // Clean up everything.
3752
    
3753
83.9k
  WriteFileTrailer ();
3754
3755
83.9k
  }
3756
3757
/*****************************************************************************/
3758
3759
void EncodeLosslessJPEG (const uint16 *srcData,
3760
             uint32 srcRows,
3761
             uint32 srcCols,
3762
             uint32 srcChannels,
3763
             uint32 srcBitDepth,
3764
             int32 srcRowStep,
3765
             int32 srcColStep,
3766
             dng_stream &stream)
3767
83.9k
  {
3768
  
3769
83.9k
  dng_lossless_encoder encoder (srcData,
3770
83.9k
                    srcRows,
3771
83.9k
                    srcCols,
3772
83.9k
                    srcChannels,
3773
83.9k
                    srcBitDepth,
3774
83.9k
                    srcRowStep,
3775
83.9k
                    srcColStep,
3776
83.9k
                    stream);
3777
3778
83.9k
  encoder.Encode ();
3779
  
3780
83.9k
    }
3781
3782
/*****************************************************************************/