libopenraw
ljpegdecompressor.cpp
1 /*
2  * libopenraw - ljpegdecompressor.cpp
3  *
4  * Copyright (C) 2007 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 <boost/scoped_ptr.hpp>
58 #include <boost/scoped_array.hpp>
59 #include <boost/format.hpp>
60 
61 #include <libopenraw++/rawdata.h>
62 #include "io/memstream.h"
63 #include "trace.h"
64 #include "rawcontainer.h"
65 #include "jfifcontainer.h"
66 #include "ljpegdecompressor.h"
67 #include "ljpegdecompressor_priv.h"
68 
69 namespace OpenRaw {
70 
71 using namespace Debug;
72 
73 namespace Internals {
74 
75 
76 static void SkipVariable(IO::Stream *s);
77 static uint16_t Get2bytes (IO::Stream * s);
78 static int32_t NextMarker(IO::Stream * );
79 static void GetSoi(DecompressInfo *dcPtr);
80 static void GetApp0(IO::Stream *);
81 
82 LJpegDecompressor::LJpegDecompressor(IO::Stream *stream,
83  RawContainer *container)
84  : Decompressor(stream, container),
85  m_slices(),
86  m_mcuROW1(NULL), m_mcuROW2(NULL),
87  m_buf1(NULL), m_buf2(NULL),
88  m_bitsLeft(0),
89  m_getBuffer(0),
90  m_output(0)
91 {
92 }
93 
94 
95 LJpegDecompressor::~LJpegDecompressor()
96 {
97  if(m_mcuROW1) {
98  free(m_mcuROW1);
99  }
100  if(m_mcuROW2) {
101  free(m_mcuROW2);
102  }
103  if(m_buf1) {
104  free(m_buf1);
105  }
106  if(m_buf2) {
107  free(m_buf2);
108  }
109 }
110 
111 
112 void LJpegDecompressor::setSlices(const std::vector<uint16_t> & slices)
113 {
114  uint16_t n = slices[0];
115  m_slices.resize(n + 1);
116  for(uint16_t i = 0; i < n; i++) {
117  m_slices[i] = slices[1];
118  }
119  m_slices[n] = slices[2];
120 }
121 
122 
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  throw(DecodingException)
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 = (char *)malloc(dcPtr->imageWidth*mcuSize))==NULL) {
377  throw DecodingException("Not enough memory for buf1\n");
378  }
379  if ((m_buf2 = (char *)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]=(MCU)(m_buf1+i*mcuSize);
385  m_mcuROW2[i]=(MCU)(m_buf2+i*mcuSize);
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  Trace(WARNING) << "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 
518 inline
519 int32_t LJpegDecompressor::get_bits(uint16_t nbits)
520 {
521  if (m_bitsLeft < nbits)
522  fillBitBuffer(m_stream, nbits);
523  return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits];
524 }
525 
526 inline
527 int32_t LJpegDecompressor::get_bit()
528 {
529  if (!m_bitsLeft)
530  fillBitBuffer(m_stream, 1);
531  return (m_getBuffer >> (--m_bitsLeft)) & 1;
532 }
533 
534 
535 inline
536 int32_t LJpegDecompressor::readBits(IO::Stream * s, uint16_t nbits)
537 {
538  if (m_bitsLeft < nbits) {
539  fillBitBuffer(s, nbits);
540  }
541  return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits];
542 }
543 
544 
545 
546 /*
547  *--------------------------------------------------------------
548  *
549  * PmPutRow --
550  *
551  * Output one row of pixels stored in RowBuf.
552  *
553  * Results:
554  * None
555  *
556  * Side effects:
557  * One row of pixels are write to file pointed by outFile.
558  *
559  *--------------------------------------------------------------
560  */
561 inline void
562 LJpegDecompressor::PmPutRow(MCU* RowBuf, int32_t numComp, int32_t numCol, int32_t Pt)
563 {
564  // TODO this might be wrong in 8 bits...
565  // original code was using putc which *i think* was a problem for
566  // 16bpp
567  int32_t comp;
568  int32_t col;
569  uint16_t v;
570 
571  for (col = 0; col < numCol; col++) {
572  for (comp = 0; comp < numComp; comp++) {
573  v = RowBuf[col][comp]<<Pt;
574  m_output->append(v);
575  }
576  }
577 // m_output->nextRow();
578 }
579 
580 /*
581  *--------------------------------------------------------------
582  *
583  * HuffDecode --
584  *
585  * Taken from Figure F.16: extract next coded symbol from
586  * input stream. This should becode a macro.
587  *
588  * Results:
589  * Next coded symbol
590  *
591  * Side effects:
592  * Bitstream is parsed.
593  *
594  *--------------------------------------------------------------
595  */
596 inline int32_t
597 LJpegDecompressor::HuffDecode(HuffmanTable *htbl)
598 {
599  int32_t rv;
600  int32_t l, temp;
601  int32_t code;
602 
603  /*
604  * If the huffman code is less than 8 bits, we can use the fast
605  * table lookup to get its value. It's more than 8 bits about
606  * 3-4% of the time.
607  */
608  code = show_bits8(m_stream);
609  if (htbl->numbits[code]) {
610  flush_bits(htbl->numbits[code]);
611  rv=htbl->value[code];
612  } else {
613  flush_bits(8);
614  l = 8;
615  while (code > htbl->maxcode[l]) {
616  temp = get_bit();
617  code = (code << 1) | temp;
618  l++;
619  }
620 
621  /*
622  * With garbage input we may reach the sentinel value l = 17.
623  */
624 
625  if (l > 16) {
626  //Trace(WARNING) << "Corrupt JPEG data: bad Huffman code " << l << "\n";
627  rv = 0; /* fake a zero as the safest result */
628  } else {
629  rv = htbl->huffval[htbl->valptr[l] +
630  ((int)(code - htbl->mincode[l]))];
631  }
632  }
633  return rv;
634 }
635 
636 /*
637  *--------------------------------------------------------------
638  *
639  * HuffExtend --
640  *
641  * Code and table for Figure F.12: extend sign bit
642  *
643  * Results:
644  * The extended value.
645  *
646  * Side effects:
647  * None.
648  *
649  *--------------------------------------------------------------
650  */
651 static int32_t extendTest[16] = /* entry n is 2**(n-1) */
652 {0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080,
653  0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000};
654 
655 static int32_t extendOffset[16] = /* entry n is (-1 << n) + 1 */
656 {0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1,
657  ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1,
658  ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1,
659  ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1};
660 
661 
662 inline
663 void HuffExtend(int32_t & x, int32_t s)
664 {
665  if ((x) < extendTest[s]) {
666  (x) += extendOffset[s];
667  }
668 }
669 
670 /*
671  *--------------------------------------------------------------
672  *
673  * HuffDecoderInit --
674  *
675  * Initialize for a Huffman-compressed scan.
676  * This is invoked after reading the SOS marker.
677  *
678  * Results:
679  * None
680  *
681  * Side effects:
682  * None.
683  *
684  *--------------------------------------------------------------
685  */
686 void
687 LJpegDecompressor::HuffDecoderInit (DecompressInfo *dcPtr)
688  throw(DecodingException)
689 {
690  int16_t ci;
691  JpegComponentInfo *compptr;
692 
693  /*
694  * Initialize static variables
695  */
696  m_bitsLeft = 0;
697 
698  for (ci = 0; ci < dcPtr->compsInScan; ci++) {
699  compptr = dcPtr->curCompInfo[ci];
700  /*
701  * Make sure requested tables are present
702  */
703  if (dcPtr->dcHuffTblPtrs[compptr->dcTblNo] == NULL) {
704  throw DecodingException("Error: Use of undefined Huffman table\n");
705  }
706 
707  /*
708  * Compute derived values for Huffman tables.
709  * We may do this more than once for same table, but it's not a
710  * big deal
711  */
712  FixHuffTbl (dcPtr->dcHuffTblPtrs[compptr->dcTblNo]);
713  }
714 
715  /*
716  * Initialize restart stuff
717  */
718  dcPtr->restartInRows = (dcPtr->restartInterval)/(dcPtr->imageWidth);
719  dcPtr->restartRowsToGo = dcPtr->restartInRows;
720  dcPtr->nextRestartNum = 0;
721 }
722 
723 /*
724  *--------------------------------------------------------------
725  *
726  * ProcessRestart --
727  *
728  * Check for a restart marker & resynchronize decoder.
729  *
730  * Results:
731  * None.
732  *
733  * Side effects:
734  * BitStream is parsed, bit buffer is reset, etc.
735  *
736  *--------------------------------------------------------------
737  */
738 void
739 LJpegDecompressor::ProcessRestart (DecompressInfo *dcPtr)
740  throw(DecodingException)
741 {
742  int32_t c, nbytes;
743 
744  /*
745  * Throw away any unused bits remaining in bit buffer
746  */
747  nbytes = m_bitsLeft / 8;
748  m_bitsLeft = 0;
749 
750  /*
751  * Scan for next JPEG marker
752  */
753  do {
754  do { /* skip any non-FF bytes */
755  nbytes++;
756  c = m_stream->readByte();
757  } while (c != 0xFF);
758  do { /* skip any duplicate FFs */
759  /*
760  * we don't increment nbytes here since extra FFs are legal
761  */
762  c = m_stream->readByte();
763  } while (c == 0xFF);
764  } while (c == 0); /* repeat if it was a stuffed FF/00 */
765 
766  if (c != (RST0 + dcPtr->nextRestartNum)) {
767 
768  /*
769  * Uh-oh, the restart markers have been messed up too.
770  * Just bail out.
771  */
772  throw DecodingException("Error: Corrupt JPEG data. "
773  "Aborting decoding...\n");
774  }
775 
776  /*
777  * Update restart state
778  */
779  dcPtr->restartRowsToGo = dcPtr->restartInRows;
780  dcPtr->nextRestartNum = (dcPtr->nextRestartNum + 1) & 7;
781 }
782 
783 /*
784  *--------------------------------------------------------------
785  *
786  * DecodeFirstRow --
787  *
788  * Decode the first raster line of samples at the start of
789  * the scan and at the beginning of each restart interval.
790  * This includes modifying the component value so the real
791  * value, not the difference is returned.
792  *
793  * Results:
794  * None.
795  *
796  * Side effects:
797  * Bitstream is parsed.
798  *
799  *--------------------------------------------------------------
800  */
801 void LJpegDecompressor::DecodeFirstRow(DecompressInfo *dcPtr,
802  MCU *curRowBuf)
803 {
804  uint16_t curComp,ci;
805  int32_t s,col,compsInScan,numCOL;
806  JpegComponentInfo *compptr;
807  int32_t Pr,Pt,d;
808  HuffmanTable *dctbl;
809 
810  Pr=dcPtr->dataPrecision;
811  Pt=dcPtr->Pt;
812  compsInScan=dcPtr->compsInScan;
813  numCOL=dcPtr->imageWidth;
814 
815  /*
816  * the start of the scan or at the beginning of restart interval.
817  */
818  for (curComp = 0; curComp < compsInScan; curComp++) {
819  ci = dcPtr->MCUmembership[curComp];
820  compptr = dcPtr->curCompInfo[ci];
821  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
822 
823  /*
824  * Section F.2.2.1: decode the difference
825  */
826  s = HuffDecode (dctbl);
827  if (s) {
828  d = get_bits(s);
829  HuffExtend(d,s);
830  } else {
831  d = 0;
832  }
833 
834  /*
835  * Add the predictor to the difference.
836  */
837  curRowBuf[0][curComp]=d+(1<<(Pr-Pt-1));
838  }
839 
840  /*
841  * the rest of the first row
842  */
843  for (col=1; col<numCOL; col++) {
844  for (curComp = 0; curComp < compsInScan; curComp++) {
845  ci = dcPtr->MCUmembership[curComp];
846  compptr = dcPtr->curCompInfo[ci];
847  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
848 
849  /*
850  * Section F.2.2.1: decode the difference
851  */
852  s = HuffDecode (dctbl);
853  if (s) {
854  d = get_bits(s);
855  HuffExtend(d,s);
856  } else {
857  d = 0;
858  }
859 
860  /*
861  * Add the predictor to the difference.
862  */
863  curRowBuf[col][curComp]=d+curRowBuf[col-1][curComp];
864  }
865  }
866 
867  if (dcPtr->restartInRows) {
868  (dcPtr->restartRowsToGo)--;
869  }
870 }
871 
872 /*
873  *--------------------------------------------------------------
874  *
875  * DecodeImage --
876  *
877  * Decode the input stream. This includes modifying
878  * the component value so the real value, not the
879  * difference is returned.
880  *
881  * Results:
882  * None.
883  *
884  * Side effects:
885  * Bitstream is parsed.
886  *
887  *--------------------------------------------------------------
888  */
889 void
890 LJpegDecompressor::DecodeImage(DecompressInfo *dcPtr)
891 {
892  int32_t s,d,col,row;
893  int16_t curComp, ci;
894  HuffmanTable *dctbl;
895  JpegComponentInfo *compptr;
896  int32_t predictor;
897  int32_t numCOL,numROW,compsInScan;
898  MCU *prevRowBuf,*curRowBuf;
899  int32_t imagewidth,Pt,psv;
900 
901  numCOL=imagewidth=dcPtr->imageWidth;
902  numROW=dcPtr->imageHeight;
903  compsInScan=dcPtr->compsInScan;
904  Pt=dcPtr->Pt;
905  psv=dcPtr->Ss;
906  prevRowBuf=m_mcuROW2;
907  curRowBuf=m_mcuROW1;
908 
909  /*
910  * Decode the first row of image. Output the row and
911  * turn this row into a previous row for later predictor
912  * calculation.
913  */
914  DecodeFirstRow(dcPtr,curRowBuf);
915  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
916  std::swap(prevRowBuf,curRowBuf);
917 
918  for (row=1; row<numROW; row++) {
919 
920  /*
921  * Account for restart interval, process restart marker if needed.
922  */
923  if (dcPtr->restartInRows) {
924  if (dcPtr->restartRowsToGo == 0) {
925  ProcessRestart (dcPtr);
926 
927  /*
928  * Reset predictors at restart.
929  */
930  DecodeFirstRow(dcPtr,curRowBuf);
931  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
932  std::swap(prevRowBuf,curRowBuf);
933  continue;
934  }
935  dcPtr->restartRowsToGo--;
936  }
937 
938  /*
939  * The upper neighbors are predictors for the first column.
940  */
941  for (curComp = 0; curComp < compsInScan; curComp++) {
942  ci = dcPtr->MCUmembership[curComp];
943  compptr = dcPtr->curCompInfo[ci];
944  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
945 
946  /*
947  * Section F.2.2.1: decode the difference
948  */
949  s = HuffDecode (dctbl);
950  if (s) {
951  d = get_bits(s);
952  HuffExtend(d,s);
953  } else {
954  d = 0;
955  }
956 
957  curRowBuf[0][curComp]=d+prevRowBuf[0][curComp];
958  }
959 
960  /*
961  * For the rest of the column on this row, predictor
962  * calculations are base on PSV.
963  */
964  for (col=1; col<numCOL; col++) {
965  for (curComp = 0; curComp < compsInScan; curComp++) {
966  ci = dcPtr->MCUmembership[curComp];
967  compptr = dcPtr->curCompInfo[ci];
968  dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo];
969 
970  /*
971  * Section F.2.2.1: decode the difference
972  */
973  s = HuffDecode (dctbl);
974  if (s) {
975  d = get_bits(s);
976  HuffExtend(d,s);
977  } else {
978  d = 0;
979  }
980  predictor = QuickPredict(col,curComp,curRowBuf,prevRowBuf,
981  psv);
982 
983  curRowBuf[col][curComp]=d+predictor;
984  }
985  }
986  PmPutRow(curRowBuf,compsInScan,numCOL,Pt);
987  std::swap(prevRowBuf,curRowBuf);
988  }
989 }
990 
991 
992 
993 
994 /*
995  *--------------------------------------------------------------
996  *
997  * Get2bytes --
998  *
999  * Get a 2-byte unsigned integer (e.g., a marker parameter length
1000  * field)
1001  *
1002  * Results:
1003  * Next two byte of input as an integer.
1004  *
1005  * Side effects:
1006  * Bitstream is parsed.
1007  *
1008  *--------------------------------------------------------------
1009  */
1010 static inline uint16_t
1011 Get2bytes (IO::Stream * s)
1012 {
1013  uint16_t a;
1014 
1015  a = s->readByte();
1016  return (a << 8) | s->readByte();
1017 }
1018 
1019 /*
1020  *--------------------------------------------------------------
1021  *
1022  * SkipVariable --
1023  *
1024  * Skip over an unknown or uninteresting variable-length marker
1025  *
1026  * Results:
1027  * None.
1028  *
1029  * Side effects:
1030  * Bitstream is parsed over marker.
1031  *
1032  *
1033  *--------------------------------------------------------------
1034  */
1035 static inline void SkipVariable(IO::Stream * s)
1036 {
1037  int32_t length;
1038 
1039  length = Get2bytes(s) - 2;
1040 
1041  s->seek(length, SEEK_CUR);
1042 }
1043 
1044 /*
1045  *--------------------------------------------------------------
1046  *
1047  * GetDht --
1048  *
1049  * Process a DHT marker
1050  *
1051  * Results:
1052  * None
1053  *
1054  * Side effects:
1055  * A huffman table is read.
1056  * Exits on error.
1057  *
1058  *--------------------------------------------------------------
1059  */
1060 void
1061 LJpegDecompressor::GetDht (DecompressInfo *dcPtr)
1062  throw(DecodingException)
1063 {
1064  int32_t length;
1065  int32_t i, index, count;
1066 
1067  length = Get2bytes(m_stream) - 2;
1068 
1069  while (length) {
1070  index = m_stream->readByte();
1071 
1072  if (index < 0 || index >= 4) {
1073  throw DecodingException(str(boost::format("Bogus DHT index %1%")
1074  % index));
1075  }
1076 
1077  HuffmanTable *& htblptr = dcPtr->dcHuffTblPtrs[index];
1078  if (htblptr == NULL) {
1079  htblptr = (HuffmanTable *) malloc(sizeof (HuffmanTable));
1080  if (htblptr==NULL) {
1081  throw DecodingException("Can't malloc HuffmanTable");
1082  }
1083  }
1084 
1085  htblptr->bits[0] = 0;
1086  count = 0;
1087  for (i = 1; i <= 16; i++) {
1088  htblptr->bits[i] = m_stream->readByte();
1089  count += htblptr->bits[i];
1090  }
1091 
1092  if (count > 256) {
1093  throw DecodingException("Bogus DHT counts");
1094  }
1095 
1096  for (i = 0; i < count; i++)
1097  htblptr->huffval[i] = m_stream->readByte();
1098 
1099  length -= 1 + 16 + count;
1100  }
1101 }
1102 
1103 /*
1104  *--------------------------------------------------------------
1105  *
1106  * GetDri --
1107  *
1108  * Process a DRI marker
1109  *
1110  * Results:
1111  * None
1112  *
1113  * Side effects:
1114  * Exits on error.
1115  * Bitstream is parsed.
1116  *
1117  *--------------------------------------------------------------
1118  */
1119 void
1120 LJpegDecompressor::GetDri(DecompressInfo *dcPtr)
1121  throw(DecodingException)
1122 {
1123  if (Get2bytes(m_stream) != 4) {
1124  throw DecodingException("Bogus length in DRI");
1125  }
1126 
1127  dcPtr->restartInterval = Get2bytes(m_stream);
1128 }
1129 
1130 /*
1131  *--------------------------------------------------------------
1132  *
1133  * GetApp0 --
1134  *
1135  * Process an APP0 marker.
1136  *
1137  * Results:
1138  * None
1139  *
1140  * Side effects:
1141  * Bitstream is parsed
1142  *
1143  *--------------------------------------------------------------
1144  */
1145 static void GetApp0(IO::Stream *s)
1146 {
1147  int32_t length;
1148 
1149  length = Get2bytes(s) - 2;
1150  s->seek(length, SEEK_CUR);
1151 }
1152 
1153 /*
1154  *--------------------------------------------------------------
1155  *
1156  * GetSof --
1157  *
1158  * Process a SOFn marker
1159  *
1160  * Results:
1161  * None.
1162  *
1163  * Side effects:
1164  * Bitstream is parsed
1165  * Exits on error
1166  * dcPtr structure is filled in
1167  *
1168  *--------------------------------------------------------------
1169  */
1170 void
1171 LJpegDecompressor::GetSof(DecompressInfo *dcPtr) throw(DecodingException)
1172 {
1173  int32_t length;
1174  int16_t ci;
1175  int32_t c;
1176  JpegComponentInfo *compptr;
1177 
1178  length = Get2bytes(m_stream);
1179 
1180  dcPtr->dataPrecision = m_stream->readByte();
1181  dcPtr->imageHeight = Get2bytes(m_stream);
1182  dcPtr->imageWidth = Get2bytes(m_stream);
1183  dcPtr->numComponents = m_stream->readByte();
1184 
1185  /*
1186  * We don't support files in which the image height is initially
1187  * specified as 0 and is later redefined by DNL. As long as we
1188  * have to check that, might as well have a general sanity check.
1189  */
1190  if ((dcPtr->imageHeight <= 0 ) ||
1191  (dcPtr->imageWidth <= 0) ||
1192  (dcPtr->numComponents <= 0)) {
1193  throw DecodingException("Empty JPEG image (DNL not supported)");
1194  }
1195 
1196  if ((dcPtr->dataPrecision<MinPrecisionBits) ||
1197  (dcPtr->dataPrecision>MaxPrecisionBits)) {
1198  throw DecodingException("Unsupported JPEG data precision");
1199  }
1200 
1201  if (length != (dcPtr->numComponents * 3 + 8)) {
1202  throw DecodingException("Bogus SOF length");
1203  }
1204 
1205  dcPtr->compInfo = (JpegComponentInfo *) malloc
1206  (dcPtr->numComponents * sizeof (JpegComponentInfo));
1207 
1208  for (ci = 0; ci < dcPtr->numComponents; ci++) {
1209  compptr = &dcPtr->compInfo[ci];
1210  compptr->componentIndex = ci;
1211  compptr->componentId = m_stream->readByte();
1212  c = m_stream->readByte();
1213  compptr->hSampFactor = (int16_t)((c >> 4) & 15);
1214  compptr->vSampFactor = (int16_t)((c) & 15);
1215  (void) m_stream->readByte(); /* skip Tq */
1216  }
1217 }
1218 
1219 /*
1220  *--------------------------------------------------------------
1221  *
1222  * GetSos --
1223  *
1224  * Process a SOS marker
1225  *
1226  * Results:
1227  * None.
1228  *
1229  * Side effects:
1230  * Bitstream is parsed.
1231  * Exits on error.
1232  *
1233  *--------------------------------------------------------------
1234  */
1235 void
1236 LJpegDecompressor::GetSos (DecompressInfo *dcPtr)
1237  throw(DecodingException)
1238 {
1239  int32_t length;
1240  int32_t i;
1241  uint16_t n, ci, c, cc;
1242  JpegComponentInfo *compptr;
1243 
1244  length = Get2bytes (m_stream);
1245 
1246  /*
1247  * Get the number of image components.
1248  */
1249  n = m_stream->readByte();
1250  dcPtr->compsInScan = n;
1251  length -= 3;
1252 
1253  if (length != (n * 2 + 3) || n < 1 || n > 4) {
1254  throw DecodingException("Bogus SOS length");
1255  }
1256 
1257 
1258  for (i = 0; i < n; i++) {
1259  cc = m_stream->readByte();
1260  c = m_stream->readByte();
1261  length -= 2;
1262 
1263  for (ci = 0; ci < dcPtr->numComponents; ci++)
1264  if (cc == dcPtr->compInfo[ci].componentId) {
1265  break;
1266  }
1267 
1268  if (ci >= dcPtr->numComponents) {
1269  throw DecodingException("Invalid component number in SOS");
1270  }
1271 
1272  compptr = &dcPtr->compInfo[ci];
1273  dcPtr->curCompInfo[i] = compptr;
1274  compptr->dcTblNo = (c >> 4) & 15;
1275  }
1276 
1277  /*
1278  * Get the PSV, skip Se, and get the point transform parameter.
1279  */
1280  dcPtr->Ss = m_stream->readByte();
1281  (void)m_stream->readByte();
1282  c = m_stream->readByte();
1283  dcPtr->Pt = c & 0x0F;
1284 }
1285 
1286 /*
1287  *--------------------------------------------------------------
1288  *
1289  * GetSoi --
1290  *
1291  * Process an SOI marker
1292  *
1293  * Results:
1294  * None.
1295  *
1296  * Side effects:
1297  * Bitstream is parsed.
1298  * Exits on error.
1299  *
1300  *--------------------------------------------------------------
1301  */
1302 static inline void
1303 GetSoi (DecompressInfo *dcPtr)
1304 {
1305 
1306  /*
1307  * Reset all parameters that are defined to be reset by SOI
1308  */
1309  dcPtr->restartInterval = 0;
1310 }
1311 
1312 /*
1313  *--------------------------------------------------------------
1314  *
1315  * NextMarker --
1316  *
1317  * Find the next JPEG marker Note that the output might not
1318  * be a valid marker code but it will never be 0 or FF
1319  *
1320  * Results:
1321  * The marker found.
1322  *
1323  * Side effects:
1324  * Bitstream is parsed.
1325  *
1326  *--------------------------------------------------------------
1327  */
1328 static int32_t
1329 NextMarker(IO::Stream *s)
1330 {
1331  int32_t c;
1332 
1333  do {
1334  /*
1335  * skip any non-FF bytes
1336  */
1337  do {
1338  c = s->readByte();
1339  } while (c != 0xFF);
1340  /*
1341  * skip any duplicate FFs without incrementing nbytes, since
1342  * extra FFs are legal
1343  */
1344  do {
1345  c = s->readByte();
1346  } while (c == 0xFF);
1347  } while (c == 0); /* repeat if it was a stuffed FF/00 */
1348 
1349  return c;
1350 }
1351 
1352 /*
1353  *--------------------------------------------------------------
1354  *
1355  * ProcessTables --
1356  *
1357  * Scan and process JPEG markers that can appear in any order
1358  * Return when an SOI, EOI, SOFn, or SOS is found
1359  *
1360  * Results:
1361  * The marker found.
1362  *
1363  * Side effects:
1364  * Bitstream is parsed.
1365  *
1366  *--------------------------------------------------------------
1367  */
1368 LJpegDecompressor::JpegMarker
1369 LJpegDecompressor::ProcessTables (DecompressInfo *dcPtr)
1370 {
1371  int c;
1372 
1373  while (1) {
1374  c = NextMarker (m_stream);
1375 
1376  switch (c) {
1377  case M_SOF0:
1378  case M_SOF1:
1379  case M_SOF2:
1380  case M_SOF3:
1381  case M_SOF5:
1382  case M_SOF6:
1383  case M_SOF7:
1384  case M_JPG:
1385  case M_SOF9:
1386  case M_SOF10:
1387  case M_SOF11:
1388  case M_SOF13:
1389  case M_SOF14:
1390  case M_SOF15:
1391  case M_SOI:
1392  case M_EOI:
1393  case M_SOS:
1394  return ((JpegMarker)c);
1395 
1396  case M_DHT:
1397  GetDht (dcPtr);
1398  break;
1399 
1400  case M_DQT:
1401  Trace(WARNING) << "Not a lossless JPEG file.\n";
1402  break;
1403 
1404  case M_DRI:
1405  GetDri (dcPtr);
1406  break;
1407 
1408  case M_APP0:
1409  GetApp0(m_stream);
1410  break;
1411 
1412  case M_RST0: /* these are all parameterless */
1413  case M_RST1:
1414  case M_RST2:
1415  case M_RST3:
1416  case M_RST4:
1417  case M_RST5:
1418  case M_RST6:
1419  case M_RST7:
1420  case M_TEM:
1421  Trace(WARNING) << str(boost::format("Warning: unexpected "
1422  "marker 0x%1%") % c);
1423  break;
1424 
1425  default: /* must be DNL, DHP, EXP, APPn, JPGn, COM,
1426  * or RESn */
1427  SkipVariable (m_stream);
1428  break;
1429  }
1430  }
1431 }
1432 
1433 /*
1434  *--------------------------------------------------------------
1435  *
1436  * ReadFileHeader --
1437  *
1438  * Initialize and read the file header (everything through
1439  * the SOF marker).
1440  *
1441  * Results:
1442  * None
1443  *
1444  * Side effects:
1445  * Exit on error.
1446  *
1447  *--------------------------------------------------------------
1448  */
1449 void
1450 LJpegDecompressor::ReadFileHeader (DecompressInfo *dcPtr)
1451  throw(DecodingException)
1452 {
1453  int c, c2;
1454 
1455  /*
1456  * Demand an SOI marker at the start of the file --- otherwise it's
1457  * probably not a JPEG file at all.
1458  */
1459  c = m_stream->readByte();
1460  c2 = m_stream->readByte();
1461  if ((c != 0xFF) || (c2 != M_SOI)) {
1462  throw DecodingException(str(boost::format("Not a JPEG file. "
1463  "marker is %1% %2%\n")
1464  % c % c2));
1465  }
1466 
1467  GetSoi (dcPtr); /* OK, process SOI */
1468 
1469  /*
1470  * Process markers until SOF
1471  */
1472  c = ProcessTables (dcPtr);
1473 
1474  switch (c) {
1475  case M_SOF0:
1476  case M_SOF1:
1477  case M_SOF3:
1478  GetSof(dcPtr);
1479  break;
1480 
1481  default:
1482  Trace(WARNING) << str(boost::format("Unsupported SOF marker "
1483  "type 0x%1%\n") % c);
1484  break;
1485  }
1486 }
1487 
1488 /*
1489  *--------------------------------------------------------------
1490  *
1491  * ReadScanHeader --
1492  *
1493  * Read the start of a scan (everything through the SOS marker).
1494  *
1495  * Results:
1496  * 1 if find SOS, 0 if find EOI
1497  *
1498  * Side effects:
1499  * Bitstream is parsed, may exit on errors.
1500  *
1501  *--------------------------------------------------------------
1502  */
1503 int32_t
1504 LJpegDecompressor::ReadScanHeader (DecompressInfo *dcPtr)
1505 {
1506  int c;
1507 
1508  /*
1509  * Process markers until SOS or EOI
1510  */
1511  c = ProcessTables (dcPtr);
1512 
1513  switch (c) {
1514  case M_SOS:
1515  GetSos (dcPtr);
1516  return 1;
1517 
1518  case M_EOI:
1519  return 0;
1520 
1521  default:
1522  Trace(WARNING) << str(boost::format("Unexpected marker "
1523  "0x%1%\n") % c);
1524  break;
1525  }
1526  return 0;
1527 }
1528 
1529 
1531 {
1532  DecompressInfo dcInfo;
1533  try {
1534  ReadFileHeader(&dcInfo);
1535  ReadScanHeader (&dcInfo);
1536 
1537  if(bitmap == NULL)
1538  {
1539  bitmap = new RawData();
1540  }
1541  m_output = bitmap;
1542  bitmap->setDataType(OR_DATA_TYPE_CFA);
1543  uint32_t bpc = dcInfo.dataPrecision;
1544 
1545  bitmap->setBpc(bpc);
1546  bitmap->setMax((1 << bpc) - 1);
1547  /*uint16_t *dataPtr = (uint16_t*)*/
1548  bitmap->allocData(dcInfo.imageWidth
1549  * sizeof(uint16_t)
1550  * dcInfo.imageHeight
1551  * dcInfo.numComponents);
1552 
1553  Trace(DEBUG1) << "dc width = " << dcInfo.imageWidth
1554  << " dc height = " << dcInfo.imageHeight
1555  << "\n";
1556  /* consistently the real width is the JPEG width * numComponent
1557  * at least with all the Canon.
1558  * @todo check that this is valid with DNG too.
1559  */
1560  uint32_t width = dcInfo.imageWidth * dcInfo.numComponents;
1561  bitmap->setDimensions(width, dcInfo.imageHeight);
1562  bitmap->setSlices(m_slices);
1563  DecoderStructInit(&dcInfo);
1564  HuffDecoderInit(&dcInfo);
1565  DecodeImage(&dcInfo);
1566  // TODO handle the error properly
1567  }
1568  catch(...)
1569  {
1570  Trace(ERROR) << "Decompression error\n";
1571  }
1572  m_output = NULL;
1573  return bitmap;
1574 }
1575 
1576 }
1577 }
1578 
1579 /*
1580  Local Variables:
1581  mode:c++
1582  c-file-style:"stroustrup"
1583  c-file-offsets:((innamespace . 0))
1584  indent-tabs-mode:nil
1585  fill-column:80
1586  End:
1587 */
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:33
virtual void setDimensions(uint32_t x, uint32_t y)
Definition: rawdata.cpp:132
RawData & append(uint16_t c)
Definition: rawdata.cpp:185
void setBpc(uint32_t _bpc)
Definition: bitmapdata.cpp:154
Definition: trace.cpp:28
void setSlices(const std::vector< uint16_t > &slices)
void setDataType(DataType _type)
Definition: bitmapdata.cpp:92
virtual RawData * decompress(RawData *in=NULL)