1
// jpgd.cpp - C++ class for JPEG decompression.
2
// Public domain, Rich Geldreich <richgel99@gmail.com>
3
// Alex Evans: Linear memory allocator (taken from jpge.h).
4
// v1.04, May. 19, 2012: Code tweaks to fix VS2008 static code analysis warnings (all looked harmless)
6
// Supports progressive and baseline sequential JPEG image files, and the most common chroma subsampling factors: Y, H1V1, H2V1, H1V2, and H2V2.
8
// Chroma upsampling quality: H2V2 is upsampled in the frequency domain, H2V1 and H1V2 are upsampled using point sampling.
9
// Chroma upsampling reference: "Fast Scheme for Image Size Change in the Compressed Domain"
10
// http://vision.ai.uiuc.edu/~dugad/research/dct/index.html
16
#define JPGD_ASSERT(x) assert(x)
19
#pragma warning (disable : 4611) // warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable
22
// Set to 1 to enable freq. domain chroma upsampling on images using H2V2 subsampling (0=faster nearest neighbor sampling).
23
// This is slower, but results in higher quality on images with highly saturated colors.
24
#define JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING 1
27
#define JPGD_FALSE (0)
29
#define JPGD_MAX(a,b) (((a)>(b)) ? (a) : (b))
30
#define JPGD_MIN(a,b) (((a)<(b)) ? (a) : (b))
34
static inline void *jpgd_malloc(size_t nSize) { return malloc(nSize); }
35
static inline void jpgd_free(void *p) { free(p); }
37
// DCT coefficients are stored in this sequence.
38
static int g_ZAG[64] = { 0,1,8,16,9,2,3,10,17,24,32,25,18,11,4,5,12,19,26,33,40,48,41,34,27,20,13,6,7,14,21,28,35,42,49,56,57,50,43,36,29,22,15,23,30,37,44,51,58,59,52,45,38,31,39,46,53,60,61,54,47,55,62,63 };
42
M_SOF0 = 0xC0, M_SOF1 = 0xC1, M_SOF2 = 0xC2, M_SOF3 = 0xC3, M_SOF5 = 0xC5, M_SOF6 = 0xC6, M_SOF7 = 0xC7, M_JPG = 0xC8,
43
M_SOF9 = 0xC9, M_SOF10 = 0xCA, M_SOF11 = 0xCB, M_SOF13 = 0xCD, M_SOF14 = 0xCE, M_SOF15 = 0xCF, M_DHT = 0xC4, M_DAC = 0xCC,
44
M_RST0 = 0xD0, M_RST1 = 0xD1, M_RST2 = 0xD2, M_RST3 = 0xD3, M_RST4 = 0xD4, M_RST5 = 0xD5, M_RST6 = 0xD6, M_RST7 = 0xD7,
45
M_SOI = 0xD8, M_EOI = 0xD9, M_SOS = 0xDA, M_DQT = 0xDB, M_DNL = 0xDC, M_DRI = 0xDD, M_DHP = 0xDE, M_EXP = 0xDF,
46
M_APP0 = 0xE0, M_APP15 = 0xEF, M_JPG0 = 0xF0, M_JPG13 = 0xFD, M_COM = 0xFE, M_TEM = 0x01, M_ERROR = 0x100, RST0 = 0xD0
49
enum JPEG_SUBSAMPLING { JPGD_GRAYSCALE = 0, JPGD_YH1V1, JPGD_YH2V1, JPGD_YH1V2, JPGD_YH2V2 };
53
#define SCALEDONE ((int32)1)
55
#define FIX_0_298631336 ((int32)2446) /* FIX(0.298631336) */
56
#define FIX_0_390180644 ((int32)3196) /* FIX(0.390180644) */
57
#define FIX_0_541196100 ((int32)4433) /* FIX(0.541196100) */
58
#define FIX_0_765366865 ((int32)6270) /* FIX(0.765366865) */
59
#define FIX_0_899976223 ((int32)7373) /* FIX(0.899976223) */
60
#define FIX_1_175875602 ((int32)9633) /* FIX(1.175875602) */
61
#define FIX_1_501321110 ((int32)12299) /* FIX(1.501321110) */
62
#define FIX_1_847759065 ((int32)15137) /* FIX(1.847759065) */
63
#define FIX_1_961570560 ((int32)16069) /* FIX(1.961570560) */
64
#define FIX_2_053119869 ((int32)16819) /* FIX(2.053119869) */
65
#define FIX_2_562915447 ((int32)20995) /* FIX(2.562915447) */
66
#define FIX_3_072711026 ((int32)25172) /* FIX(3.072711026) */
68
#define DESCALE(x,n) (((x) + (SCALEDONE << ((n)-1))) >> (n))
69
#define DESCALE_ZEROSHIFT(x,n) (((x) + (128 << (n)) + (SCALEDONE << ((n)-1))) >> (n))
71
#define MULTIPLY(var, cnst) ((var) * (cnst))
73
#define CLAMP(i) ((static_cast<uint>(i) > 255) ? (((~i) >> 31) & 0xFF) : (i))
75
// Compiler creates a fast path 1D IDCT for X non-zero columns
76
template <int NONZERO_COLS>
79
static void idct(int* pTemp, const jpgd_block_t* pSrc)
81
// ACCESS_COL() will be optimized at compile time to either an array access, or 0.
82
#define ACCESS_COL(x) (((x) < NONZERO_COLS) ? (int)pSrc[x] : 0)
84
const int z2 = ACCESS_COL(2), z3 = ACCESS_COL(6);
86
const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
87
const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
88
const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
90
const int tmp0 = (ACCESS_COL(0) + ACCESS_COL(4)) << CONST_BITS;
91
const int tmp1 = (ACCESS_COL(0) - ACCESS_COL(4)) << CONST_BITS;
93
const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
95
const int atmp0 = ACCESS_COL(7), atmp1 = ACCESS_COL(5), atmp2 = ACCESS_COL(3), atmp3 = ACCESS_COL(1);
97
const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
98
const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
100
const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
101
const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
102
const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
103
const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
105
const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
106
const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
107
const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
108
const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
110
pTemp[0] = DESCALE(tmp10 + btmp3, CONST_BITS-PASS1_BITS);
111
pTemp[7] = DESCALE(tmp10 - btmp3, CONST_BITS-PASS1_BITS);
112
pTemp[1] = DESCALE(tmp11 + btmp2, CONST_BITS-PASS1_BITS);
113
pTemp[6] = DESCALE(tmp11 - btmp2, CONST_BITS-PASS1_BITS);
114
pTemp[2] = DESCALE(tmp12 + btmp1, CONST_BITS-PASS1_BITS);
115
pTemp[5] = DESCALE(tmp12 - btmp1, CONST_BITS-PASS1_BITS);
116
pTemp[3] = DESCALE(tmp13 + btmp0, CONST_BITS-PASS1_BITS);
117
pTemp[4] = DESCALE(tmp13 - btmp0, CONST_BITS-PASS1_BITS);
124
static void idct(int* pTemp, const jpgd_block_t* pSrc)
135
static void idct(int* pTemp, const jpgd_block_t* pSrc)
137
const int dcval = (pSrc[0] << PASS1_BITS);
150
// Compiler creates a fast path 1D IDCT for X non-zero rows
151
template <int NONZERO_ROWS>
154
static void idct(uint8* pDst_ptr, const int* pTemp)
156
// ACCESS_ROW() will be optimized at compile time to either an array access, or 0.
157
#define ACCESS_ROW(x) (((x) < NONZERO_ROWS) ? pTemp[x * 8] : 0)
159
const int z2 = ACCESS_ROW(2);
160
const int z3 = ACCESS_ROW(6);
162
const int z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
163
const int tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
164
const int tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
166
const int tmp0 = (ACCESS_ROW(0) + ACCESS_ROW(4)) << CONST_BITS;
167
const int tmp1 = (ACCESS_ROW(0) - ACCESS_ROW(4)) << CONST_BITS;
169
const int tmp10 = tmp0 + tmp3, tmp13 = tmp0 - tmp3, tmp11 = tmp1 + tmp2, tmp12 = tmp1 - tmp2;
171
const int atmp0 = ACCESS_ROW(7), atmp1 = ACCESS_ROW(5), atmp2 = ACCESS_ROW(3), atmp3 = ACCESS_ROW(1);
173
const int bz1 = atmp0 + atmp3, bz2 = atmp1 + atmp2, bz3 = atmp0 + atmp2, bz4 = atmp1 + atmp3;
174
const int bz5 = MULTIPLY(bz3 + bz4, FIX_1_175875602);
176
const int az1 = MULTIPLY(bz1, - FIX_0_899976223);
177
const int az2 = MULTIPLY(bz2, - FIX_2_562915447);
178
const int az3 = MULTIPLY(bz3, - FIX_1_961570560) + bz5;
179
const int az4 = MULTIPLY(bz4, - FIX_0_390180644) + bz5;
181
const int btmp0 = MULTIPLY(atmp0, FIX_0_298631336) + az1 + az3;
182
const int btmp1 = MULTIPLY(atmp1, FIX_2_053119869) + az2 + az4;
183
const int btmp2 = MULTIPLY(atmp2, FIX_3_072711026) + az2 + az3;
184
const int btmp3 = MULTIPLY(atmp3, FIX_1_501321110) + az1 + az4;
186
int i = DESCALE_ZEROSHIFT(tmp10 + btmp3, CONST_BITS+PASS1_BITS+3);
187
pDst_ptr[8*0] = (uint8)CLAMP(i);
189
i = DESCALE_ZEROSHIFT(tmp10 - btmp3, CONST_BITS+PASS1_BITS+3);
190
pDst_ptr[8*7] = (uint8)CLAMP(i);
192
i = DESCALE_ZEROSHIFT(tmp11 + btmp2, CONST_BITS+PASS1_BITS+3);
193
pDst_ptr[8*1] = (uint8)CLAMP(i);
195
i = DESCALE_ZEROSHIFT(tmp11 - btmp2, CONST_BITS+PASS1_BITS+3);
196
pDst_ptr[8*6] = (uint8)CLAMP(i);
198
i = DESCALE_ZEROSHIFT(tmp12 + btmp1, CONST_BITS+PASS1_BITS+3);
199
pDst_ptr[8*2] = (uint8)CLAMP(i);
201
i = DESCALE_ZEROSHIFT(tmp12 - btmp1, CONST_BITS+PASS1_BITS+3);
202
pDst_ptr[8*5] = (uint8)CLAMP(i);
204
i = DESCALE_ZEROSHIFT(tmp13 + btmp0, CONST_BITS+PASS1_BITS+3);
205
pDst_ptr[8*3] = (uint8)CLAMP(i);
207
i = DESCALE_ZEROSHIFT(tmp13 - btmp0, CONST_BITS+PASS1_BITS+3);
208
pDst_ptr[8*4] = (uint8)CLAMP(i);
215
static void idct(uint8* pDst_ptr, const int* pTemp)
217
int dcval = DESCALE_ZEROSHIFT(pTemp[0], PASS1_BITS+3);
218
const uint8 dcval_clamped = (uint8)CLAMP(dcval);
219
pDst_ptr[0*8] = dcval_clamped;
220
pDst_ptr[1*8] = dcval_clamped;
221
pDst_ptr[2*8] = dcval_clamped;
222
pDst_ptr[3*8] = dcval_clamped;
223
pDst_ptr[4*8] = dcval_clamped;
224
pDst_ptr[5*8] = dcval_clamped;
225
pDst_ptr[6*8] = dcval_clamped;
226
pDst_ptr[7*8] = dcval_clamped;
230
static const uint8 s_idct_row_table[] =
232
1,0,0,0,0,0,0,0, 2,0,0,0,0,0,0,0, 2,1,0,0,0,0,0,0, 2,1,1,0,0,0,0,0, 2,2,1,0,0,0,0,0, 3,2,1,0,0,0,0,0, 4,2,1,0,0,0,0,0, 4,3,1,0,0,0,0,0,
233
4,3,2,0,0,0,0,0, 4,3,2,1,0,0,0,0, 4,3,2,1,1,0,0,0, 4,3,2,2,1,0,0,0, 4,3,3,2,1,0,0,0, 4,4,3,2,1,0,0,0, 5,4,3,2,1,0,0,0, 6,4,3,2,1,0,0,0,
234
6,5,3,2,1,0,0,0, 6,5,4,2,1,0,0,0, 6,5,4,3,1,0,0,0, 6,5,4,3,2,0,0,0, 6,5,4,3,2,1,0,0, 6,5,4,3,2,1,1,0, 6,5,4,3,2,2,1,0, 6,5,4,3,3,2,1,0,
235
6,5,4,4,3,2,1,0, 6,5,5,4,3,2,1,0, 6,6,5,4,3,2,1,0, 7,6,5,4,3,2,1,0, 8,6,5,4,3,2,1,0, 8,7,5,4,3,2,1,0, 8,7,6,4,3,2,1,0, 8,7,6,5,3,2,1,0,
236
8,7,6,5,4,2,1,0, 8,7,6,5,4,3,1,0, 8,7,6,5,4,3,2,0, 8,7,6,5,4,3,2,1, 8,7,6,5,4,3,2,2, 8,7,6,5,4,3,3,2, 8,7,6,5,4,4,3,2, 8,7,6,5,5,4,3,2,
237
8,7,6,6,5,4,3,2, 8,7,7,6,5,4,3,2, 8,8,7,6,5,4,3,2, 8,8,8,6,5,4,3,2, 8,8,8,7,5,4,3,2, 8,8,8,7,6,4,3,2, 8,8,8,7,6,5,3,2, 8,8,8,7,6,5,4,2,
238
8,8,8,7,6,5,4,3, 8,8,8,7,6,5,4,4, 8,8,8,7,6,5,5,4, 8,8,8,7,6,6,5,4, 8,8,8,7,7,6,5,4, 8,8,8,8,7,6,5,4, 8,8,8,8,8,6,5,4, 8,8,8,8,8,7,5,4,
239
8,8,8,8,8,7,6,4, 8,8,8,8,8,7,6,5, 8,8,8,8,8,7,6,6, 8,8,8,8,8,7,7,6, 8,8,8,8,8,8,7,6, 8,8,8,8,8,8,8,6, 8,8,8,8,8,8,8,7, 8,8,8,8,8,8,8,8,
242
static const uint8 s_idct_col_table[] = { 1, 1, 2, 3, 3, 3, 3, 3, 3, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8 };
244
void idct(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr, int block_max_zag)
246
JPGD_ASSERT(block_max_zag >= 1);
247
JPGD_ASSERT(block_max_zag <= 64);
249
if (block_max_zag <= 1)
251
int k = ((pSrc_ptr[0] + 4) >> 3) + 128;
256
for (int i = 8; i > 0; i--)
258
*(int*)&pDst_ptr[0] = k;
259
*(int*)&pDst_ptr[4] = k;
267
const jpgd_block_t* pSrc = pSrc_ptr;
270
const uint8* pRow_tab = &s_idct_row_table[(block_max_zag - 1) * 8];
272
for (i = 8; i > 0; i--, pRow_tab++)
276
case 0: Row<0>::idct(pTemp, pSrc); break;
277
case 1: Row<1>::idct(pTemp, pSrc); break;
278
case 2: Row<2>::idct(pTemp, pSrc); break;
279
case 3: Row<3>::idct(pTemp, pSrc); break;
280
case 4: Row<4>::idct(pTemp, pSrc); break;
281
case 5: Row<5>::idct(pTemp, pSrc); break;
282
case 6: Row<6>::idct(pTemp, pSrc); break;
283
case 7: Row<7>::idct(pTemp, pSrc); break;
284
case 8: Row<8>::idct(pTemp, pSrc); break;
293
const int nonzero_rows = s_idct_col_table[block_max_zag - 1];
294
for (i = 8; i > 0; i--)
296
switch (nonzero_rows)
298
case 1: Col<1>::idct(pDst_ptr, pTemp); break;
299
case 2: Col<2>::idct(pDst_ptr, pTemp); break;
300
case 3: Col<3>::idct(pDst_ptr, pTemp); break;
301
case 4: Col<4>::idct(pDst_ptr, pTemp); break;
302
case 5: Col<5>::idct(pDst_ptr, pTemp); break;
303
case 6: Col<6>::idct(pDst_ptr, pTemp); break;
304
case 7: Col<7>::idct(pDst_ptr, pTemp); break;
305
case 8: Col<8>::idct(pDst_ptr, pTemp); break;
313
void idct_4x4(const jpgd_block_t* pSrc_ptr, uint8* pDst_ptr)
317
const jpgd_block_t* pSrc = pSrc_ptr;
319
for (int i = 4; i > 0; i--)
321
Row<4>::idct(pTemp, pSrc);
327
for (int i = 8; i > 0; i--)
329
Col<4>::idct(pDst_ptr, pTemp);
335
// Retrieve one character from the input stream.
336
inline uint jpeg_decoder::get_char()
338
// Any bytes remaining in buffer?
341
// Try to get more bytes.
343
// Still nothing to get?
346
// Pad the end of the stream with 0xFF 0xD9 (EOI marker)
356
uint c = *m_pIn_buf_ofs++;
362
// Same as previous method, except can indicate if the character is a pad character or not.
363
inline uint jpeg_decoder::get_char(bool *pPadding_flag)
370
*pPadding_flag = true;
380
*pPadding_flag = false;
382
uint c = *m_pIn_buf_ofs++;
388
// Inserts a previously retrieved character back into the input buffer.
389
inline void jpeg_decoder::stuff_char(uint8 q)
391
*(--m_pIn_buf_ofs) = q;
395
// Retrieves one character from the input stream, but does not read past markers. Will continue to return 0xFF when a marker is encountered.
396
inline uint8 jpeg_decoder::get_octet()
399
int c = get_char(&padding_flag);
406
c = get_char(&padding_flag);
417
stuff_char(static_cast<uint8>(c));
423
return static_cast<uint8>(c);
426
// Retrieves a variable number of bits from the input stream. Does not recognize markers.
427
inline uint jpeg_decoder::get_bits(int num_bits)
432
uint i = m_bit_buf >> (32 - num_bits);
434
if ((m_bits_left -= num_bits) <= 0)
436
m_bit_buf <<= (num_bits += m_bits_left);
438
uint c1 = get_char();
439
uint c2 = get_char();
440
m_bit_buf = (m_bit_buf & 0xFFFF0000) | (c1 << 8) | c2;
442
m_bit_buf <<= -m_bits_left;
446
JPGD_ASSERT(m_bits_left >= 0);
449
m_bit_buf <<= num_bits;
454
// Retrieves a variable number of bits from the input stream. Markers will not be read into the input bit buffer. Instead, an infinite number of all 1's will be returned when a marker is encountered.
455
inline uint jpeg_decoder::get_bits_no_markers(int num_bits)
460
uint i = m_bit_buf >> (32 - num_bits);
462
if ((m_bits_left -= num_bits) <= 0)
464
m_bit_buf <<= (num_bits += m_bits_left);
466
if ((m_in_buf_left < 2) || (m_pIn_buf_ofs[0] == 0xFF) || (m_pIn_buf_ofs[1] == 0xFF))
468
uint c1 = get_octet();
469
uint c2 = get_octet();
470
m_bit_buf |= (c1 << 8) | c2;
474
m_bit_buf |= ((uint)m_pIn_buf_ofs[0] << 8) | m_pIn_buf_ofs[1];
479
m_bit_buf <<= -m_bits_left;
483
JPGD_ASSERT(m_bits_left >= 0);
486
m_bit_buf <<= num_bits;
491
// Decodes a Huffman encoded symbol.
492
inline int jpeg_decoder::huff_decode(huff_tables *pH)
496
// Check first 8-bits: do we have a complete symbol?
497
if ((symbol = pH->look_up[m_bit_buf >> 24]) < 0)
499
// Decode more bits, use a tree traversal to find symbol.
503
symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
505
} while (symbol < 0);
507
get_bits_no_markers(8 + (23 - ofs));
510
get_bits_no_markers(pH->code_size[symbol]);
515
// Decodes a Huffman encoded symbol.
516
inline int jpeg_decoder::huff_decode(huff_tables *pH, int& extra_bits)
520
// Check first 8-bits: do we have a complete symbol?
521
if ((symbol = pH->look_up2[m_bit_buf >> 24]) < 0)
523
// Use a tree traversal to find symbol.
527
symbol = pH->tree[-(int)(symbol + ((m_bit_buf >> ofs) & 1))];
529
} while (symbol < 0);
531
get_bits_no_markers(8 + (23 - ofs));
533
extra_bits = get_bits_no_markers(symbol & 0xF);
537
JPGD_ASSERT(((symbol >> 8) & 31) == pH->code_size[symbol & 255] + ((symbol & 0x8000) ? (symbol & 15) : 0));
541
get_bits_no_markers((symbol >> 8) & 31);
542
extra_bits = symbol >> 16;
546
int code_size = (symbol >> 8) & 31;
547
int num_extra_bits = symbol & 0xF;
548
int bits = code_size + num_extra_bits;
549
if (bits <= (m_bits_left + 16))
550
extra_bits = get_bits_no_markers(bits) & ((1 << num_extra_bits) - 1);
553
get_bits_no_markers(code_size);
554
extra_bits = get_bits_no_markers(num_extra_bits);
564
// Tables and macro used to fully decode the DPCM differences.
565
static const int s_extend_test[16] = { 0, 0x0001, 0x0002, 0x0004, 0x0008, 0x0010, 0x0020, 0x0040, 0x0080, 0x0100, 0x0200, 0x0400, 0x0800, 0x1000, 0x2000, 0x4000 };
566
static const int s_extend_offset[16] = { 0, ((-1)<<1) + 1, ((-1)<<2) + 1, ((-1)<<3) + 1, ((-1)<<4) + 1, ((-1)<<5) + 1, ((-1)<<6) + 1, ((-1)<<7) + 1, ((-1)<<8) + 1, ((-1)<<9) + 1, ((-1)<<10) + 1, ((-1)<<11) + 1, ((-1)<<12) + 1, ((-1)<<13) + 1, ((-1)<<14) + 1, ((-1)<<15) + 1 };
567
//static const int s_extend_mask[] = { 0, (1<<0), (1<<1), (1<<2), (1<<3), (1<<4), (1<<5), (1<<6), (1<<7), (1<<8), (1<<9), (1<<10), (1<<11), (1<<12), (1<<13), (1<<14), (1<<15), (1<<16) };
568
// The logical AND's in this macro are to shut up static code analysis (aren't really necessary - couldn't find another way to do this)
569
#define JPGD_HUFF_EXTEND(x, s) (((x) < s_extend_test[s & 15]) ? ((x) + s_extend_offset[s & 15]) : (x))
571
// Clamps a value between 0-255.
572
inline uint8 jpeg_decoder::clamp(int i)
574
if (static_cast<uint>(i) > 255)
575
i = (((~i) >> 31) & 0xFF);
577
return static_cast<uint8>(i);
580
namespace DCT_Upsample
584
typedef int Element_Type;
585
enum { NUM_ROWS = 4, NUM_COLS = 4 };
587
Element_Type v[NUM_ROWS][NUM_COLS];
589
inline int rows() const { return NUM_ROWS; }
590
inline int cols() const { return NUM_COLS; }
592
inline const Element_Type & at(int r, int c) const { return v[r][c]; }
593
inline Element_Type & at(int r, int c) { return v[r][c]; }
595
inline Matrix44() { }
597
inline Matrix44& operator += (const Matrix44& a)
599
for (int r = 0; r < NUM_ROWS; r++)
601
at(r, 0) += a.at(r, 0);
602
at(r, 1) += a.at(r, 1);
603
at(r, 2) += a.at(r, 2);
604
at(r, 3) += a.at(r, 3);
609
inline Matrix44& operator -= (const Matrix44& a)
611
for (int r = 0; r < NUM_ROWS; r++)
613
at(r, 0) -= a.at(r, 0);
614
at(r, 1) -= a.at(r, 1);
615
at(r, 2) -= a.at(r, 2);
616
at(r, 3) -= a.at(r, 3);
621
friend inline Matrix44 operator + (const Matrix44& a, const Matrix44& b)
624
for (int r = 0; r < NUM_ROWS; r++)
626
ret.at(r, 0) = a.at(r, 0) + b.at(r, 0);
627
ret.at(r, 1) = a.at(r, 1) + b.at(r, 1);
628
ret.at(r, 2) = a.at(r, 2) + b.at(r, 2);
629
ret.at(r, 3) = a.at(r, 3) + b.at(r, 3);
634
friend inline Matrix44 operator - (const Matrix44& a, const Matrix44& b)
637
for (int r = 0; r < NUM_ROWS; r++)
639
ret.at(r, 0) = a.at(r, 0) - b.at(r, 0);
640
ret.at(r, 1) = a.at(r, 1) - b.at(r, 1);
641
ret.at(r, 2) = a.at(r, 2) - b.at(r, 2);
642
ret.at(r, 3) = a.at(r, 3) - b.at(r, 3);
647
static inline void add_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
649
for (int r = 0; r < 4; r++)
651
pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) + b.at(r, 0));
652
pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) + b.at(r, 1));
653
pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) + b.at(r, 2));
654
pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) + b.at(r, 3));
658
static inline void sub_and_store(jpgd_block_t* pDst, const Matrix44& a, const Matrix44& b)
660
for (int r = 0; r < 4; r++)
662
pDst[0*8 + r] = static_cast<jpgd_block_t>(a.at(r, 0) - b.at(r, 0));
663
pDst[1*8 + r] = static_cast<jpgd_block_t>(a.at(r, 1) - b.at(r, 1));
664
pDst[2*8 + r] = static_cast<jpgd_block_t>(a.at(r, 2) - b.at(r, 2));
665
pDst[3*8 + r] = static_cast<jpgd_block_t>(a.at(r, 3) - b.at(r, 3));
670
const int FRACT_BITS = 10;
671
const int SCALE = 1 << FRACT_BITS;
673
typedef int Temp_Type;
674
#define D(i) (((i) + (SCALE >> 1)) >> FRACT_BITS)
675
#define F(i) ((int)((i) * SCALE + .5f))
677
// Any decent C++ compiler will optimize this at compile time to a 0, or an array access.
678
#define AT(c, r) ((((c)>=NUM_COLS)||((r)>=NUM_ROWS)) ? 0 : pSrc[(c)+(r)*8])
680
// NUM_ROWS/NUM_COLS = # of non-zero rows/cols in input matrix
681
template<int NUM_ROWS, int NUM_COLS>
684
static void calc(Matrix44& P, Matrix44& Q, const jpgd_block_t* pSrc)
686
// 4x8 = 4x8 times 8x8, matrix 0 is constant
687
const Temp_Type X000 = AT(0, 0);
688
const Temp_Type X001 = AT(0, 1);
689
const Temp_Type X002 = AT(0, 2);
690
const Temp_Type X003 = AT(0, 3);
691
const Temp_Type X004 = AT(0, 4);
692
const Temp_Type X005 = AT(0, 5);
693
const Temp_Type X006 = AT(0, 6);
694
const Temp_Type X007 = AT(0, 7);
695
const Temp_Type X010 = D(F(0.415735f) * AT(1, 0) + F(0.791065f) * AT(3, 0) + F(-0.352443f) * AT(5, 0) + F(0.277785f) * AT(7, 0));
696
const Temp_Type X011 = D(F(0.415735f) * AT(1, 1) + F(0.791065f) * AT(3, 1) + F(-0.352443f) * AT(5, 1) + F(0.277785f) * AT(7, 1));
697
const Temp_Type X012 = D(F(0.415735f) * AT(1, 2) + F(0.791065f) * AT(3, 2) + F(-0.352443f) * AT(5, 2) + F(0.277785f) * AT(7, 2));
698
const Temp_Type X013 = D(F(0.415735f) * AT(1, 3) + F(0.791065f) * AT(3, 3) + F(-0.352443f) * AT(5, 3) + F(0.277785f) * AT(7, 3));
699
const Temp_Type X014 = D(F(0.415735f) * AT(1, 4) + F(0.791065f) * AT(3, 4) + F(-0.352443f) * AT(5, 4) + F(0.277785f) * AT(7, 4));
700
const Temp_Type X015 = D(F(0.415735f) * AT(1, 5) + F(0.791065f) * AT(3, 5) + F(-0.352443f) * AT(5, 5) + F(0.277785f) * AT(7, 5));
701
const Temp_Type X016 = D(F(0.415735f) * AT(1, 6) + F(0.791065f) * AT(3, 6) + F(-0.352443f) * AT(5, 6) + F(0.277785f) * AT(7, 6));
702
const Temp_Type X017 = D(F(0.415735f) * AT(1, 7) + F(0.791065f) * AT(3, 7) + F(-0.352443f) * AT(5, 7) + F(0.277785f) * AT(7, 7));
703
const Temp_Type X020 = AT(4, 0);
704
const Temp_Type X021 = AT(4, 1);
705
const Temp_Type X022 = AT(4, 2);
706
const Temp_Type X023 = AT(4, 3);
707
const Temp_Type X024 = AT(4, 4);
708
const Temp_Type X025 = AT(4, 5);
709
const Temp_Type X026 = AT(4, 6);
710
const Temp_Type X027 = AT(4, 7);
711
const Temp_Type X030 = D(F(0.022887f) * AT(1, 0) + F(-0.097545f) * AT(3, 0) + F(0.490393f) * AT(5, 0) + F(0.865723f) * AT(7, 0));
712
const Temp_Type X031 = D(F(0.022887f) * AT(1, 1) + F(-0.097545f) * AT(3, 1) + F(0.490393f) * AT(5, 1) + F(0.865723f) * AT(7, 1));
713
const Temp_Type X032 = D(F(0.022887f) * AT(1, 2) + F(-0.097545f) * AT(3, 2) + F(0.490393f) * AT(5, 2) + F(0.865723f) * AT(7, 2));
714
const Temp_Type X033 = D(F(0.022887f) * AT(1, 3) + F(-0.097545f) * AT(3, 3) + F(0.490393f) * AT(5, 3) + F(0.865723f) * AT(7, 3));
715
const Temp_Type X034 = D(F(0.022887f) * AT(1, 4) + F(-0.097545f) * AT(3, 4) + F(0.490393f) * AT(5, 4) + F(0.865723f) * AT(7, 4));
716
const Temp_Type X035 = D(F(0.022887f) * AT(1, 5) + F(-0.097545f) * AT(3, 5) + F(0.490393f) * AT(5, 5) + F(0.865723f) * AT(7, 5));
717
const Temp_Type X036 = D(F(0.022887f) * AT(1, 6) + F(-0.097545f) * AT(3, 6) + F(0.490393f) * AT(5, 6) + F(0.865723f) * AT(7, 6));
718
const Temp_Type X037 = D(F(0.022887f) * AT(1, 7) + F(-0.097545f) * AT(3, 7) + F(0.490393f) * AT(5, 7) + F(0.865723f) * AT(7, 7));
720
// 4x4 = 4x8 times 8x4, matrix 1 is constant
722
P.at(0, 1) = D(X001 * F(0.415735f) + X003 * F(0.791065f) + X005 * F(-0.352443f) + X007 * F(0.277785f));
724
P.at(0, 3) = D(X001 * F(0.022887f) + X003 * F(-0.097545f) + X005 * F(0.490393f) + X007 * F(0.865723f));
726
P.at(1, 1) = D(X011 * F(0.415735f) + X013 * F(0.791065f) + X015 * F(-0.352443f) + X017 * F(0.277785f));
728
P.at(1, 3) = D(X011 * F(0.022887f) + X013 * F(-0.097545f) + X015 * F(0.490393f) + X017 * F(0.865723f));
730
P.at(2, 1) = D(X021 * F(0.415735f) + X023 * F(0.791065f) + X025 * F(-0.352443f) + X027 * F(0.277785f));
732
P.at(2, 3) = D(X021 * F(0.022887f) + X023 * F(-0.097545f) + X025 * F(0.490393f) + X027 * F(0.865723f));
734
P.at(3, 1) = D(X031 * F(0.415735f) + X033 * F(0.791065f) + X035 * F(-0.352443f) + X037 * F(0.277785f));
736
P.at(3, 3) = D(X031 * F(0.022887f) + X033 * F(-0.097545f) + X035 * F(0.490393f) + X037 * F(0.865723f));
739
// 4x4 = 4x8 times 8x4, matrix 1 is constant
740
Q.at(0, 0) = D(X001 * F(0.906127f) + X003 * F(-0.318190f) + X005 * F(0.212608f) + X007 * F(-0.180240f));
742
Q.at(0, 2) = D(X001 * F(-0.074658f) + X003 * F(0.513280f) + X005 * F(0.768178f) + X007 * F(-0.375330f));
744
Q.at(1, 0) = D(X011 * F(0.906127f) + X013 * F(-0.318190f) + X015 * F(0.212608f) + X017 * F(-0.180240f));
746
Q.at(1, 2) = D(X011 * F(-0.074658f) + X013 * F(0.513280f) + X015 * F(0.768178f) + X017 * F(-0.375330f));
748
Q.at(2, 0) = D(X021 * F(0.906127f) + X023 * F(-0.318190f) + X025 * F(0.212608f) + X027 * F(-0.180240f));
750
Q.at(2, 2) = D(X021 * F(-0.074658f) + X023 * F(0.513280f) + X025 * F(0.768178f) + X027 * F(-0.375330f));
752
Q.at(3, 0) = D(X031 * F(0.906127f) + X033 * F(-0.318190f) + X035 * F(0.212608f) + X037 * F(-0.180240f));
754
Q.at(3, 2) = D(X031 * F(-0.074658f) + X033 * F(0.513280f) + X035 * F(0.768178f) + X037 * F(-0.375330f));
760
template<int NUM_ROWS, int NUM_COLS>
763
static void calc(Matrix44& R, Matrix44& S, const jpgd_block_t* pSrc)
765
// 4x8 = 4x8 times 8x8, matrix 0 is constant
766
const Temp_Type X100 = D(F(0.906127f) * AT(1, 0) + F(-0.318190f) * AT(3, 0) + F(0.212608f) * AT(5, 0) + F(-0.180240f) * AT(7, 0));
767
const Temp_Type X101 = D(F(0.906127f) * AT(1, 1) + F(-0.318190f) * AT(3, 1) + F(0.212608f) * AT(5, 1) + F(-0.180240f) * AT(7, 1));
768
const Temp_Type X102 = D(F(0.906127f) * AT(1, 2) + F(-0.318190f) * AT(3, 2) + F(0.212608f) * AT(5, 2) + F(-0.180240f) * AT(7, 2));
769
const Temp_Type X103 = D(F(0.906127f) * AT(1, 3) + F(-0.318190f) * AT(3, 3) + F(0.212608f) * AT(5, 3) + F(-0.180240f) * AT(7, 3));
770
const Temp_Type X104 = D(F(0.906127f) * AT(1, 4) + F(-0.318190f) * AT(3, 4) + F(0.212608f) * AT(5, 4) + F(-0.180240f) * AT(7, 4));
771
const Temp_Type X105 = D(F(0.906127f) * AT(1, 5) + F(-0.318190f) * AT(3, 5) + F(0.212608f) * AT(5, 5) + F(-0.180240f) * AT(7, 5));
772
const Temp_Type X106 = D(F(0.906127f) * AT(1, 6) + F(-0.318190f) * AT(3, 6) + F(0.212608f) * AT(5, 6) + F(-0.180240f) * AT(7, 6));
773
const Temp_Type X107 = D(F(0.906127f) * AT(1, 7) + F(-0.318190f) * AT(3, 7) + F(0.212608f) * AT(5, 7) + F(-0.180240f) * AT(7, 7));
774
const Temp_Type X110 = AT(2, 0);
775
const Temp_Type X111 = AT(2, 1);
776
const Temp_Type X112 = AT(2, 2);
777
const Temp_Type X113 = AT(2, 3);
778
const Temp_Type X114 = AT(2, 4);
779
const Temp_Type X115 = AT(2, 5);
780
const Temp_Type X116 = AT(2, 6);
781
const Temp_Type X117 = AT(2, 7);
782
const Temp_Type X120 = D(F(-0.074658f) * AT(1, 0) + F(0.513280f) * AT(3, 0) + F(0.768178f) * AT(5, 0) + F(-0.375330f) * AT(7, 0));
783
const Temp_Type X121 = D(F(-0.074658f) * AT(1, 1) + F(0.513280f) * AT(3, 1) + F(0.768178f) * AT(5, 1) + F(-0.375330f) * AT(7, 1));
784
const Temp_Type X122 = D(F(-0.074658f) * AT(1, 2) + F(0.513280f) * AT(3, 2) + F(0.768178f) * AT(5, 2) + F(-0.375330f) * AT(7, 2));
785
const Temp_Type X123 = D(F(-0.074658f) * AT(1, 3) + F(0.513280f) * AT(3, 3) + F(0.768178f) * AT(5, 3) + F(-0.375330f) * AT(7, 3));
786
const Temp_Type X124 = D(F(-0.074658f) * AT(1, 4) + F(0.513280f) * AT(3, 4) + F(0.768178f) * AT(5, 4) + F(-0.375330f) * AT(7, 4));
787
const Temp_Type X125 = D(F(-0.074658f) * AT(1, 5) + F(0.513280f) * AT(3, 5) + F(0.768178f) * AT(5, 5) + F(-0.375330f) * AT(7, 5));
788
const Temp_Type X126 = D(F(-0.074658f) * AT(1, 6) + F(0.513280f) * AT(3, 6) + F(0.768178f) * AT(5, 6) + F(-0.375330f) * AT(7, 6));
789
const Temp_Type X127 = D(F(-0.074658f) * AT(1, 7) + F(0.513280f) * AT(3, 7) + F(0.768178f) * AT(5, 7) + F(-0.375330f) * AT(7, 7));
790
const Temp_Type X130 = AT(6, 0);
791
const Temp_Type X131 = AT(6, 1);
792
const Temp_Type X132 = AT(6, 2);
793
const Temp_Type X133 = AT(6, 3);
794
const Temp_Type X134 = AT(6, 4);
795
const Temp_Type X135 = AT(6, 5);
796
const Temp_Type X136 = AT(6, 6);
797
const Temp_Type X137 = AT(6, 7);
800
// 4x4 = 4x8 times 8x4, matrix 1 is constant
802
R.at(0, 1) = D(X101 * F(0.415735f) + X103 * F(0.791065f) + X105 * F(-0.352443f) + X107 * F(0.277785f));
804
R.at(0, 3) = D(X101 * F(0.022887f) + X103 * F(-0.097545f) + X105 * F(0.490393f) + X107 * F(0.865723f));
806
R.at(1, 1) = D(X111 * F(0.415735f) + X113 * F(0.791065f) + X115 * F(-0.352443f) + X117 * F(0.277785f));
808
R.at(1, 3) = D(X111 * F(0.022887f) + X113 * F(-0.097545f) + X115 * F(0.490393f) + X117 * F(0.865723f));
810
R.at(2, 1) = D(X121 * F(0.415735f) + X123 * F(0.791065f) + X125 * F(-0.352443f) + X127 * F(0.277785f));
812
R.at(2, 3) = D(X121 * F(0.022887f) + X123 * F(-0.097545f) + X125 * F(0.490393f) + X127 * F(0.865723f));
814
R.at(3, 1) = D(X131 * F(0.415735f) + X133 * F(0.791065f) + X135 * F(-0.352443f) + X137 * F(0.277785f));
816
R.at(3, 3) = D(X131 * F(0.022887f) + X133 * F(-0.097545f) + X135 * F(0.490393f) + X137 * F(0.865723f));
818
// 4x4 = 4x8 times 8x4, matrix 1 is constant
819
S.at(0, 0) = D(X101 * F(0.906127f) + X103 * F(-0.318190f) + X105 * F(0.212608f) + X107 * F(-0.180240f));
821
S.at(0, 2) = D(X101 * F(-0.074658f) + X103 * F(0.513280f) + X105 * F(0.768178f) + X107 * F(-0.375330f));
823
S.at(1, 0) = D(X111 * F(0.906127f) + X113 * F(-0.318190f) + X115 * F(0.212608f) + X117 * F(-0.180240f));
825
S.at(1, 2) = D(X111 * F(-0.074658f) + X113 * F(0.513280f) + X115 * F(0.768178f) + X117 * F(-0.375330f));
827
S.at(2, 0) = D(X121 * F(0.906127f) + X123 * F(-0.318190f) + X125 * F(0.212608f) + X127 * F(-0.180240f));
829
S.at(2, 2) = D(X121 * F(-0.074658f) + X123 * F(0.513280f) + X125 * F(0.768178f) + X127 * F(-0.375330f));
831
S.at(3, 0) = D(X131 * F(0.906127f) + X133 * F(-0.318190f) + X135 * F(0.212608f) + X137 * F(-0.180240f));
833
S.at(3, 2) = D(X131 * F(-0.074658f) + X133 * F(0.513280f) + X135 * F(0.768178f) + X137 * F(-0.375330f));
838
} // end namespace DCT_Upsample
840
// Unconditionally frees all allocated m_blocks.
841
void jpeg_decoder::free_all_blocks()
844
for (mem_block *b = m_pMem_blocks; b; )
846
mem_block *n = b->m_pNext;
850
m_pMem_blocks = NULL;
853
// This method handles all errors. It will never return.
854
// It could easily be changed to use C++ exceptions.
855
JPGD_NORETURN void jpeg_decoder::stop_decoding(jpgd_status status)
857
m_error_code = status;
859
longjmp(m_jmp_state, status);
862
void *jpeg_decoder::alloc(size_t nSize, bool zero)
864
nSize = (JPGD_MAX(nSize, 1) + 3) & ~3;
866
for (mem_block *b = m_pMem_blocks; b; b = b->m_pNext)
868
if ((b->m_used_count + nSize) <= b->m_size)
870
rv = b->m_data + b->m_used_count;
871
b->m_used_count += nSize;
877
int capacity = (int)JPGD_MAX(32768 - 256, (nSize + 2047) & ~2047);
878
mem_block *b = (mem_block*)jpgd_malloc(sizeof(mem_block) + capacity);
879
if (!b) { stop_decoding(JPGD_NOTENOUGHMEM); }
880
b->m_pNext = m_pMem_blocks; m_pMem_blocks = b;
881
b->m_used_count = nSize;
882
b->m_size = capacity;
885
if (zero) memset(rv, 0, nSize);
889
void jpeg_decoder::word_clear(void *p, uint16 c, uint n)
891
uint8 *pD = (uint8*)p;
892
const uint8 l = c & 0xFF, h = (c >> 8) & 0xFF;
895
pD[0] = l; pD[1] = h; pD += 2;
900
// Refill the input buffer.
901
// This method will sit in a loop until (A) the buffer is full or (B)
902
// the stream's read() method reports and end of file condition.
903
void jpeg_decoder::prep_in_buffer()
906
m_pIn_buf_ofs = m_in_buf;
913
int bytes_read = m_pStream->read(m_in_buf + m_in_buf_left, JPGD_IN_BUF_SIZE - m_in_buf_left, &m_eof_flag);
914
if (bytes_read == -1)
915
stop_decoding(JPGD_STREAM_READ);
917
m_in_buf_left += bytes_read;
918
} while ((m_in_buf_left < JPGD_IN_BUF_SIZE) && (!m_eof_flag));
920
m_total_bytes_read += m_in_buf_left;
922
// Pad the end of the block with M_EOI (prevents the decompressor from going off the rails if the stream is invalid).
923
// (This dates way back to when this decompressor was written in C/asm, and the all-asm Huffman decoder did some fancy things to increase perf.)
924
word_clear(m_pIn_buf_ofs + m_in_buf_left, 0xD9FF, 64);
927
// Read a Huffman code table.
928
void jpeg_decoder::read_dht_marker()
934
uint num_left = get_bits(16);
937
stop_decoding(JPGD_BAD_DHT_MARKER);
949
for (i = 1; i <= 16; i++)
951
huff_num[i] = static_cast<uint8>(get_bits(8));
952
count += huff_num[i];
956
stop_decoding(JPGD_BAD_DHT_COUNTS);
958
for (i = 0; i < count; i++)
959
huff_val[i] = static_cast<uint8>(get_bits(8));
963
if (num_left < (uint)i)
964
stop_decoding(JPGD_BAD_DHT_MARKER);
968
if ((index & 0x10) > 0x10)
969
stop_decoding(JPGD_BAD_DHT_INDEX);
971
index = (index & 0x0F) + ((index & 0x10) >> 4) * (JPGD_MAX_HUFF_TABLES >> 1);
973
if (index >= JPGD_MAX_HUFF_TABLES)
974
stop_decoding(JPGD_BAD_DHT_INDEX);
976
if (!m_huff_num[index])
977
m_huff_num[index] = (uint8 *)alloc(17);
979
if (!m_huff_val[index])
980
m_huff_val[index] = (uint8 *)alloc(256);
982
m_huff_ac[index] = (index & 0x10) != 0;
983
memcpy(m_huff_num[index], huff_num, 17);
984
memcpy(m_huff_val[index], huff_val, 256);
988
// Read a quantization table.
989
void jpeg_decoder::read_dqt_marker()
995
num_left = get_bits(16);
998
stop_decoding(JPGD_BAD_DQT_MARKER);
1008
if (n >= JPGD_MAX_QUANT_TABLES)
1009
stop_decoding(JPGD_BAD_DQT_TABLE);
1012
m_quant[n] = (jpgd_quant_t *)alloc(64 * sizeof(jpgd_quant_t));
1014
// read quantization entries, in zag order
1015
for (i = 0; i < 64; i++)
1020
temp = (temp << 8) + get_bits(8);
1022
m_quant[n][i] = static_cast<jpgd_quant_t>(temp);
1030
if (num_left < (uint)i)
1031
stop_decoding(JPGD_BAD_DQT_LENGTH);
1037
// Read the start of frame (SOF) marker.
1038
void jpeg_decoder::read_sof_marker()
1043
num_left = get_bits(16);
1045
if (get_bits(8) != 8) /* precision: sorry, only 8-bit precision is supported right now */
1046
stop_decoding(JPGD_BAD_PRECISION);
1048
m_image_y_size = get_bits(16);
1050
if ((m_image_y_size < 1) || (m_image_y_size > JPGD_MAX_HEIGHT))
1051
stop_decoding(JPGD_BAD_HEIGHT);
1053
m_image_x_size = get_bits(16);
1055
if ((m_image_x_size < 1) || (m_image_x_size > JPGD_MAX_WIDTH))
1056
stop_decoding(JPGD_BAD_WIDTH);
1058
m_comps_in_frame = get_bits(8);
1060
if (m_comps_in_frame > JPGD_MAX_COMPONENTS)
1061
stop_decoding(JPGD_TOO_MANY_COMPONENTS);
1063
if (num_left != (uint)(m_comps_in_frame * 3 + 8))
1064
stop_decoding(JPGD_BAD_SOF_LENGTH);
1066
for (i = 0; i < m_comps_in_frame; i++)
1068
m_comp_ident[i] = get_bits(8);
1069
m_comp_h_samp[i] = get_bits(4);
1070
m_comp_v_samp[i] = get_bits(4);
1071
m_comp_quant[i] = get_bits(8);
1075
// Used to skip unrecognized markers.
1076
void jpeg_decoder::skip_variable_marker()
1080
num_left = get_bits(16);
1083
stop_decoding(JPGD_BAD_VARIABLE_MARKER);
1094
// Read a define restart interval (DRI) marker.
1095
void jpeg_decoder::read_dri_marker()
1097
if (get_bits(16) != 4)
1098
stop_decoding(JPGD_BAD_DRI_LENGTH);
1100
m_restart_interval = get_bits(16);
1103
// Read a start of scan (SOS) marker.
1104
void jpeg_decoder::read_sos_marker()
1107
int i, ci, n, c, cc;
1109
num_left = get_bits(16);
1113
m_comps_in_scan = n;
1117
if ( (num_left != (uint)(n * 2 + 3)) || (n < 1) || (n > JPGD_MAX_COMPS_IN_SCAN) )
1118
stop_decoding(JPGD_BAD_SOS_LENGTH);
1120
for (i = 0; i < n; i++)
1126
for (ci = 0; ci < m_comps_in_frame; ci++)
1127
if (cc == m_comp_ident[ci])
1130
if (ci >= m_comps_in_frame)
1131
stop_decoding(JPGD_BAD_SOS_COMP_ID);
1133
m_comp_list[i] = ci;
1134
m_comp_dc_tab[ci] = (c >> 4) & 15;
1135
m_comp_ac_tab[ci] = (c & 15) + (JPGD_MAX_HUFF_TABLES >> 1);
1138
m_spectral_start = get_bits(8);
1139
m_spectral_end = get_bits(8);
1140
m_successive_high = get_bits(4);
1141
m_successive_low = get_bits(4);
1143
if (!m_progressive_flag)
1145
m_spectral_start = 0;
1146
m_spectral_end = 63;
1151
while (num_left) /* read past whatever is num_left */
1158
// Finds the next marker.
1159
int jpeg_decoder::next_marker()
1171
} while (c != 0xFF);
1176
} while (c == 0xFF);
1180
// If bytes > 0 here, there where extra bytes before the marker (not good).
1185
// Process markers. Returns when an SOFx, SOI, EOI, or SOS marker is
1187
int jpeg_decoder::process_markers()
1222
// No arithmitic support - dumb patents!
1225
stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
1238
//case M_APP0: /* no need to read the JFIF marker */
1241
case M_RST0: /* no parameters */
1251
stop_decoding(JPGD_UNEXPECTED_MARKER);
1254
default: /* must be DNL, DHP, EXP, APPn, JPGn, COM, or RESn or APP0 */
1256
skip_variable_marker();
1263
// Finds the start of image (SOI) marker.
1264
// This code is rather defensive: it only checks the first 512 bytes to avoid
1266
void jpeg_decoder::locate_soi_marker()
1268
uint lastchar, thischar;
1271
lastchar = get_bits(8);
1273
thischar = get_bits(8);
1275
/* ok if it's a normal JPEG file without a special header */
1277
if ((lastchar == 0xFF) && (thischar == M_SOI))
1280
bytesleft = 4096; //512;
1284
if (--bytesleft == 0)
1285
stop_decoding(JPGD_NOT_JPEG);
1287
lastchar = thischar;
1289
thischar = get_bits(8);
1291
if (lastchar == 0xFF)
1293
if (thischar == M_SOI)
1295
else if (thischar == M_EOI) // get_bits will keep returning M_EOI if we read past the end
1296
stop_decoding(JPGD_NOT_JPEG);
1300
// Check the next character after marker: if it's not 0xFF, it can't be the start of the next marker, so the file is bad.
1301
thischar = (m_bit_buf >> 24) & 0xFF;
1303
if (thischar != 0xFF)
1304
stop_decoding(JPGD_NOT_JPEG);
1307
// Find a start of frame (SOF) marker.
1308
void jpeg_decoder::locate_sof_marker()
1310
locate_soi_marker();
1312
int c = process_markers();
1317
m_progressive_flag = JPGD_TRUE;
1318
case M_SOF0: /* baseline DCT */
1319
case M_SOF1: /* extended sequential DCT */
1324
case M_SOF9: /* Arithmitic coding */
1326
stop_decoding(JPGD_NO_ARITHMITIC_SUPPORT);
1331
stop_decoding(JPGD_UNSUPPORTED_MARKER);
1337
// Find a start of scan (SOS) marker.
1338
int jpeg_decoder::locate_sos_marker()
1342
c = process_markers();
1346
else if (c != M_SOS)
1347
stop_decoding(JPGD_UNEXPECTED_MARKER);
1354
// Reset everything to default/uninitialized state.
1355
void jpeg_decoder::init(jpeg_decoder_stream *pStream)
1357
m_pMem_blocks = NULL;
1358
m_error_code = JPGD_SUCCESS;
1359
m_ready_flag = false;
1360
m_image_x_size = m_image_y_size = 0;
1361
m_pStream = pStream;
1362
m_progressive_flag = JPGD_FALSE;
1364
memset(m_huff_ac, 0, sizeof(m_huff_ac));
1365
memset(m_huff_num, 0, sizeof(m_huff_num));
1366
memset(m_huff_val, 0, sizeof(m_huff_val));
1367
memset(m_quant, 0, sizeof(m_quant));
1370
m_comps_in_frame = 0;
1372
memset(m_comp_h_samp, 0, sizeof(m_comp_h_samp));
1373
memset(m_comp_v_samp, 0, sizeof(m_comp_v_samp));
1374
memset(m_comp_quant, 0, sizeof(m_comp_quant));
1375
memset(m_comp_ident, 0, sizeof(m_comp_ident));
1376
memset(m_comp_h_blocks, 0, sizeof(m_comp_h_blocks));
1377
memset(m_comp_v_blocks, 0, sizeof(m_comp_v_blocks));
1379
m_comps_in_scan = 0;
1380
memset(m_comp_list, 0, sizeof(m_comp_list));
1381
memset(m_comp_dc_tab, 0, sizeof(m_comp_dc_tab));
1382
memset(m_comp_ac_tab, 0, sizeof(m_comp_ac_tab));
1384
m_spectral_start = 0;
1386
m_successive_low = 0;
1387
m_successive_high = 0;
1388
m_max_mcu_x_size = 0;
1389
m_max_mcu_y_size = 0;
1390
m_blocks_per_mcu = 0;
1391
m_max_blocks_per_row = 0;
1394
m_expanded_blocks_per_component = 0;
1395
m_expanded_blocks_per_mcu = 0;
1396
m_expanded_blocks_per_row = 0;
1397
m_freq_domain_chroma_upsample = false;
1399
memset(m_mcu_org, 0, sizeof(m_mcu_org));
1401
m_total_lines_left = 0;
1402
m_mcu_lines_left = 0;
1403
m_real_dest_bytes_per_scan_line = 0;
1404
m_dest_bytes_per_scan_line = 0;
1405
m_dest_bytes_per_pixel = 0;
1407
memset(m_pHuff_tabs, 0, sizeof(m_pHuff_tabs));
1409
memset(m_dc_coeffs, 0, sizeof(m_dc_coeffs));
1410
memset(m_ac_coeffs, 0, sizeof(m_ac_coeffs));
1411
memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
1415
memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
1417
m_pIn_buf_ofs = m_in_buf;
1422
memset(m_in_buf_pad_start, 0, sizeof(m_in_buf_pad_start));
1423
memset(m_in_buf, 0, sizeof(m_in_buf));
1424
memset(m_in_buf_pad_end, 0, sizeof(m_in_buf_pad_end));
1426
m_restart_interval = 0;
1427
m_restarts_left = 0;
1428
m_next_restart_num = 0;
1430
m_max_mcus_per_row = 0;
1431
m_max_blocks_per_mcu = 0;
1432
m_max_mcus_per_col = 0;
1434
memset(m_last_dc_val, 0, sizeof(m_last_dc_val));
1435
m_pMCU_coefficients = NULL;
1436
m_pSample_buf = NULL;
1438
m_total_bytes_read = 0;
1440
m_pScan_line_0 = NULL;
1441
m_pScan_line_1 = NULL;
1443
// Ready the input buffer.
1446
// Prime the bit buffer.
1453
for (int i = 0; i < JPGD_MAX_BLOCKS_PER_MCU; i++)
1454
m_mcu_block_max_zag[i] = 64;
1457
#define SCALEBITS 16
1458
#define ONE_HALF ((int) 1 << (SCALEBITS-1))
1459
#define FIX(x) ((int) ((x) * (1L<<SCALEBITS) + 0.5f))
1461
// Create a few tables that allow us to quickly convert YCbCr to RGB.
1462
void jpeg_decoder::create_look_ups()
1464
for (int i = 0; i <= 255; i++)
1467
m_crr[i] = ( FIX(1.40200f) * k + ONE_HALF) >> SCALEBITS;
1468
m_cbb[i] = ( FIX(1.77200f) * k + ONE_HALF) >> SCALEBITS;
1469
m_crg[i] = (-FIX(0.71414f)) * k;
1470
m_cbg[i] = (-FIX(0.34414f)) * k + ONE_HALF;
1474
// This method throws back into the stream any bytes that where read
1475
// into the bit buffer during initial marker scanning.
1476
void jpeg_decoder::fix_in_buffer()
1478
// In case any 0xFF's where pulled into the buffer during marker scanning.
1479
JPGD_ASSERT((m_bits_left & 7) == 0);
1481
if (m_bits_left == 16)
1482
stuff_char( (uint8)(m_bit_buf & 0xFF));
1484
if (m_bits_left >= 8)
1485
stuff_char( (uint8)((m_bit_buf >> 8) & 0xFF));
1487
stuff_char((uint8)((m_bit_buf >> 16) & 0xFF));
1488
stuff_char((uint8)((m_bit_buf >> 24) & 0xFF));
1491
get_bits_no_markers(16);
1492
get_bits_no_markers(16);
1495
void jpeg_decoder::transform_mcu(int mcu_row)
1497
jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
1498
uint8* pDst_ptr = m_pSample_buf + mcu_row * m_blocks_per_mcu * 64;
1500
for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
1502
idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
1508
static const uint8 s_max_rc[64] =
1510
17, 18, 34, 50, 50, 51, 52, 52, 52, 68, 84, 84, 84, 84, 85, 86, 86, 86, 86, 86,
1511
102, 118, 118, 118, 118, 118, 118, 119, 120, 120, 120, 120, 120, 120, 120, 136,
1512
136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136,
1513
136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136, 136
1516
void jpeg_decoder::transform_mcu_expand(int mcu_row)
1518
jpgd_block_t* pSrc_ptr = m_pMCU_coefficients;
1519
uint8* pDst_ptr = m_pSample_buf + mcu_row * m_expanded_blocks_per_mcu * 64;
1523
for (mcu_block = 0; mcu_block < m_expanded_blocks_per_component; mcu_block++)
1525
idct(pSrc_ptr, pDst_ptr, m_mcu_block_max_zag[mcu_block]);
1530
// Chroma IDCT, with upsampling
1531
jpgd_block_t temp_block[64];
1533
for (int i = 0; i < 2; i++)
1535
DCT_Upsample::Matrix44 P, Q, R, S;
1537
JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] >= 1);
1538
JPGD_ASSERT(m_mcu_block_max_zag[mcu_block] <= 64);
1540
int max_zag = m_mcu_block_max_zag[mcu_block++] - 1;
1541
if (max_zag <= 0) max_zag = 0; // should never happen, only here to shut up static analysis
1542
switch (s_max_rc[max_zag])
1545
DCT_Upsample::P_Q<1, 1>::calc(P, Q, pSrc_ptr);
1546
DCT_Upsample::R_S<1, 1>::calc(R, S, pSrc_ptr);
1549
DCT_Upsample::P_Q<1, 2>::calc(P, Q, pSrc_ptr);
1550
DCT_Upsample::R_S<1, 2>::calc(R, S, pSrc_ptr);
1553
DCT_Upsample::P_Q<2, 2>::calc(P, Q, pSrc_ptr);
1554
DCT_Upsample::R_S<2, 2>::calc(R, S, pSrc_ptr);
1557
DCT_Upsample::P_Q<3, 2>::calc(P, Q, pSrc_ptr);
1558
DCT_Upsample::R_S<3, 2>::calc(R, S, pSrc_ptr);
1561
DCT_Upsample::P_Q<3, 3>::calc(P, Q, pSrc_ptr);
1562
DCT_Upsample::R_S<3, 3>::calc(R, S, pSrc_ptr);
1565
DCT_Upsample::P_Q<3, 4>::calc(P, Q, pSrc_ptr);
1566
DCT_Upsample::R_S<3, 4>::calc(R, S, pSrc_ptr);
1569
DCT_Upsample::P_Q<4, 4>::calc(P, Q, pSrc_ptr);
1570
DCT_Upsample::R_S<4, 4>::calc(R, S, pSrc_ptr);
1573
DCT_Upsample::P_Q<5, 4>::calc(P, Q, pSrc_ptr);
1574
DCT_Upsample::R_S<5, 4>::calc(R, S, pSrc_ptr);
1577
DCT_Upsample::P_Q<5, 5>::calc(P, Q, pSrc_ptr);
1578
DCT_Upsample::R_S<5, 5>::calc(R, S, pSrc_ptr);
1581
DCT_Upsample::P_Q<5, 6>::calc(P, Q, pSrc_ptr);
1582
DCT_Upsample::R_S<5, 6>::calc(R, S, pSrc_ptr);
1585
DCT_Upsample::P_Q<6, 6>::calc(P, Q, pSrc_ptr);
1586
DCT_Upsample::R_S<6, 6>::calc(R, S, pSrc_ptr);
1589
DCT_Upsample::P_Q<7, 6>::calc(P, Q, pSrc_ptr);
1590
DCT_Upsample::R_S<7, 6>::calc(R, S, pSrc_ptr);
1593
DCT_Upsample::P_Q<7, 7>::calc(P, Q, pSrc_ptr);
1594
DCT_Upsample::R_S<7, 7>::calc(R, S, pSrc_ptr);
1597
DCT_Upsample::P_Q<7, 8>::calc(P, Q, pSrc_ptr);
1598
DCT_Upsample::R_S<7, 8>::calc(R, S, pSrc_ptr);
1601
DCT_Upsample::P_Q<8, 8>::calc(P, Q, pSrc_ptr);
1602
DCT_Upsample::R_S<8, 8>::calc(R, S, pSrc_ptr);
1608
DCT_Upsample::Matrix44 a(P + Q); P -= Q;
1609
DCT_Upsample::Matrix44& b = P;
1610
DCT_Upsample::Matrix44 c(R + S); R -= S;
1611
DCT_Upsample::Matrix44& d = R;
1613
DCT_Upsample::Matrix44::add_and_store(temp_block, a, c);
1614
idct_4x4(temp_block, pDst_ptr);
1617
DCT_Upsample::Matrix44::sub_and_store(temp_block, a, c);
1618
idct_4x4(temp_block, pDst_ptr);
1621
DCT_Upsample::Matrix44::add_and_store(temp_block, b, d);
1622
idct_4x4(temp_block, pDst_ptr);
1625
DCT_Upsample::Matrix44::sub_and_store(temp_block, b, d);
1626
idct_4x4(temp_block, pDst_ptr);
1633
// Loads and dequantizes the next row of (already decoded) coefficients.
1634
// Progressive images only.
1635
void jpeg_decoder::load_next_row()
1640
int mcu_row, mcu_block, row_block = 0;
1641
int component_num, component_id;
1642
int block_x_mcu[JPGD_MAX_COMPONENTS];
1644
memset(block_x_mcu, 0, JPGD_MAX_COMPONENTS * sizeof(int));
1646
for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
1648
int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
1650
for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
1652
component_id = m_mcu_org[mcu_block];
1653
q = m_quant[m_comp_quant[component_id]];
1655
p = m_pMCU_coefficients + 64 * mcu_block;
1657
jpgd_block_t* pAC = coeff_buf_getp(m_ac_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
1658
jpgd_block_t* pDC = coeff_buf_getp(m_dc_coeffs[component_id], block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
1660
memcpy(&p[1], &pAC[1], 63 * sizeof(jpgd_block_t));
1662
for (i = 63; i > 0; i--)
1666
m_mcu_block_max_zag[mcu_block] = i + 1;
1668
for ( ; i >= 0; i--)
1670
p[g_ZAG[i]] = static_cast<jpgd_block_t>(p[g_ZAG[i]] * q[i]);
1674
if (m_comps_in_scan == 1)
1675
block_x_mcu[component_id]++;
1678
if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
1680
block_x_mcu_ofs = 0;
1682
if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
1684
block_y_mcu_ofs = 0;
1686
block_x_mcu[component_id] += m_comp_h_samp[component_id];
1692
if (m_freq_domain_chroma_upsample)
1693
transform_mcu_expand(mcu_row);
1695
transform_mcu(mcu_row);
1698
if (m_comps_in_scan == 1)
1699
m_block_y_mcu[m_comp_list[0]]++;
1702
for (component_num = 0; component_num < m_comps_in_scan; component_num++)
1704
component_id = m_comp_list[component_num];
1706
m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
1711
// Restart interval processing.
1712
void jpeg_decoder::process_restart()
1717
// Align to a byte boundry
1718
// FIXME: Is this really necessary? get_bits_no_markers() never reads in markers!
1719
//get_bits_no_markers(m_bits_left & 7);
1721
// Let's scan a little bit to find the marker, but not _too_ far.
1722
// 1536 is a "fudge factor" that determines how much to scan.
1723
for (i = 1536; i > 0; i--)
1724
if (get_char() == 0xFF)
1728
stop_decoding(JPGD_BAD_RESTART_MARKER);
1731
if ((c = get_char()) != 0xFF)
1735
stop_decoding(JPGD_BAD_RESTART_MARKER);
1737
// Is it the expected marker? If not, something bad happened.
1738
if (c != (m_next_restart_num + M_RST0))
1739
stop_decoding(JPGD_BAD_RESTART_MARKER);
1741
// Reset each component's DC prediction values.
1742
memset(&m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
1746
m_restarts_left = m_restart_interval;
1748
m_next_restart_num = (m_next_restart_num + 1) & 7;
1750
// Get the bit buffer going again...
1753
get_bits_no_markers(16);
1754
get_bits_no_markers(16);
1757
static inline int dequantize_ac(int c, int q) { c *= q; return c; }
1759
// Decodes and dequantizes the next row of coefficients.
1760
void jpeg_decoder::decode_next_row()
1764
for (int mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
1766
if ((m_restart_interval) && (m_restarts_left == 0))
1769
jpgd_block_t* p = m_pMCU_coefficients;
1770
for (int mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++, p += 64)
1772
int component_id = m_mcu_org[mcu_block];
1773
jpgd_quant_t* q = m_quant[m_comp_quant[component_id]];
1776
s = huff_decode(m_pHuff_tabs[m_comp_dc_tab[component_id]], r);
1777
s = JPGD_HUFF_EXTEND(r, s);
1779
m_last_dc_val[component_id] = (s += m_last_dc_val[component_id]);
1781
p[0] = static_cast<jpgd_block_t>(s * q[0]);
1783
int prev_num_set = m_mcu_block_max_zag[mcu_block];
1785
huff_tables *pH = m_pHuff_tabs[m_comp_ac_tab[component_id]];
1788
for (k = 1; k < 64; k++)
1791
s = huff_decode(pH, extra_bits);
1801
stop_decoding(JPGD_DECODE_ERROR);
1803
if (k < prev_num_set)
1805
int n = JPGD_MIN(r, prev_num_set - k);
1814
s = JPGD_HUFF_EXTEND(extra_bits, s);
1816
JPGD_ASSERT(k < 64);
1818
p[g_ZAG[k]] = static_cast<jpgd_block_t>(dequantize_ac(s, q[k])); //s * q[k];
1825
stop_decoding(JPGD_DECODE_ERROR);
1827
if (k < prev_num_set)
1829
int n = JPGD_MIN(16, prev_num_set - k);
1833
JPGD_ASSERT(kt <= 63);
1838
k += 16 - 1; // - 1 because the loop counter is k
1839
JPGD_ASSERT(p[g_ZAG[k]] == 0);
1846
if (k < prev_num_set)
1849
while (kt < prev_num_set)
1853
m_mcu_block_max_zag[mcu_block] = k;
1858
if (m_freq_domain_chroma_upsample)
1859
transform_mcu_expand(mcu_row);
1861
transform_mcu(mcu_row);
1867
// YCbCr H1V1 (1x1:1:1, 3 m_blocks per MCU) to RGB
1868
void jpeg_decoder::H1V1Convert()
1870
int row = m_max_mcu_y_size - m_mcu_lines_left;
1871
uint8 *d = m_pScan_line_0;
1872
uint8 *s = m_pSample_buf + row * 8;
1874
for (int i = m_max_mcus_per_row; i > 0; i--)
1876
for (int j = 0; j < 8; j++)
1882
d[0] = clamp(y + m_crr[cr]);
1883
d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
1884
d[2] = clamp(y + m_cbb[cb]);
1894
// YCbCr H2V1 (2x1:1:1, 4 m_blocks per MCU) to RGB
1895
void jpeg_decoder::H2V1Convert()
1897
int row = m_max_mcu_y_size - m_mcu_lines_left;
1898
uint8 *d0 = m_pScan_line_0;
1899
uint8 *y = m_pSample_buf + row * 8;
1900
uint8 *c = m_pSample_buf + 2*64 + row * 8;
1902
for (int i = m_max_mcus_per_row; i > 0; i--)
1904
for (int l = 0; l < 2; l++)
1906
for (int j = 0; j < 4; j++)
1912
int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
1916
d0[0] = clamp(yy+rc);
1917
d0[1] = clamp(yy+gc);
1918
d0[2] = clamp(yy+bc);
1922
d0[4] = clamp(yy+rc);
1923
d0[5] = clamp(yy+gc);
1924
d0[6] = clamp(yy+bc);
1939
// YCbCr H2V1 (1x2:1:1, 4 m_blocks per MCU) to RGB
1940
void jpeg_decoder::H1V2Convert()
1942
int row = m_max_mcu_y_size - m_mcu_lines_left;
1943
uint8 *d0 = m_pScan_line_0;
1944
uint8 *d1 = m_pScan_line_1;
1949
y = m_pSample_buf + row * 8;
1951
y = m_pSample_buf + 64*1 + (row & 7) * 8;
1953
c = m_pSample_buf + 64*2 + (row >> 1) * 8;
1955
for (int i = m_max_mcus_per_row; i > 0; i--)
1957
for (int j = 0; j < 8; j++)
1963
int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
1967
d0[0] = clamp(yy+rc);
1968
d0[1] = clamp(yy+gc);
1969
d0[2] = clamp(yy+bc);
1973
d1[0] = clamp(yy+rc);
1974
d1[1] = clamp(yy+gc);
1975
d1[2] = clamp(yy+bc);
1987
// YCbCr H2V2 (2x2:1:1, 6 m_blocks per MCU) to RGB
1988
void jpeg_decoder::H2V2Convert()
1990
int row = m_max_mcu_y_size - m_mcu_lines_left;
1991
uint8 *d0 = m_pScan_line_0;
1992
uint8 *d1 = m_pScan_line_1;
1997
y = m_pSample_buf + row * 8;
1999
y = m_pSample_buf + 64*2 + (row & 7) * 8;
2001
c = m_pSample_buf + 64*4 + (row >> 1) * 8;
2003
for (int i = m_max_mcus_per_row; i > 0; i--)
2005
for (int l = 0; l < 2; l++)
2007
for (int j = 0; j < 8; j += 2)
2013
int gc = ((m_crg[cr] + m_cbg[cb]) >> 16);
2017
d0[0] = clamp(yy+rc);
2018
d0[1] = clamp(yy+gc);
2019
d0[2] = clamp(yy+bc);
2023
d0[4] = clamp(yy+rc);
2024
d0[5] = clamp(yy+gc);
2025
d0[6] = clamp(yy+bc);
2029
d1[0] = clamp(yy+rc);
2030
d1[1] = clamp(yy+gc);
2031
d1[2] = clamp(yy+bc);
2035
d1[4] = clamp(yy+rc);
2036
d1[5] = clamp(yy+gc);
2037
d1[6] = clamp(yy+bc);
2053
// Y (1 block per MCU) to 8-bit grayscale
2054
void jpeg_decoder::gray_convert()
2056
int row = m_max_mcu_y_size - m_mcu_lines_left;
2057
uint8 *d = m_pScan_line_0;
2058
uint8 *s = m_pSample_buf + row * 8;
2060
for (int i = m_max_mcus_per_row; i > 0; i--)
2062
*(uint *)d = *(uint *)s;
2063
*(uint *)(&d[4]) = *(uint *)(&s[4]);
2070
void jpeg_decoder::expanded_convert()
2072
int row = m_max_mcu_y_size - m_mcu_lines_left;
2074
uint8* Py = m_pSample_buf + (row / 8) * 64 * m_comp_h_samp[0] + (row & 7) * 8;
2076
uint8* d = m_pScan_line_0;
2078
for (int i = m_max_mcus_per_row; i > 0; i--)
2080
for (int k = 0; k < m_max_mcu_x_size; k += 8)
2082
const int Y_ofs = k * 8;
2083
const int Cb_ofs = Y_ofs + 64 * m_expanded_blocks_per_component;
2084
const int Cr_ofs = Y_ofs + 64 * m_expanded_blocks_per_component * 2;
2085
for (int j = 0; j < 8; j++)
2087
int y = Py[Y_ofs + j];
2088
int cb = Py[Cb_ofs + j];
2089
int cr = Py[Cr_ofs + j];
2091
d[0] = clamp(y + m_crr[cr]);
2092
d[1] = clamp(y + ((m_crg[cr] + m_cbg[cb]) >> 16));
2093
d[2] = clamp(y + m_cbb[cb]);
2100
Py += 64 * m_expanded_blocks_per_mcu;
2104
// Find end of image (EOI) marker, so we can return to the user the exact size of the input stream.
2105
void jpeg_decoder::find_eoi()
2107
if (!m_progressive_flag)
2109
// Attempt to read the EOI marker.
2110
//get_bits_no_markers(m_bits_left & 7);
2112
// Prime the bit buffer
2117
// The next marker _should_ be EOI
2121
m_total_bytes_read -= m_in_buf_left;
2124
int jpeg_decoder::decode(const void** pScan_line, uint* pScan_line_len)
2126
if ((m_error_code) || (!m_ready_flag))
2129
if (m_total_lines_left == 0)
2132
if (m_mcu_lines_left == 0)
2134
if (setjmp(m_jmp_state))
2137
if (m_progressive_flag)
2142
// Find the EOI marker if that was the last row.
2143
if (m_total_lines_left <= m_max_mcu_y_size)
2146
m_mcu_lines_left = m_max_mcu_y_size;
2149
if (m_freq_domain_chroma_upsample)
2152
*pScan_line = m_pScan_line_0;
2156
switch (m_scan_type)
2160
if ((m_mcu_lines_left & 1) == 0)
2163
*pScan_line = m_pScan_line_0;
2166
*pScan_line = m_pScan_line_1;
2173
*pScan_line = m_pScan_line_0;
2178
if ((m_mcu_lines_left & 1) == 0)
2181
*pScan_line = m_pScan_line_0;
2184
*pScan_line = m_pScan_line_1;
2191
*pScan_line = m_pScan_line_0;
2194
case JPGD_GRAYSCALE:
2197
*pScan_line = m_pScan_line_0;
2204
*pScan_line_len = m_real_dest_bytes_per_scan_line;
2207
m_total_lines_left--;
2209
return JPGD_SUCCESS;
2212
// Creates the tables needed for efficient Huffman decoding.
2213
void jpeg_decoder::make_huff_table(int index, huff_tables *pH)
2216
uint8 huffsize[257];
2225
pH->ac_table = m_huff_ac[index] != 0;
2229
for (l = 1; l <= 16; l++)
2231
for (i = 1; i <= m_huff_num[index][l]; i++)
2232
huffsize[p++] = static_cast<uint8>(l);
2245
while (huffsize[p] == si)
2247
huffcode[p++] = code;
2255
memset(pH->look_up, 0, sizeof(pH->look_up));
2256
memset(pH->look_up2, 0, sizeof(pH->look_up2));
2257
memset(pH->tree, 0, sizeof(pH->tree));
2258
memset(pH->code_size, 0, sizeof(pH->code_size));
2266
i = m_huff_val[index][p];
2268
code_size = huffsize[p];
2270
pH->code_size[i] = static_cast<uint8>(code_size);
2274
code <<= (8 - code_size);
2276
for (l = 1 << (8 - code_size); l > 0; l--)
2278
JPGD_ASSERT(i < 256);
2280
pH->look_up[code] = i;
2282
bool has_extrabits = false;
2284
int num_extra_bits = i & 15;
2286
int bits_to_fetch = code_size;
2289
int total_codesize = code_size + num_extra_bits;
2290
if (total_codesize <= 8)
2292
has_extrabits = true;
2293
extra_bits = ((1 << num_extra_bits) - 1) & (code >> (8 - total_codesize));
2294
JPGD_ASSERT(extra_bits <= 0x7FFF);
2295
bits_to_fetch += num_extra_bits;
2300
pH->look_up2[code] = i | (bits_to_fetch << 8);
2302
pH->look_up2[code] = i | 0x8000 | (extra_bits << 16) | (bits_to_fetch << 8);
2309
subtree = (code >> (code_size - 8)) & 0xFF;
2311
currententry = pH->look_up[subtree];
2313
if (currententry == 0)
2315
pH->look_up[subtree] = currententry = nextfreeentry;
2316
pH->look_up2[subtree] = currententry = nextfreeentry;
2321
code <<= (16 - (code_size - 8));
2323
for (l = code_size; l > 9; l--)
2325
if ((code & 0x8000) == 0)
2328
if (pH->tree[-currententry - 1] == 0)
2330
pH->tree[-currententry - 1] = nextfreeentry;
2332
currententry = nextfreeentry;
2337
currententry = pH->tree[-currententry - 1];
2342
if ((code & 0x8000) == 0)
2345
pH->tree[-currententry - 1] = i;
2352
// Verifies the quantization tables needed for this scan are available.
2353
void jpeg_decoder::check_quant_tables()
2355
for (int i = 0; i < m_comps_in_scan; i++)
2356
if (m_quant[m_comp_quant[m_comp_list[i]]] == NULL)
2357
stop_decoding(JPGD_UNDEFINED_QUANT_TABLE);
2360
// Verifies that all the Huffman tables needed for this scan are available.
2361
void jpeg_decoder::check_huff_tables()
2363
for (int i = 0; i < m_comps_in_scan; i++)
2365
if ((m_spectral_start == 0) && (m_huff_num[m_comp_dc_tab[m_comp_list[i]]] == NULL))
2366
stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
2368
if ((m_spectral_end > 0) && (m_huff_num[m_comp_ac_tab[m_comp_list[i]]] == NULL))
2369
stop_decoding(JPGD_UNDEFINED_HUFF_TABLE);
2372
for (int i = 0; i < JPGD_MAX_HUFF_TABLES; i++)
2375
if (!m_pHuff_tabs[i])
2376
m_pHuff_tabs[i] = (huff_tables *)alloc(sizeof(huff_tables));
2378
make_huff_table(i, m_pHuff_tabs[i]);
2382
// Determines the component order inside each MCU.
2383
// Also calcs how many MCU's are on each row, etc.
2384
void jpeg_decoder::calc_mcu_block_order()
2386
int component_num, component_id;
2387
int max_h_samp = 0, max_v_samp = 0;
2389
for (component_id = 0; component_id < m_comps_in_frame; component_id++)
2391
if (m_comp_h_samp[component_id] > max_h_samp)
2392
max_h_samp = m_comp_h_samp[component_id];
2394
if (m_comp_v_samp[component_id] > max_v_samp)
2395
max_v_samp = m_comp_v_samp[component_id];
2398
for (component_id = 0; component_id < m_comps_in_frame; component_id++)
2400
m_comp_h_blocks[component_id] = ((((m_image_x_size * m_comp_h_samp[component_id]) + (max_h_samp - 1)) / max_h_samp) + 7) / 8;
2401
m_comp_v_blocks[component_id] = ((((m_image_y_size * m_comp_v_samp[component_id]) + (max_v_samp - 1)) / max_v_samp) + 7) / 8;
2404
if (m_comps_in_scan == 1)
2406
m_mcus_per_row = m_comp_h_blocks[m_comp_list[0]];
2407
m_mcus_per_col = m_comp_v_blocks[m_comp_list[0]];
2411
m_mcus_per_row = (((m_image_x_size + 7) / 8) + (max_h_samp - 1)) / max_h_samp;
2412
m_mcus_per_col = (((m_image_y_size + 7) / 8) + (max_v_samp - 1)) / max_v_samp;
2415
if (m_comps_in_scan == 1)
2417
m_mcu_org[0] = m_comp_list[0];
2419
m_blocks_per_mcu = 1;
2423
m_blocks_per_mcu = 0;
2425
for (component_num = 0; component_num < m_comps_in_scan; component_num++)
2429
component_id = m_comp_list[component_num];
2431
num_blocks = m_comp_h_samp[component_id] * m_comp_v_samp[component_id];
2433
while (num_blocks--)
2434
m_mcu_org[m_blocks_per_mcu++] = component_id;
2439
// Starts a new scan.
2440
int jpeg_decoder::init_scan()
2442
if (!locate_sos_marker())
2445
calc_mcu_block_order();
2447
check_huff_tables();
2449
check_quant_tables();
2451
memset(m_last_dc_val, 0, m_comps_in_frame * sizeof(uint));
2455
if (m_restart_interval)
2457
m_restarts_left = m_restart_interval;
2458
m_next_restart_num = 0;
2466
// Starts a frame. Determines if the number of components or sampling factors
2468
void jpeg_decoder::init_frame()
2472
if (m_comps_in_frame == 1)
2474
if ((m_comp_h_samp[0] != 1) || (m_comp_v_samp[0] != 1))
2475
stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
2477
m_scan_type = JPGD_GRAYSCALE;
2478
m_max_blocks_per_mcu = 1;
2479
m_max_mcu_x_size = 8;
2480
m_max_mcu_y_size = 8;
2482
else if (m_comps_in_frame == 3)
2484
if ( ((m_comp_h_samp[1] != 1) || (m_comp_v_samp[1] != 1)) ||
2485
((m_comp_h_samp[2] != 1) || (m_comp_v_samp[2] != 1)) )
2486
stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
2488
if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 1))
2490
m_scan_type = JPGD_YH1V1;
2492
m_max_blocks_per_mcu = 3;
2493
m_max_mcu_x_size = 8;
2494
m_max_mcu_y_size = 8;
2496
else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 1))
2498
m_scan_type = JPGD_YH2V1;
2499
m_max_blocks_per_mcu = 4;
2500
m_max_mcu_x_size = 16;
2501
m_max_mcu_y_size = 8;
2503
else if ((m_comp_h_samp[0] == 1) && (m_comp_v_samp[0] == 2))
2505
m_scan_type = JPGD_YH1V2;
2506
m_max_blocks_per_mcu = 4;
2507
m_max_mcu_x_size = 8;
2508
m_max_mcu_y_size = 16;
2510
else if ((m_comp_h_samp[0] == 2) && (m_comp_v_samp[0] == 2))
2512
m_scan_type = JPGD_YH2V2;
2513
m_max_blocks_per_mcu = 6;
2514
m_max_mcu_x_size = 16;
2515
m_max_mcu_y_size = 16;
2518
stop_decoding(JPGD_UNSUPPORTED_SAMP_FACTORS);
2521
stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
2523
m_max_mcus_per_row = (m_image_x_size + (m_max_mcu_x_size - 1)) / m_max_mcu_x_size;
2524
m_max_mcus_per_col = (m_image_y_size + (m_max_mcu_y_size - 1)) / m_max_mcu_y_size;
2526
// These values are for the *destination* pixels: after conversion.
2527
if (m_scan_type == JPGD_GRAYSCALE)
2528
m_dest_bytes_per_pixel = 1;
2530
m_dest_bytes_per_pixel = 4;
2532
m_dest_bytes_per_scan_line = ((m_image_x_size + 15) & 0xFFF0) * m_dest_bytes_per_pixel;
2534
m_real_dest_bytes_per_scan_line = (m_image_x_size * m_dest_bytes_per_pixel);
2536
// Initialize two scan line buffers.
2537
m_pScan_line_0 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
2538
if ((m_scan_type == JPGD_YH1V2) || (m_scan_type == JPGD_YH2V2))
2539
m_pScan_line_1 = (uint8 *)alloc(m_dest_bytes_per_scan_line, true);
2541
m_max_blocks_per_row = m_max_mcus_per_row * m_max_blocks_per_mcu;
2543
// Should never happen
2544
if (m_max_blocks_per_row > JPGD_MAX_BLOCKS_PER_ROW)
2545
stop_decoding(JPGD_ASSERTION_ERROR);
2547
// Allocate the coefficient buffer, enough for one MCU
2548
m_pMCU_coefficients = (jpgd_block_t*)alloc(m_max_blocks_per_mcu * 64 * sizeof(jpgd_block_t));
2550
for (i = 0; i < m_max_blocks_per_mcu; i++)
2551
m_mcu_block_max_zag[i] = 64;
2553
m_expanded_blocks_per_component = m_comp_h_samp[0] * m_comp_v_samp[0];
2554
m_expanded_blocks_per_mcu = m_expanded_blocks_per_component * m_comps_in_frame;
2555
m_expanded_blocks_per_row = m_max_mcus_per_row * m_expanded_blocks_per_mcu;
2556
// Freq. domain chroma upsampling is only supported for H2V2 subsampling factor (the most common one I've seen).
2557
m_freq_domain_chroma_upsample = false;
2558
#if JPGD_SUPPORT_FREQ_DOMAIN_UPSAMPLING
2559
m_freq_domain_chroma_upsample = (m_expanded_blocks_per_mcu == 4*3);
2562
if (m_freq_domain_chroma_upsample)
2563
m_pSample_buf = (uint8 *)alloc(m_expanded_blocks_per_row * 64);
2565
m_pSample_buf = (uint8 *)alloc(m_max_blocks_per_row * 64);
2567
m_total_lines_left = m_image_y_size;
2569
m_mcu_lines_left = 0;
2574
// The coeff_buf series of methods originally stored the coefficients
2575
// into a "virtual" file which was located in EMS, XMS, or a disk file. A cache
2576
// was used to make this process more efficient. Now, we can store the entire
2578
jpeg_decoder::coeff_buf* jpeg_decoder::coeff_buf_open(int block_num_x, int block_num_y, int block_len_x, int block_len_y)
2580
coeff_buf* cb = (coeff_buf*)alloc(sizeof(coeff_buf));
2582
cb->block_num_x = block_num_x;
2583
cb->block_num_y = block_num_y;
2584
cb->block_len_x = block_len_x;
2585
cb->block_len_y = block_len_y;
2586
cb->block_size = (block_len_x * block_len_y) * sizeof(jpgd_block_t);
2587
cb->pData = (uint8 *)alloc(cb->block_size * block_num_x * block_num_y, true);
2591
inline jpgd_block_t *jpeg_decoder::coeff_buf_getp(coeff_buf *cb, int block_x, int block_y)
2593
JPGD_ASSERT((block_x < cb->block_num_x) && (block_y < cb->block_num_y));
2594
return (jpgd_block_t *)(cb->pData + block_x * cb->block_size + block_y * (cb->block_size * cb->block_num_x));
2597
// The following methods decode the various types of m_blocks encountered
2598
// in progressively encoded images.
2599
void jpeg_decoder::decode_block_dc_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
2602
jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
2604
if ((s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_dc_tab[component_id]])) != 0)
2606
r = pD->get_bits_no_markers(s);
2607
s = JPGD_HUFF_EXTEND(r, s);
2610
pD->m_last_dc_val[component_id] = (s += pD->m_last_dc_val[component_id]);
2612
p[0] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
2615
void jpeg_decoder::decode_block_dc_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
2617
if (pD->get_bits_no_markers(1))
2619
jpgd_block_t *p = pD->coeff_buf_getp(pD->m_dc_coeffs[component_id], block_x, block_y);
2621
p[0] |= (1 << pD->m_successive_low);
2625
void jpeg_decoder::decode_block_ac_first(jpeg_decoder *pD, int component_id, int block_x, int block_y)
2635
jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
2637
for (k = pD->m_spectral_start; k <= pD->m_spectral_end; k++)
2639
s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
2647
pD->stop_decoding(JPGD_DECODE_ERROR);
2649
r = pD->get_bits_no_markers(s);
2650
s = JPGD_HUFF_EXTEND(r, s);
2652
p[g_ZAG[k]] = static_cast<jpgd_block_t>(s << pD->m_successive_low);
2659
pD->stop_decoding(JPGD_DECODE_ERROR);
2663
pD->m_eob_run = 1 << r;
2666
pD->m_eob_run += pD->get_bits_no_markers(r);
2676
void jpeg_decoder::decode_block_ac_refine(jpeg_decoder *pD, int component_id, int block_x, int block_y)
2679
int p1 = 1 << pD->m_successive_low;
2680
int m1 = (-1) << pD->m_successive_low;
2681
jpgd_block_t *p = pD->coeff_buf_getp(pD->m_ac_coeffs[component_id], block_x, block_y);
2683
JPGD_ASSERT(pD->m_spectral_end <= 63);
2685
k = pD->m_spectral_start;
2687
if (pD->m_eob_run == 0)
2689
for ( ; k <= pD->m_spectral_end; k++)
2691
s = pD->huff_decode(pD->m_pHuff_tabs[pD->m_comp_ac_tab[component_id]]);
2699
pD->stop_decoding(JPGD_DECODE_ERROR);
2701
if (pD->get_bits_no_markers(1))
2710
pD->m_eob_run = 1 << r;
2713
pD->m_eob_run += pD->get_bits_no_markers(r);
2721
jpgd_block_t *this_coef = p + g_ZAG[k & 63];
2723
if (*this_coef != 0)
2725
if (pD->get_bits_no_markers(1))
2727
if ((*this_coef & p1) == 0)
2729
if (*this_coef >= 0)
2730
*this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
2732
*this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
2744
} while (k <= pD->m_spectral_end);
2746
if ((s) && (k < 64))
2748
p[g_ZAG[k]] = static_cast<jpgd_block_t>(s);
2753
if (pD->m_eob_run > 0)
2755
for ( ; k <= pD->m_spectral_end; k++)
2757
jpgd_block_t *this_coef = p + g_ZAG[k & 63]; // logical AND to shut up static code analysis
2759
if (*this_coef != 0)
2761
if (pD->get_bits_no_markers(1))
2763
if ((*this_coef & p1) == 0)
2765
if (*this_coef >= 0)
2766
*this_coef = static_cast<jpgd_block_t>(*this_coef + p1);
2768
*this_coef = static_cast<jpgd_block_t>(*this_coef + m1);
2778
// Decode a scan in a progressively encoded image.
2779
void jpeg_decoder::decode_scan(pDecode_block_func decode_block_func)
2781
int mcu_row, mcu_col, mcu_block;
2782
int block_x_mcu[JPGD_MAX_COMPONENTS], m_block_y_mcu[JPGD_MAX_COMPONENTS];
2784
memset(m_block_y_mcu, 0, sizeof(m_block_y_mcu));
2786
for (mcu_col = 0; mcu_col < m_mcus_per_col; mcu_col++)
2788
int component_num, component_id;
2790
memset(block_x_mcu, 0, sizeof(block_x_mcu));
2792
for (mcu_row = 0; mcu_row < m_mcus_per_row; mcu_row++)
2794
int block_x_mcu_ofs = 0, block_y_mcu_ofs = 0;
2796
if ((m_restart_interval) && (m_restarts_left == 0))
2799
for (mcu_block = 0; mcu_block < m_blocks_per_mcu; mcu_block++)
2801
component_id = m_mcu_org[mcu_block];
2803
decode_block_func(this, component_id, block_x_mcu[component_id] + block_x_mcu_ofs, m_block_y_mcu[component_id] + block_y_mcu_ofs);
2805
if (m_comps_in_scan == 1)
2806
block_x_mcu[component_id]++;
2809
if (++block_x_mcu_ofs == m_comp_h_samp[component_id])
2811
block_x_mcu_ofs = 0;
2813
if (++block_y_mcu_ofs == m_comp_v_samp[component_id])
2815
block_y_mcu_ofs = 0;
2816
block_x_mcu[component_id] += m_comp_h_samp[component_id];
2825
if (m_comps_in_scan == 1)
2826
m_block_y_mcu[m_comp_list[0]]++;
2829
for (component_num = 0; component_num < m_comps_in_scan; component_num++)
2831
component_id = m_comp_list[component_num];
2832
m_block_y_mcu[component_id] += m_comp_v_samp[component_id];
2838
// Decode a progressively encoded image.
2839
void jpeg_decoder::init_progressive()
2843
if (m_comps_in_frame == 4)
2844
stop_decoding(JPGD_UNSUPPORTED_COLORSPACE);
2846
// Allocate the coefficient buffers.
2847
for (i = 0; i < m_comps_in_frame; i++)
2849
m_dc_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 1, 1);
2850
m_ac_coeffs[i] = coeff_buf_open(m_max_mcus_per_row * m_comp_h_samp[i], m_max_mcus_per_col * m_comp_v_samp[i], 8, 8);
2855
int dc_only_scan, refinement_scan;
2856
pDecode_block_func decode_block_func;
2861
dc_only_scan = (m_spectral_start == 0);
2862
refinement_scan = (m_successive_high != 0);
2864
if ((m_spectral_start > m_spectral_end) || (m_spectral_end > 63))
2865
stop_decoding(JPGD_BAD_SOS_SPECTRAL);
2870
stop_decoding(JPGD_BAD_SOS_SPECTRAL);
2872
else if (m_comps_in_scan != 1) /* AC scans can only contain one component */
2873
stop_decoding(JPGD_BAD_SOS_SPECTRAL);
2875
if ((refinement_scan) && (m_successive_low != m_successive_high - 1))
2876
stop_decoding(JPGD_BAD_SOS_SUCCESSIVE);
2880
if (refinement_scan)
2881
decode_block_func = decode_block_dc_refine;
2883
decode_block_func = decode_block_dc_first;
2887
if (refinement_scan)
2888
decode_block_func = decode_block_ac_refine;
2890
decode_block_func = decode_block_ac_first;
2893
decode_scan(decode_block_func);
2900
m_comps_in_scan = m_comps_in_frame;
2902
for (i = 0; i < m_comps_in_frame; i++)
2905
calc_mcu_block_order();
2908
void jpeg_decoder::init_sequential()
2911
stop_decoding(JPGD_UNEXPECTED_MARKER);
2914
void jpeg_decoder::decode_start()
2918
if (m_progressive_flag)
2924
void jpeg_decoder::decode_init(jpeg_decoder_stream *pStream)
2927
locate_sof_marker();
2930
jpeg_decoder::jpeg_decoder(jpeg_decoder_stream *pStream)
2932
if (setjmp(m_jmp_state))
2934
decode_init(pStream);
2937
int jpeg_decoder::begin_decoding()
2940
return JPGD_SUCCESS;
2945
if (setjmp(m_jmp_state))
2950
m_ready_flag = true;
2952
return JPGD_SUCCESS;
2955
jpeg_decoder::~jpeg_decoder()
2960
jpeg_decoder_file_stream::jpeg_decoder_file_stream()
2964
m_error_flag = false;
2967
void jpeg_decoder_file_stream::close()
2976
m_error_flag = false;
2979
jpeg_decoder_file_stream::~jpeg_decoder_file_stream()
2984
bool jpeg_decoder_file_stream::open(const char *Pfilename)
2989
m_error_flag = false;
2991
#if defined(_MSC_VER)
2993
fopen_s(&m_pFile, Pfilename, "rb");
2995
m_pFile = fopen(Pfilename, "rb");
2997
return m_pFile != NULL;
3000
int jpeg_decoder_file_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
3014
int bytes_read = static_cast<int>(fread(pBuf, 1, max_bytes_to_read, m_pFile));
3015
if (bytes_read < max_bytes_to_read)
3017
if (ferror(m_pFile))
3019
m_error_flag = true;
3030
bool jpeg_decoder_mem_stream::open(const uint8 *pSrc_data, uint size)
3033
m_pSrc_data = pSrc_data;
3039
int jpeg_decoder_mem_stream::read(uint8 *pBuf, int max_bytes_to_read, bool *pEOF_flag)
3046
uint bytes_remaining = m_size - m_ofs;
3047
if ((uint)max_bytes_to_read > bytes_remaining)
3049
max_bytes_to_read = bytes_remaining;
3053
memcpy(pBuf, m_pSrc_data + m_ofs, max_bytes_to_read);
3054
m_ofs += max_bytes_to_read;
3056
return max_bytes_to_read;
3059
unsigned char *decompress_jpeg_image_from_stream(jpeg_decoder_stream *pStream, int *width, int *height, int *actual_comps, int req_comps)
3065
if ((!pStream) || (!width) || (!height) || (!req_comps))
3068
if ((req_comps != 1) && (req_comps != 3) && (req_comps != 4))
3071
jpeg_decoder decoder(pStream);
3072
if (decoder.get_error_code() != JPGD_SUCCESS)
3075
const int image_width = decoder.get_width(), image_height = decoder.get_height();
3076
*width = image_width;
3077
*height = image_height;
3078
*actual_comps = decoder.get_num_components();
3080
if (decoder.begin_decoding() != JPGD_SUCCESS)
3083
const int dst_bpl = image_width * req_comps;
3085
uint8 *pImage_data = (uint8*)jpgd_malloc(dst_bpl * image_height);
3089
for (int y = 0; y < image_height; y++)
3091
const uint8* pScan_line;
3093
if (decoder.decode((const void**)&pScan_line, &scan_line_len) != JPGD_SUCCESS)
3095
jpgd_free(pImage_data);
3099
uint8 *pDst = pImage_data + y * dst_bpl;
3101
if (((req_comps == 1) && (decoder.get_num_components() == 1)) || ((req_comps == 4) && (decoder.get_num_components() == 3)))
3102
memcpy(pDst, pScan_line, dst_bpl);
3103
else if (decoder.get_num_components() == 1)
3107
for (int x = 0; x < image_width; x++)
3109
uint8 luma = pScan_line[x];
3118
for (int x = 0; x < image_width; x++)
3120
uint8 luma = pScan_line[x];
3129
else if (decoder.get_num_components() == 3)
3133
const int YR = 19595, YG = 38470, YB = 7471;
3134
for (int x = 0; x < image_width; x++)
3136
int r = pScan_line[x*4+0];
3137
int g = pScan_line[x*4+1];
3138
int b = pScan_line[x*4+2];
3139
*pDst++ = static_cast<uint8>((r * YR + g * YG + b * YB + 32768) >> 16);
3144
for (int x = 0; x < image_width; x++)
3146
pDst[0] = pScan_line[x*4+0];
3147
pDst[1] = pScan_line[x*4+1];
3148
pDst[2] = pScan_line[x*4+2];
3158
unsigned char *decompress_jpeg_image_from_memory(const unsigned char *pSrc_data, int src_data_size, int *width, int *height, int *actual_comps, int req_comps)
3160
jpgd::jpeg_decoder_mem_stream mem_stream(pSrc_data, src_data_size);
3161
return decompress_jpeg_image_from_stream(&mem_stream, width, height, actual_comps, req_comps);
3164
unsigned char *decompress_jpeg_image_from_file(const char *pSrc_filename, int *width, int *height, int *actual_comps, int req_comps)
3166
jpgd::jpeg_decoder_file_stream file_stream;
3167
if (!file_stream.open(pSrc_filename))
3169
return decompress_jpeg_image_from_stream(&file_stream, width, height, actual_comps, req_comps);
b'\\ No newline at end of file'