libopenraw
|
00001 /* 00002 * libopenraw - ljpegdecompressor.cpp 00003 * 00004 * Copyright (C) 2007 Hubert Figuiere 00005 * 00006 * This library is free software: you can redistribute it and/or 00007 * modify it under the terms of the GNU Lesser General Public License 00008 * as published by the Free Software Foundation, either version 3 of 00009 * the License, or (at your option) any later version. 00010 * 00011 * This library is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 * Lesser General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU Lesser General Public 00017 * License along with this library. If not, see 00018 * <http://www.gnu.org/licenses/>. 00019 */ 00020 /* 00021 * Code for JPEG lossless decoding. Large parts are grabbed from the IJG 00022 * software, so: 00023 * 00024 * Copyright (C) 1991, 1992, Thomas G. Lane. 00025 * Part of the Independent JPEG Group's software. 00026 * See the file Copyright for more details. 00027 * 00028 * Copyright (c) 1993 Brian C. Smith, The Regents of the University 00029 * of California 00030 * All rights reserved. 00031 * 00032 * Copyright (c) 1994 Kongji Huang and Brian C. Smith. 00033 * Cornell University 00034 * All rights reserved. 00035 * 00036 * Permission to use, copy, modify, and distribute this software and its 00037 * documentation for any purpose, without fee, and without written agreement is 00038 * hereby granted, provided that the above copyright notice and the following 00039 * two paragraphs appear in all copies of this software. 00040 * 00041 * IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR 00042 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT 00043 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL 00044 * UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00045 * 00046 * CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES, 00047 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 00048 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS 00049 * ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO 00050 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. 00051 */ 00052 00053 00054 #include <stdlib.h> 00055 #include <string.h> 00056 00057 #include <boost/scoped_ptr.hpp> 00058 #include <boost/scoped_array.hpp> 00059 #include <boost/format.hpp> 00060 00061 #include <libopenraw++/rawdata.h> 00062 #include "io/memstream.h" 00063 #include "trace.h" 00064 #include "rawcontainer.h" 00065 #include "jfifcontainer.h" 00066 #include "ljpegdecompressor.h" 00067 #include "ljpegdecompressor_priv.h" 00068 00069 namespace OpenRaw { 00070 00071 using namespace Debug; 00072 00073 namespace Internals { 00074 00075 00076 static void SkipVariable(IO::Stream *s); 00077 static uint16_t Get2bytes (IO::Stream * s); 00078 static int32_t NextMarker(IO::Stream * ); 00079 static void GetSoi(DecompressInfo *dcPtr); 00080 static void GetApp0(IO::Stream *); 00081 00082 LJpegDecompressor::LJpegDecompressor(IO::Stream *stream, 00083 RawContainer *container) 00084 : Decompressor(stream, container), 00085 m_slices(), 00086 m_mcuROW1(NULL), m_mcuROW2(NULL), 00087 m_buf1(NULL), m_buf2(NULL), 00088 m_bitsLeft(0), 00089 m_getBuffer(0), 00090 m_output(0) 00091 { 00092 } 00093 00094 00095 LJpegDecompressor::~LJpegDecompressor() 00096 { 00097 if(m_mcuROW1) { 00098 free(m_mcuROW1); 00099 } 00100 if(m_mcuROW2) { 00101 free(m_mcuROW2); 00102 } 00103 if(m_buf1) { 00104 free(m_buf1); 00105 } 00106 if(m_buf2) { 00107 free(m_buf2); 00108 } 00109 } 00110 00111 00112 void LJpegDecompressor::setSlices(const std::vector<uint16_t> & slices) 00113 { 00114 uint16_t n = slices[0]; 00115 m_slices.resize(n + 1); 00116 for(uint16_t i = 0; i < n; i++) { 00117 m_slices[i] = slices[1]; 00118 } 00119 m_slices[n] = slices[2]; 00120 } 00121 00122 00123 00124 00125 static uint32_t bitMask[] = { 0xffffffff, 0x7fffffff, 00126 0x3fffffff, 0x1fffffff, 00127 0x0fffffff, 0x07ffffff, 00128 0x03ffffff, 0x01ffffff, 00129 0x00ffffff, 0x007fffff, 00130 0x003fffff, 0x001fffff, 00131 0x000fffff, 0x0007ffff, 00132 0x0003ffff, 0x0001ffff, 00133 0x0000ffff, 0x00007fff, 00134 0x00003fff, 0x00001fff, 00135 0x00000fff, 0x000007ff, 00136 0x000003ff, 0x000001ff, 00137 0x000000ff, 0x0000007f, 00138 0x0000003f, 0x0000001f, 00139 0x0000000f, 0x00000007, 00140 0x00000003, 0x00000001}; 00141 00142 void FixHuffTbl (HuffmanTable *htbl); 00143 00144 00145 /* 00146 *-------------------------------------------------------------- 00147 * 00148 * FixHuffTbl -- 00149 * 00150 * Compute derived values for a Huffman table one the DHT marker 00151 * has been processed. This generates both the encoding and 00152 * decoding tables. 00153 * 00154 * Results: 00155 * None. 00156 * 00157 * Side effects: 00158 * None. 00159 * 00160 *-------------------------------------------------------------- 00161 */ 00162 void 00163 FixHuffTbl (HuffmanTable *htbl) 00164 { 00165 int32_t p, i, l, lastp, si; 00166 char huffsize[257]; 00167 uint16_t huffcode[257]; 00168 uint16_t code; 00169 int32_t size; 00170 int32_t value, ll, ul; 00171 00172 /* 00173 * Figure C.1: make table of Huffman code length for each symbol 00174 * Note that this is in code-length order. 00175 */ 00176 p = 0; 00177 for (l = 1; l <= 16; l++) { 00178 for (i = 1; i <= (int)htbl->bits[l]; i++) 00179 huffsize[p++] = (char)l; 00180 } 00181 huffsize[p] = 0; 00182 lastp = p; 00183 00184 00185 /* 00186 * Figure C.2: generate the codes themselves 00187 * Note that this is in code-length order. 00188 */ 00189 code = 0; 00190 si = huffsize[0]; 00191 p = 0; 00192 while (huffsize[p]) { 00193 while (((int)huffsize[p]) == si) { 00194 huffcode[p++] = code; 00195 code++; 00196 } 00197 code <<= 1; 00198 si++; 00199 } 00200 00201 /* 00202 * Figure C.3: generate encoding tables 00203 * These are code and size indexed by symbol value 00204 * Set any codeless symbols to have code length 0; this allows 00205 * EmitBits to detect any attempt to emit such symbols. 00206 */ 00207 memset(htbl->ehufsi, 0, sizeof(htbl->ehufsi)); 00208 00209 for (p = 0; p < lastp; p++) { 00210 htbl->ehufco[htbl->huffval[p]] = huffcode[p]; 00211 htbl->ehufsi[htbl->huffval[p]] = huffsize[p]; 00212 } 00213 00214 /* 00215 * Figure F.15: generate decoding tables 00216 */ 00217 p = 0; 00218 for (l = 1; l <= 16; l++) { 00219 if (htbl->bits[l]) { 00220 htbl->valptr[l] = p; 00221 htbl->mincode[l] = huffcode[p]; 00222 p += htbl->bits[l]; 00223 htbl->maxcode[l] = huffcode[p - 1]; 00224 } else { 00225 htbl->maxcode[l] = -1; 00226 } 00227 } 00228 00229 /* 00230 * We put in this value to ensure HuffDecode terminates. 00231 */ 00232 htbl->maxcode[17] = 0xFFFFFL; 00233 00234 /* 00235 * Build the numbits, value lookup tables. 00236 * These table allow us to gather 8 bits from the bits stream, 00237 * and immediately lookup the size and value of the huffman codes. 00238 * If size is zero, it means that more than 8 bits are in the huffman 00239 * code (this happens about 3-4% of the time). 00240 */ 00241 bzero (htbl->numbits, sizeof(htbl->numbits)); 00242 for (p=0; p<lastp; p++) { 00243 size = huffsize[p]; 00244 if (size <= 8) { 00245 value = htbl->huffval[p]; 00246 code = huffcode[p]; 00247 ll = code << (8-size); 00248 if (size < 8) { 00249 ul = ll | bitMask[24+size]; 00250 } else { 00251 ul = ll; 00252 } 00253 for (i=ll; i<=ul; i++) { 00254 htbl->numbits[i] = size; 00255 htbl->value[i] = value; 00256 } 00257 } 00258 } 00259 } 00260 00261 00262 #define RST0 0xD0 /* RST0 marker code */ 00263 00264 00265 #if 0 00266 /* 00267 * The following variables keep track of the input buffer 00268 * for the JPEG data, which is read by ReadJpegData. 00269 */ 00270 uint8_t inputBuffer[JPEG_BUF_SIZE]; /* Input buffer for JPEG data */ 00271 int numInputBytes; /* The total number of bytes in inputBuffer */ 00272 int maxInputBytes; /* Size of inputBuffer */ 00273 int inputBufferOffset; /* Offset of current byte */ 00274 #endif 00275 00276 00277 /* 00278 * Code for extracting the next N bits from the input stream. 00279 * (N never exceeds 15 for JPEG data.) 00280 * This needs to go as fast as possible! 00281 * 00282 * We read source bytes into getBuffer and dole out bits as needed. 00283 * If getBuffer already contains enough bits, they are fetched in-line 00284 * by the macros get_bits() and get_bit(). When there aren't enough bits, 00285 * fillBitBuffer is called; it will attempt to fill getBuffer to the 00286 * "high water mark", then extract the desired number of bits. The idea, 00287 * of course, is to minimize the function-call overhead cost of entering 00288 * fillBitBuffer. 00289 * On most machines MIN_GET_BITS should be 25 to allow the full 32-bit width 00290 * of getBuffer to be used. (On machines with wider words, an even larger 00291 * buffer could be used.) 00292 */ 00293 00294 #define BITS_PER_LONG (8*sizeof(int32_t)) 00295 #define MIN_GET_BITS (BITS_PER_LONG-7) /* max value for long getBuffer */ 00296 00297 /* 00298 * bmask[n] is mask for n rightmost bits 00299 */ 00300 static int32_t bmask[] = {0x0000, 00301 0x0001, 0x0003, 0x0007, 0x000F, 00302 0x001F, 0x003F, 0x007F, 0x00FF, 00303 0x01FF, 0x03FF, 0x07FF, 0x0FFF, 00304 0x1FFF, 0x3FFF, 0x7FFF, 0xFFFF}; 00305 00306 00307 /* 00308 * Lossless JPEG specifies data precision to be from 2 to 16 bits/sample. 00309 */ 00310 #define MinPrecisionBits 2 00311 #define MaxPrecisionBits 16 00312 00313 00314 /* 00315 *-------------------------------------------------------------- 00316 * 00317 * DecoderStructInit -- 00318 * 00319 * Initalize the rest of the fields in the decompression 00320 * structure. 00321 * 00322 * Results: 00323 * None. 00324 * 00325 * Side effects: 00326 * None. 00327 * 00328 *-------------------------------------------------------------- 00329 */ 00330 void 00331 LJpegDecompressor::DecoderStructInit (DecompressInfo *dcPtr) 00332 throw(DecodingException) 00333 { 00334 int16_t ci,i; 00335 JpegComponentInfo *compPtr; 00336 int32_t mcuSize; 00337 00338 /* 00339 * Check sampling factor validity. 00340 */ 00341 for (ci = 0; ci < dcPtr->numComponents; ci++) { 00342 compPtr = &dcPtr->compInfo[ci]; 00343 if ((compPtr->hSampFactor != 1) || (compPtr->vSampFactor != 1)) { 00344 throw DecodingException("Error: Downsampling is not supported.\n"); 00345 } 00346 } 00347 00348 /* 00349 * Prepare array describing MCU composition 00350 */ 00351 if (dcPtr->compsInScan == 1) { 00352 dcPtr->MCUmembership[0] = 0; 00353 } else { 00354 if (dcPtr->compsInScan > 4) { 00355 throw DecodingException("Too many components for interleaved scan"); 00356 } 00357 00358 for (ci = 0; ci < dcPtr->compsInScan; ci++) { 00359 dcPtr->MCUmembership[ci] = ci; 00360 } 00361 } 00362 00363 /* 00364 * Initialize mucROW1 and mcuROW2 which buffer two rows of 00365 * pixels for predictor calculation. 00366 */ 00367 00368 if ((m_mcuROW1 = (MCU *)malloc(dcPtr->imageWidth*sizeof(MCU)))==NULL) { 00369 throw DecodingException("Not enough memory for mcuROW1\n"); 00370 } 00371 if ((m_mcuROW2 = (MCU *)malloc(dcPtr->imageWidth*sizeof(MCU)))==NULL) { 00372 throw DecodingException("Not enough memory for mcuROW2\n"); 00373 } 00374 00375 mcuSize=dcPtr->compsInScan * sizeof(ComponentType); 00376 if ((m_buf1 = (char *)malloc(dcPtr->imageWidth*mcuSize))==NULL) { 00377 throw DecodingException("Not enough memory for buf1\n"); 00378 } 00379 if ((m_buf2 = (char *)malloc(dcPtr->imageWidth*mcuSize))==NULL) { 00380 throw DecodingException("Not enough memory for buf2\n"); 00381 } 00382 00383 for (i=0;i<dcPtr->imageWidth;i++) { 00384 m_mcuROW1[i]=(MCU)(m_buf1+i*mcuSize); 00385 m_mcuROW2[i]=(MCU)(m_buf2+i*mcuSize); 00386 } 00387 } 00388 00389 00390 /* 00391 *-------------------------------------------------------------- 00392 * 00393 * fillBitBuffer -- 00394 * 00395 * Load up the bit buffer with at least nbits 00396 * Process any stuffed bytes at this time. 00397 * 00398 * Results: 00399 * None 00400 * 00401 * Side effects: 00402 * The bitwise global variables are updated. 00403 * 00404 *-------------------------------------------------------------- 00405 */ 00406 void 00407 LJpegDecompressor::fillBitBuffer (IO::Stream * s,uint16_t nbits) 00408 { 00409 uint8_t c, c2; 00410 00411 while (m_bitsLeft < MIN_GET_BITS) { 00412 c = s->readByte(); 00413 00414 /* 00415 * If it's 0xFF, check and discard stuffed zero byte 00416 */ 00417 if (c == 0xFF) { 00418 c2 = s->readByte(); 00419 00420 if (c2 != 0) { 00421 00422 /* 00423 * Oops, it's actually a marker indicating end of 00424 * compressed data. Better put it back for use later. 00425 */ 00426 s->seek(-2, SEEK_CUR); 00427 00428 /* 00429 * There should be enough bits still left in the data 00430 * segment; if so, just break out of the while loop. 00431 */ 00432 if (m_bitsLeft >= nbits) 00433 break; 00434 00435 /* 00436 * Uh-oh. Corrupted data: stuff zeroes into the data 00437 * stream, since this sometimes occurs when we are on the 00438 * last show_bits(8) during decoding of the Huffman 00439 * segment. 00440 */ 00441 c = 0; 00442 } 00443 } 00444 /* 00445 * OK, load c into getBuffer 00446 */ 00447 m_getBuffer = (m_getBuffer << 8) | c; 00448 m_bitsLeft += 8; 00449 } 00450 } 00451 00452 00453 00454 inline int32_t LJpegDecompressor::QuickPredict(int32_t col, int16_t curComp, 00455 MCU *curRowBuf, 00456 MCU *prevRowBuf, 00457 int32_t psv) 00458 { 00459 int32_t left,upper,diag,leftcol; 00460 int32_t predictor; 00461 00462 leftcol=col-1; 00463 upper=prevRowBuf[col][curComp]; 00464 left=curRowBuf[leftcol][curComp]; 00465 diag=prevRowBuf[leftcol][curComp]; 00466 00467 /* 00468 * All predictor are calculated according to psv. 00469 */ 00470 switch (psv) { 00471 case 0: 00472 predictor = 0; 00473 break; 00474 case 1: 00475 predictor = left; 00476 break; 00477 case 2: 00478 predictor = upper; 00479 break; 00480 case 3: 00481 predictor = diag; 00482 break; 00483 case 4: 00484 predictor = left+upper-diag; 00485 break; 00486 case 5: 00487 predictor = left+((upper-diag)>>1); 00488 break; 00489 case 6: 00490 predictor = upper+((left-diag)>>1); 00491 break; 00492 case 7: 00493 predictor = (left+upper)>>1; 00494 break; 00495 default: 00496 Trace(WARNING) << "Warning: Undefined PSV\n"; 00497 predictor = 0; 00498 } 00499 return predictor; 00500 } 00501 00502 inline 00503 int32_t LJpegDecompressor::show_bits8(IO::Stream * s) 00504 { 00505 if (m_bitsLeft < 8) { 00506 fillBitBuffer(s, 8); 00507 } 00508 return (m_getBuffer >> (m_bitsLeft-8)) & 0xff; 00509 } 00510 00511 inline 00512 void LJpegDecompressor::flush_bits(uint16_t nbits) 00513 { 00514 m_bitsLeft -= (nbits); 00515 } 00516 00517 00518 inline 00519 int32_t LJpegDecompressor::get_bits(uint16_t nbits) 00520 { 00521 if (m_bitsLeft < nbits) 00522 fillBitBuffer(m_stream, nbits); 00523 return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits]; 00524 } 00525 00526 inline 00527 int32_t LJpegDecompressor::get_bit() 00528 { 00529 if (!m_bitsLeft) 00530 fillBitBuffer(m_stream, 1); 00531 return (m_getBuffer >> (--m_bitsLeft)) & 1; 00532 } 00533 00534 00535 inline 00536 int32_t LJpegDecompressor::readBits(IO::Stream * s, uint16_t nbits) 00537 { 00538 if (m_bitsLeft < nbits) { 00539 fillBitBuffer(s, nbits); 00540 } 00541 return ((m_getBuffer >> (m_bitsLeft -= (nbits)))) & bmask[nbits]; 00542 } 00543 00544 00545 00546 /* 00547 *-------------------------------------------------------------- 00548 * 00549 * PmPutRow -- 00550 * 00551 * Output one row of pixels stored in RowBuf. 00552 * 00553 * Results: 00554 * None 00555 * 00556 * Side effects: 00557 * One row of pixels are write to file pointed by outFile. 00558 * 00559 *-------------------------------------------------------------- 00560 */ 00561 inline void 00562 LJpegDecompressor::PmPutRow(MCU* RowBuf, int32_t numComp, int32_t numCol, int32_t Pt) 00563 { 00564 // TODO this might be wrong in 8 bits... 00565 // original code was using putc which *i think* was a problem for 00566 // 16bpp 00567 int32_t comp; 00568 int32_t col; 00569 uint16_t v; 00570 00571 for (col = 0; col < numCol; col++) { 00572 for (comp = 0; comp < numComp; comp++) { 00573 v = RowBuf[col][comp]<<Pt; 00574 m_output->append(v); 00575 } 00576 } 00577 // m_output->nextRow(); 00578 } 00579 00580 /* 00581 *-------------------------------------------------------------- 00582 * 00583 * HuffDecode -- 00584 * 00585 * Taken from Figure F.16: extract next coded symbol from 00586 * input stream. This should becode a macro. 00587 * 00588 * Results: 00589 * Next coded symbol 00590 * 00591 * Side effects: 00592 * Bitstream is parsed. 00593 * 00594 *-------------------------------------------------------------- 00595 */ 00596 inline int32_t 00597 LJpegDecompressor::HuffDecode(HuffmanTable *htbl) 00598 { 00599 int32_t rv; 00600 int32_t l, temp; 00601 int32_t code; 00602 00603 /* 00604 * If the huffman code is less than 8 bits, we can use the fast 00605 * table lookup to get its value. It's more than 8 bits about 00606 * 3-4% of the time. 00607 */ 00608 code = show_bits8(m_stream); 00609 if (htbl->numbits[code]) { 00610 flush_bits(htbl->numbits[code]); 00611 rv=htbl->value[code]; 00612 } else { 00613 flush_bits(8); 00614 l = 8; 00615 while (code > htbl->maxcode[l]) { 00616 temp = get_bit(); 00617 code = (code << 1) | temp; 00618 l++; 00619 } 00620 00621 /* 00622 * With garbage input we may reach the sentinel value l = 17. 00623 */ 00624 00625 if (l > 16) { 00626 //Trace(WARNING) << "Corrupt JPEG data: bad Huffman code " << l << "\n"; 00627 rv = 0; /* fake a zero as the safest result */ 00628 } else { 00629 rv = htbl->huffval[htbl->valptr[l] + 00630 ((int)(code - htbl->mincode[l]))]; 00631 } 00632 } 00633 return rv; 00634 } 00635 00636 /* 00637 *-------------------------------------------------------------- 00638 * 00639 * HuffExtend -- 00640 * 00641 * Code and table for Figure F.12: extend sign bit 00642 * 00643 * Results: 00644 * The extended value. 00645 * 00646 * Side effects: 00647 * None. 00648 * 00649 *-------------------------------------------------------------- 00650 */ 00651 static int32_t extendTest[16] = /* entry n is 2**(n-1) */ 00652 {0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 00653 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000}; 00654 00655 static int32_t extendOffset[16] = /* entry n is (-1 << n) + 1 */ 00656 {0, ((-1) << 1) + 1, ((-1) << 2) + 1, ((-1) << 3) + 1, ((-1) << 4) + 1, 00657 ((-1) << 5) + 1, ((-1) << 6) + 1, ((-1) << 7) + 1, ((-1) << 8) + 1, 00658 ((-1) << 9) + 1, ((-1) << 10) + 1, ((-1) << 11) + 1, ((-1) << 12) + 1, 00659 ((-1) << 13) + 1, ((-1) << 14) + 1, ((-1) << 15) + 1}; 00660 00661 00662 inline 00663 void HuffExtend(int32_t & x, int32_t s) 00664 { 00665 if ((x) < extendTest[s]) { 00666 (x) += extendOffset[s]; 00667 } 00668 } 00669 00670 /* 00671 *-------------------------------------------------------------- 00672 * 00673 * HuffDecoderInit -- 00674 * 00675 * Initialize for a Huffman-compressed scan. 00676 * This is invoked after reading the SOS marker. 00677 * 00678 * Results: 00679 * None 00680 * 00681 * Side effects: 00682 * None. 00683 * 00684 *-------------------------------------------------------------- 00685 */ 00686 void 00687 LJpegDecompressor::HuffDecoderInit (DecompressInfo *dcPtr) 00688 throw(DecodingException) 00689 { 00690 int16_t ci; 00691 JpegComponentInfo *compptr; 00692 00693 /* 00694 * Initialize static variables 00695 */ 00696 m_bitsLeft = 0; 00697 00698 for (ci = 0; ci < dcPtr->compsInScan; ci++) { 00699 compptr = dcPtr->curCompInfo[ci]; 00700 /* 00701 * Make sure requested tables are present 00702 */ 00703 if (dcPtr->dcHuffTblPtrs[compptr->dcTblNo] == NULL) { 00704 throw DecodingException("Error: Use of undefined Huffman table\n"); 00705 } 00706 00707 /* 00708 * Compute derived values for Huffman tables. 00709 * We may do this more than once for same table, but it's not a 00710 * big deal 00711 */ 00712 FixHuffTbl (dcPtr->dcHuffTblPtrs[compptr->dcTblNo]); 00713 } 00714 00715 /* 00716 * Initialize restart stuff 00717 */ 00718 dcPtr->restartInRows = (dcPtr->restartInterval)/(dcPtr->imageWidth); 00719 dcPtr->restartRowsToGo = dcPtr->restartInRows; 00720 dcPtr->nextRestartNum = 0; 00721 } 00722 00723 /* 00724 *-------------------------------------------------------------- 00725 * 00726 * ProcessRestart -- 00727 * 00728 * Check for a restart marker & resynchronize decoder. 00729 * 00730 * Results: 00731 * None. 00732 * 00733 * Side effects: 00734 * BitStream is parsed, bit buffer is reset, etc. 00735 * 00736 *-------------------------------------------------------------- 00737 */ 00738 void 00739 LJpegDecompressor::ProcessRestart (DecompressInfo *dcPtr) 00740 throw(DecodingException) 00741 { 00742 int32_t c, nbytes; 00743 00744 /* 00745 * Throw away any unused bits remaining in bit buffer 00746 */ 00747 nbytes = m_bitsLeft / 8; 00748 m_bitsLeft = 0; 00749 00750 /* 00751 * Scan for next JPEG marker 00752 */ 00753 do { 00754 do { /* skip any non-FF bytes */ 00755 nbytes++; 00756 c = m_stream->readByte(); 00757 } while (c != 0xFF); 00758 do { /* skip any duplicate FFs */ 00759 /* 00760 * we don't increment nbytes here since extra FFs are legal 00761 */ 00762 c = m_stream->readByte(); 00763 } while (c == 0xFF); 00764 } while (c == 0); /* repeat if it was a stuffed FF/00 */ 00765 00766 if (c != (RST0 + dcPtr->nextRestartNum)) { 00767 00768 /* 00769 * Uh-oh, the restart markers have been messed up too. 00770 * Just bail out. 00771 */ 00772 throw DecodingException("Error: Corrupt JPEG data. " 00773 "Aborting decoding...\n"); 00774 } 00775 00776 /* 00777 * Update restart state 00778 */ 00779 dcPtr->restartRowsToGo = dcPtr->restartInRows; 00780 dcPtr->nextRestartNum = (dcPtr->nextRestartNum + 1) & 7; 00781 } 00782 00783 /* 00784 *-------------------------------------------------------------- 00785 * 00786 * DecodeFirstRow -- 00787 * 00788 * Decode the first raster line of samples at the start of 00789 * the scan and at the beginning of each restart interval. 00790 * This includes modifying the component value so the real 00791 * value, not the difference is returned. 00792 * 00793 * Results: 00794 * None. 00795 * 00796 * Side effects: 00797 * Bitstream is parsed. 00798 * 00799 *-------------------------------------------------------------- 00800 */ 00801 void LJpegDecompressor::DecodeFirstRow(DecompressInfo *dcPtr, 00802 MCU *curRowBuf) 00803 { 00804 uint16_t curComp,ci; 00805 int32_t s,col,compsInScan,numCOL; 00806 JpegComponentInfo *compptr; 00807 int32_t Pr,Pt,d; 00808 HuffmanTable *dctbl; 00809 00810 Pr=dcPtr->dataPrecision; 00811 Pt=dcPtr->Pt; 00812 compsInScan=dcPtr->compsInScan; 00813 numCOL=dcPtr->imageWidth; 00814 00815 /* 00816 * the start of the scan or at the beginning of restart interval. 00817 */ 00818 for (curComp = 0; curComp < compsInScan; curComp++) { 00819 ci = dcPtr->MCUmembership[curComp]; 00820 compptr = dcPtr->curCompInfo[ci]; 00821 dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; 00822 00823 /* 00824 * Section F.2.2.1: decode the difference 00825 */ 00826 s = HuffDecode (dctbl); 00827 if (s) { 00828 d = get_bits(s); 00829 HuffExtend(d,s); 00830 } else { 00831 d = 0; 00832 } 00833 00834 /* 00835 * Add the predictor to the difference. 00836 */ 00837 curRowBuf[0][curComp]=d+(1<<(Pr-Pt-1)); 00838 } 00839 00840 /* 00841 * the rest of the first row 00842 */ 00843 for (col=1; col<numCOL; col++) { 00844 for (curComp = 0; curComp < compsInScan; curComp++) { 00845 ci = dcPtr->MCUmembership[curComp]; 00846 compptr = dcPtr->curCompInfo[ci]; 00847 dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; 00848 00849 /* 00850 * Section F.2.2.1: decode the difference 00851 */ 00852 s = HuffDecode (dctbl); 00853 if (s) { 00854 d = get_bits(s); 00855 HuffExtend(d,s); 00856 } else { 00857 d = 0; 00858 } 00859 00860 /* 00861 * Add the predictor to the difference. 00862 */ 00863 curRowBuf[col][curComp]=d+curRowBuf[col-1][curComp]; 00864 } 00865 } 00866 00867 if (dcPtr->restartInRows) { 00868 (dcPtr->restartRowsToGo)--; 00869 } 00870 } 00871 00872 /* 00873 *-------------------------------------------------------------- 00874 * 00875 * DecodeImage -- 00876 * 00877 * Decode the input stream. This includes modifying 00878 * the component value so the real value, not the 00879 * difference is returned. 00880 * 00881 * Results: 00882 * None. 00883 * 00884 * Side effects: 00885 * Bitstream is parsed. 00886 * 00887 *-------------------------------------------------------------- 00888 */ 00889 void 00890 LJpegDecompressor::DecodeImage(DecompressInfo *dcPtr) 00891 { 00892 int32_t s,d,col,row; 00893 int16_t curComp, ci; 00894 HuffmanTable *dctbl; 00895 JpegComponentInfo *compptr; 00896 int32_t predictor; 00897 int32_t numCOL,numROW,compsInScan; 00898 MCU *prevRowBuf,*curRowBuf; 00899 int32_t imagewidth,Pt,psv; 00900 00901 numCOL=imagewidth=dcPtr->imageWidth; 00902 numROW=dcPtr->imageHeight; 00903 compsInScan=dcPtr->compsInScan; 00904 Pt=dcPtr->Pt; 00905 psv=dcPtr->Ss; 00906 prevRowBuf=m_mcuROW2; 00907 curRowBuf=m_mcuROW1; 00908 00909 /* 00910 * Decode the first row of image. Output the row and 00911 * turn this row into a previous row for later predictor 00912 * calculation. 00913 */ 00914 DecodeFirstRow(dcPtr,curRowBuf); 00915 PmPutRow(curRowBuf,compsInScan,numCOL,Pt); 00916 std::swap(prevRowBuf,curRowBuf); 00917 00918 for (row=1; row<numROW; row++) { 00919 00920 /* 00921 * Account for restart interval, process restart marker if needed. 00922 */ 00923 if (dcPtr->restartInRows) { 00924 if (dcPtr->restartRowsToGo == 0) { 00925 ProcessRestart (dcPtr); 00926 00927 /* 00928 * Reset predictors at restart. 00929 */ 00930 DecodeFirstRow(dcPtr,curRowBuf); 00931 PmPutRow(curRowBuf,compsInScan,numCOL,Pt); 00932 std::swap(prevRowBuf,curRowBuf); 00933 continue; 00934 } 00935 dcPtr->restartRowsToGo--; 00936 } 00937 00938 /* 00939 * The upper neighbors are predictors for the first column. 00940 */ 00941 for (curComp = 0; curComp < compsInScan; curComp++) { 00942 ci = dcPtr->MCUmembership[curComp]; 00943 compptr = dcPtr->curCompInfo[ci]; 00944 dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; 00945 00946 /* 00947 * Section F.2.2.1: decode the difference 00948 */ 00949 s = HuffDecode (dctbl); 00950 if (s) { 00951 d = get_bits(s); 00952 HuffExtend(d,s); 00953 } else { 00954 d = 0; 00955 } 00956 00957 curRowBuf[0][curComp]=d+prevRowBuf[0][curComp]; 00958 } 00959 00960 /* 00961 * For the rest of the column on this row, predictor 00962 * calculations are base on PSV. 00963 */ 00964 for (col=1; col<numCOL; col++) { 00965 for (curComp = 0; curComp < compsInScan; curComp++) { 00966 ci = dcPtr->MCUmembership[curComp]; 00967 compptr = dcPtr->curCompInfo[ci]; 00968 dctbl = dcPtr->dcHuffTblPtrs[compptr->dcTblNo]; 00969 00970 /* 00971 * Section F.2.2.1: decode the difference 00972 */ 00973 s = HuffDecode (dctbl); 00974 if (s) { 00975 d = get_bits(s); 00976 HuffExtend(d,s); 00977 } else { 00978 d = 0; 00979 } 00980 predictor = QuickPredict(col,curComp,curRowBuf,prevRowBuf, 00981 psv); 00982 00983 curRowBuf[col][curComp]=d+predictor; 00984 } 00985 } 00986 PmPutRow(curRowBuf,compsInScan,numCOL,Pt); 00987 std::swap(prevRowBuf,curRowBuf); 00988 } 00989 } 00990 00991 00992 00993 00994 /* 00995 *-------------------------------------------------------------- 00996 * 00997 * Get2bytes -- 00998 * 00999 * Get a 2-byte unsigned integer (e.g., a marker parameter length 01000 * field) 01001 * 01002 * Results: 01003 * Next two byte of input as an integer. 01004 * 01005 * Side effects: 01006 * Bitstream is parsed. 01007 * 01008 *-------------------------------------------------------------- 01009 */ 01010 static inline uint16_t 01011 Get2bytes (IO::Stream * s) 01012 { 01013 uint16_t a; 01014 01015 a = s->readByte(); 01016 return (a << 8) | s->readByte(); 01017 } 01018 01019 /* 01020 *-------------------------------------------------------------- 01021 * 01022 * SkipVariable -- 01023 * 01024 * Skip over an unknown or uninteresting variable-length marker 01025 * 01026 * Results: 01027 * None. 01028 * 01029 * Side effects: 01030 * Bitstream is parsed over marker. 01031 * 01032 * 01033 *-------------------------------------------------------------- 01034 */ 01035 static inline void SkipVariable(IO::Stream * s) 01036 { 01037 int32_t length; 01038 01039 length = Get2bytes(s) - 2; 01040 01041 s->seek(length, SEEK_CUR); 01042 } 01043 01044 /* 01045 *-------------------------------------------------------------- 01046 * 01047 * GetDht -- 01048 * 01049 * Process a DHT marker 01050 * 01051 * Results: 01052 * None 01053 * 01054 * Side effects: 01055 * A huffman table is read. 01056 * Exits on error. 01057 * 01058 *-------------------------------------------------------------- 01059 */ 01060 void 01061 LJpegDecompressor::GetDht (DecompressInfo *dcPtr) 01062 throw(DecodingException) 01063 { 01064 int32_t length; 01065 int32_t i, index, count; 01066 01067 length = Get2bytes(m_stream) - 2; 01068 01069 while (length) { 01070 index = m_stream->readByte(); 01071 01072 if (index < 0 || index >= 4) { 01073 throw DecodingException(str(boost::format("Bogus DHT index %1%") 01074 % index)); 01075 } 01076 01077 HuffmanTable *& htblptr = dcPtr->dcHuffTblPtrs[index]; 01078 if (htblptr == NULL) { 01079 htblptr = (HuffmanTable *) malloc(sizeof (HuffmanTable)); 01080 if (htblptr==NULL) { 01081 throw DecodingException("Can't malloc HuffmanTable"); 01082 } 01083 } 01084 01085 htblptr->bits[0] = 0; 01086 count = 0; 01087 for (i = 1; i <= 16; i++) { 01088 htblptr->bits[i] = m_stream->readByte(); 01089 count += htblptr->bits[i]; 01090 } 01091 01092 if (count > 256) { 01093 throw DecodingException("Bogus DHT counts"); 01094 } 01095 01096 for (i = 0; i < count; i++) 01097 htblptr->huffval[i] = m_stream->readByte(); 01098 01099 length -= 1 + 16 + count; 01100 } 01101 } 01102 01103 /* 01104 *-------------------------------------------------------------- 01105 * 01106 * GetDri -- 01107 * 01108 * Process a DRI marker 01109 * 01110 * Results: 01111 * None 01112 * 01113 * Side effects: 01114 * Exits on error. 01115 * Bitstream is parsed. 01116 * 01117 *-------------------------------------------------------------- 01118 */ 01119 void 01120 LJpegDecompressor::GetDri(DecompressInfo *dcPtr) 01121 throw(DecodingException) 01122 { 01123 if (Get2bytes(m_stream) != 4) { 01124 throw DecodingException("Bogus length in DRI"); 01125 } 01126 01127 dcPtr->restartInterval = Get2bytes(m_stream); 01128 } 01129 01130 /* 01131 *-------------------------------------------------------------- 01132 * 01133 * GetApp0 -- 01134 * 01135 * Process an APP0 marker. 01136 * 01137 * Results: 01138 * None 01139 * 01140 * Side effects: 01141 * Bitstream is parsed 01142 * 01143 *-------------------------------------------------------------- 01144 */ 01145 static void GetApp0(IO::Stream *s) 01146 { 01147 int32_t length; 01148 01149 length = Get2bytes(s) - 2; 01150 s->seek(length, SEEK_CUR); 01151 } 01152 01153 /* 01154 *-------------------------------------------------------------- 01155 * 01156 * GetSof -- 01157 * 01158 * Process a SOFn marker 01159 * 01160 * Results: 01161 * None. 01162 * 01163 * Side effects: 01164 * Bitstream is parsed 01165 * Exits on error 01166 * dcPtr structure is filled in 01167 * 01168 *-------------------------------------------------------------- 01169 */ 01170 void 01171 LJpegDecompressor::GetSof(DecompressInfo *dcPtr) throw(DecodingException) 01172 { 01173 int32_t length; 01174 int16_t ci; 01175 int32_t c; 01176 JpegComponentInfo *compptr; 01177 01178 length = Get2bytes(m_stream); 01179 01180 dcPtr->dataPrecision = m_stream->readByte(); 01181 dcPtr->imageHeight = Get2bytes(m_stream); 01182 dcPtr->imageWidth = Get2bytes(m_stream); 01183 dcPtr->numComponents = m_stream->readByte(); 01184 01185 /* 01186 * We don't support files in which the image height is initially 01187 * specified as 0 and is later redefined by DNL. As long as we 01188 * have to check that, might as well have a general sanity check. 01189 */ 01190 if ((dcPtr->imageHeight <= 0 ) || 01191 (dcPtr->imageWidth <= 0) || 01192 (dcPtr->numComponents <= 0)) { 01193 throw DecodingException("Empty JPEG image (DNL not supported)"); 01194 } 01195 01196 if ((dcPtr->dataPrecision<MinPrecisionBits) || 01197 (dcPtr->dataPrecision>MaxPrecisionBits)) { 01198 throw DecodingException("Unsupported JPEG data precision"); 01199 } 01200 01201 if (length != (dcPtr->numComponents * 3 + 8)) { 01202 throw DecodingException("Bogus SOF length"); 01203 } 01204 01205 dcPtr->compInfo = (JpegComponentInfo *) malloc 01206 (dcPtr->numComponents * sizeof (JpegComponentInfo)); 01207 01208 for (ci = 0; ci < dcPtr->numComponents; ci++) { 01209 compptr = &dcPtr->compInfo[ci]; 01210 compptr->componentIndex = ci; 01211 compptr->componentId = m_stream->readByte(); 01212 c = m_stream->readByte(); 01213 compptr->hSampFactor = (int16_t)((c >> 4) & 15); 01214 compptr->vSampFactor = (int16_t)((c) & 15); 01215 (void) m_stream->readByte(); /* skip Tq */ 01216 } 01217 } 01218 01219 /* 01220 *-------------------------------------------------------------- 01221 * 01222 * GetSos -- 01223 * 01224 * Process a SOS marker 01225 * 01226 * Results: 01227 * None. 01228 * 01229 * Side effects: 01230 * Bitstream is parsed. 01231 * Exits on error. 01232 * 01233 *-------------------------------------------------------------- 01234 */ 01235 void 01236 LJpegDecompressor::GetSos (DecompressInfo *dcPtr) 01237 throw(DecodingException) 01238 { 01239 int32_t length; 01240 int32_t i; 01241 uint16_t n, ci, c, cc; 01242 JpegComponentInfo *compptr; 01243 01244 length = Get2bytes (m_stream); 01245 01246 /* 01247 * Get the number of image components. 01248 */ 01249 n = m_stream->readByte(); 01250 dcPtr->compsInScan = n; 01251 length -= 3; 01252 01253 if (length != (n * 2 + 3) || n < 1 || n > 4) { 01254 throw DecodingException("Bogus SOS length"); 01255 } 01256 01257 01258 for (i = 0; i < n; i++) { 01259 cc = m_stream->readByte(); 01260 c = m_stream->readByte(); 01261 length -= 2; 01262 01263 for (ci = 0; ci < dcPtr->numComponents; ci++) 01264 if (cc == dcPtr->compInfo[ci].componentId) { 01265 break; 01266 } 01267 01268 if (ci >= dcPtr->numComponents) { 01269 throw DecodingException("Invalid component number in SOS"); 01270 } 01271 01272 compptr = &dcPtr->compInfo[ci]; 01273 dcPtr->curCompInfo[i] = compptr; 01274 compptr->dcTblNo = (c >> 4) & 15; 01275 } 01276 01277 /* 01278 * Get the PSV, skip Se, and get the point transform parameter. 01279 */ 01280 dcPtr->Ss = m_stream->readByte(); 01281 (void)m_stream->readByte(); 01282 c = m_stream->readByte(); 01283 dcPtr->Pt = c & 0x0F; 01284 } 01285 01286 /* 01287 *-------------------------------------------------------------- 01288 * 01289 * GetSoi -- 01290 * 01291 * Process an SOI marker 01292 * 01293 * Results: 01294 * None. 01295 * 01296 * Side effects: 01297 * Bitstream is parsed. 01298 * Exits on error. 01299 * 01300 *-------------------------------------------------------------- 01301 */ 01302 static inline void 01303 GetSoi (DecompressInfo *dcPtr) 01304 { 01305 01306 /* 01307 * Reset all parameters that are defined to be reset by SOI 01308 */ 01309 dcPtr->restartInterval = 0; 01310 } 01311 01312 /* 01313 *-------------------------------------------------------------- 01314 * 01315 * NextMarker -- 01316 * 01317 * Find the next JPEG marker Note that the output might not 01318 * be a valid marker code but it will never be 0 or FF 01319 * 01320 * Results: 01321 * The marker found. 01322 * 01323 * Side effects: 01324 * Bitstream is parsed. 01325 * 01326 *-------------------------------------------------------------- 01327 */ 01328 static int32_t 01329 NextMarker(IO::Stream *s) 01330 { 01331 int32_t c; 01332 01333 do { 01334 /* 01335 * skip any non-FF bytes 01336 */ 01337 do { 01338 c = s->readByte(); 01339 } while (c != 0xFF); 01340 /* 01341 * skip any duplicate FFs without incrementing nbytes, since 01342 * extra FFs are legal 01343 */ 01344 do { 01345 c = s->readByte(); 01346 } while (c == 0xFF); 01347 } while (c == 0); /* repeat if it was a stuffed FF/00 */ 01348 01349 return c; 01350 } 01351 01352 /* 01353 *-------------------------------------------------------------- 01354 * 01355 * ProcessTables -- 01356 * 01357 * Scan and process JPEG markers that can appear in any order 01358 * Return when an SOI, EOI, SOFn, or SOS is found 01359 * 01360 * Results: 01361 * The marker found. 01362 * 01363 * Side effects: 01364 * Bitstream is parsed. 01365 * 01366 *-------------------------------------------------------------- 01367 */ 01368 LJpegDecompressor::JpegMarker 01369 LJpegDecompressor::ProcessTables (DecompressInfo *dcPtr) 01370 { 01371 int c; 01372 01373 while (1) { 01374 c = NextMarker (m_stream); 01375 01376 switch (c) { 01377 case M_SOF0: 01378 case M_SOF1: 01379 case M_SOF2: 01380 case M_SOF3: 01381 case M_SOF5: 01382 case M_SOF6: 01383 case M_SOF7: 01384 case M_JPG: 01385 case M_SOF9: 01386 case M_SOF10: 01387 case M_SOF11: 01388 case M_SOF13: 01389 case M_SOF14: 01390 case M_SOF15: 01391 case M_SOI: 01392 case M_EOI: 01393 case M_SOS: 01394 return ((JpegMarker)c); 01395 01396 case M_DHT: 01397 GetDht (dcPtr); 01398 break; 01399 01400 case M_DQT: 01401 Trace(WARNING) << "Not a lossless JPEG file.\n"; 01402 break; 01403 01404 case M_DRI: 01405 GetDri (dcPtr); 01406 break; 01407 01408 case M_APP0: 01409 GetApp0(m_stream); 01410 break; 01411 01412 case M_RST0: /* these are all parameterless */ 01413 case M_RST1: 01414 case M_RST2: 01415 case M_RST3: 01416 case M_RST4: 01417 case M_RST5: 01418 case M_RST6: 01419 case M_RST7: 01420 case M_TEM: 01421 Trace(WARNING) << str(boost::format("Warning: unexpected " 01422 "marker 0x%1%") % c); 01423 break; 01424 01425 default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, 01426 * or RESn */ 01427 SkipVariable (m_stream); 01428 break; 01429 } 01430 } 01431 } 01432 01433 /* 01434 *-------------------------------------------------------------- 01435 * 01436 * ReadFileHeader -- 01437 * 01438 * Initialize and read the file header (everything through 01439 * the SOF marker). 01440 * 01441 * Results: 01442 * None 01443 * 01444 * Side effects: 01445 * Exit on error. 01446 * 01447 *-------------------------------------------------------------- 01448 */ 01449 void 01450 LJpegDecompressor::ReadFileHeader (DecompressInfo *dcPtr) 01451 throw(DecodingException) 01452 { 01453 int c, c2; 01454 01455 /* 01456 * Demand an SOI marker at the start of the file --- otherwise it's 01457 * probably not a JPEG file at all. 01458 */ 01459 c = m_stream->readByte(); 01460 c2 = m_stream->readByte(); 01461 if ((c != 0xFF) || (c2 != M_SOI)) { 01462 throw DecodingException(str(boost::format("Not a JPEG file. " 01463 "marker is %1% %2%\n") 01464 % c % c2)); 01465 } 01466 01467 GetSoi (dcPtr); /* OK, process SOI */ 01468 01469 /* 01470 * Process markers until SOF 01471 */ 01472 c = ProcessTables (dcPtr); 01473 01474 switch (c) { 01475 case M_SOF0: 01476 case M_SOF1: 01477 case M_SOF3: 01478 GetSof(dcPtr); 01479 break; 01480 01481 default: 01482 Trace(WARNING) << str(boost::format("Unsupported SOF marker " 01483 "type 0x%1%\n") % c); 01484 break; 01485 } 01486 } 01487 01488 /* 01489 *-------------------------------------------------------------- 01490 * 01491 * ReadScanHeader -- 01492 * 01493 * Read the start of a scan (everything through the SOS marker). 01494 * 01495 * Results: 01496 * 1 if find SOS, 0 if find EOI 01497 * 01498 * Side effects: 01499 * Bitstream is parsed, may exit on errors. 01500 * 01501 *-------------------------------------------------------------- 01502 */ 01503 int32_t 01504 LJpegDecompressor::ReadScanHeader (DecompressInfo *dcPtr) 01505 { 01506 int c; 01507 01508 /* 01509 * Process markers until SOS or EOI 01510 */ 01511 c = ProcessTables (dcPtr); 01512 01513 switch (c) { 01514 case M_SOS: 01515 GetSos (dcPtr); 01516 return 1; 01517 01518 case M_EOI: 01519 return 0; 01520 01521 default: 01522 Trace(WARNING) << str(boost::format("Unexpected marker " 01523 "0x%1%\n") % c); 01524 break; 01525 } 01526 return 0; 01527 } 01528 01529 01530 RawData *LJpegDecompressor::decompress(RawData *bitmap) 01531 { 01532 DecompressInfo dcInfo; 01533 try { 01534 ReadFileHeader(&dcInfo); 01535 ReadScanHeader (&dcInfo); 01536 01537 if(bitmap == NULL) 01538 { 01539 bitmap = new RawData(); 01540 } 01541 m_output = bitmap; 01542 bitmap->setDataType(OR_DATA_TYPE_CFA); 01543 uint32_t bpc = dcInfo.dataPrecision; 01544 01545 bitmap->setBpc(bpc); 01546 bitmap->setMax((1 << bpc) - 1); 01547 /*uint16_t *dataPtr = (uint16_t*)*/ 01548 bitmap->allocData(dcInfo.imageWidth 01549 * sizeof(uint16_t) 01550 * dcInfo.imageHeight 01551 * dcInfo.numComponents); 01552 01553 Trace(DEBUG1) << "dc width = " << dcInfo.imageWidth 01554 << " dc height = " << dcInfo.imageHeight 01555 << "\n"; 01556 /* consistently the real width is the JPEG width * numComponent 01557 * at least with all the Canon. 01558 * @todo check that this is valid with DNG too. 01559 */ 01560 uint32_t width = dcInfo.imageWidth * dcInfo.numComponents; 01561 bitmap->setDimensions(width, dcInfo.imageHeight); 01562 bitmap->setSlices(m_slices); 01563 DecoderStructInit(&dcInfo); 01564 HuffDecoderInit(&dcInfo); 01565 DecodeImage(&dcInfo); 01566 // TODO handle the error properly 01567 } 01568 catch(...) 01569 { 01570 Trace(ERROR) << "Decompression error\n"; 01571 } 01572 m_output = NULL; 01573 return bitmap; 01574 } 01575 01576 } 01577 } 01578 01579 /* 01580 Local Variables: 01581 mode:c++ 01582 c-file-style:"stroustrup" 01583 c-file-offsets:((innamespace . 0)) 01584 indent-tabs-mode:nil 01585 fill-column:80 01586 End: 01587 */