]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/jpeg6/jdmarker.cpp
transfer from internal tree r5311 branches/1.4-gpl
[xonotic/netradiant.git] / libs / jpeg6 / jdmarker.cpp
1 /*\r
2  * jdmarker.c\r
3  *\r
4  * Copyright (C) 1991-1995, Thomas G. Lane.\r
5  * This file is part of the Independent JPEG Group's software.\r
6  * For conditions of distribution and use, see the accompanying README file.\r
7  *\r
8  * This file contains routines to decode JPEG datastream markers.\r
9  * Most of the complexity arises from our desire to support input\r
10  * suspension: if not all of the data for a marker is available,\r
11  * we must exit back to the application.  On resumption, we reprocess\r
12  * the marker.\r
13  */\r
14 \r
15 #define JPEG_INTERNALS\r
16 #include "jinclude.h"\r
17 #include "radiant_jpeglib.h"\r
18 \r
19 \r
20 typedef enum {                  /* JPEG marker codes */\r
21   M_SOF0  = 0xc0,\r
22   M_SOF1  = 0xc1,\r
23   M_SOF2  = 0xc2,\r
24   M_SOF3  = 0xc3,\r
25   \r
26   M_SOF5  = 0xc5,\r
27   M_SOF6  = 0xc6,\r
28   M_SOF7  = 0xc7,\r
29   \r
30   M_JPG   = 0xc8,\r
31   M_SOF9  = 0xc9,\r
32   M_SOF10 = 0xca,\r
33   M_SOF11 = 0xcb,\r
34   \r
35   M_SOF13 = 0xcd,\r
36   M_SOF14 = 0xce,\r
37   M_SOF15 = 0xcf,\r
38   \r
39   M_DHT   = 0xc4,\r
40   \r
41   M_DAC   = 0xcc,\r
42   \r
43   M_RST0  = 0xd0,\r
44   M_RST1  = 0xd1,\r
45   M_RST2  = 0xd2,\r
46   M_RST3  = 0xd3,\r
47   M_RST4  = 0xd4,\r
48   M_RST5  = 0xd5,\r
49   M_RST6  = 0xd6,\r
50   M_RST7  = 0xd7,\r
51   \r
52   M_SOI   = 0xd8,\r
53   M_EOI   = 0xd9,\r
54   M_SOS   = 0xda,\r
55   M_DQT   = 0xdb,\r
56   M_DNL   = 0xdc,\r
57   M_DRI   = 0xdd,\r
58   M_DHP   = 0xde,\r
59   M_EXP   = 0xdf,\r
60   \r
61   M_APP0  = 0xe0,\r
62   M_APP1  = 0xe1,\r
63   M_APP2  = 0xe2,\r
64   M_APP3  = 0xe3,\r
65   M_APP4  = 0xe4,\r
66   M_APP5  = 0xe5,\r
67   M_APP6  = 0xe6,\r
68   M_APP7  = 0xe7,\r
69   M_APP8  = 0xe8,\r
70   M_APP9  = 0xe9,\r
71   M_APP10 = 0xea,\r
72   M_APP11 = 0xeb,\r
73   M_APP12 = 0xec,\r
74   M_APP13 = 0xed,\r
75   M_APP14 = 0xee,\r
76   M_APP15 = 0xef,\r
77   \r
78   M_JPG0  = 0xf0,\r
79   M_JPG13 = 0xfd,\r
80   M_COM   = 0xfe,\r
81   \r
82   M_TEM   = 0x01,\r
83   \r
84   M_ERROR = 0x100\r
85 } JPEG_MARKER;\r
86 \r
87 \r
88 /*\r
89  * Macros for fetching data from the data source module.\r
90  *\r
91  * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect\r
92  * the current restart point; we update them only when we have reached a\r
93  * suitable place to restart if a suspension occurs.\r
94  */\r
95 \r
96 /* Declare and initialize local copies of input pointer/count */\r
97 #define INPUT_VARS(cinfo)  \\r
98         struct jpeg_source_mgr * datasrc = (cinfo)->src;  \\r
99         const JOCTET * next_input_byte = datasrc->next_input_byte;  \\r
100         size_t bytes_in_buffer = datasrc->bytes_in_buffer\r
101 \r
102 /* Unload the local copies --- do this only at a restart boundary */\r
103 #define INPUT_SYNC(cinfo)  \\r
104         ( datasrc->next_input_byte = next_input_byte,  \\r
105           datasrc->bytes_in_buffer = bytes_in_buffer )\r
106 \r
107 /* Reload the local copies --- seldom used except in MAKE_BYTE_AVAIL */\r
108 #define INPUT_RELOAD(cinfo)  \\r
109         ( next_input_byte = datasrc->next_input_byte,  \\r
110           bytes_in_buffer = datasrc->bytes_in_buffer )\r
111 \r
112 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.\r
113  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,\r
114  * but we must reload the local copies after a successful fill.\r
115  */\r
116 #define MAKE_BYTE_AVAIL(cinfo,action)  \\r
117         if (bytes_in_buffer == 0) {  \\r
118           if (! (*datasrc->fill_input_buffer) (cinfo))  \\r
119             { action; }  \\r
120           INPUT_RELOAD(cinfo);  \\r
121         }  \\r
122         bytes_in_buffer--\r
123 \r
124 /* Read a byte into variable V.\r
125  * If must suspend, take the specified action (typically "return FALSE").\r
126  */\r
127 #define INPUT_BYTE(cinfo,V,action)  \\r
128         MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \\r
129                   V = GETJOCTET(*next_input_byte++); )\r
130 \r
131 /* As above, but read two bytes interpreted as an unsigned 16-bit integer.\r
132  * V should be declared unsigned int or perhaps INT32.\r
133  */\r
134 #define INPUT_2BYTES(cinfo,V,action)  \\r
135         MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \\r
136                   V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \\r
137                   MAKE_BYTE_AVAIL(cinfo,action); \\r
138                   V += GETJOCTET(*next_input_byte++); )\r
139 \r
140 \r
141 /*\r
142  * Routines to process JPEG markers.\r
143  *\r
144  * Entry condition: JPEG marker itself has been read and its code saved\r
145  *   in cinfo->unread_marker; input restart point is just after the marker.\r
146  *\r
147  * Exit: if return TRUE, have read and processed any parameters, and have\r
148  *   updated the restart point to point after the parameters.\r
149  *   If return FALSE, was forced to suspend before reaching end of\r
150  *   marker parameters; restart point has not been moved.  Same routine\r
151  *   will be called again after application supplies more input data.\r
152  *\r
153  * This approach to suspension assumes that all of a marker's parameters can\r
154  * fit into a single input bufferload.  This should hold for "normal"\r
155  * markers.  Some COM/APPn markers might have large parameter segments,\r
156  * but we use skip_input_data to get past those, and thereby put the problem\r
157  * on the source manager's shoulders.\r
158  *\r
159  * Note that we don't bother to avoid duplicate trace messages if a\r
160  * suspension occurs within marker parameters.  Other side effects\r
161  * require more care.\r
162  */\r
163 \r
164 \r
165 LOCAL boolean\r
166 get_soi (j_decompress_ptr cinfo)\r
167 /* Process an SOI marker */\r
168 {\r
169   int i;\r
170   \r
171   TRACEMS(cinfo, 1, JTRC_SOI);\r
172 \r
173   if (cinfo->marker->saw_SOI)\r
174     ERREXIT(cinfo, JERR_SOI_DUPLICATE);\r
175 \r
176   /* Reset all parameters that are defined to be reset by SOI */\r
177 \r
178   for (i = 0; i < NUM_ARITH_TBLS; i++) {\r
179     cinfo->arith_dc_L[i] = 0;\r
180     cinfo->arith_dc_U[i] = 1;\r
181     cinfo->arith_ac_K[i] = 5;\r
182   }\r
183   cinfo->restart_interval = 0;\r
184 \r
185   /* Set initial assumptions for colorspace etc */\r
186 \r
187   cinfo->jpeg_color_space = JCS_UNKNOWN;\r
188   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */\r
189 \r
190   cinfo->saw_JFIF_marker = FALSE;\r
191   cinfo->density_unit = 0;      /* set default JFIF APP0 values */\r
192   cinfo->X_density = 1;\r
193   cinfo->Y_density = 1;\r
194   cinfo->saw_Adobe_marker = FALSE;\r
195   cinfo->Adobe_transform = 0;\r
196 \r
197   cinfo->marker->saw_SOI = TRUE;\r
198 \r
199   return TRUE;\r
200 }\r
201 \r
202 \r
203 LOCAL boolean\r
204 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)\r
205 /* Process a SOFn marker */\r
206 {\r
207   INT32 length;\r
208   int c, ci;\r
209   jpeg_component_info * compptr;\r
210   INPUT_VARS(cinfo);\r
211 \r
212   cinfo->progressive_mode = is_prog;\r
213   cinfo->arith_code = is_arith;\r
214 \r
215   INPUT_2BYTES(cinfo, length, return FALSE);\r
216 \r
217   INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);\r
218   INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);\r
219   INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);\r
220   INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);\r
221 \r
222   length -= 8;\r
223 \r
224   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,\r
225            (int) cinfo->image_width, (int) cinfo->image_height,\r
226            cinfo->num_components);\r
227 \r
228   if (cinfo->marker->saw_SOF)\r
229     ERREXIT(cinfo, JERR_SOF_DUPLICATE);\r
230 \r
231   /* We don't support files in which the image height is initially specified */\r
232   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */\r
233   /* might as well have a general sanity check. */\r
234   if (cinfo->image_height <= 0 || cinfo->image_width <= 0\r
235       || cinfo->num_components <= 0)\r
236     ERREXIT(cinfo, JERR_EMPTY_IMAGE);\r
237 \r
238   if (length != (cinfo->num_components * 3))\r
239     ERREXIT(cinfo, JERR_BAD_LENGTH);\r
240 \r
241   if (cinfo->comp_info == NULL) /* do only once, even if suspend */\r
242     cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)\r
243                         ((j_common_ptr) cinfo, JPOOL_IMAGE,\r
244                          cinfo->num_components * SIZEOF(jpeg_component_info));\r
245   \r
246   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
247        ci++, compptr++) {\r
248     compptr->component_index = ci;\r
249     INPUT_BYTE(cinfo, compptr->component_id, return FALSE);\r
250     INPUT_BYTE(cinfo, c, return FALSE);\r
251     compptr->h_samp_factor = (c >> 4) & 15;\r
252     compptr->v_samp_factor = (c     ) & 15;\r
253     INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);\r
254 \r
255     TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,\r
256              compptr->component_id, compptr->h_samp_factor,\r
257              compptr->v_samp_factor, compptr->quant_tbl_no);\r
258   }\r
259 \r
260   cinfo->marker->saw_SOF = TRUE;\r
261 \r
262   INPUT_SYNC(cinfo);\r
263   return TRUE;\r
264 }\r
265 \r
266 \r
267 LOCAL boolean\r
268 get_sos (j_decompress_ptr cinfo)\r
269 /* Process a SOS marker */\r
270 {\r
271   INT32 length;\r
272   int i, ci, n, c, cc;\r
273   jpeg_component_info * compptr;\r
274   INPUT_VARS(cinfo);\r
275 \r
276   if (! cinfo->marker->saw_SOF)\r
277     ERREXIT(cinfo, JERR_SOS_NO_SOF);\r
278 \r
279   INPUT_2BYTES(cinfo, length, return FALSE);\r
280 \r
281   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */\r
282 \r
283   if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)\r
284     ERREXIT(cinfo, JERR_BAD_LENGTH);\r
285 \r
286   TRACEMS1(cinfo, 1, JTRC_SOS, n);\r
287 \r
288   cinfo->comps_in_scan = n;\r
289 \r
290   /* Collect the component-spec parameters */\r
291 \r
292   for (i = 0; i < n; i++) {\r
293     INPUT_BYTE(cinfo, cc, return FALSE);\r
294     INPUT_BYTE(cinfo, c, return FALSE);\r
295     \r
296     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;\r
297          ci++, compptr++) {\r
298       if (cc == compptr->component_id)\r
299         goto id_found;\r
300     }\r
301 \r
302     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);\r
303 \r
304   id_found:\r
305 \r
306     cinfo->cur_comp_info[i] = compptr;\r
307     compptr->dc_tbl_no = (c >> 4) & 15;\r
308     compptr->ac_tbl_no = (c     ) & 15;\r
309     \r
310     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,\r
311              compptr->dc_tbl_no, compptr->ac_tbl_no);\r
312   }\r
313 \r
314   /* Collect the additional scan parameters Ss, Se, Ah/Al. */\r
315   INPUT_BYTE(cinfo, c, return FALSE);\r
316   cinfo->Ss = c;\r
317   INPUT_BYTE(cinfo, c, return FALSE);\r
318   cinfo->Se = c;\r
319   INPUT_BYTE(cinfo, c, return FALSE);\r
320   cinfo->Ah = (c >> 4) & 15;\r
321   cinfo->Al = (c     ) & 15;\r
322 \r
323   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,\r
324            cinfo->Ah, cinfo->Al);\r
325 \r
326   /* Prepare to scan data & restart markers */\r
327   cinfo->marker->next_restart_num = 0;\r
328 \r
329   /* Count another SOS marker */\r
330   cinfo->input_scan_number++;\r
331 \r
332   INPUT_SYNC(cinfo);\r
333   return TRUE;\r
334 }\r
335 \r
336 \r
337 METHODDEF boolean\r
338 get_app0 (j_decompress_ptr cinfo)\r
339 /* Process an APP0 marker */\r
340 {\r
341 #define JFIF_LEN 14\r
342   INT32 length;\r
343   UINT8 b[JFIF_LEN];\r
344   int buffp;\r
345   INPUT_VARS(cinfo);\r
346 \r
347   INPUT_2BYTES(cinfo, length, return FALSE);\r
348   length -= 2;\r
349 \r
350   /* See if a JFIF APP0 marker is present */\r
351 \r
352   if (length >= JFIF_LEN) {\r
353     for (buffp = 0; buffp < JFIF_LEN; buffp++)\r
354       INPUT_BYTE(cinfo, b[buffp], return FALSE);\r
355     length -= JFIF_LEN;\r
356 \r
357     if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {\r
358       /* Found JFIF APP0 marker: check version */\r
359       /* Major version must be 1, anything else signals an incompatible change.\r
360        * We used to treat this as an error, but now it's a nonfatal warning,\r
361        * because some bozo at Hijaak couldn't read the spec.\r
362        * Minor version should be 0..2, but process anyway if newer.\r
363        */\r
364       if (b[5] != 1)\r
365         WARNMS2(cinfo, JWRN_JFIF_MAJOR, b[5], b[6]);\r
366       else if (b[6] > 2)\r
367         TRACEMS2(cinfo, 1, JTRC_JFIF_MINOR, b[5], b[6]);\r
368       /* Save info */\r
369       cinfo->saw_JFIF_marker = TRUE;\r
370       cinfo->density_unit = b[7];\r
371       cinfo->X_density = (b[8] << 8) + b[9];\r
372       cinfo->Y_density = (b[10] << 8) + b[11];\r
373       TRACEMS3(cinfo, 1, JTRC_JFIF,\r
374                cinfo->X_density, cinfo->Y_density, cinfo->density_unit);\r
375       if (b[12] | b[13])\r
376         TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL, b[12], b[13]);\r
377       if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))\r
378         TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) length);\r
379     } else {\r
380       /* Start of APP0 does not match "JFIF" */\r
381       TRACEMS1(cinfo, 1, JTRC_APP0, (int) length + JFIF_LEN);\r
382     }\r
383   } else {\r
384     /* Too short to be JFIF marker */\r
385     TRACEMS1(cinfo, 1, JTRC_APP0, (int) length);\r
386   }\r
387 \r
388   INPUT_SYNC(cinfo);\r
389   if (length > 0)               /* skip any remaining data -- could be lots */\r
390     (*cinfo->src->skip_input_data) (cinfo, (long) length);\r
391 \r
392   return TRUE;\r
393 }\r
394 \r
395 \r
396 METHODDEF boolean\r
397 get_app14 (j_decompress_ptr cinfo)\r
398 /* Process an APP14 marker */\r
399 {\r
400 #define ADOBE_LEN 12\r
401   INT32 length;\r
402   UINT8 b[ADOBE_LEN];\r
403   int buffp;\r
404   unsigned int version, flags0, flags1, transform;\r
405   INPUT_VARS(cinfo);\r
406 \r
407   INPUT_2BYTES(cinfo, length, return FALSE);\r
408   length -= 2;\r
409 \r
410   /* See if an Adobe APP14 marker is present */\r
411 \r
412   if (length >= ADOBE_LEN) {\r
413     for (buffp = 0; buffp < ADOBE_LEN; buffp++)\r
414       INPUT_BYTE(cinfo, b[buffp], return FALSE);\r
415     length -= ADOBE_LEN;\r
416 \r
417     if (b[0]==0x41 && b[1]==0x64 && b[2]==0x6F && b[3]==0x62 && b[4]==0x65) {\r
418       /* Found Adobe APP14 marker */\r
419       version = (b[5] << 8) + b[6];\r
420       flags0 = (b[7] << 8) + b[8];\r
421       flags1 = (b[9] << 8) + b[10];\r
422       transform = b[11];\r
423       TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);\r
424       cinfo->saw_Adobe_marker = TRUE;\r
425       cinfo->Adobe_transform = (UINT8) transform;\r
426     } else {\r
427       /* Start of APP14 does not match "Adobe" */\r
428       TRACEMS1(cinfo, 1, JTRC_APP14, (int) length + ADOBE_LEN);\r
429     }\r
430   } else {\r
431     /* Too short to be Adobe marker */\r
432     TRACEMS1(cinfo, 1, JTRC_APP14, (int) length);\r
433   }\r
434 \r
435   INPUT_SYNC(cinfo);\r
436   if (length > 0)               /* skip any remaining data -- could be lots */\r
437     (*cinfo->src->skip_input_data) (cinfo, (long) length);\r
438 \r
439   return TRUE;\r
440 }\r
441 \r
442 \r
443 LOCAL boolean\r
444 get_dac (j_decompress_ptr cinfo)\r
445 /* Process a DAC marker */\r
446 {\r
447   INT32 length;\r
448   int index, val;\r
449   INPUT_VARS(cinfo);\r
450 \r
451   INPUT_2BYTES(cinfo, length, return FALSE);\r
452   length -= 2;\r
453   \r
454   while (length > 0) {\r
455     INPUT_BYTE(cinfo, index, return FALSE);\r
456     INPUT_BYTE(cinfo, val, return FALSE);\r
457 \r
458     length -= 2;\r
459 \r
460     TRACEMS2(cinfo, 1, JTRC_DAC, index, val);\r
461 \r
462     if (index < 0 || index >= (2*NUM_ARITH_TBLS))\r
463       ERREXIT1(cinfo, JERR_DAC_INDEX, index);\r
464 \r
465     if (index >= NUM_ARITH_TBLS) { /* define AC table */\r
466       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;\r
467     } else {                    /* define DC table */\r
468       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);\r
469       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);\r
470       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])\r
471         ERREXIT1(cinfo, JERR_DAC_VALUE, val);\r
472     }\r
473   }\r
474 \r
475   INPUT_SYNC(cinfo);\r
476   return TRUE;\r
477 }\r
478 \r
479 \r
480 LOCAL boolean\r
481 get_dht (j_decompress_ptr cinfo)\r
482 /* Process a DHT marker */\r
483 {\r
484   INT32 length;\r
485   UINT8 bits[17];\r
486   UINT8 huffval[256];\r
487   int i, index, count;\r
488   JHUFF_TBL **htblptr;\r
489   INPUT_VARS(cinfo);\r
490 \r
491   INPUT_2BYTES(cinfo, length, return FALSE);\r
492   length -= 2;\r
493   \r
494   while (length > 0) {\r
495     INPUT_BYTE(cinfo, index, return FALSE);\r
496 \r
497     TRACEMS1(cinfo, 1, JTRC_DHT, index);\r
498       \r
499     bits[0] = 0;\r
500     count = 0;\r
501     for (i = 1; i <= 16; i++) {\r
502       INPUT_BYTE(cinfo, bits[i], return FALSE);\r
503       count += bits[i];\r
504     }\r
505 \r
506     length -= 1 + 16;\r
507 \r
508     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,\r
509              bits[1], bits[2], bits[3], bits[4],\r
510              bits[5], bits[6], bits[7], bits[8]);\r
511     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,\r
512              bits[9], bits[10], bits[11], bits[12],\r
513              bits[13], bits[14], bits[15], bits[16]);\r
514 \r
515     if (count > 256 || ((INT32) count) > length)\r
516       ERREXIT(cinfo, JERR_DHT_COUNTS);\r
517 \r
518     for (i = 0; i < count; i++)\r
519       INPUT_BYTE(cinfo, huffval[i], return FALSE);\r
520 \r
521     length -= count;\r
522 \r
523     if (index & 0x10) {         /* AC table definition */\r
524       index -= 0x10;\r
525       htblptr = &cinfo->ac_huff_tbl_ptrs[index];\r
526     } else {                    /* DC table definition */\r
527       htblptr = &cinfo->dc_huff_tbl_ptrs[index];\r
528     }\r
529 \r
530     if (index < 0 || index >= NUM_HUFF_TBLS)\r
531       ERREXIT1(cinfo, JERR_DHT_INDEX, index);\r
532 \r
533     if (*htblptr == NULL)\r
534       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);\r
535   \r
536     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));\r
537     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));\r
538   }\r
539 \r
540   INPUT_SYNC(cinfo);\r
541   return TRUE;\r
542 }\r
543 \r
544 \r
545 LOCAL boolean\r
546 get_dqt (j_decompress_ptr cinfo)\r
547 /* Process a DQT marker */\r
548 {\r
549   INT32 length;\r
550   int n, i, prec;\r
551   unsigned int tmp;\r
552   JQUANT_TBL *quant_ptr;\r
553   INPUT_VARS(cinfo);\r
554 \r
555   INPUT_2BYTES(cinfo, length, return FALSE);\r
556   length -= 2;\r
557 \r
558   while (length > 0) {\r
559     INPUT_BYTE(cinfo, n, return FALSE);\r
560     prec = n >> 4;\r
561     n &= 0x0F;\r
562 \r
563     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);\r
564 \r
565     if (n >= NUM_QUANT_TBLS)\r
566       ERREXIT1(cinfo, JERR_DQT_INDEX, n);\r
567       \r
568     if (cinfo->quant_tbl_ptrs[n] == NULL)\r
569       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);\r
570     quant_ptr = cinfo->quant_tbl_ptrs[n];\r
571 \r
572     for (i = 0; i < DCTSIZE2; i++) {\r
573       if (prec)\r
574         INPUT_2BYTES(cinfo, tmp, return FALSE);\r
575       else\r
576         INPUT_BYTE(cinfo, tmp, return FALSE);\r
577       quant_ptr->quantval[i] = (UINT16) tmp;\r
578     }\r
579 \r
580     for (i = 0; i < DCTSIZE2; i += 8) {\r
581       TRACEMS8(cinfo, 2, JTRC_QUANTVALS,\r
582                quant_ptr->quantval[i  ], quant_ptr->quantval[i+1],\r
583                quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],\r
584                quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],\r
585                quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);\r
586     }\r
587 \r
588     length -= DCTSIZE2+1;\r
589     if (prec) length -= DCTSIZE2;\r
590   }\r
591 \r
592   INPUT_SYNC(cinfo);\r
593   return TRUE;\r
594 }\r
595 \r
596 \r
597 LOCAL boolean\r
598 get_dri (j_decompress_ptr cinfo)\r
599 /* Process a DRI marker */\r
600 {\r
601   INT32 length;\r
602   unsigned int tmp;\r
603   INPUT_VARS(cinfo);\r
604 \r
605   INPUT_2BYTES(cinfo, length, return FALSE);\r
606   \r
607   if (length != 4)\r
608     ERREXIT(cinfo, JERR_BAD_LENGTH);\r
609 \r
610   INPUT_2BYTES(cinfo, tmp, return FALSE);\r
611 \r
612   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);\r
613 \r
614   cinfo->restart_interval = tmp;\r
615 \r
616   INPUT_SYNC(cinfo);\r
617   return TRUE;\r
618 }\r
619 \r
620 \r
621 METHODDEF boolean\r
622 skip_variable (j_decompress_ptr cinfo)\r
623 /* Skip over an unknown or uninteresting variable-length marker */\r
624 {\r
625   INT32 length;\r
626   INPUT_VARS(cinfo);\r
627 \r
628   INPUT_2BYTES(cinfo, length, return FALSE);\r
629   \r
630   TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);\r
631 \r
632   INPUT_SYNC(cinfo);            /* do before skip_input_data */\r
633   (*cinfo->src->skip_input_data) (cinfo, (long) length - 2L);\r
634 \r
635   return TRUE;\r
636 }\r
637 \r
638 \r
639 /*\r
640  * Find the next JPEG marker, save it in cinfo->unread_marker.\r
641  * Returns FALSE if had to suspend before reaching a marker;\r
642  * in that case cinfo->unread_marker is unchanged.\r
643  *\r
644  * Note that the result might not be a valid marker code,\r
645  * but it will never be 0 or FF.\r
646  */\r
647 \r
648 LOCAL boolean\r
649 next_marker (j_decompress_ptr cinfo)\r
650 {\r
651   int c;\r
652   INPUT_VARS(cinfo);\r
653 \r
654   for (;;) {\r
655     INPUT_BYTE(cinfo, c, return FALSE);\r
656     /* Skip any non-FF bytes.\r
657      * This may look a bit inefficient, but it will not occur in a valid file.\r
658      * We sync after each discarded byte so that a suspending data source\r
659      * can discard the byte from its buffer.\r
660      */\r
661     while (c != 0xFF) {\r
662       cinfo->marker->discarded_bytes++;\r
663       INPUT_SYNC(cinfo);\r
664       INPUT_BYTE(cinfo, c, return FALSE);\r
665     }\r
666     /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as\r
667      * pad bytes, so don't count them in discarded_bytes.  We assume there\r
668      * will not be so many consecutive FF bytes as to overflow a suspending\r
669      * data source's input buffer.\r
670      */\r
671     do {\r
672       INPUT_BYTE(cinfo, c, return FALSE);\r
673     } while (c == 0xFF);\r
674     if (c != 0)\r
675       break;                    /* found a valid marker, exit loop */\r
676     /* Reach here if we found a stuffed-zero data sequence (FF/00).\r
677      * Discard it and loop back to try again.\r
678      */\r
679     cinfo->marker->discarded_bytes += 2;\r
680     INPUT_SYNC(cinfo);\r
681   }\r
682 \r
683   if (cinfo->marker->discarded_bytes != 0) {\r
684     WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);\r
685     cinfo->marker->discarded_bytes = 0;\r
686   }\r
687 \r
688   cinfo->unread_marker = c;\r
689 \r
690   INPUT_SYNC(cinfo);\r
691   return TRUE;\r
692 }\r
693 \r
694 \r
695 LOCAL boolean\r
696 first_marker (j_decompress_ptr cinfo)\r
697 /* Like next_marker, but used to obtain the initial SOI marker. */\r
698 /* For this marker, we do not allow preceding garbage or fill; otherwise,\r
699  * we might well scan an entire input file before realizing it ain't JPEG.\r
700  * If an application wants to process non-JFIF files, it must seek to the\r
701  * SOI before calling the JPEG library.\r
702  */\r
703 {\r
704   int c, c2;\r
705   INPUT_VARS(cinfo);\r
706 \r
707   INPUT_BYTE(cinfo, c, return FALSE);\r
708   INPUT_BYTE(cinfo, c2, return FALSE);\r
709   if (c != 0xFF || c2 != (int) M_SOI)\r
710     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);\r
711 \r
712   cinfo->unread_marker = c2;\r
713 \r
714   INPUT_SYNC(cinfo);\r
715   return TRUE;\r
716 }\r
717 \r
718 \r
719 /*\r
720  * Read markers until SOS or EOI.\r
721  *\r
722  * Returns same codes as are defined for jpeg_consume_input:\r
723  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.\r
724  */\r
725 \r
726 METHODDEF int\r
727 read_markers (j_decompress_ptr cinfo)\r
728 {\r
729   /* Outer loop repeats once for each marker. */\r
730   for (;;) {\r
731     /* Collect the marker proper, unless we already did. */\r
732     /* NB: first_marker() enforces the requirement that SOI appear first. */\r
733     if (cinfo->unread_marker == 0) {\r
734       if (! cinfo->marker->saw_SOI) {\r
735         if (! first_marker(cinfo))\r
736           return JPEG_SUSPENDED;\r
737       } else {\r
738         if (! next_marker(cinfo))\r
739           return JPEG_SUSPENDED;\r
740       }\r
741     }\r
742     /* At this point cinfo->unread_marker contains the marker code and the\r
743      * input point is just past the marker proper, but before any parameters.\r
744      * A suspension will cause us to return with this state still true.\r
745      */\r
746     switch (cinfo->unread_marker) {\r
747     case M_SOI:\r
748       if (! get_soi(cinfo))\r
749         return JPEG_SUSPENDED;\r
750       break;\r
751 \r
752     case M_SOF0:                /* Baseline */\r
753     case M_SOF1:                /* Extended sequential, Huffman */\r
754       if (! get_sof(cinfo, FALSE, FALSE))\r
755         return JPEG_SUSPENDED;\r
756       break;\r
757 \r
758     case M_SOF2:                /* Progressive, Huffman */\r
759       if (! get_sof(cinfo, TRUE, FALSE))\r
760         return JPEG_SUSPENDED;\r
761       break;\r
762 \r
763     case M_SOF9:                /* Extended sequential, arithmetic */\r
764       if (! get_sof(cinfo, FALSE, TRUE))\r
765         return JPEG_SUSPENDED;\r
766       break;\r
767 \r
768     case M_SOF10:               /* Progressive, arithmetic */\r
769       if (! get_sof(cinfo, TRUE, TRUE))\r
770         return JPEG_SUSPENDED;\r
771       break;\r
772 \r
773     /* Currently unsupported SOFn types */\r
774     case M_SOF3:                /* Lossless, Huffman */\r
775     case M_SOF5:                /* Differential sequential, Huffman */\r
776     case M_SOF6:                /* Differential progressive, Huffman */\r
777     case M_SOF7:                /* Differential lossless, Huffman */\r
778     case M_JPG:                 /* Reserved for JPEG extensions */\r
779     case M_SOF11:               /* Lossless, arithmetic */\r
780     case M_SOF13:               /* Differential sequential, arithmetic */\r
781     case M_SOF14:               /* Differential progressive, arithmetic */\r
782     case M_SOF15:               /* Differential lossless, arithmetic */\r
783       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);\r
784       break;\r
785 \r
786     case M_SOS:\r
787       if (! get_sos(cinfo))\r
788         return JPEG_SUSPENDED;\r
789       cinfo->unread_marker = 0; /* processed the marker */\r
790       return JPEG_REACHED_SOS;\r
791     \r
792     case M_EOI:\r
793       TRACEMS(cinfo, 1, JTRC_EOI);\r
794       cinfo->unread_marker = 0; /* processed the marker */\r
795       return JPEG_REACHED_EOI;\r
796       \r
797     case M_DAC:\r
798       if (! get_dac(cinfo))\r
799         return JPEG_SUSPENDED;\r
800       break;\r
801       \r
802     case M_DHT:\r
803       if (! get_dht(cinfo))\r
804         return JPEG_SUSPENDED;\r
805       break;\r
806       \r
807     case M_DQT:\r
808       if (! get_dqt(cinfo))\r
809         return JPEG_SUSPENDED;\r
810       break;\r
811       \r
812     case M_DRI:\r
813       if (! get_dri(cinfo))\r
814         return JPEG_SUSPENDED;\r
815       break;\r
816       \r
817     case M_APP0:\r
818     case M_APP1:\r
819     case M_APP2:\r
820     case M_APP3:\r
821     case M_APP4:\r
822     case M_APP5:\r
823     case M_APP6:\r
824     case M_APP7:\r
825     case M_APP8:\r
826     case M_APP9:\r
827     case M_APP10:\r
828     case M_APP11:\r
829     case M_APP12:\r
830     case M_APP13:\r
831     case M_APP14:\r
832     case M_APP15:\r
833       if (! (*cinfo->marker->process_APPn[cinfo->unread_marker - (int) M_APP0]) (cinfo))\r
834         return JPEG_SUSPENDED;\r
835       break;\r
836       \r
837     case M_COM:\r
838       if (! (*cinfo->marker->process_COM) (cinfo))\r
839         return JPEG_SUSPENDED;\r
840       break;\r
841 \r
842     case M_RST0:                /* these are all parameterless */\r
843     case M_RST1:\r
844     case M_RST2:\r
845     case M_RST3:\r
846     case M_RST4:\r
847     case M_RST5:\r
848     case M_RST6:\r
849     case M_RST7:\r
850     case M_TEM:\r
851       TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);\r
852       break;\r
853 \r
854     case M_DNL:                 /* Ignore DNL ... perhaps the wrong thing */\r
855       if (! skip_variable(cinfo))\r
856         return JPEG_SUSPENDED;\r
857       break;\r
858 \r
859     default:                    /* must be DHP, EXP, JPGn, or RESn */\r
860       /* For now, we treat the reserved markers as fatal errors since they are\r
861        * likely to be used to signal incompatible JPEG Part 3 extensions.\r
862        * Once the JPEG 3 version-number marker is well defined, this code\r
863        * ought to change!\r
864        */\r
865       ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);\r
866       break;\r
867     }\r
868     /* Successfully processed marker, so reset state variable */\r
869     cinfo->unread_marker = 0;\r
870   } /* end loop */\r
871 }\r
872 \r
873 \r
874 /*\r
875  * Read a restart marker, which is expected to appear next in the datastream;\r
876  * if the marker is not there, take appropriate recovery action.\r
877  * Returns FALSE if suspension is required.\r
878  *\r
879  * This is called by the entropy decoder after it has read an appropriate\r
880  * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder\r
881  * has already read a marker from the data source.  Under normal conditions\r
882  * cinfo->unread_marker will be reset to 0 before returning; if not reset,\r
883  * it holds a marker which the decoder will be unable to read past.\r
884  */\r
885 \r
886 METHODDEF boolean\r
887 read_restart_marker (j_decompress_ptr cinfo)\r
888 {\r
889   /* Obtain a marker unless we already did. */\r
890   /* Note that next_marker will complain if it skips any data. */\r
891   if (cinfo->unread_marker == 0) {\r
892     if (! next_marker(cinfo))\r
893       return FALSE;\r
894   }\r
895 \r
896   if (cinfo->unread_marker ==\r
897       ((int) M_RST0 + cinfo->marker->next_restart_num)) {\r
898     /* Normal case --- swallow the marker and let entropy decoder continue */\r
899     TRACEMS1(cinfo, 2, JTRC_RST, cinfo->marker->next_restart_num);\r
900     cinfo->unread_marker = 0;\r
901   } else {\r
902     /* Uh-oh, the restart markers have been messed up. */\r
903     /* Let the data source manager determine how to resync. */\r
904     if (! (*cinfo->src->resync_to_restart) (cinfo,\r
905                                             cinfo->marker->next_restart_num))\r
906       return FALSE;\r
907   }\r
908 \r
909   /* Update next-restart state */\r
910   cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;\r
911 \r
912   return TRUE;\r
913 }\r
914 \r
915 \r
916 /*\r
917  * This is the default resync_to_restart method for data source managers\r
918  * to use if they don't have any better approach.  Some data source managers\r
919  * may be able to back up, or may have additional knowledge about the data\r
920  * which permits a more intelligent recovery strategy; such managers would\r
921  * presumably supply their own resync method.\r
922  *\r
923  * read_restart_marker calls resync_to_restart if it finds a marker other than\r
924  * the restart marker it was expecting.  (This code is *not* used unless\r
925  * a nonzero restart interval has been declared.)  cinfo->unread_marker is\r
926  * the marker code actually found (might be anything, except 0 or FF).\r
927  * The desired restart marker number (0..7) is passed as a parameter.\r
928  * This routine is supposed to apply whatever error recovery strategy seems\r
929  * appropriate in order to position the input stream to the next data segment.\r
930  * Note that cinfo->unread_marker is treated as a marker appearing before\r
931  * the current data-source input point; usually it should be reset to zero\r
932  * before returning.\r
933  * Returns FALSE if suspension is required.\r
934  *\r
935  * This implementation is substantially constrained by wanting to treat the\r
936  * input as a data stream; this means we can't back up.  Therefore, we have\r
937  * only the following actions to work with:\r
938  *   1. Simply discard the marker and let the entropy decoder resume at next\r
939  *      byte of file.\r
940  *   2. Read forward until we find another marker, discarding intervening\r
941  *      data.  (In theory we could look ahead within the current bufferload,\r
942  *      without having to discard data if we don't find the desired marker.\r
943  *      This idea is not implemented here, in part because it makes behavior\r
944  *      dependent on buffer size and chance buffer-boundary positions.)\r
945  *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).\r
946  *      This will cause the entropy decoder to process an empty data segment,\r
947  *      inserting dummy zeroes, and then we will reprocess the marker.\r
948  *\r
949  * #2 is appropriate if we think the desired marker lies ahead, while #3 is\r
950  * appropriate if the found marker is a future restart marker (indicating\r
951  * that we have missed the desired restart marker, probably because it got\r
952  * corrupted).\r
953  * We apply #2 or #3 if the found marker is a restart marker no more than\r
954  * two counts behind or ahead of the expected one.  We also apply #2 if the\r
955  * found marker is not a legal JPEG marker code (it's certainly bogus data).\r
956  * If the found marker is a restart marker more than 2 counts away, we do #1\r
957  * (too much risk that the marker is erroneous; with luck we will be able to\r
958  * resync at some future point).\r
959  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from\r
960  * overrunning the end of a scan.  An implementation limited to single-scan\r
961  * files might find it better to apply #2 for markers other than EOI, since\r
962  * any other marker would have to be bogus data in that case.\r
963  */\r
964 \r
965 GLOBAL boolean\r
966 jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)\r
967 {\r
968   int marker = cinfo->unread_marker;\r
969   int action = 1;\r
970   \r
971   /* Always put up a warning. */\r
972   WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);\r
973   \r
974   /* Outer loop handles repeated decision after scanning forward. */\r
975   for (;;) {\r
976     if (marker < (int) M_SOF0)\r
977       action = 2;               /* invalid marker */\r
978     else if (marker < (int) M_RST0 || marker > (int) M_RST7)\r
979       action = 3;               /* valid non-restart marker */\r
980     else {\r
981       if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||\r
982           marker == ((int) M_RST0 + ((desired+2) & 7)))\r
983         action = 3;             /* one of the next two expected restarts */\r
984       else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||\r
985                marker == ((int) M_RST0 + ((desired-2) & 7)))\r
986         action = 2;             /* a prior restart, so advance */\r
987       else\r
988         action = 1;             /* desired restart or too far away */\r
989     }\r
990     TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);\r
991     switch (action) {\r
992     case 1:\r
993       /* Discard marker and let entropy decoder resume processing. */\r
994       cinfo->unread_marker = 0;\r
995       return TRUE;\r
996     case 2:\r
997       /* Scan to the next marker, and repeat the decision loop. */\r
998       if (! next_marker(cinfo))\r
999         return FALSE;\r
1000       marker = cinfo->unread_marker;\r
1001       break;\r
1002     case 3:\r
1003       /* Return without advancing past this marker. */\r
1004       /* Entropy decoder will be forced to process an empty segment. */\r
1005       return TRUE;\r
1006     }\r
1007   } /* end loop */\r
1008 }\r
1009 \r
1010 \r
1011 /*\r
1012  * Reset marker processing state to begin a fresh datastream.\r
1013  */\r
1014 \r
1015 METHODDEF void\r
1016 reset_marker_reader (j_decompress_ptr cinfo)\r
1017 {\r
1018   cinfo->comp_info = NULL;              /* until allocated by get_sof */\r
1019   cinfo->input_scan_number = 0;         /* no SOS seen yet */\r
1020   cinfo->unread_marker = 0;             /* no pending marker */\r
1021   cinfo->marker->saw_SOI = FALSE;       /* set internal state too */\r
1022   cinfo->marker->saw_SOF = FALSE;\r
1023   cinfo->marker->discarded_bytes = 0;\r
1024 }\r
1025 \r
1026 \r
1027 /*\r
1028  * Initialize the marker reader module.\r
1029  * This is called only once, when the decompression object is created.\r
1030  */\r
1031 \r
1032 GLOBAL void\r
1033 jinit_marker_reader (j_decompress_ptr cinfo)\r
1034 {\r
1035   int i;\r
1036 \r
1037   /* Create subobject in permanent pool */\r
1038   cinfo->marker = (struct jpeg_marker_reader *)\r
1039     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,\r
1040                                 SIZEOF(struct jpeg_marker_reader));\r
1041   /* Initialize method pointers */\r
1042   cinfo->marker->reset_marker_reader = reset_marker_reader;\r
1043   cinfo->marker->read_markers = read_markers;\r
1044   cinfo->marker->read_restart_marker = read_restart_marker;\r
1045   cinfo->marker->process_COM = skip_variable;\r
1046   for (i = 0; i < 16; i++)\r
1047     cinfo->marker->process_APPn[i] = skip_variable;\r
1048   cinfo->marker->process_APPn[0] = get_app0;\r
1049   cinfo->marker->process_APPn[14] = get_app14;\r
1050   /* Reset marker processing state */\r
1051   reset_marker_reader(cinfo);\r
1052 }\r