~ubuntu-branches/ubuntu/vivid/rawstudio/vivid

« back to all changes in this revision

Viewing changes to plugins/load-rawspeed/rawspeed/LJpegDecompressor.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2011-07-28 17:36:32 UTC
  • mfrom: (2.1.11 upstream)
  • Revision ID: james.westby@ubuntu.com-20110728173632-5czluz9ye3c83zc5
Tags: 2.0-1
* [3750b2cf] Merge commit 'upstream/2.0'
* [63637468] Removing Patch, not necessary anymore.
* [2fb580dc] Add new build-dependencies.
* [c57d953b] Run dh_autoreconf due to patches in configure.in
* [13febe39] Add patch to remove the libssl requirement.
* [5ae773fe] Replace libjpeg62-dev by libjpeg8-dev :)
* [1969d755] Don't build static libraries.
* [7cfe0a2e] Add a patch to fix the plugin directory path.
  As plugins are shared libraries, they need to go into /usr/lib,
  not into /usr/share.
  Thanks to Andrew McMillan
* [c1d0d9dd] Don't install .la files for all plugins and libraries.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include "StdAfx.h"
 
2
#include "LJpegDecompressor.h"
 
3
#include "ByteStreamSwap.h"
 
4
 
 
5
/*
 
6
    RawSpeed - RAW file decoder.
 
7
 
 
8
    Copyright (C) 2009 Klaus Post
 
9
 
 
10
    This library is free software; you can redistribute it and/or
 
11
    modify it under the terms of the GNU Lesser General Public
 
12
    License as published by the Free Software Foundation; either
 
13
    version 2 of the License, or (at your option) any later version.
 
14
 
 
15
    This library is distributed in the hope that it will be useful,
 
16
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
17
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
18
    Lesser General Public License for more details.
 
19
 
 
20
    You should have received a copy of the GNU Lesser General Public
 
21
    License along with this library; if not, write to the Free Software
 
22
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 
23
 
 
24
    http://www.klauspost.com
 
25
*/
 
26
 
 
27
/*
 
28
* Huffman table generation:
 
29
* LJpegDecompressor::HuffDecode,
 
30
* LJpegDecompressor::createHuffmanTable
 
31
* and used data structures are originally grabbed from the IJG software,
 
32
* and adapted by Hubert Figuiere.
 
33
*
 
34
* Copyright (C) 1991, 1992, Thomas G. Lane.
 
35
* Part of the Independent JPEG Group's software.
 
36
* See the file Copyright for more details.
 
37
*
 
38
* Copyright (c) 1993 Brian C. Smith, The Regents of the University
 
39
* of California
 
40
* All rights reserved.
 
41
*
 
42
* Copyright (c) 1994 Kongji Huang and Brian C. Smith.
 
43
* Cornell University
 
44
* All rights reserved.
 
45
*
 
46
* Permission to use, copy, modify, and distribute this software and its
 
47
* documentation for any purpose, without fee, and without written agreement is
 
48
* hereby granted, provided that the above copyright notice and the following
 
49
* two paragraphs appear in all copies of this software.
 
50
*
 
51
* IN NO EVENT SHALL CORNELL UNIVERSITY BE LIABLE TO ANY PARTY FOR
 
52
* DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 
53
* OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF CORNELL
 
54
* UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
55
*
 
56
* CORNELL UNIVERSITY SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 
57
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 
58
* AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 
59
* ON AN "AS IS" BASIS, AND CORNELL UNIVERSITY HAS NO OBLIGATION TO
 
60
* PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 
61
*/
 
62
 
 
63
namespace RawSpeed {
 
64
 
 
65
const uint32 bitMask[] = {  0xffffffff, 0x7fffffff,
 
66
                           0x3fffffff, 0x1fffffff,
 
67
                           0x0fffffff, 0x07ffffff,
 
68
                           0x03ffffff, 0x01ffffff,
 
69
                           0x00ffffff, 0x007fffff,
 
70
                           0x003fffff, 0x001fffff,
 
71
                           0x000fffff, 0x0007ffff,
 
72
                           0x0003ffff, 0x0001ffff,
 
73
                           0x0000ffff, 0x00007fff,
 
74
                           0x00003fff, 0x00001fff,
 
75
                           0x00000fff, 0x000007ff,
 
76
                           0x000003ff, 0x000001ff,
 
77
                           0x000000ff, 0x0000007f,
 
78
                           0x0000003f, 0x0000001f,
 
79
                           0x0000000f, 0x00000007,
 
80
                           0x00000003, 0x00000001
 
81
                        };
 
82
 
 
83
LJpegDecompressor::LJpegDecompressor(FileMap* file, RawImage img):
 
84
    mFile(file), mRaw(img) {
 
85
  input = 0;
 
86
  skipX = skipY = 0;
 
87
  for (int i = 0; i < 4; i++) {
 
88
    huff[i].initialized = false;
 
89
    huff[i].bigTable = 0;
 
90
  }
 
91
  mDNGCompatible = false;
 
92
  slicesW.clear();
 
93
  mUseBigtable = false;
 
94
}
 
95
 
 
96
LJpegDecompressor::~LJpegDecompressor(void) {
 
97
  if (input)
 
98
    delete input;
 
99
  input = 0;
 
100
  for (int i = 0; i < 4; i++) {
 
101
    if (huff[i].bigTable)
 
102
      _aligned_free(huff[i].bigTable);
 
103
  }
 
104
 
 
105
}
 
106
 
 
107
void LJpegDecompressor::getSOF(SOFInfo* sof, uint32 offset, uint32 size) {
 
108
  if (!mFile->isValid(offset + size - 1))
 
109
    ThrowRDE("LJpegDecompressor::getSOF: Start offset plus size is longer than file. Truncated file.");
 
110
  try {
 
111
    Endianness host_endian = getHostEndianness();
 
112
    // JPEG is big endian
 
113
    if (host_endian == big)
 
114
      input = new ByteStream(mFile->getData(offset), size);
 
115
    else 
 
116
      input = new ByteStreamSwap(mFile->getData(offset), size);
 
117
 
 
118
    if (getNextMarker(false) != M_SOI)
 
119
      ThrowRDE("LJpegDecompressor::getSOF: Image did not start with SOI. Probably not an LJPEG");
 
120
 
 
121
    while (true) {
 
122
      JpegMarker m = getNextMarker(true);
 
123
      if (M_SOF3 == m) {
 
124
        parseSOF(sof);
 
125
        return;
 
126
      }
 
127
      if (M_EOI == m) {
 
128
        ThrowRDE("LJpegDecompressor: Could not locate Start of Frame.");
 
129
        return;
 
130
      }
 
131
    }
 
132
  } catch (IOException) {
 
133
    ThrowRDE("LJpegDecompressor: IO exception, read outside file. Corrupt File.");
 
134
  }
 
135
}
 
136
 
 
137
void LJpegDecompressor::startDecoder(uint32 offset, uint32 size, uint32 offsetX, uint32 offsetY) {
 
138
  if (!mFile->isValid(offset + size - 1))
 
139
    ThrowRDE("LJpegDecompressor::startDecoder: Start offset plus size is longer than file. Truncated file.");
 
140
  if ((int)offsetX >= mRaw->dim.x)
 
141
    ThrowRDE("LJpegDecompressor::startDecoder: X offset outside of image");
 
142
  if ((int)offsetY >= mRaw->dim.y)
 
143
    ThrowRDE("LJpegDecompressor::startDecoder: Y offset outside of image");
 
144
  offX = offsetX;
 
145
  offY = offsetY;
 
146
 
 
147
  try {
 
148
    Endianness host_endian = getHostEndianness();
 
149
    // JPEG is big endian
 
150
    if (host_endian == big)
 
151
      input = new ByteStream(mFile->getData(offset), size);
 
152
    else 
 
153
      input = new ByteStreamSwap(mFile->getData(offset), size);
 
154
 
 
155
    if (getNextMarker(false) != M_SOI)
 
156
      ThrowRDE("LJpegDecompressor::startDecoder: Image did not start with SOI. Probably not an LJPEG");
 
157
//    _RPT0(0,"Found SOI marker\n");
 
158
 
 
159
    bool moreImage = true;
 
160
    while (moreImage) {
 
161
      JpegMarker m = getNextMarker(true);
 
162
 
 
163
      switch (m) {
 
164
        case M_SOS:
 
165
//          _RPT0(0,"Found SOS marker\n");
 
166
          parseSOS();
 
167
          break;
 
168
        case M_EOI:
 
169
//          _RPT0(0,"Found EOI marker\n");
 
170
          moreImage = false;
 
171
          break;
 
172
 
 
173
        case M_DHT:
 
174
//          _RPT0(0,"Found DHT marker\n");
 
175
          parseDHT();
 
176
          break;
 
177
 
 
178
        case M_DQT:
 
179
          ThrowRDE("LJpegDecompressor: Not a valid RAW file.");
 
180
          break;
 
181
 
 
182
        case M_DRI:
 
183
//          _RPT0(0,"Found DRI marker\n");
 
184
          break;
 
185
 
 
186
        case M_APP0:
 
187
//          _RPT0(0,"Found APP0 marker\n");
 
188
          break;
 
189
 
 
190
        case M_SOF3:
 
191
//          _RPT0(0,"Found SOF 3 marker:\n");
 
192
          parseSOF(&frame);
 
193
          break;
 
194
 
 
195
        default:  // Just let it skip to next marker
 
196
          _RPT1(0, "Found marker:0x%x. Skipping\n", (int)m);
 
197
          break;
 
198
      }
 
199
    }
 
200
 
 
201
  } catch (IOException) {
 
202
    throw;
 
203
  }
 
204
}
 
205
 
 
206
void LJpegDecompressor::parseSOF(SOFInfo* sof) {
 
207
  uint32 headerLength = input->getShort();
 
208
  sof->prec = input->getByte();
 
209
  sof->h = input->getShort();
 
210
  sof->w = input->getShort();
 
211
 
 
212
  sof->cps = input->getByte();
 
213
 
 
214
  if (sof->prec > 16)
 
215
    ThrowRDE("LJpegDecompressor: More than 16 bits per channel is not supported.");
 
216
 
 
217
  if (sof->cps > 4 || sof->cps < 2)
 
218
    ThrowRDE("LJpegDecompressor: Only from 2 to 4 components are supported.");
 
219
 
 
220
  if (headerLength != 8 + sof->cps*3)
 
221
    ThrowRDE("LJpegDecompressor: Header size mismatch.");
 
222
 
 
223
  for (uint32 i = 0; i < sof->cps; i++) {
 
224
    sof->compInfo[i].componentId = input->getByte();
 
225
    uint32 subs = input->getByte();
 
226
    frame.compInfo[i].superV = subs & 0xf;
 
227
    frame.compInfo[i].superH = subs >> 4;
 
228
    uint32 Tq = input->getByte();
 
229
    if (Tq != 0)
 
230
      ThrowRDE("LJpegDecompressor: Quantized components not supported.");
 
231
  }
 
232
  sof->initialized = true;
 
233
}
 
234
 
 
235
void LJpegDecompressor::parseSOS() {
 
236
  if (!frame.initialized)
 
237
    ThrowRDE("LJpegDecompressor::parseSOS: Frame not yet initialized (SOF Marker not parsed)");
 
238
 
 
239
  uint32 headerLength = input->getShort();
 
240
  uint32 soscps = input->getByte();
 
241
  if (frame.cps != soscps)
 
242
    ThrowRDE("LJpegDecompressor::parseSOS: Component number mismatch.");
 
243
 
 
244
  for (uint32 i = 0;i < frame.cps;i++) {
 
245
    uint32 cs = input->getByte();
 
246
 
 
247
    uint32 count = 0;  // Find the correct component
 
248
    while (frame.compInfo[count].componentId != cs) {
 
249
      if (count >= frame.cps)
 
250
        ThrowRDE("LJpegDecompressor::parseSOS: Invalid Component Selector");
 
251
      count++;
 
252
    }
 
253
 
 
254
    uint32 b = input->getByte();
 
255
    uint32 td = b >> 4;
 
256
    if (td > 3)
 
257
      ThrowRDE("LJpegDecompressor::parseSOS: Invalid Huffman table selection");
 
258
    if (!huff[td].initialized)
 
259
      ThrowRDE("LJpegDecompressor::parseSOS: Invalid Huffman table selection, not defined.");
 
260
 
 
261
    frame.compInfo[count].dcTblNo = td;
 
262
  }
 
263
 
 
264
  // Get predictor
 
265
  pred = input->getByte();
 
266
  if (pred > 7)
 
267
    ThrowRDE("LJpegDecompressor::parseSOS: Invalid predictor mode.");
 
268
 
 
269
  input->skipBytes(1);                    // Se + Ah Not used in LJPEG
 
270
  uint32 b = input->getByte();
 
271
  Pt = b & 0xf;        // Point Transform
 
272
 
 
273
  uint32 cheadersize = 3 + frame.cps * 2 + 3;
 
274
  _ASSERTE(cheadersize == headerLength);
 
275
 
 
276
  bits = new BitPumpJPEG(input);
 
277
  try {
 
278
    decodeScan();
 
279
  } catch (...) {
 
280
    delete bits;
 
281
    throw;
 
282
  }
 
283
  input->skipBytes(bits->getOffset());
 
284
  delete bits;
 
285
}
 
286
 
 
287
void LJpegDecompressor::parseDHT() {
 
288
  uint32 headerLength = input->getShort() - 2; // Subtract myself
 
289
 
 
290
  while (headerLength)  {
 
291
    uint32 b = input->getByte();
 
292
 
 
293
    uint32 Tc = (b >> 4);
 
294
    if (Tc != 0)
 
295
      ThrowRDE("LJpegDecompressor::parseDHT: Unsupported Table class.");
 
296
 
 
297
    uint32 Th = b & 0xf;
 
298
    if (Th > 3)
 
299
      ThrowRDE("LJpegDecompressor::parseDHT: Invalid huffman table destination id.");
 
300
 
 
301
    uint32 acc = 0;
 
302
    HuffmanTable* t = &huff[Th];
 
303
 
 
304
    if (t->initialized)
 
305
      ThrowRDE("LJpegDecompressor::parseDHT: Duplicate table definition");
 
306
 
 
307
    for (uint32 i = 0; i < 16 ;i++) {
 
308
      t->bits[i+1] = input->getByte();
 
309
      acc += t->bits[i+1];
 
310
    }
 
311
    t->bits[0] = 0;
 
312
    memset(t->huffval, 0, sizeof(t->huffval));
 
313
    if (acc > 256)
 
314
      ThrowRDE("LJpegDecompressor::parseDHT: Invalid DHT table.");
 
315
 
 
316
    if (headerLength < 1 + 16 + acc)
 
317
      ThrowRDE("LJpegDecompressor::parseDHT: Invalid DHT table length.");
 
318
 
 
319
    for (uint32 i = 0 ; i < acc; i++) {
 
320
      t->huffval[i] = input->getByte();
 
321
    }
 
322
    createHuffmanTable(t);
 
323
    headerLength -= 1 + 16 + acc;
 
324
  }
 
325
}
 
326
 
 
327
 
 
328
JpegMarker LJpegDecompressor::getNextMarker(bool allowskip) {
 
329
 
 
330
  if (!allowskip) {
 
331
    uchar8 id = input->getByte();
 
332
    if (id != 0xff)
 
333
      ThrowRDE("LJpegDecompressor::getNextMarker: (Noskip) Expected marker not found. Propably corrupt file.");
 
334
 
 
335
    JpegMarker mark = (JpegMarker)input->getByte();
 
336
 
 
337
    if (M_FILL == mark || M_STUFF == mark)
 
338
      ThrowRDE("LJpegDecompressor::getNextMarker: (Noskip) Expected marker, but found stuffed 00 or ff.");
 
339
 
 
340
    return mark;
 
341
  }
 
342
  input->skipToMarker();
 
343
  uchar8 id = input->getByte();
 
344
  _ASSERTE(0xff == id);
 
345
  JpegMarker mark = (JpegMarker)input->getByte();
 
346
  return mark;
 
347
}
 
348
 
 
349
void LJpegDecompressor::createHuffmanTable(HuffmanTable *htbl) {
 
350
  int p, i, l, lastp, si;
 
351
  char huffsize[257];
 
352
  ushort16 huffcode[257];
 
353
  ushort16 code;
 
354
  int size;
 
355
  int value, ll, ul;
 
356
 
 
357
  /*
 
358
  * Figure C.1: make table of Huffman code length for each symbol
 
359
  * Note that this is in code-length order.
 
360
  */
 
361
  p = 0;
 
362
  for (l = 1; l <= 16; l++) {
 
363
    for (i = 1; i <= (int)htbl->bits[l]; i++) {
 
364
      huffsize[p++] = (char)l;
 
365
      if (p > 256)
 
366
        ThrowRDE("LJpegDecompressor::createHuffmanTable: Code length too long. Corrupt data.");
 
367
    }
 
368
  }
 
369
  huffsize[p] = 0;
 
370
  lastp = p;
 
371
 
 
372
 
 
373
  /*
 
374
  * Figure C.2: generate the codes themselves
 
375
  * Note that this is in code-length order.
 
376
  */
 
377
  code = 0;
 
378
  si = huffsize[0];
 
379
  p = 0;
 
380
  while (huffsize[p]) {
 
381
    while (((int)huffsize[p]) == si) {
 
382
      huffcode[p++] = code;
 
383
      code++;
 
384
    }
 
385
    code <<= 1;
 
386
    si++;
 
387
    if (p > 256)
 
388
      ThrowRDE("createHuffmanTable: Code length too long. Corrupt data.");
 
389
  }
 
390
 
 
391
 
 
392
  /*
 
393
  * Figure F.15: generate decoding tables
 
394
  */
 
395
  htbl->mincode[0] = 0;
 
396
  htbl->maxcode[0] = 0;
 
397
  p = 0;
 
398
  for (l = 1; l <= 16; l++) {
 
399
    if (htbl->bits[l]) {
 
400
      htbl->valptr[l] = p;
 
401
      htbl->mincode[l] = huffcode[p];
 
402
      p += htbl->bits[l];
 
403
      htbl->maxcode[l] = huffcode[p - 1];
 
404
    } else {
 
405
      htbl->valptr[l] = 0xff;   // This check must be present to avoid crash on junk
 
406
      htbl->maxcode[l] = -1;
 
407
    }
 
408
    if (p > 256)
 
409
      ThrowRDE("createHuffmanTable: Code length too long. Corrupt data.");
 
410
  }
 
411
 
 
412
  /*
 
413
  * We put in this value to ensure HuffDecode terminates.
 
414
  */
 
415
  htbl->maxcode[17] = 0xFFFFFL;
 
416
 
 
417
  /*
 
418
  * Build the numbits, value lookup tables.
 
419
  * These table allow us to gather 8 bits from the bits stream,
 
420
  * and immediately lookup the size and value of the huffman codes.
 
421
  * If size is zero, it means that more than 8 bits are in the huffman
 
422
  * code (this happens about 3-4% of the time).
 
423
  */
 
424
  memset(htbl->numbits, 0, sizeof(htbl->numbits));
 
425
  for (p = 0; p < lastp; p++) {
 
426
    size = huffsize[p];
 
427
    if (size <= 8) {
 
428
      value = htbl->huffval[p];
 
429
      code = huffcode[p];
 
430
      ll = code << (8 - size);
 
431
      if (size < 8) {
 
432
        ul = ll | bitMask[24+size];
 
433
      } else {
 
434
        ul = ll;
 
435
      }
 
436
      if (ul > 256 || ll > ul)
 
437
        ThrowRDE("createHuffmanTable: Code length too long. Corrupt data.");
 
438
      for (i = ll; i <= ul; i++) {
 
439
        htbl->numbits[i] = size | (value << 4);
 
440
      }
 
441
    }
 
442
  }
 
443
  if (mUseBigtable)
 
444
    createBigTable(htbl);
 
445
  htbl->initialized = true;
 
446
}
 
447
 
 
448
/************************************
 
449
 * Bitable creation
 
450
 *
 
451
 * This is expanding the concept of fast lookups
 
452
 *
 
453
 * A complete table for 14 arbitrary bits will be
 
454
 * created that enables fast lookup of number of bits used,
 
455
 * and final delta result.
 
456
 * Hit rate is about 90-99% for typical LJPEGS, usually about 98%
 
457
 *
 
458
 ************************************/
 
459
 
 
460
void LJpegDecompressor::createBigTable(HuffmanTable *htbl) {
 
461
  const uint32 bits = 14;      // HuffDecode functions must be changed, if this is modified.
 
462
  const uint32 size = 1 << bits;
 
463
  int rv = 0;
 
464
  int temp;
 
465
  uint32 l;
 
466
 
 
467
  htbl->bigTable = (int*)_aligned_malloc(size * sizeof(int), 16);
 
468
  for (uint32 i = 0; i < size; i++) {
 
469
    ushort16 input = i << 2; // Calculate input value
 
470
    int code = input >> 8;   // Get 8 bits
 
471
    uint32 val = htbl->numbits[code];
 
472
    l = val & 15;
 
473
    if (l) {
 
474
      rv = val >> 4;
 
475
    }  else {
 
476
      l = 8;
 
477
      while (code > htbl->maxcode[l]) {
 
478
        temp = input >> (15 - l) & 1;
 
479
        code = (code << 1) | temp;
 
480
        l++;
 
481
      }
 
482
 
 
483
      /*
 
484
      * With garbage input we may reach the sentinel value l = 17.
 
485
      */
 
486
 
 
487
      if (l > frame.prec || htbl->valptr[l] == 0xff) {
 
488
        htbl->bigTable[i] = 0xff;
 
489
        continue;
 
490
      } else {
 
491
        rv = htbl->huffval[htbl->valptr[l] +
 
492
                           ((int)(code - htbl->mincode[l]))];
 
493
      }
 
494
    }
 
495
 
 
496
 
 
497
    if (rv == 16) {
 
498
      if (mDNGCompatible)
 
499
        htbl->bigTable[i] = (-32768 << 8) | (16 + l);
 
500
      else
 
501
        htbl->bigTable[i] = (-32768 << 8) | l;
 
502
      continue;
 
503
    }
 
504
 
 
505
    if (rv + l > bits) {
 
506
      htbl->bigTable[i] = 0xff;
 
507
      continue;
 
508
    }
 
509
 
 
510
    if (rv) {
 
511
      int x = input >> (16 - l - rv) & ((1 << rv) - 1);
 
512
      if ((x & (1 << (rv - 1))) == 0)
 
513
        x -= (1 << rv) - 1;
 
514
      htbl->bigTable[i] = (x << 8) | (l + rv);
 
515
    } else {
 
516
      htbl->bigTable[i] = l;
 
517
    }
 
518
  }
 
519
}
 
520
 
 
521
 
 
522
/*
 
523
*--------------------------------------------------------------
 
524
*
 
525
* HuffDecode --
 
526
*
 
527
* Taken from Figure F.16: extract next coded symbol from
 
528
* input stream.  This should becode a macro.
 
529
*
 
530
* Results:
 
531
* Next coded symbol
 
532
*
 
533
* Side effects:
 
534
* Bitstream is parsed.
 
535
*
 
536
*--------------------------------------------------------------
 
537
*/
 
538
int LJpegDecompressor::HuffDecode(HuffmanTable *htbl) {
 
539
  int rv;
 
540
  int temp;
 
541
  int code, val;
 
542
  uint32 l;
 
543
  /**
 
544
   * First attempt to do complete decode, by using the first 14 bits
 
545
   */
 
546
 
 
547
  bits->fill();
 
548
  code = bits->peekBitsNoFill(14);
 
549
  if (htbl->bigTable) {
 
550
    val = htbl->bigTable[code];
 
551
    if ((val&0xff) !=  0xff) {
 
552
      bits->skipBitsNoFill(val&0xff);
 
553
      return val >> 8;
 
554
    }
 
555
  }
 
556
  /*
 
557
  * If the huffman code is less than 8 bits, we can use the fast
 
558
  * table lookup to get its value.  It's more than 8 bits about
 
559
  * 3-4% of the time.
 
560
  */
 
561
  rv = 0;
 
562
  code = code>>6;
 
563
  val = htbl->numbits[code];
 
564
  l = val & 15;
 
565
  if (l) {
 
566
    bits->skipBitsNoFill(l);
 
567
    rv = val >> 4;
 
568
  }  else {
 
569
    bits->skipBitsNoFill(8);
 
570
    l = 8;
 
571
    while (code > htbl->maxcode[l]) {
 
572
      temp = bits->getBitNoFill();
 
573
      code = (code << 1) | temp;
 
574
      l++;
 
575
    }
 
576
 
 
577
    /*
 
578
    * With garbage input we may reach the sentinel value l = 17.
 
579
    */
 
580
 
 
581
    if (l > frame.prec || htbl->valptr[l] == 0xff) {
 
582
      ThrowIOE("Corrupt JPEG data: bad Huffman code:%u", l);
 
583
    } else {
 
584
      rv = htbl->huffval[htbl->valptr[l] +
 
585
                         ((int)(code - htbl->mincode[l]))];
 
586
    }
 
587
  }
 
588
 
 
589
  if (rv == 16) {
 
590
    if (mDNGCompatible)
 
591
      bits->skipBitsNoFill(16);
 
592
    return -32768;
 
593
  }
 
594
 
 
595
  // Ensure we have enough bits
 
596
  if ((rv + l) > 24) {
 
597
    if (rv > 16) // There is no values above 16 bits.
 
598
      ThrowIOE("Corrupt JPEG data: Too many bits requested.");
 
599
    else
 
600
      bits->fill();
 
601
  }
 
602
 
 
603
  /*
 
604
  * Section F.2.2.1: decode the difference and
 
605
  * Figure F.12: extend sign bit
 
606
  */
 
607
 
 
608
  if (rv) {
 
609
    int x = bits->getBitsNoFill(rv);
 
610
    if ((x & (1 << (rv - 1))) == 0)
 
611
      x -= (1 << rv) - 1;
 
612
    return x;
 
613
  }
 
614
  return 0;
 
615
}
 
616
 
 
617
} // namespace RawSpeed