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