libopenraw
ljpegdecompressor.cpp
1 /*
2  * libopenraw - ljpegdecompressor.cpp
3  *
4  * Copyright (C) 2007-2016 Hubert Figuiere
5  *
6  * This library is free software: you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public License
8  * as published by the Free Software Foundation, either version 3 of
9  * the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library. If not, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 /*
21  * Code for JPEG lossless decoding. Large parts are grabbed from the IJG
22  * software, so:
23  *
24  * Copyright (C) 1991, 1992, Thomas G. Lane.
25  * Part of the Independent JPEG Group's software.
26  * See the file Copyright for more details.
27  *
28  * Copyright (c) 1993 Brian C. Smith, The Regents of the University
29  * of California
30  * All rights reserved.
31  *
32  * Copyright (c) 1994 Kongji Huang and Brian C. Smith.
33  * Cornell University
34  * All rights reserved.
35  *
36  * Permission to use, copy, modify, and distribute this software and its
37  * documentation for any purpose, without fee, and without written agreement is
38  * hereby granted, provided that the above copyright notice and the following
39  * two paragraphs appear in all copies of this software.
40  *
41  * IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR
42  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
43  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL
44  * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
45  *
46  * CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
47  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
48  * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
49  * ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO
50  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
51  */
52 
53 
54 #include <stdlib.h>
55 #include <string.h>
56 
57 #include <fcntl.h>
58 #include <algorithm>
59 #include <cstdint>
60 
61 #include <boost/format.hpp>
62 
63 #include <libopenraw/consts.h>
64 #include <libopenraw/debug.h>
65 
66 #include "rawdata.hpp"
67 #include "exception.hpp"
68 #include "io/stream.hpp"
69 #include "trace.hpp"
70 #include "ljpegdecompressor.hpp"
71 #include "ljpegdecompressor_priv.hpp"
72 
73 namespace OpenRaw {
74 
75 using namespace Debug;
76 
77 namespace Internals {
78 
79 
80 static void SkipVariable(IO::Stream *s);
81 static uint16_t Get2bytes (IO::Stream * s);
82 static int32_t NextMarker(IO::Stream * );
83 static void GetSoi(DecompressInfo *dcPtr);
84 static void GetApp0(IO::Stream *);
85 
86 LJpegDecompressor::LJpegDecompressor(IO::Stream *stream,
87  RawContainer *container)
88  : Decompressor(stream, container),
89  m_slices(),
90  m_mcuROW1(NULL), m_mcuROW2(NULL),
91  m_buf1(NULL), m_buf2(NULL),
92  m_bitsLeft(0),
93  m_getBuffer(0),
94  m_output(0)
95 {
96 }
97 
98 
99 LJpegDecompressor::~LJpegDecompressor()
100 {
101  if(m_mcuROW1) {
102  free(m_mcuROW1);
103  }
104  if(m_mcuROW2) {
105  free(m_mcuROW2);
106  }
107  if(m_buf1) {
108  free(m_buf1);
109  }
110  if(m_buf2) {
111  free(m_buf2);
112  }
113 }
114 
115 
116 void LJpegDecompressor::setSlices(const std::vector<uint16_t> & slices)
117 {
118  uint16_t n = slices[0];
119  m_slices.resize(n + 1);
120  for(uint16_t i = 0; i < n; i++) {
121  m_slices[i] = slices[1];
122  }
123  m_slices[n] = slices[2];
124 }
125 
126 
127 
128 
129 static uint32_t bitMask[] = { 0xffffffff, 0x7fffffff,
130  0x3fffffff, 0x1fffffff,
131  0x0fffffff, 0x07ffffff,
132  0x03ffffff, 0x01ffffff,
133  0x00ffffff, 0x007fffff,
134  0x003fffff, 0x001fffff,
135  0x000fffff, 0x0007ffff,
136  0x0003ffff, 0x0001ffff,
137  0x0000ffff, 0x00007fff,
138  0x00003fff, 0x00001fff,
139  0x00000fff, 0x000007ff,
140  0x000003ff, 0x000001ff,
141  0x000000ff, 0x0000007f,
142  0x0000003f, 0x0000001f,
143  0x0000000f, 0x00000007,
144  0x00000003, 0x00000001};
145 
146 void FixHuffTbl (HuffmanTable *htbl);
147 
148 
149 /*
150  *--------------------------------------------------------------
151  *
152  * FixHuffTbl --
153  *
154  * Compute derived values for a Huffman table one the DHT marker
155  * has been processed. This generates both the encoding and
156  * decoding tables.
157  *
158  * Results:
159  * None.
160  *
161  * Side effects:
162  * None.
163  *
164  *--------------------------------------------------------------
165  */
166 void
167 FixHuffTbl (HuffmanTable *htbl)
168 {
169  int32_t p, i, l, lastp, si;
170  char huffsize[257];
171  uint16_t huffcode[257];
172  uint16_t code;
173  int32_t size;
174  int32_t value, ll, ul;
175 
176  /*
177  * Figure C.1: make table of Huffman code length for each symbol
178  * Note that this is in code-length order.
179  */
180  p = 0;
181  for (l = 1; l <= 16; l++) {
182  for (i = 1; i <= (int)htbl->bits[l]; i++)
183  huffsize[p++] = (char)l;
184  }
185  huffsize[p] = 0;
186  lastp = p;
187 
188 
189  /*
190  * Figure C.2: generate the codes themselves
191  * Note that this is in code-length order.
192  */
193  code = 0;
194  si = huffsize[0];
195  p = 0;
196  while (huffsize[p]) {
197  while (((int)huffsize[p]) == si) {
198  huffcode[p++] = code;
199  code++;
200  }
201  code <<= 1;
202  si++;
203  }
204 
205  /*
206  * Figure C.3: generate encoding tables
207  * These are code and size indexed by symbol value
208  * Set any codeless symbols to have code length 0; this allows
209  * EmitBits to detect any attempt to emit such symbols.
210  */
211  memset(htbl->ehufsi, 0, sizeof(htbl->ehufsi));
212 
213  for (p = 0; p < lastp; p++) {
214  htbl->ehufco[htbl->huffval[p]] = huffcode[p];
215  htbl->ehufsi[htbl->huffval[p]] = huffsize[p];
216  }
217 
218  /*
219  * Figure F.15: generate decoding tables
220  */
221  p = 0;
222  for (l = 1; l <= 16; l++) {
223  if (htbl->bits[l]) {
224  htbl->valptr[l] = p;
225  htbl->mincode[l] = huffcode[p];
226  p += htbl->bits[l];
227  htbl->maxcode[l] = huffcode[p - 1];
228  } else {
229  htbl->maxcode[l] = -1;
230  }
231  }
232 
233  /*
234  * We put in this value to ensure HuffDecode terminates.
235  */
236  htbl->maxcode[17] = 0xFFFFFL;
237 
238  /*
239  * Build the numbits, value lookup tables.
240  * These table allow us to gather 8 bits from the bits stream,
241  * and immediately lookup the size and value of the huffman codes.
242  * If size is zero, it means that more than 8 bits are in the huffman
243  * code (this happens about 3-4% of the time).
244  */
245  bzero (htbl->numbits, sizeof(htbl->numbits));
246  for (p=0; p<lastp; p++) {
247  size = huffsize[p];
248  if (size <= 8) {
249  value = htbl->huffval[p];
250  code = huffcode[p];
251  ll = code << (8-size);
252  if (size < 8) {
253  ul = ll | bitMask[24+size];
254  } else {
255  ul = ll;
256  }
257  for (i=ll; i<=ul; i++) {
258  htbl->numbits[i] = size;
259  htbl->value[i] = value;
260  }
261  }
262  }
263 }
264 
265 
266 #define RST0 0xD0 /* RST0 marker code */
267 
268 
269 #if 0
270 /*
271  * The following variables keep track of the input buffer
272  * for the JPEG data, which is read by ReadJpegData.
273  */
274 uint8_t inputBuffer[JPEG_BUF_SIZE]; /* Input buffer for JPEG data */
275 int numInputBytes; /* The total number of bytes in inputBuffer */
276 int maxInputBytes; /* Size of inputBuffer */
277 int inputBufferOffset; /* Offset of current byte */
278 #endif
279 
280 
281 /*
282  * Code for extracting the next N bits from the input stream.
283  * (N never exceeds 15 for JPEG data.)
284  * This needs to go as fast as possible!
285  *
286  * We read source bytes into getBuffer and dole out bits as needed.
287  * If getBuffer already contains enough bits, they are fetched in-line
288  * by the macros get_bits() and get_bit(). When there aren't enough bits,
289  * fillBitBuffer is called; it will attempt to fill getBuffer to the
290  * "high water mark", then extract the desired number of bits. The idea,
291  * of course, is to minimize the function-call overhead cost of entering
292  * fillBitBuffer.
293  * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width
294  * of getBuffer to be used. (On machines with wider words, an even larger
295  * buffer could be used.)
296  */
297 
298 #define BITS_PER_LONG (8*sizeof(int32_t))
299 #define MIN_GET_BITS (BITS_PER_LONG-7) /* max value for long getBuffer */
300 
301 /*
302  * bmask[n] is mask for n rightmost bits
303  */
304 static int32_t bmask[] = {0x0000,
305  0x0001, 0x0003, 0x0007, 0x000F,
306  0x001F, 0x003F, 0x007F, 0x00FF,
307  0x01FF, 0x03FF, 0x07FF, 0x0FFF,
308  0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF};
309 
310 
311 /*
312  * Lossless JPEG specifies data precision to be from 2 to 16 bits/sample.
313  */
314 #define MinPrecisionBits 2
315 #define MaxPrecisionBits 16
316 
317 
318 /*
319  *--------------------------------------------------------------
320  *
321  * DecoderStructInit --
322  *
323  * Initalize the rest of the fields in the decompression
324  * structure.
325  *
326  * Results:
327  * None.
328  *
329  * Side effects:
330  * None.
331  *
332  *--------------------------------------------------------------
333  */
334 void
335 LJpegDecompressor::DecoderStructInit (DecompressInfo *dcPtr)
336  noexcept(false)
337 {
338  int16_t ci,i;
339  JpegComponentInfo *compPtr;
340  int32_t mcuSize;
341 
342  /*
343  * Check sampling factor validity.
344  */
345  for (ci = 0; ci < dcPtr->numComponents; ci++) {
346  compPtr = &dcPtr->compInfo[ci];
347  if ((compPtr->hSampFactor != 1) || (compPtr->vSampFactor != 1)) {
348  throw DecodingException("Error: Downsampling is not supported.\n");
349  }
350  }
351 
352  /*
353  * Prepare array describing MCU composition
354  */
355  if (dcPtr->compsInScan == 1) {
356  dcPtr->MCUmembership[0] = 0;
357  } else {
358  if (dcPtr->compsInScan > 4) {
359  throw DecodingException("Too many components for interleaved scan");
360  }
361 
362  for (ci = 0; ci < dcPtr->compsInScan; ci++) {
363  dcPtr->MCUmembership[ci] = ci;
364  }
365  }
366 
367  /*
368  * Initialize mucROW1 and mcuROW2 which buffer two rows of
369  * pixels for predictor calculation.
370  */
371 
372  if ((m_mcuROW1 = (MCU *)malloc(dcPtr->imageWidth*sizeof(MCU)))==NULL) {
373  throw DecodingException("Not enough memory for mcuROW1\n");
374  }
375  if ((m_mcuROW2 = (MCU *)malloc(dcPtr->imageWidth*sizeof(MCU)))==NULL) {
376  throw DecodingException("Not enough memory for mcuROW2\n");
377  }
378 
379  mcuSize=dcPtr->compsInScan * sizeof(ComponentType);
380  if ((m_buf1 = (char *)malloc(dcPtr->imageWidth*mcuSize))==NULL) {
381  throw DecodingException("Not enough memory for buf1\n");
382  }
383  if ((m_buf2 = (char *)malloc(dcPtr->imageWidth*mcuSize))==NULL) {
384  throw DecodingException("Not enough memory for buf2\n");
385  }
386 
387  for (i=0;i<dcPtr->imageWidth;i++) {
388  m_mcuROW1[i]=(MCU)(m_buf1+i*mcuSize);
389  m_mcuROW2[i]=(MCU)(m_buf2+i*mcuSize);
390  }
391 }
392 
393 
394 /*
395  *--------------------------------------------------------------
396  *
397  * fillBitBuffer --
398  *
399  * Load up the bit buffer with at least nbits
400  * Process any stuffed bytes at this time.
401  *
402  * Results:
403  * None
404  *
405  * Side effects:
406  * The bitwise global variables are updated.
407  *
408  *--------------------------------------------------------------
409  */
410 void
411 LJpegDecompressor::fillBitBuffer (IO::Stream * s,uint16_t nbits)
412 {
413  uint8_t c, c2;
414 
415  while (m_bitsLeft < MIN_GET_BITS) {
416  c = s->readByte();
417 
418  /*
419  * If it's 0xFF, check and discard stuffed zero byte
420  */
421  if (c == 0xFF) {
422  c2 = s->readByte();
423 
424  if (c2 != 0) {
425 
426  /*
427  * Oops, it's actually a marker indicating end of
428  * compressed data. Better put it back for use later.
429  */
430  s->seek(-2, SEEK_CUR);
431 
432  /*
433  * There should be enough bits still left in the data
434  * segment; if so, just break out of the while loop.
435  */
436  if (m_bitsLeft >= nbits)
437  break;
438 
439  /*
440  * Uh-oh. Corrupted data: stuff zeroes into the data
441  * stream, since this sometimes occurs when we are on the
442  * last show_bits(8) during decoding of the Huffman
443  * segment.
444  */
445  c = 0;
446  }
447  }
448  /*
449  * OK, load c into getBuffer
450  */
451  m_getBuffer = (m_getBuffer << 8) | c;
452  m_bitsLeft += 8;
453  }
454 }
455 
456 
457 
458 inline int32_t LJpegDecompressor::QuickPredict(int32_t col, int16_t curComp,
459  MCU *curRowBuf,
460  MCU *prevRowBuf,
461  int32_t psv)
462 {
463  int32_t left,upper,diag,leftcol;
464  int32_t predictor;
465 
466  leftcol=col-1;
467  upper=prevRowBuf[col][curComp];
468  left=curRowBuf[leftcol][curComp];
469  diag=prevRowBuf[leftcol][curComp];
470 
471  /*
472  * All predictor are calculated according to psv.
473  */
474  switch (psv) {
475  case 0:
476  predictor = 0;
477  break;
478  case 1:
479  predictor = left;
480  break;
481  case 2:
482  predictor = upper;
483  break;
484  case 3:
485  predictor = diag;
486  break;
487  case 4:
488  predictor = left+upper-diag;
489  break;
490  case 5:
491  predictor = left+((upper-diag)>>1);
492  break;
493  case 6:
494  predictor = upper+((left-diag)>>1);
495  break;
496  case 7:
497  predictor = (left+upper)>>1;
498  break;
499  default:
500  Trace(WARNING) << "Warning: Undefined PSV\n";
501  predictor = 0;
502  }
503  return predictor;
504 }
505 
506 inline
507 int32_t LJpegDecompressor::show_bits8(IO::Stream * s)
508 {
509  if (m_bitsLeft < 8) {
510  fillBitBuffer(s, 8);
511  }
512  return (m_getBuffer >> (m_bitsLeft-8)) & 0xff;
513 }
514 
515 inline
516 void LJpegDecompressor::flush_bits(uint16_t nbits)
517 {
518  m_bitsLeft -= (nbits);
519 }
520 
521 
522 inline
523 int32_t LJpegDecompressor::get_bits(uint16_t nbits)
524 {
525  if (m_bitsLeft < nbits)
526  fillBitBuffer(m_stream, nbits);
527  return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits];
528 }
529 
530 inline
531 int32_t LJpegDecompressor::get_bit()
532 {
533  if (!m_bitsLeft)
534  fillBitBuffer(m_stream, 1);
535  return (m_getBuffer >> (--m_bitsLeft)) & 1;
536 }
537 
538 
539 inline
540 int32_t LJpegDecompressor::readBits(IO::Stream * s, uint16_t nbits)
541 {
542  if (m_bitsLeft < nbits) {
543  fillBitBuffer(s, nbits);
544  }
545  return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits];
546 }
547 
548 
549 
550 /*
551  *--------------------------------------------------------------
552  *
553  * PmPutRow --
554  *
555  * Output one row of pixels stored in RowBuf.
556  *
557  * Results:
558  * None
559  *
560  * Side effects:
561  * One row of pixels are write to file pointed by outFile.
562  *
563  *--------------------------------------------------------------
564  */
565 inline void
566 LJpegDecompressor::PmPutRow(MCU* RowBuf, int32_t numComp, int32_t numCol, int32_t Pt)
567 {
568  // TODO this might be wrong in 8 bits...
569  // original code was using putc which *i think* was a problem for
570  // 16bpp
571  int32_t comp;
572  int32_t col;
573  uint16_t v;
574 
575  for (col = 0; col < numCol; col++) {
576  for (comp = 0; comp < numComp; comp++) {
577  v = RowBuf[col][comp]<<Pt;
578  m_output->append(v);
579  }
580  }
581 // m_output->nextRow();
582 }
583 
584 /*
585  *--------------------------------------------------------------
586  *
587  * HuffDecode --
588  *
589  * Taken from Figure F.16: extract next coded symbol from
590  * input stream. This should becode a macro.
591  *
592  * Results:
593  * Next coded symbol
594  *
595  * Side effects:
596  * Bitstream is parsed.
597  *
598  *--------------------------------------------------------------
599  */
600 inline int32_t
601 LJpegDecompressor::HuffDecode(HuffmanTable *htbl)
602 {
603  int32_t rv;
604  int32_t l, temp;
605  int32_t code;
606 
607  /*
608  * If the huffman code is less than 8 bits, we can use the fast
609  * table lookup to get its value. It's more than 8 bits about
610  * 3-4% of the time.
611  */
612  code = show_bits8(m_stream);
613  if (htbl->numbits[code]) {
614  flush_bits(htbl->numbits[code]);
615  rv=htbl->value[code];
616  } else {
617  flush_bits(8);
618  l = 8;
619  while (code > htbl->maxcode[l]) {
620  temp = get_bit();
621  code = (code << 1) | temp;
622  l++;
623  }
624 
625  /*
626  * With garbage input we may reach the sentinel value l = 17.
627  */
628 
629  if (l > 16) {
630  //Trace(WARNING) << "Corrupt JPEG data: bad Huffman code " << l << "\n";
631  rv = 0; /* fake a zero as the safest result */
632  } else {
633  rv = htbl->huffval[htbl->valptr[l] +
634  ((int)(code - htbl->mincode[l]))];
635  }
636  }
637  return rv;
638 }
639 
640 /*
641  *--------------------------------------------------------------
642  *
643  * HuffExtend --
644  *
645  * Code and table for Figure F.12: extend sign bit
646  *
647  * Results:
648  * The extended value.
649  *
650  * Side effects:
651  * None.
652  *
653  *--------------------------------------------------------------
654  */
655 static const int32_t extendTest[16] = /* entry n is 2**(n-1) */
656 {0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
657  0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000};
658 
659 // we can't bitshift -1. So we use 0xffffffff as a value.
660 // gcc complain about it otherwise.
661 #define EXTEND(n) (int32_t)(0xffffffff << n) + 1
662 static const int32_t extendOffset[16] = /* entry n is (-1 << n) + 1 */
663 {
664  0, EXTEND(1), EXTEND(2), EXTEND(3),
665  EXTEND(4), EXTEND(5), EXTEND(6), EXTEND(7),
666  EXTEND(8), EXTEND(9), EXTEND(10), EXTEND(11),
667  EXTEND(12), EXTEND(13), EXTEND(14), EXTEND(15)
668 };
669 
670 inline
671 void HuffExtend(int32_t & x, int32_t s) noexcept
672 {
673  if ((x) < extendTest[s]) {
674  (x) += extendOffset[s];
675  }
676 }
677 
678 /*
679  *--------------------------------------------------------------
680  *
681  * HuffDecoderInit --
682  *
683  * Initialize for a Huffman-compressed scan.
684  * This is invoked after reading the SOS marker.
685  *
686  * Results:
687  * None
688  *
689  * Side effects:
690  * None.
691  *
692  *--------------------------------------------------------------
693  */
694 void
695 LJpegDecompressor::HuffDecoderInit (DecompressInfo *dcPtr)
696  noexcept(false)
697 {
698  int16_t ci;
699  JpegComponentInfo *compptr;
700 
701  /*
702  * Initialize static variables
703  */
704  m_bitsLeft = 0;
705 
706  for (ci = 0; ci < dcPtr->compsInScan; ci++) {
707  compptr = dcPtr->curCompInfo[ci];
708  /*
709  * Make sure requested tables are present
710  */
711  if (dcPtr->dcHuffTblPtrs[compptr->dcTblNo] == NULL) {
712  throw DecodingException("Error: Use of undefined Huffman table\n");
713  }
714 
715  /*
716  * Compute derived values for Huffman tables.
717  * We may do this more than once for same table, but it's not a
718  * big deal
719  */
720  FixHuffTbl (dcPtr->dcHuffTblPtrs[compptr->dcTblNo]);
721  }
722 
723  /*
724  * Initialize restart stuff
725  */
726  dcPtr->restartInRows = (dcPtr->restartInterval)/(dcPtr->imageWidth);
727  dcPtr->restartRowsToGo = dcPtr->restartInRows;
728  dcPtr->nextRestartNum = 0;
729 }
730 
731 /*
732  *--------------------------------------------------------------
733  *
734  * ProcessRestart --
735  *
736  * Check for a restart marker & resynchronize decoder.
737  *
738  * Results:
739  * None.
740  *
741  * Side effects:
742  * BitStream is parsed, bit buffer is reset, etc.
743  *
744  *--------------------------------------------------------------
745  */
746 void
747 LJpegDecompressor::ProcessRestart (DecompressInfo *dcPtr)
748  noexcept(false)
749 {
750  int32_t c, nbytes;
751 
752  /*
753  * Throw away any unused bits remaining in bit buffer
754  */
755  nbytes = m_bitsLeft / 8;
756  m_bitsLeft = 0;
757 
758  /*
759  * Scan for next JPEG marker
760  */
761  do {
762  do { /* skip any non-FF bytes */
763  nbytes++;
764  c = m_stream->readByte();
765  } while (c != 0xFF);
766  do { /* skip any duplicate FFs */
767  /*
768  * we don't increment nbytes here since extra FFs are legal
769  */
770  c = m_stream->readByte();
771  } while (c == 0xFF);
772  } while (c == 0); /* repeat if it was a stuffed FF/00 */
773 
774  if (c != (RST0 + dcPtr->nextRestartNum)) {
775 
776  /*
777  * Uh-oh, the restart markers have been messed up too.
778  * Just bail out.
779  */
780  throw DecodingException("Error: Corrupt JPEG data. "
781  "Aborting decoding...\n");
782  }
783 
784  /*
785  * Update restart state
786  */
787  dcPtr->restartRowsToGo = dcPtr->restartInRows;
788  dcPtr->nextRestartNum = (dcPtr->nextRestartNum + 1) & 7;
789 }
790 
791 /*
792  *--------------------------------------------------------------
793  *
794  * DecodeFirstRow --
795  *
796  * Decode the first raster line of samples at the start of
797  * the scan and at the beginning of each restart interval.
798  * This includes modifying the component value so the real
799  * value, not the difference is returned.
800  *
801  * Results:
802  * None.
803  *
804  * Side effects:
805  * Bitstream is parsed.
806  *
807  *--------------------------------------------------------------
808  */
809 void LJpegDecompressor::DecodeFirstRow(DecompressInfo *dcPtr,
810  MCU *curRowBuf)
811 {
812  uint16_t curComp,ci;
813  int32_t s,col,compsInScan,numCOL;
814  JpegComponentInfo *compptr;
815  int32_t Pr,Pt,d;
816  HuffmanTable *dctbl;
817 
818  Pr=dcPtr->dataPrecision;
819  Pt=dcPtr->Pt;
820  compsInScan=dcPtr->compsInScan;
821  numCOL=dcPtr->imageWidth;
822 
823  /*
824  * the start of the scan or at the beginning of restart interval.
825  */
826  for (curComp = 0; curComp < compsInScan; curComp++) {
827  ci = dcPtr->MCUmembership[curComp];
828  compptr = dcPtr->curCompInfo[ci];
829  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
830 
831  /*
832  * Section F.2.2.1: decode the difference
833  */
834  s = HuffDecode (dctbl);
835  if (s) {
836  d = get_bits(s);
837  HuffExtend(d,s);
838  } else {
839  d = 0;
840  }
841 
842  /*
843  * Add the predictor to the difference.
844  */
845  curRowBuf[0][curComp]=d+(1<<(Pr-Pt-1));
846  }
847 
848  /*
849  * the rest of the first row
850  */
851  for (col=1; col<numCOL; col++) {
852  for (curComp = 0; curComp < compsInScan; curComp++) {
853  ci = dcPtr->MCUmembership[curComp];
854  compptr = dcPtr->curCompInfo[ci];
855  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
856 
857  /*
858  * Section F.2.2.1: decode the difference
859  */
860  s = HuffDecode (dctbl);
861  if (s) {
862  d = get_bits(s);
863  HuffExtend(d,s);
864  } else {
865  d = 0;
866  }
867 
868  /*
869  * Add the predictor to the difference.
870  */
871  curRowBuf[col][curComp]=d+curRowBuf[col-1][curComp];
872  }
873  }
874 
875  if (dcPtr->restartInRows) {
876  (dcPtr->restartRowsToGo)--;
877  }
878 }
879 
880 /*
881  *--------------------------------------------------------------
882  *
883  * DecodeImage --
884  *
885  * Decode the input stream. This includes modifying
886  * the component value so the real value, not the
887  * difference is returned.
888  *
889  * Results:
890  * None.
891  *
892  * Side effects:
893  * Bitstream is parsed.
894  *
895  *--------------------------------------------------------------
896  */
897 void
898 LJpegDecompressor::DecodeImage(DecompressInfo *dcPtr)
899 {
900  int32_t s,d,col,row;
901  int16_t curComp, ci;
902  HuffmanTable *dctbl;
903  JpegComponentInfo *compptr;
904  int32_t predictor;
905  int32_t numCOL,numROW,compsInScan;
906  MCU *prevRowBuf,*curRowBuf;
907  int32_t imagewidth,Pt,psv;
908 
909  numCOL=imagewidth=dcPtr->imageWidth;
910  numROW=dcPtr->imageHeight;
911  compsInScan=dcPtr->compsInScan;
912  Pt=dcPtr->Pt;
913  psv=dcPtr->Ss;
914  prevRowBuf=m_mcuROW2;
915  curRowBuf=m_mcuROW1;
916 
917  /*
918  * Decode the first row of image. Output the row and
919  * turn this row into a previous row for later predictor
920  * calculation.
921  */
922  DecodeFirstRow(dcPtr,curRowBuf);
923  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
924  std::swap(prevRowBuf,curRowBuf);
925 
926  for (row=1; row<numROW; row++) {
927 
928  /*
929  * Account for restart interval, process restart marker if needed.
930  */
931  if (dcPtr->restartInRows) {
932  if (dcPtr->restartRowsToGo == 0) {
933  ProcessRestart (dcPtr);
934 
935  /*
936  * Reset predictors at restart.
937  */
938  DecodeFirstRow(dcPtr,curRowBuf);
939  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
940  std::swap(prevRowBuf,curRowBuf);
941  continue;
942  }
943  dcPtr->restartRowsToGo--;
944  }
945 
946  /*
947  * The upper neighbors are predictors for the first column.
948  */
949  for (curComp = 0; curComp < compsInScan; curComp++) {
950  ci = dcPtr->MCUmembership[curComp];
951  compptr = dcPtr->curCompInfo[ci];
952  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
953 
954  /*
955  * Section F.2.2.1: decode the difference
956  */
957  s = HuffDecode (dctbl);
958  if (s) {
959  d = get_bits(s);
960  HuffExtend(d,s);
961  } else {
962  d = 0;
963  }
964 
965  curRowBuf[0][curComp]=d+prevRowBuf[0][curComp];
966  }
967 
968  /*
969  * For the rest of the column on this row, predictor
970  * calculations are base on PSV.
971  */
972  for (col=1; col<numCOL; col++) {
973  for (curComp = 0; curComp < compsInScan; curComp++) {
974  ci = dcPtr->MCUmembership[curComp];
975  compptr = dcPtr->curCompInfo[ci];
976  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
977 
978  /*
979  * Section F.2.2.1: decode the difference
980  */
981  s = HuffDecode (dctbl);
982  if (s) {
983  d = get_bits(s);
984  HuffExtend(d,s);
985  } else {
986  d = 0;
987  }
988  predictor = QuickPredict(col,curComp,curRowBuf,prevRowBuf,
989  psv);
990 
991  curRowBuf[col][curComp]=d+predictor;
992  }
993  }
994  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
995  std::swap(prevRowBuf,curRowBuf);
996  }
997 }
998 
999 
1000 
1001 
1002 /*
1003  *--------------------------------------------------------------
1004  *
1005  * Get2bytes --
1006  *
1007  * Get a 2-byte unsigned integer (e.g., a marker parameter length
1008  * field)
1009  *
1010  * Results:
1011  * Next two byte of input as an integer.
1012  *
1013  * Side effects:
1014  * Bitstream is parsed.
1015  *
1016  *--------------------------------------------------------------
1017  */
1018 static inline uint16_t
1019 Get2bytes (IO::Stream * s)
1020 {
1021  uint16_t a;
1022 
1023  a = s->readByte();
1024  return (a << 8) | s->readByte();
1025 }
1026 
1027 /*
1028  *--------------------------------------------------------------
1029  *
1030  * SkipVariable --
1031  *
1032  * Skip over an unknown or uninteresting variable-length marker
1033  *
1034  * Results:
1035  * None.
1036  *
1037  * Side effects:
1038  * Bitstream is parsed over marker.
1039  *
1040  *
1041  *--------------------------------------------------------------
1042  */
1043 static inline void SkipVariable(IO::Stream * s)
1044 {
1045  int32_t length;
1046 
1047  length = Get2bytes(s) - 2;
1048 
1049  s->seek(length, SEEK_CUR);
1050 }
1051 
1052 /*
1053  *--------------------------------------------------------------
1054  *
1055  * GetDht --
1056  *
1057  * Process a DHT marker
1058  *
1059  * Results:
1060  * None
1061  *
1062  * Side effects:
1063  * A huffman table is read.
1064  * Exits on error.
1065  *
1066  *--------------------------------------------------------------
1067  */
1068 void
1069 LJpegDecompressor::GetDht (DecompressInfo *dcPtr)
1070  noexcept(false)
1071 {
1072  int32_t length;
1073  int32_t i, index, count;
1074 
1075  length = Get2bytes(m_stream) - 2;
1076 
1077  while (length) {
1078  index = m_stream->readByte();
1079 
1080  if (index < 0 || index >= 4) {
1081  throw DecodingException(str(boost::format("Bogus DHT index %1%")
1082  % index));
1083  }
1084 
1085  HuffmanTable *& htblptr = dcPtr->dcHuffTblPtrs[index];
1086  if (htblptr == NULL) {
1087  htblptr = (HuffmanTable *) malloc(sizeof (HuffmanTable));
1088  if (htblptr==NULL) {
1089  throw DecodingException("Can't malloc HuffmanTable");
1090  }
1091  }
1092 
1093  htblptr->bits[0] = 0;
1094  count = 0;
1095  for (i = 1; i <= 16; i++) {
1096  htblptr->bits[i] = m_stream->readByte();
1097  count += htblptr->bits[i];
1098  }
1099 
1100  if (count > 256) {
1101  throw DecodingException("Bogus DHT counts");
1102  }
1103 
1104  for (i = 0; i < count; i++)
1105  htblptr->huffval[i] = m_stream->readByte();
1106 
1107  length -= 1 + 16 + count;
1108  }
1109 }
1110 
1111 /*
1112  *--------------------------------------------------------------
1113  *
1114  * GetDri --
1115  *
1116  * Process a DRI marker
1117  *
1118  * Results:
1119  * None
1120  *
1121  * Side effects:
1122  * Exits on error.
1123  * Bitstream is parsed.
1124  *
1125  *--------------------------------------------------------------
1126  */
1127 void
1128 LJpegDecompressor::GetDri(DecompressInfo *dcPtr)
1129  noexcept(false)
1130 {
1131  if (Get2bytes(m_stream) != 4) {
1132  throw DecodingException("Bogus length in DRI");
1133  }
1134 
1135  dcPtr->restartInterval = Get2bytes(m_stream);
1136 }
1137 
1138 /*
1139  *--------------------------------------------------------------
1140  *
1141  * GetApp0 --
1142  *
1143  * Process an APP0 marker.
1144  *
1145  * Results:
1146  * None
1147  *
1148  * Side effects:
1149  * Bitstream is parsed
1150  *
1151  *--------------------------------------------------------------
1152  */
1153 static void GetApp0(IO::Stream *s)
1154 {
1155  int32_t length;
1156 
1157  length = Get2bytes(s) - 2;
1158  s->seek(length, SEEK_CUR);
1159 }
1160 
1161 /*
1162  *--------------------------------------------------------------
1163  *
1164  * GetSof --
1165  *
1166  * Process a SOFn marker
1167  *
1168  * Results:
1169  * None.
1170  *
1171  * Side effects:
1172  * Bitstream is parsed
1173  * Exits on error
1174  * dcPtr structure is filled in
1175  *
1176  *--------------------------------------------------------------
1177  */
1178 void
1179 LJpegDecompressor::GetSof(DecompressInfo *dcPtr)
1180  noexcept(false)
1181 {
1182  int32_t length;
1183  int16_t ci;
1184  int32_t c;
1185  JpegComponentInfo *compptr;
1186 
1187  length = Get2bytes(m_stream);
1188 
1189  dcPtr->dataPrecision = m_stream->readByte();
1190  dcPtr->imageHeight = Get2bytes(m_stream);
1191  dcPtr->imageWidth = Get2bytes(m_stream);
1192  dcPtr->numComponents = m_stream->readByte();
1193 
1194  /*
1195  * We don't support files in which the image height is initially
1196  * specified as 0 and is later redefined by DNL. As long as we
1197  * have to check that, might as well have a general sanity check.
1198  */
1199  if ((dcPtr->imageHeight <= 0 ) ||
1200  (dcPtr->imageWidth <= 0) ||
1201  (dcPtr->numComponents <= 0)) {
1202  throw DecodingException("Empty JPEG image (DNL not supported)");
1203  }
1204 
1205  if ((dcPtr->dataPrecision<MinPrecisionBits) ||
1206  (dcPtr->dataPrecision>MaxPrecisionBits)) {
1207  throw DecodingException("Unsupported JPEG data precision");
1208  }
1209 
1210  if (length != (dcPtr->numComponents * 3 + 8)) {
1211  throw DecodingException("Bogus SOF length");
1212  }
1213 
1214  dcPtr->compInfo = (JpegComponentInfo *) malloc
1215  (dcPtr->numComponents * sizeof (JpegComponentInfo));
1216 
1217  for (ci = 0; ci < dcPtr->numComponents; ci++) {
1218  compptr = &dcPtr->compInfo[ci];
1219  compptr->componentIndex = ci;
1220  compptr->componentId = m_stream->readByte();
1221  c = m_stream->readByte();
1222  compptr->hSampFactor = (int16_t)((c >> 4) & 15);
1223  compptr->vSampFactor = (int16_t)((c) & 15);
1224  (void) m_stream->readByte(); /* skip Tq */
1225  }
1226 }
1227 
1228 /*
1229  *--------------------------------------------------------------
1230  *
1231  * GetSos --
1232  *
1233  * Process a SOS marker
1234  *
1235  * Results:
1236  * None.
1237  *
1238  * Side effects:
1239  * Bitstream is parsed.
1240  * Exits on error.
1241  *
1242  *--------------------------------------------------------------
1243  */
1244 void
1245 LJpegDecompressor::GetSos (DecompressInfo *dcPtr)
1246  noexcept(false)
1247 {
1248  int32_t length;
1249  int32_t i;
1250  uint16_t n, ci, c, cc;
1251  JpegComponentInfo *compptr;
1252 
1253  length = Get2bytes (m_stream);
1254 
1255  /*
1256  * Get the number of image components.
1257  */
1258  n = m_stream->readByte();
1259  dcPtr->compsInScan = n;
1260  length -= 3;
1261 
1262  if (length != (n * 2 + 3) || n < 1 || n > 4) {
1263  throw DecodingException("Bogus SOS length");
1264  }
1265 
1266 
1267  for (i = 0; i < n; i++) {
1268  cc = m_stream->readByte();
1269  c = m_stream->readByte();
1270  length -= 2;
1271 
1272  for (ci = 0; ci < dcPtr->numComponents; ci++)
1273  if (cc == dcPtr->compInfo[ci].componentId) {
1274  break;
1275  }
1276 
1277  if (ci >= dcPtr->numComponents) {
1278  throw DecodingException("Invalid component number in SOS");
1279  }
1280 
1281  compptr = &dcPtr->compInfo[ci];
1282  dcPtr->curCompInfo[i] = compptr;
1283  compptr->dcTblNo = (c >> 4) & 15;
1284  }
1285 
1286  /*
1287  * Get the PSV, skip Se, and get the point transform parameter.
1288  */
1289  dcPtr->Ss = m_stream->readByte();
1290  (void)m_stream->readByte();
1291  c = m_stream->readByte();
1292  dcPtr->Pt = c & 0x0F;
1293 }
1294 
1295 /*
1296  *--------------------------------------------------------------
1297  *
1298  * GetSoi --
1299  *
1300  * Process an SOI marker
1301  *
1302  * Results:
1303  * None.
1304  *
1305  * Side effects:
1306  * Bitstream is parsed.
1307  * Exits on error.
1308  *
1309  *--------------------------------------------------------------
1310  */
1311 static inline void
1312 GetSoi (DecompressInfo *dcPtr)
1313 {
1314 
1315  /*
1316  * Reset all parameters that are defined to be reset by SOI
1317  */
1318  dcPtr->restartInterval = 0;
1319 }
1320 
1321 /*
1322  *--------------------------------------------------------------
1323  *
1324  * NextMarker --
1325  *
1326  * Find the next JPEG marker Note that the output might not
1327  * be a valid marker code but it will never be 0 or FF
1328  *
1329  * Results:
1330  * The marker found.
1331  *
1332  * Side effects:
1333  * Bitstream is parsed.
1334  *
1335  *--------------------------------------------------------------
1336  */
1337 static int32_t
1338 NextMarker(IO::Stream *s)
1339 {
1340  int32_t c;
1341 
1342  do {
1343  /*
1344  * skip any non-FF bytes
1345  */
1346  do {
1347  c = s->readByte();
1348  } while (c != 0xFF);
1349  /*
1350  * skip any duplicate FFs without incrementing nbytes, since
1351  * extra FFs are legal
1352  */
1353  do {
1354  c = s->readByte();
1355  } while (c == 0xFF);
1356  } while (c == 0); /* repeat if it was a stuffed FF/00 */
1357 
1358  return c;
1359 }
1360 
1361 /*
1362  *--------------------------------------------------------------
1363  *
1364  * ProcessTables --
1365  *
1366  * Scan and process JPEG markers that can appear in any order
1367  * Return when an SOI, EOI, SOFn, or SOS is found
1368  *
1369  * Results:
1370  * The marker found.
1371  *
1372  * Side effects:
1373  * Bitstream is parsed.
1374  *
1375  *--------------------------------------------------------------
1376  */
1377 LJpegDecompressor::JpegMarker
1378 LJpegDecompressor::ProcessTables (DecompressInfo *dcPtr)
1379 {
1380  int c;
1381 
1382  while (1) {
1383  c = NextMarker (m_stream);
1384 
1385  switch (c) {
1386  case M_SOF0:
1387  case M_SOF1:
1388  case M_SOF2:
1389  case M_SOF3:
1390  case M_SOF5:
1391  case M_SOF6:
1392  case M_SOF7:
1393  case M_JPG:
1394  case M_SOF9:
1395  case M_SOF10:
1396  case M_SOF11:
1397  case M_SOF13:
1398  case M_SOF14:
1399  case M_SOF15:
1400  case M_SOI:
1401  case M_EOI:
1402  case M_SOS:
1403  return ((JpegMarker)c);
1404 
1405  case M_DHT:
1406  GetDht (dcPtr);
1407  break;
1408 
1409  case M_DQT:
1410  LOGWARN("Not a lossless JPEG file.\n");
1411  break;
1412 
1413  case M_DRI:
1414  GetDri (dcPtr);
1415  break;
1416 
1417  case M_APP0:
1418  GetApp0(m_stream);
1419  break;
1420 
1421  case M_RST0: /* these are all parameterless */
1422  case M_RST1:
1423  case M_RST2:
1424  case M_RST3:
1425  case M_RST4:
1426  case M_RST5:
1427  case M_RST6:
1428  case M_RST7:
1429  case M_TEM:
1430  LOGWARN("Warning: unexpected marker 0x%x", c);
1431  break;
1432 
1433  default: /* must be DNL, DHP, EXP, APPn, JPGn, COM,
1434  * or RESn */
1435  SkipVariable (m_stream);
1436  break;
1437  }
1438  }
1439 }
1440 
1441 /*
1442  *--------------------------------------------------------------
1443  *
1444  * ReadFileHeader --
1445  *
1446  * Initialize and read the file header (everything through
1447  * the SOF marker).
1448  *
1449  * Results:
1450  * None
1451  *
1452  * Side effects:
1453  * Exit on error.
1454  *
1455  *--------------------------------------------------------------
1456  */
1457 void
1458 LJpegDecompressor::ReadFileHeader (DecompressInfo *dcPtr)
1459  noexcept(false)
1460 {
1461  int c, c2;
1462 
1463  /*
1464  * Demand an SOI marker at the start of the file --- otherwise it's
1465  * probably not a JPEG file at all.
1466  */
1467  c = m_stream->readByte();
1468  c2 = m_stream->readByte();
1469  if ((c != 0xFF) || (c2 != M_SOI)) {
1470  throw DecodingException(str(boost::format("Not a JPEG file. "
1471  "marker is %1% %2%\n")
1472  % c % c2));
1473  }
1474 
1475  GetSoi (dcPtr); /* OK, process SOI */
1476 
1477  /*
1478  * Process markers until SOF
1479  */
1480  c = ProcessTables (dcPtr);
1481 
1482  switch (c) {
1483  case M_SOF0:
1484  case M_SOF1:
1485  case M_SOF3:
1486  GetSof(dcPtr);
1487  break;
1488 
1489  default:
1490  LOGWARN("Unsupported SOF marker type 0x%x\n", c);
1491  break;
1492  }
1493 }
1494 
1495 /*
1496  *--------------------------------------------------------------
1497  *
1498  * ReadScanHeader --
1499  *
1500  * Read the start of a scan (everything through the SOS marker).
1501  *
1502  * Results:
1503  * 1 if find SOS, 0 if find EOI
1504  *
1505  * Side effects:
1506  * Bitstream is parsed, may exit on errors.
1507  *
1508  *--------------------------------------------------------------
1509  */
1510 int32_t
1511 LJpegDecompressor::ReadScanHeader (DecompressInfo *dcPtr)
1512 {
1513  int c;
1514 
1515  /*
1516  * Process markers until SOS or EOI
1517  */
1518  c = ProcessTables (dcPtr);
1519 
1520  switch (c) {
1521  case M_SOS:
1522  GetSos (dcPtr);
1523  return 1;
1524 
1525  case M_EOI:
1526  return 0;
1527 
1528  default:
1529  Trace(WARNING) << str(boost::format("Unexpected marker "
1530  "0x%1%\n") % c);
1531  break;
1532  }
1533  return 0;
1534 }
1535 
1536 
1538 {
1539  DecompressInfo dcInfo;
1540  try {
1541  ReadFileHeader(&dcInfo);
1542  ReadScanHeader (&dcInfo);
1543 
1544  if(bitmap == NULL)
1545  {
1546  bitmap = new RawData();
1547  }
1548  m_output = bitmap;
1549  bitmap->setDataType(OR_DATA_TYPE_RAW);
1550  uint32_t bpc = dcInfo.dataPrecision;
1551 
1552  bitmap->setBpc(bpc);
1553  bitmap->setWhiteLevel((1 << bpc) - 1);
1554  /*uint16_t *dataPtr = (uint16_t*)*/
1555  bitmap->allocData(dcInfo.imageWidth
1556  * sizeof(uint16_t)
1557  * dcInfo.imageHeight
1558  * dcInfo.numComponents);
1559 
1560  Trace(DEBUG1) << "dc width = " << dcInfo.imageWidth
1561  << " dc height = " << dcInfo.imageHeight
1562  << "\n";
1563  /* consistently the real width is the JPEG width * numComponent
1564  * at least with all the Canon.
1565  * @todo check that this is valid with DNG too.
1566  */
1567  uint32_t width = dcInfo.imageWidth * dcInfo.numComponents;
1568  bitmap->setDimensions(width, dcInfo.imageHeight);
1569  bitmap->setSlices(m_slices);
1570  DecoderStructInit(&dcInfo);
1571  HuffDecoderInit(&dcInfo);
1572  DecodeImage(&dcInfo);
1573  // TODO handle the error properly
1574  }
1575  catch(...)
1576  {
1577  Trace(ERROR) << "Decompression error\n";
1578  }
1579  m_output = NULL;
1580  return bitmap;
1581 }
1582 
1583 }
1584 }
1585 
1586 /*
1587  Local Variables:
1588  mode:c++
1589  c-file-style:"stroustrup"
1590  c-file-offsets:((innamespace . 0))
1591  indent-tabs-mode:nil
1592  fill-column:80
1593  End:
1594 */
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:30
virtual RawData * decompress(RawData *in=NULL) override
void setBpc(uint32_t _bpc)
Definition: bitmapdata.cpp:164
virtual int seek(off_t offset, int whence)=0
Definition: trace.cpp:30
void setSlices(const std::vector< uint16_t > &slices)
void setDataType(DataType _type)
Definition: bitmapdata.cpp:100
base virtual class for IO
Definition: stream.hpp:41
virtual void setDimensions(uint32_t x, uint32_t y) override
Definition: rawdata.cpp:260