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