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