Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__jpeg_decoder__Proj
view VSs_tinyjpeg/tinyjpeg-parse.c @ 0:a8af8b3fc99d
initial commit
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Thu, 05 Jul 2012 11:35:03 +0200 |
| parents | |
| children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdint.h>
5 #include <errno.h>
7 #include "tinyjpeg.h"
8 #include "tinyjpeg-internal.h"
10 extern char error_string[256];
12 static const unsigned char zigzag[64] =
13 {
14 0, 1, 5, 6, 14, 15, 27, 28,
15 2, 4, 7, 13, 16, 26, 29, 42,
16 3, 8, 12, 17, 25, 30, 41, 43,
17 9, 11, 18, 24, 31, 40, 44, 53,
18 10, 19, 23, 32, 39, 45, 52, 54,
19 20, 22, 33, 38, 46, 51, 55, 60,
20 21, 34, 37, 47, 50, 56, 59, 61,
21 35, 36, 48, 49, 57, 58, 62, 63
22 };
24 /* Set up the standard Huffman tables (cf. JPEG standard section K.3) */
25 /* IMPORTANT: these are only valid for 8-bit data precision! */
26 static const unsigned char bits_dc_luminance[17] =
27 {
28 0, 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0
29 };
30 static const unsigned char val_dc_luminance[] =
31 {
32 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
33 };
35 static const unsigned char bits_dc_chrominance[17] =
36 {
37 0, 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0
38 };
39 static const unsigned char val_dc_chrominance[] =
40 {
41 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
42 };
44 static const unsigned char bits_ac_luminance[17] =
45 {
46 0, 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d
47 };
48 static const unsigned char val_ac_luminance[] =
49 {
50 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
51 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
52 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
53 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
54 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
55 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
56 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
57 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
58 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
59 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
60 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
61 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
62 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
63 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
64 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
65 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
66 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
67 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
68 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
69 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
70 0xf9, 0xfa
71 };
73 static const unsigned char bits_ac_chrominance[17] =
74 {
75 0, 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77
76 };
78 static const unsigned char val_ac_chrominance[] =
79 {
80 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
81 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
82 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
83 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
84 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
85 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
86 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
87 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
88 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
89 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
90 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
91 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
92 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
93 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
94 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
95 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
96 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
97 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
98 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
99 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
100 0xf9, 0xfa
101 };
103 static void print_SOF(const unsigned char *stream)
104 {
105 int width, height, nr_components, precision;
107 precision = stream[2];
108 height = be16_to_cpu(stream+3);
109 width = be16_to_cpu(stream+5);
110 nr_components = stream[7];
112 trace("> SOF marker\n");
113 trace("Size:%dx%d nr_components:%d precision:%d\n",
114 width, height,
115 nr_components,
116 precision);
117 }
120 /*
121 * Takes two array of bits, and build the huffman table for size, and code
122 *
123 * lookup will return the symbol if the code is less or equal than HUFFMAN_HASH_NBITS.
124 * code_size will be used to known how many bits this symbol is encoded.
125 * slowtable will be used when the first lookup didn't give the result.
126 */
127 static void build_huffman_table(const unsigned char *bits, const unsigned char *vals, struct huffman_table *table)
128 {
129 unsigned int i, j, code, code_size, val, nbits;
130 unsigned char huffsize[HUFFMAN_BITS_SIZE+1], *hz;
131 unsigned int huffcode[HUFFMAN_BITS_SIZE+1], *hc;
132 int next_free_entry;
134 /*
135 * Build a temp array
136 * huffsize[X] => numbers of bits to write vals[X]
137 */
138 hz = huffsize;
139 for (i=1; i<=16; i++)
140 {
141 for (j=1; j<=bits[i]; j++)
142 *hz++ = i;
143 }
144 *hz = 0;
146 memset(table->lookup, 0xff, sizeof(table->lookup));
147 for (i=0; i<(16-HUFFMAN_HASH_NBITS); i++)
148 table->slowtable[i][0] = 0;
150 /* Build a temp array
151 * huffcode[X] => code used to write vals[X]
152 */
153 code = 0;
154 hc = huffcode;
155 hz = huffsize;
156 nbits = *hz;
157 while (*hz)
158 {
159 while (*hz == nbits)
160 {
161 *hc++ = code++;
162 hz++;
163 }
164 code <<= 1;
165 nbits++;
166 }
168 /*
169 * Build the lookup table, and the slowtable if needed.
170 */
171 next_free_entry = -1;
172 for (i=0; huffsize[i]; i++)
173 {
174 val = vals[i];
175 code = huffcode[i];
176 code_size = huffsize[i];
178 trace("val=%2.2x code=%8.8x codesize=%2.2d\n", val, code, code_size);
180 table->code_size[val] = code_size;
181 if (code_size <= HUFFMAN_HASH_NBITS)
182 {
183 /*
184 * Good: val can be put in the lookup table, so fill all value of this
185 * column with value val
186 */
187 int repeat = 1UL<<(HUFFMAN_HASH_NBITS - code_size);
188 code <<= HUFFMAN_HASH_NBITS - code_size;
189 while ( repeat-- )
190 table->lookup[code++] = val;
192 }
193 else
194 {
195 /* Perhaps sorting the array will be an optimization */
196 uint16_t *slowtable = table->slowtable[code_size-HUFFMAN_HASH_NBITS-1];
197 while(slowtable[0])
198 slowtable+=2;
199 slowtable[0] = code;
200 slowtable[1] = val;
201 slowtable[2] = 0;
202 }
204 }
205 }
207 static void build_default_huffman_tables(struct jdec_private *priv)
208 {
209 if ( (priv->flags & TINYJPEG_FLAGS_MJPEG_TABLE)
210 && priv->default_huffman_table_initialized)
211 return;
213 build_huffman_table(bits_dc_luminance, val_dc_luminance, &priv->HTDC[0]);
214 build_huffman_table(bits_ac_luminance, val_ac_luminance, &priv->HTAC[0]);
216 build_huffman_table(bits_dc_chrominance, val_dc_chrominance, &priv->HTDC[1]);
217 build_huffman_table(bits_ac_chrominance, val_ac_chrominance, &priv->HTAC[1]);
219 priv->default_huffman_table_initialized = 1;
220 }
222 /*******************************************************************************
223 *
224 * JPEG/JFIF Parsing functions
225 *
226 * Note: only a small subset of the jpeg file format is supported. No markers,
227 * nor progressive stream is supported.
228 *
229 ******************************************************************************/
231 static void build_quantization_table(float *qtable, const unsigned char *ref_table)
232 {
233 /* Taken from libjpeg. Copyright Independent JPEG Group's LLM idct.
234 * For float AA&N IDCT method, divisors are equal to quantization
235 * coefficients scaled by scalefactor[row]*scalefactor[col], where
236 * scalefactor[0] = 1
237 * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
238 * We apply a further scale factor of 8.
239 * What's actually stored is 1/divisor so that the inner loop can
240 * use a multiplication rather than a division.
241 */
242 int i, j;
243 static const double aanscalefactor[8] = {
244 1.0, 1.387039845, 1.306562965, 1.175875602,
245 1.0, 0.785694958, 0.541196100, 0.275899379
246 };
247 const unsigned char *zz = zigzag;
249 for (i=0; i<8; i++) {
250 for (j=0; j<8; j++) {
251 *qtable++ = ref_table[*zz++] * aanscalefactor[i] * aanscalefactor[j];
252 }
253 }
255 }
257 static int parse_DQT(struct jdec_private *priv, const unsigned char *stream)
258 {
259 int qi;
260 float *table;
261 const unsigned char *dqt_block_end;
263 trace("> DQT marker\n");
264 dqt_block_end = stream + be16_to_cpu(stream);
265 stream += 2; /* Skip length */
267 while (stream < dqt_block_end)
268 {
269 qi = *stream++;
270 #if SANITY_CHECK
271 if (qi>>4)
272 error("16 bits quantization table is not supported\n");
273 if (qi>4)
274 error("No more 4 quantization table is supported (got %d)\n", qi);
275 #endif
276 table = priv->Q_tables[qi];
277 build_quantization_table(table, stream);
278 stream += 64;
279 }
280 trace("< DQT marker\n");
281 return 0;
282 }
284 static int parse_SOF(struct jdec_private *priv, const unsigned char *stream)
285 {
286 int i, width, height, nr_components, cid, sampling_factor;
287 int Q_table;
288 struct component *c;
290 trace("> SOF marker\n");
291 print_SOF(stream);
293 height = be16_to_cpu(stream+3);
294 width = be16_to_cpu(stream+5);
295 nr_components = stream[7];
296 #if SANITY_CHECK
297 if (stream[2] != 8)
298 error("Precision other than 8 is not supported\n");
299 if (nr_components != 3)
300 error("We only support YUV images\n");
301 if (height%16)
302 error("Height need to be a multiple of 16 (current height is %d)\n", height);
303 if (width%16)
304 error("Width need to be a multiple of 16 (current Width is %d)\n", width);
305 #endif
306 stream += 8;
307 for (i=0; i<nr_components; i++) {
308 cid = *stream++;
309 sampling_factor = *stream++;
310 Q_table = *stream++;
311 c = &priv->component_infos[i];
312 #if SANITY_CHECK
313 c->cid = cid;
314 if (Q_table >= COMPONENTS)
315 error("Bad Quantization table index (got %d, max allowed %d)\n", Q_table, COMPONENTS-1);
316 #endif
317 c->Vfactor = sampling_factor&0xf;
318 c->Hfactor = sampling_factor>>4;
319 c->Q_table = priv->Q_tables[Q_table];
320 trace("Component:%d factor:%dx%d Quantization table:%d\n", cid, c->Hfactor, c->Hfactor, Q_table );
322 }
323 priv->width = width;
324 priv->height = height;
325 priv->mcus_in_width = width/16;
326 priv->mcus_in_height = height/16;
327 priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height;
329 trace("< SOF marker\n");
331 return 0;
332 }
334 static int parse_SOS(struct jdec_private *priv, const unsigned char *stream)
335 {
336 unsigned int i, cid, table;
337 unsigned int nr_components = stream[2];
339 trace("> SOS marker\n");
341 #if SANITY_CHECK
342 if (nr_components != 3)
343 error("We only support YCbCr image\n");
344 #endif
346 stream += 3;
347 for (i=0;i<nr_components;i++) {
348 cid = *stream++;
349 table = *stream++;
350 #if SANITY_CHECK
351 if ((table&0xf)>=4)
352 error("We do not support more than 2 AC Huffman table\n");
353 if ((table>>4)>=4)
354 error("We do not support more than 2 DC Huffman table\n");
355 if (cid != priv->component_infos[i].cid)
356 error("SOS cid order (%d:%d) isn't compatible with the SOF marker (%d:%d)\n",
357 i, cid, i, priv->component_infos[i].cid);
358 trace("ComponentId:%d tableAC:%d tableDC:%d\n", cid, table&0xf, table>>4);
359 #endif
360 priv->component_infos[i].AC_table = &priv->HTAC[table&0xf];
361 priv->component_infos[i].DC_table = &priv->HTDC[table>>4];
362 }
363 priv->stream = stream+3;
364 trace("< SOS marker\n");
365 return 0;
366 }
368 static int parse_DHT(struct jdec_private *priv, const unsigned char *stream)
369 {
370 unsigned int count, i;
371 unsigned char huff_bits[17];
372 int length, index;
374 length = be16_to_cpu(stream) - 2;
375 stream += 2; /* Skip length */
377 trace("> DHT marker (length=%d)\n", length);
379 while (length>0) {
380 index = *stream++;
382 /* We need to calculate the number of bytes 'vals' will takes */
383 huff_bits[0] = 0;
384 count = 0;
385 for (i=1; i<17; i++) {
386 huff_bits[i] = *stream++;
387 count += huff_bits[i];
388 }
389 #if SANITY_CHECK
390 if (count >= HUFFMAN_BITS_SIZE)
391 error("No more than %d bytes is allowed to describe a huffman table", HUFFMAN_BITS_SIZE);
392 if ( (index &0xf) >= HUFFMAN_TABLES)
393 error("No more than %d Huffman tables is supported (got %d)\n", HUFFMAN_TABLES, index&0xf);
394 trace("Huffman table %s[%d] length=%d\n", (index&0xf0)?"AC":"DC", index&0xf, count);
395 #endif
397 if (index & 0xf0 )
398 build_huffman_table(huff_bits, stream, &priv->HTAC[index&0xf]);
399 else
400 build_huffman_table(huff_bits, stream, &priv->HTDC[index&0xf]);
402 length -= 1;
403 length -= 16;
404 length -= count;
405 stream += count;
406 }
407 trace("< DHT marker\n");
408 return 0;
409 }
411 static int parse_DRI(struct jdec_private *priv, const unsigned char *stream)
412 {
413 unsigned int length;
415 trace("> DRI marker\n");
417 length = be16_to_cpu(stream);
419 #if SANITY_CHECK
420 if (length != 4)
421 error("Length of DRI marker need to be 4\n");
422 #endif
424 priv->restart_interval = be16_to_cpu(stream+2);
425 if (priv->restart_interval == 0)
426 priv->restart_interval = priv->mcus_in_width * priv->mcus_in_height;
428 #if DEBUG
429 trace("Restart interval = %d\n", priv->restart_interval);
430 #endif
432 trace("< DRI marker\n");
434 return 0;
435 }
437 int parse_JFIF(struct jdec_private *priv, const unsigned char *stream)
438 {
439 int chuck_len;
440 int marker;
441 int sos_marker_found = 0;
442 int dht_marker_found = 0;
443 const unsigned char *next_chunck;
445 /* Parse marker */
446 while (!sos_marker_found)
447 {
448 if (*stream++ != 0xff) {
449 trace("Bogus jpeg format\n");
450 return -1;
451 }
452 /* Skip any padding ff byte (this is normal) */
453 while (*stream == 0xff)
454 stream++;
456 marker = *stream++;
457 chuck_len = be16_to_cpu(stream);
458 next_chunck = stream + chuck_len;
459 switch (marker)
460 {
461 case SOF:
462 if (parse_SOF(priv, stream) < 0)
463 return -1;
464 break;
465 case DQT:
466 if (parse_DQT(priv, stream) < 0)
467 return -1;
468 break;
469 case SOS:
470 if (parse_SOS(priv, stream) < 0)
471 return -1;
472 sos_marker_found = 1;
473 break;
474 case DHT:
475 if (parse_DHT(priv, stream) < 0)
476 return -1;
477 dht_marker_found = 1;
478 break;
479 case DRI:
480 if (parse_DRI(priv, stream) < 0)
481 return -1;
482 break;
483 default:
484 trace("> Unknown marker %2.2x\n", marker);
485 break;
486 }
488 stream = next_chunck;
489 }
491 if (!dht_marker_found) {
492 trace("No Huffman table loaded, using the default one\n");
493 build_default_huffman_tables(priv);
494 }
496 #ifdef SANITY_CHECK
497 if ( (priv->component_infos[cY].Hfactor < priv->component_infos[cCb].Hfactor)
498 || (priv->component_infos[cY].Hfactor < priv->component_infos[cCr].Hfactor))
499 error("Horizontal sampling factor for Y should be greater than horitontal sampling factor for Cb or Cr\n");
500 if ( (priv->component_infos[cY].Vfactor < priv->component_infos[cCb].Vfactor)
501 || (priv->component_infos[cY].Vfactor < priv->component_infos[cCr].Vfactor))
502 error("Vertical sampling factor for Y should be greater than vertical sampling factor for Cb or Cr\n");
503 if ( (priv->component_infos[cCb].Hfactor!=1)
504 || (priv->component_infos[cCr].Hfactor!=1)
505 || (priv->component_infos[cCb].Vfactor!=1)
506 || (priv->component_infos[cCr].Vfactor!=1))
507 error("Sampling other than 1x1 for Cr and Cb is not supported");
508 #endif
510 return 0;
511 }
