Main MRPT website > C++ reference for MRPT 1.5.7
jdmarker.cpp
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +---------------------------------------------------------------------------+ */
9 
10 #define JPEG_INTERNALS
11 #include "jinclude.h"
12 #include "mrpt_jpeglib.h"
13 
14 
15 typedef enum { /* JPEG marker codes */
16  M_SOF0 = 0xc0,
17  M_SOF1 = 0xc1,
18  M_SOF2 = 0xc2,
19  M_SOF3 = 0xc3,
20 
21  M_SOF5 = 0xc5,
22  M_SOF6 = 0xc6,
23  M_SOF7 = 0xc7,
24 
25  M_JPG = 0xc8,
26  M_SOF9 = 0xc9,
27  M_SOF10 = 0xca,
28  M_SOF11 = 0xcb,
29 
30  M_SOF13 = 0xcd,
31  M_SOF14 = 0xce,
32  M_SOF15 = 0xcf,
33 
34  M_DHT = 0xc4,
35 
36  M_DAC = 0xcc,
37 
38  M_RST0 = 0xd0,
39  M_RST1 = 0xd1,
40  M_RST2 = 0xd2,
41  M_RST3 = 0xd3,
42  M_RST4 = 0xd4,
43  M_RST5 = 0xd5,
44  M_RST6 = 0xd6,
45  M_RST7 = 0xd7,
46 
47  M_SOI = 0xd8,
48  M_EOI = 0xd9,
49  M_SOS = 0xda,
50  M_DQT = 0xdb,
51  M_DNL = 0xdc,
52  M_DRI = 0xdd,
53  M_DHP = 0xde,
54  M_EXP = 0xdf,
55 
56  M_APP0 = 0xe0,
57  M_APP1 = 0xe1,
58  M_APP2 = 0xe2,
59  M_APP3 = 0xe3,
60  M_APP4 = 0xe4,
61  M_APP5 = 0xe5,
62  M_APP6 = 0xe6,
63  M_APP7 = 0xe7,
64  M_APP8 = 0xe8,
65  M_APP9 = 0xe9,
66  M_APP10 = 0xea,
67  M_APP11 = 0xeb,
68  M_APP12 = 0xec,
69  M_APP13 = 0xed,
70  M_APP14 = 0xee,
71  M_APP15 = 0xef,
72 
73  M_JPG0 = 0xf0,
74  M_JPG13 = 0xfd,
75  M_COM = 0xfe,
76 
77  M_TEM = 0x01,
78 
79  M_ERROR = 0x100
80 } JPEG_MARKER;
81 
82 
83 /* Private state */
84 
85 typedef struct {
86  struct jpeg_marker_reader pub; /* public fields */
87 
88  /* Application-overridable marker processing methods */
89  jpeg_marker_parser_method process_COM;
90  jpeg_marker_parser_method process_APPn[16];
91 
92  /* Limit on marker data length to save for each marker type */
93  unsigned int length_limit_COM;
94  unsigned int length_limit_APPn[16];
95 
96  /* Status of COM/APPn marker saving */
97  jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
98  unsigned int bytes_read; /* data bytes read so far in marker */
99  /* Note: cur_marker is not linked into marker_list until it's all read. */
101 
103 
104 
105 /*
106  * Macros for fetching data from the data source module.
107  *
108  * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
109  * the current restart point; we update them only when we have reached a
110  * suitable place to restart if a suspension occurs.
111  */
112 
113 /* Declare and initialize local copies of input pointer/count */
114 #define INPUT_VARS(cinfo) \
115  struct jpeg_source_mgr * datasrc = (cinfo)->src; \
116  const JOCTET * next_input_byte = datasrc->next_input_byte; \
117  size_t bytes_in_buffer = datasrc->bytes_in_buffer
118 
119 /* Unload the local copies --- do this only at a restart boundary */
120 #define INPUT_SYNC(cinfo) \
121  ( datasrc->next_input_byte = next_input_byte, \
122  datasrc->bytes_in_buffer = bytes_in_buffer )
123 
124 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
125 #define INPUT_RELOAD(cinfo) \
126  ( next_input_byte = datasrc->next_input_byte, \
127  bytes_in_buffer = datasrc->bytes_in_buffer )
128 
129 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
130  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
131  * but we must reload the local copies after a successful fill.
132  */
133 #define MAKE_BYTE_AVAIL(cinfo,action) \
134  if (bytes_in_buffer == 0) { \
135  if (! (*datasrc->fill_input_buffer) (cinfo)) \
136  { action; } \
137  INPUT_RELOAD(cinfo); \
138  }
139 
140 /* Read a byte into variable V.
141  * If must suspend, take the specified action (typically "return FALSE").
142  */
143 #define INPUT_BYTE(cinfo,V,action) \
144  MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
145  bytes_in_buffer--; \
146  V = GETJOCTET(*next_input_byte++); )
147 
148 /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
149  * V should be declared unsigned int or perhaps INT32.
150  */
151 #define INPUT_2BYTES(cinfo,V,action) \
152  MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
153  bytes_in_buffer--; \
154  V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
155  MAKE_BYTE_AVAIL(cinfo,action); \
156  bytes_in_buffer--; \
157  V += GETJOCTET(*next_input_byte++); )
158 
159 
160 /*
161  * Routines to process JPEG markers.
162  *
163  * Entry condition: JPEG marker itself has been read and its code saved
164  * in cinfo->unread_marker; input restart point is just after the marker.
165  *
166  * Exit: if return TRUE, have read and processed any parameters, and have
167  * updated the restart point to point after the parameters.
168  * If return FALSE, was forced to suspend before reaching end of
169  * marker parameters; restart point has not been moved. Same routine
170  * will be called again after application supplies more input data.
171  *
172  * This approach to suspension assumes that all of a marker's parameters
173  * can fit into a single input bufferload. This should hold for "normal"
174  * markers. Some COM/APPn markers might have large parameter segments
175  * that might not fit. If we are simply dropping such a marker, we use
176  * skip_input_data to get past it, and thereby put the problem on the
177  * source manager's shoulders. If we are saving the marker's contents
178  * into memory, we use a slightly different convention: when forced to
179  * suspend, the marker processor updates the restart point to the end of
180  * what it's consumed (ie, the end of the buffer) before returning FALSE.
181  * On resumption, cinfo->unread_marker still contains the marker code,
182  * but the data source will point to the next chunk of marker data.
183  * The marker processor must retain internal state to deal with this.
184  *
185  * Note that we don't bother to avoid duplicate trace messages if a
186  * suspension occurs within marker parameters. Other side effects
187  * require more care.
188  */
189 
190 
191 LOCAL(boolean)
193 /* Process an SOI marker */
194 {
195  int i;
196 
197  TRACEMS(cinfo, 1, JTRC_SOI);
198 
199  if (cinfo->marker->saw_SOI)
200  ERREXIT(cinfo, JERR_SOI_DUPLICATE);
201 
202  /* Reset all parameters that are defined to be reset by SOI */
203 
204  for (i = 0; i < NUM_ARITH_TBLS; i++) {
205  cinfo->arith_dc_L[i] = 0;
206  cinfo->arith_dc_U[i] = 1;
207  cinfo->arith_ac_K[i] = 5;
208  }
209  cinfo->restart_interval = 0;
210 
211  /* Set initial assumptions for colorspace etc */
212 
213  cinfo->jpeg_color_space = JCS_UNKNOWN;
214  cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
215 
216  cinfo->saw_JFIF_marker = FALSE;
217  cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
218  cinfo->JFIF_minor_version = 1;
219  cinfo->density_unit = 0;
220  cinfo->X_density = 1;
221  cinfo->Y_density = 1;
222  cinfo->saw_Adobe_marker = FALSE;
223  cinfo->Adobe_transform = 0;
224 
225  cinfo->marker->saw_SOI = TRUE;
226 
227  return TRUE;
228 }
229 
230 
231 LOCAL(boolean)
232 get_sof (j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
233 /* Process a SOFn marker */
234 {
235  INT32 length;
236  int c, ci;
238  INPUT_VARS(cinfo);
239 
240  cinfo->progressive_mode = is_prog;
241  cinfo->arith_code = is_arith;
242 
243  INPUT_2BYTES(cinfo, length, return FALSE);
244 
245  INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
246  INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
247  INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
248  INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
249 
250  length -= 8;
251 
252  TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
253  (int) cinfo->image_width, (int) cinfo->image_height,
254  cinfo->num_components);
255 
256  if (cinfo->marker->saw_SOF)
257  ERREXIT(cinfo, JERR_SOF_DUPLICATE);
258 
259  /* We don't support files in which the image height is initially specified */
260  /* as 0 and is later redefined by DNL. As long as we have to check that, */
261  /* might as well have a general sanity check. */
262  if (cinfo->image_height <= 0 || cinfo->image_width <= 0
263  || cinfo->num_components <= 0)
264  ERREXIT(cinfo, JERR_EMPTY_IMAGE);
265 
266  if (length != (cinfo->num_components * 3))
267  ERREXIT(cinfo, JERR_BAD_LENGTH);
268 
269  if (cinfo->comp_info == NULL) /* do only once, even if suspend */
270  cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
271  ((j_common_ptr) cinfo, JPOOL_IMAGE,
272  cinfo->num_components * SIZEOF(jpeg_component_info));
273 
274  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
275  ci++, compptr++) {
276  compptr->component_index = ci;
277  INPUT_BYTE(cinfo, compptr->component_id, return FALSE);
278  INPUT_BYTE(cinfo, c, return FALSE);
279  compptr->h_samp_factor = (c >> 4) & 15;
280  compptr->v_samp_factor = (c ) & 15;
281  INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
282 
283  TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
286  }
287 
288  cinfo->marker->saw_SOF = TRUE;
289 
290  INPUT_SYNC(cinfo);
291  return TRUE;
292 }
293 
294 
295 LOCAL(boolean)
297 /* Process a SOS marker */
298 {
299  INT32 length;
300  int i, ci, n, c, cc;
302  INPUT_VARS(cinfo);
303 
304  if (! cinfo->marker->saw_SOF)
305  ERREXIT(cinfo, JERR_SOS_NO_SOF);
306 
307  INPUT_2BYTES(cinfo, length, return FALSE);
308 
309  INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
310 
311  TRACEMS1(cinfo, 1, JTRC_SOS, n);
312 
313  if (length != (n * 2 + 6) || n < 1 || n > MAX_COMPS_IN_SCAN)
314  ERREXIT(cinfo, JERR_BAD_LENGTH);
315 
316  cinfo->comps_in_scan = n;
317 
318  /* Collect the component-spec parameters */
319 
320  for (i = 0; i < n; i++) {
321  INPUT_BYTE(cinfo, cc, return FALSE);
322  INPUT_BYTE(cinfo, c, return FALSE);
323 
324  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
325  ci++, compptr++) {
326  if (cc == compptr->component_id)
327  goto id_found;
328  }
329 
330  ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, cc);
331 
332  id_found:
333 
334  cinfo->cur_comp_info[i] = compptr;
335  compptr->dc_tbl_no = (c >> 4) & 15;
336  compptr->ac_tbl_no = (c ) & 15;
337 
338  TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, cc,
340  }
341 
342  /* Collect the additional scan parameters Ss, Se, Ah/Al. */
343  INPUT_BYTE(cinfo, c, return FALSE);
344  cinfo->Ss = c;
345  INPUT_BYTE(cinfo, c, return FALSE);
346  cinfo->Se = c;
347  INPUT_BYTE(cinfo, c, return FALSE);
348  cinfo->Ah = (c >> 4) & 15;
349  cinfo->Al = (c ) & 15;
350 
351  TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
352  cinfo->Ah, cinfo->Al);
353 
354  /* Prepare to scan data & restart markers */
355  cinfo->marker->next_restart_num = 0;
356 
357  /* Count another SOS marker */
358  cinfo->input_scan_number++;
359 
360  INPUT_SYNC(cinfo);
361  return TRUE;
362 }
363 
364 
365 #ifdef D_ARITH_CODING_SUPPORTED
366 
367 LOCAL(boolean)
369 /* Process a DAC marker */
370 {
371  INT32 length;
372  int index, val;
373  INPUT_VARS(cinfo);
374 
375  INPUT_2BYTES(cinfo, length, return FALSE);
376  length -= 2;
377 
378  while (length > 0) {
379  INPUT_BYTE(cinfo, index, return FALSE);
380  INPUT_BYTE(cinfo, val, return FALSE);
381 
382  length -= 2;
383 
384  TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
385 
387  ERREXIT1(cinfo, JERR_DAC_INDEX, index);
388 
389  if (index >= NUM_ARITH_TBLS) { /* define AC table */
391  } else { /* define DC table */
392  cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
393  cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
394  if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
395  ERREXIT1(cinfo, JERR_DAC_VALUE, val);
396  }
397  }
398 
399  if (length != 0)
400  ERREXIT(cinfo, JERR_BAD_LENGTH);
401 
402  INPUT_SYNC(cinfo);
403  return TRUE;
404 }
405 
406 #else /* ! D_ARITH_CODING_SUPPORTED */
407 
408 #define get_dac(cinfo) skip_variable(cinfo)
409 
410 #endif /* D_ARITH_CODING_SUPPORTED */
411 
412 
413 LOCAL(boolean)
415 /* Process a DHT marker */
416 {
417  INT32 length;
418  UINT8 bits[17];
419  UINT8 huffval[256];
420  int i, index, count;
421  JHUFF_TBL **htblptr;
422  INPUT_VARS(cinfo);
423 
424  INPUT_2BYTES(cinfo, length, return FALSE);
425  length -= 2;
426 
427  while (length > 16) {
428  INPUT_BYTE(cinfo, index, return FALSE);
429 
430  TRACEMS1(cinfo, 1, JTRC_DHT, index);
431 
432  bits[0] = 0;
433  count = 0;
434  for (i = 1; i <= 16; i++) {
435  INPUT_BYTE(cinfo, bits[i], return FALSE);
436  count += bits[i];
437  }
438 
439  length -= 1 + 16;
440 
441  TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
442  bits[1], bits[2], bits[3], bits[4],
443  bits[5], bits[6], bits[7], bits[8]);
444  TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
445  bits[9], bits[10], bits[11], bits[12],
446  bits[13], bits[14], bits[15], bits[16]);
447 
448  /* Here we just do minimal validation of the counts to avoid walking
449  * off the end of our table space. jdhuff.c will check more carefully.
450  */
451  if (count > 256 || ((INT32) count) > length)
452  ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
453 
454  for (i = 0; i < count; i++)
455  INPUT_BYTE(cinfo, huffval[i], return FALSE);
456 
457  length -= count;
458 
459  if (index & 0x10) { /* AC table definition */
460  index -= 0x10;
461  htblptr = &cinfo->ac_huff_tbl_ptrs[index];
462  } else { /* DC table definition */
463  htblptr = &cinfo->dc_huff_tbl_ptrs[index];
464  }
465 
467  ERREXIT1(cinfo, JERR_DHT_INDEX, index);
468 
469  if (*htblptr == NULL)
470  *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
471 
472  MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
473  MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
474  }
475 
476  if (length != 0)
477  ERREXIT(cinfo, JERR_BAD_LENGTH);
478 
479  INPUT_SYNC(cinfo);
480  return TRUE;
481 }
482 
483 
484 LOCAL(boolean)
486 /* Process a DQT marker */
487 {
488  INT32 length;
489  int n, i, prec;
490  unsigned int tmp;
491  JQUANT_TBL *quant_ptr;
492  INPUT_VARS(cinfo);
493 
494  INPUT_2BYTES(cinfo, length, return FALSE);
495  length -= 2;
496 
497  while (length > 0) {
498  INPUT_BYTE(cinfo, n, return FALSE);
499  prec = n >> 4;
500  n &= 0x0F;
501 
502  TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
503 
504  if (n >= NUM_QUANT_TBLS)
505  ERREXIT1(cinfo, JERR_DQT_INDEX, n);
506 
507  if (cinfo->quant_tbl_ptrs[n] == NULL)
508  cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
509  quant_ptr = cinfo->quant_tbl_ptrs[n];
510 
511  for (i = 0; i < DCTSIZE2; i++) {
512  if (prec)
513  INPUT_2BYTES(cinfo, tmp, return FALSE);
514  else
515  INPUT_BYTE(cinfo, tmp, return FALSE);
516  /* We convert the zigzag-order table to natural array order. */
517  quant_ptr->quantval[jpeg_natural_order[i]] = (UINT16) tmp;
518  }
519 
520  if (cinfo->err->trace_level >= 2) {
521  for (i = 0; i < DCTSIZE2; i += 8) {
522  TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
523  quant_ptr->quantval[i], quant_ptr->quantval[i+1],
524  quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
525  quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
526  quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
527  }
528  }
529 
530  length -= DCTSIZE2+1;
531  if (prec) length -= DCTSIZE2;
532  }
533 
534  if (length != 0)
535  ERREXIT(cinfo, JERR_BAD_LENGTH);
536 
537  INPUT_SYNC(cinfo);
538  return TRUE;
539 }
540 
541 
542 LOCAL(boolean)
544 /* Process a DRI marker */
545 {
546  INT32 length;
547  unsigned int tmp;
548  INPUT_VARS(cinfo);
549 
550  INPUT_2BYTES(cinfo, length, return FALSE);
551 
552  if (length != 4)
553  ERREXIT(cinfo, JERR_BAD_LENGTH);
554 
555  INPUT_2BYTES(cinfo, tmp, return FALSE);
556 
557  TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
558 
559  cinfo->restart_interval = tmp;
560 
561  INPUT_SYNC(cinfo);
562  return TRUE;
563 }
564 
565 
566 /*
567  * Routines for processing APPn and COM markers.
568  * These are either saved in memory or discarded, per application request.
569  * APP0 and APP14 are specially checked to see if they are
570  * JFIF and Adobe markers, respectively.
571  */
572 
573 #define APP0_DATA_LEN 14 /* Length of interesting data in APP0 */
574 #define APP14_DATA_LEN 12 /* Length of interesting data in APP14 */
575 #define APPN_DATA_LEN 14 /* Must be the largest of the above!! */
576 
577 
578 LOCAL(void)
580  unsigned int datalen, INT32 remaining)
581 /* Examine first few bytes from an APP0.
582  * Take appropriate action if it is a JFIF marker.
583  * datalen is # of bytes at data[], remaining is length of rest of marker data.
584  */
585 {
586  INT32 totallen = (INT32) datalen + remaining;
587 
588  if (datalen >= APP0_DATA_LEN &&
589  GETJOCTET(data[0]) == 0x4A &&
590  GETJOCTET(data[1]) == 0x46 &&
591  GETJOCTET(data[2]) == 0x49 &&
592  GETJOCTET(data[3]) == 0x46 &&
593  GETJOCTET(data[4]) == 0) {
594  /* Found JFIF APP0 marker: save info */
595  cinfo->saw_JFIF_marker = TRUE;
596  cinfo->JFIF_major_version = GETJOCTET(data[5]);
597  cinfo->JFIF_minor_version = GETJOCTET(data[6]);
598  cinfo->density_unit = GETJOCTET(data[7]);
599  cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
600  cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
601  /* Check version.
602  * Major version must be 1, anything else signals an incompatible change.
603  * (We used to treat this as an error, but now it's a nonfatal warning,
604  * because some bozo at Hijaak couldn't read the spec.)
605  * Minor version should be 0..2, but process anyway if newer.
606  */
607  if (cinfo->JFIF_major_version != 1)
608  WARNMS2(cinfo, JWRN_JFIF_MAJOR,
609  cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
610  /* Generate trace messages */
611  TRACEMS5(cinfo, 1, JTRC_JFIF,
612  cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
613  cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
614  /* Validate thumbnail dimensions and issue appropriate messages */
615  if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
616  TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
617  GETJOCTET(data[12]), GETJOCTET(data[13]));
618  totallen -= APP0_DATA_LEN;
619  if (totallen !=
620  ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
621  TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
622  } else if (datalen >= 6 &&
623  GETJOCTET(data[0]) == 0x4A &&
624  GETJOCTET(data[1]) == 0x46 &&
625  GETJOCTET(data[2]) == 0x58 &&
626  GETJOCTET(data[3]) == 0x58 &&
627  GETJOCTET(data[4]) == 0) {
628  /* Found JFIF "JFXX" extension APP0 marker */
629  /* The library doesn't actually do anything with these,
630  * but we try to produce a helpful trace message.
631  */
632  switch (GETJOCTET(data[5])) {
633  case 0x10:
634  TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
635  break;
636  case 0x11:
637  TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
638  break;
639  case 0x13:
640  TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
641  break;
642  default:
643  TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
644  GETJOCTET(data[5]), (int) totallen);
645  break;
646  }
647  } else {
648  /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
649  TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
650  }
651 }
652 
653 
654 LOCAL(void)
656  unsigned int datalen, INT32 remaining)
657 /* Examine first few bytes from an APP14.
658  * Take appropriate action if it is an Adobe marker.
659  * datalen is # of bytes at data[], remaining is length of rest of marker data.
660  */
661 {
662  unsigned int version, flags0, flags1, transform;
663 
664  if (datalen >= APP14_DATA_LEN &&
665  GETJOCTET(data[0]) == 0x41 &&
666  GETJOCTET(data[1]) == 0x64 &&
667  GETJOCTET(data[2]) == 0x6F &&
668  GETJOCTET(data[3]) == 0x62 &&
669  GETJOCTET(data[4]) == 0x65) {
670  /* Found Adobe APP14 marker */
671  version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
672  flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
673  flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
674  transform = GETJOCTET(data[11]);
675  TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
676  cinfo->saw_Adobe_marker = TRUE;
677  cinfo->Adobe_transform = (UINT8) transform;
678  } else {
679  /* Start of APP14 does not match "Adobe", or too short */
680  TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
681  }
682 }
683 
684 
685 METHODDEF(boolean)
687 /* Process an APP0 or APP14 marker without saving it */
688 {
689  INT32 length;
691  unsigned int i, numtoread;
692  INPUT_VARS(cinfo);
693 
694  INPUT_2BYTES(cinfo, length, return FALSE);
695  length -= 2;
696 
697  /* get the interesting part of the marker data */
698  if (length >= APPN_DATA_LEN)
699  numtoread = APPN_DATA_LEN;
700  else if (length > 0)
701  numtoread = (unsigned int) length;
702  else
703  numtoread = 0;
704  for (i = 0; i < numtoread; i++)
705  INPUT_BYTE(cinfo, b[i], return FALSE);
706  length -= numtoread;
707 
708  /* process it */
709  switch (cinfo->unread_marker) {
710  case M_APP0:
711  examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
712  break;
713  case M_APP14:
714  examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
715  break;
716  default:
717  /* can't get here unless jpeg_save_markers chooses wrong processor */
718  ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
719  break;
720  }
721 
722  /* skip any remaining data -- could be lots */
723  INPUT_SYNC(cinfo);
724  if (length > 0)
725  (*cinfo->src->skip_input_data) (cinfo, (long) length);
726 
727  return TRUE;
728 }
729 
730 
731 #ifdef SAVE_MARKERS_SUPPORTED
732 
733 METHODDEF(boolean)
735 /* Save an APPn or COM marker into the marker list */
736 {
737  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
738  jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
739  unsigned int bytes_read, data_length;
740  JOCTET FAR * data;
741  INT32 length = 0;
742  INPUT_VARS(cinfo);
743 
744  if (cur_marker == NULL) {
745  /* begin reading a marker */
746  INPUT_2BYTES(cinfo, length, return FALSE);
747  length -= 2;
748  if (length >= 0) { /* watch out for bogus length word */
749  /* figure out how much we want to save */
750  unsigned int limit;
751  if (cinfo->unread_marker == (int) M_COM)
752  limit = marker->length_limit_COM;
753  else
754  limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
755  if ((unsigned int) length < limit)
756  limit = (unsigned int) length;
757  /* allocate and initialize the marker item */
758  cur_marker = (jpeg_saved_marker_ptr)
759  (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
760  SIZEOF(struct jpeg_marker_struct) + limit);
761  cur_marker->next = NULL;
762  cur_marker->marker = (UINT8) cinfo->unread_marker;
763  cur_marker->original_length = (unsigned int) length;
764  cur_marker->data_length = limit;
765  /* data area is just beyond the jpeg_marker_struct */
766  data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
767  marker->cur_marker = cur_marker;
768  marker->bytes_read = 0;
769  bytes_read = 0;
770  data_length = limit;
771  } else {
772  /* deal with bogus length word */
773  bytes_read = data_length = 0;
774  data = NULL;
775  }
776  } else {
777  /* resume reading a marker */
778  bytes_read = marker->bytes_read;
779  data_length = cur_marker->data_length;
780  data = cur_marker->data + bytes_read;
781  }
782 
783  while (bytes_read < data_length) {
784  INPUT_SYNC(cinfo); /* move the restart point to here */
785  marker->bytes_read = bytes_read;
786  /* If there's not at least one byte in buffer, suspend */
787  MAKE_BYTE_AVAIL(cinfo, return FALSE);
788  /* Copy bytes with reasonable rapidity */
789  while (bytes_read < data_length && bytes_in_buffer > 0) {
790  *data++ = *next_input_byte++;
791  bytes_in_buffer--;
792  bytes_read++;
793  }
794  }
795 
796  /* Done reading what we want to read */
797  if (cur_marker != NULL) { /* will be NULL if bogus length word */
798  /* Add new marker to end of list */
799  if (cinfo->marker_list == NULL) {
800  cinfo->marker_list = cur_marker;
801  } else {
802  jpeg_saved_marker_ptr prev = cinfo->marker_list;
803  while (prev->next != NULL)
804  prev = prev->next;
805  prev->next = cur_marker;
806  }
807  /* Reset pointer & calc remaining data length */
808  data = cur_marker->data;
809  length = cur_marker->original_length - data_length;
810  }
811  /* Reset to initial state for next marker */
812  marker->cur_marker = NULL;
813 
814  /* Process the marker if interesting; else just make a generic trace msg */
815  switch (cinfo->unread_marker) {
816  case M_APP0:
817  examine_app0(cinfo, data, data_length, length);
818  break;
819  case M_APP14:
820  examine_app14(cinfo, data, data_length, length);
821  break;
822  default:
823  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
824  (int) (data_length + length));
825  break;
826  }
827 
828  /* skip any remaining data -- could be lots */
829  INPUT_SYNC(cinfo); /* do before skip_input_data */
830  if (length > 0)
831  (*cinfo->src->skip_input_data) (cinfo, (long) length);
832 
833  return TRUE;
834 }
835 
836 #endif /* SAVE_MARKERS_SUPPORTED */
837 
838 
839 METHODDEF(boolean)
841 /* Skip over an unknown or uninteresting variable-length marker */
842 {
843  INT32 length;
844  INPUT_VARS(cinfo);
845 
846  INPUT_2BYTES(cinfo, length, return FALSE);
847  length -= 2;
848 
849  TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
850 
851  INPUT_SYNC(cinfo); /* do before skip_input_data */
852  if (length > 0)
853  (*cinfo->src->skip_input_data) (cinfo, (long) length);
854 
855  return TRUE;
856 }
857 
858 
859 /*
860  * Find the next JPEG marker, save it in cinfo->unread_marker.
861  * Returns FALSE if had to suspend before reaching a marker;
862  * in that case cinfo->unread_marker is unchanged.
863  *
864  * Note that the result might not be a valid marker code,
865  * but it will never be 0 or FF.
866  */
867 
868 LOCAL(boolean)
870 {
871  int c;
872  INPUT_VARS(cinfo);
873 
874  for (;;) {
875  INPUT_BYTE(cinfo, c, return FALSE);
876  /* Skip any non-FF bytes.
877  * This may look a bit inefficient, but it will not occur in a valid file.
878  * We sync after each discarded byte so that a suspending data source
879  * can discard the byte from its buffer.
880  */
881  while (c != 0xFF) {
882  cinfo->marker->discarded_bytes++;
883  INPUT_SYNC(cinfo);
884  INPUT_BYTE(cinfo, c, return FALSE);
885  }
886  /* This loop swallows any duplicate FF bytes. Extra FFs are legal as
887  * pad bytes, so don't count them in discarded_bytes. We assume there
888  * will not be so many consecutive FF bytes as to overflow a suspending
889  * data source's input buffer.
890  */
891  do {
892  INPUT_BYTE(cinfo, c, return FALSE);
893  } while (c == 0xFF);
894  if (c != 0)
895  break; /* found a valid marker, exit loop */
896  /* Reach here if we found a stuffed-zero data sequence (FF/00).
897  * Discard it and loop back to try again.
898  */
899  cinfo->marker->discarded_bytes += 2;
900  INPUT_SYNC(cinfo);
901  }
902 
903  if (cinfo->marker->discarded_bytes != 0) {
904  WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
905  cinfo->marker->discarded_bytes = 0;
906  }
907 
908  cinfo->unread_marker = c;
909 
910  INPUT_SYNC(cinfo);
911  return TRUE;
912 }
913 
914 
915 LOCAL(boolean)
917 /* Like next_marker, but used to obtain the initial SOI marker. */
918 /* For this marker, we do not allow preceding garbage or fill; otherwise,
919  * we might well scan an entire input file before realizing it ain't JPEG.
920  * If an application wants to process non-JFIF files, it must seek to the
921  * SOI before calling the JPEG library.
922  */
923 {
924  int c, c2;
925  INPUT_VARS(cinfo);
926 
927  INPUT_BYTE(cinfo, c, return FALSE);
928  INPUT_BYTE(cinfo, c2, return FALSE);
929  if (c != 0xFF || c2 != (int) M_SOI)
930  ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
931 
932  cinfo->unread_marker = c2;
933 
934  INPUT_SYNC(cinfo);
935  return TRUE;
936 }
937 
938 
939 /*
940  * Read markers until SOS or EOI.
941  *
942  * Returns same codes as are defined for jpeg_consume_input:
943  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
944  */
945 
946 METHODDEF(int)
948 {
949  /* Outer loop repeats once for each marker. */
950  for (;;) {
951  /* Collect the marker proper, unless we already did. */
952  /* NB: first_marker() enforces the requirement that SOI appear first. */
953  if (cinfo->unread_marker == 0) {
954  if (! cinfo->marker->saw_SOI) {
955  if (! first_marker(cinfo))
956  return JPEG_SUSPENDED;
957  } else {
958  if (! next_marker(cinfo))
959  return JPEG_SUSPENDED;
960  }
961  }
962  /* At this point cinfo->unread_marker contains the marker code and the
963  * input point is just past the marker proper, but before any parameters.
964  * A suspension will cause us to return with this state still true.
965  */
966  switch (cinfo->unread_marker) {
967  case M_SOI:
968  if (! get_soi(cinfo))
969  return JPEG_SUSPENDED;
970  break;
971 
972  case M_SOF0: /* Baseline */
973  case M_SOF1: /* Extended sequential, Huffman */
974  if (! get_sof(cinfo, FALSE, FALSE))
975  return JPEG_SUSPENDED;
976  break;
977 
978  case M_SOF2: /* Progressive, Huffman */
979  if (! get_sof(cinfo, TRUE, FALSE))
980  return JPEG_SUSPENDED;
981  break;
982 
983  case M_SOF9: /* Extended sequential, arithmetic */
984  if (! get_sof(cinfo, FALSE, TRUE))
985  return JPEG_SUSPENDED;
986  break;
987 
988  case M_SOF10: /* Progressive, arithmetic */
989  if (! get_sof(cinfo, TRUE, TRUE))
990  return JPEG_SUSPENDED;
991  break;
992 
993  /* Currently unsupported SOFn types */
994  case M_SOF3: /* Lossless, Huffman */
995  case M_SOF5: /* Differential sequential, Huffman */
996  case M_SOF6: /* Differential progressive, Huffman */
997  case M_SOF7: /* Differential lossless, Huffman */
998  case M_JPG: /* Reserved for JPEG extensions */
999  case M_SOF11: /* Lossless, arithmetic */
1000  case M_SOF13: /* Differential sequential, arithmetic */
1001  case M_SOF14: /* Differential progressive, arithmetic */
1002  case M_SOF15: /* Differential lossless, arithmetic */
1003  ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
1004  break;
1005 
1006  case M_SOS:
1007  if (! get_sos(cinfo))
1008  return JPEG_SUSPENDED;
1009  cinfo->unread_marker = 0; /* processed the marker */
1010  return JPEG_REACHED_SOS;
1011 
1012  case M_EOI:
1013  TRACEMS(cinfo, 1, JTRC_EOI);
1014  cinfo->unread_marker = 0; /* processed the marker */
1015  return JPEG_REACHED_EOI;
1016 
1017  case M_DAC:
1018  if (! get_dac(cinfo))
1019  return JPEG_SUSPENDED;
1020  break;
1021 
1022  case M_DHT:
1023  if (! get_dht(cinfo))
1024  return JPEG_SUSPENDED;
1025  break;
1026 
1027  case M_DQT:
1028  if (! get_dqt(cinfo))
1029  return JPEG_SUSPENDED;
1030  break;
1031 
1032  case M_DRI:
1033  if (! get_dri(cinfo))
1034  return JPEG_SUSPENDED;
1035  break;
1036 
1037  case M_APP0:
1038  case M_APP1:
1039  case M_APP2:
1040  case M_APP3:
1041  case M_APP4:
1042  case M_APP5:
1043  case M_APP6:
1044  case M_APP7:
1045  case M_APP8:
1046  case M_APP9:
1047  case M_APP10:
1048  case M_APP11:
1049  case M_APP12:
1050  case M_APP13:
1051  case M_APP14:
1052  case M_APP15:
1053  if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
1054  cinfo->unread_marker - (int) M_APP0]) (cinfo))
1055  return JPEG_SUSPENDED;
1056  break;
1057 
1058  case M_COM:
1059  if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
1060  return JPEG_SUSPENDED;
1061  break;
1062 
1063  case M_RST0: /* these are all parameterless */
1064  case M_RST1:
1065  case M_RST2:
1066  case M_RST3:
1067  case M_RST4:
1068  case M_RST5:
1069  case M_RST6:
1070  case M_RST7:
1071  case M_TEM:
1072  TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
1073  break;
1074 
1075  case M_DNL: /* Ignore DNL ... perhaps the wrong thing */
1076  if (! skip_variable(cinfo))
1077  return JPEG_SUSPENDED;
1078  break;
1079 
1080  default: /* must be DHP, EXP, JPGn, or RESn */
1081  /* For now, we treat the reserved markers as fatal errors since they are
1082  * likely to be used to signal incompatible JPEG Part 3 extensions.
1083  * Once the JPEG 3 version-number marker is well defined, this code
1084  * ought to change!
1085  */
1086  ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
1087  break;
1088  }
1089  /* Successfully processed marker, so reset state variable */
1090  cinfo->unread_marker = 0;
1091  } /* end loop */
1092 }
1093 
1094 
1095 /*
1096  * Read a restart marker, which is expected to appear next in the datastream;
1097  * if the marker is not there, take appropriate recovery action.
1098  * Returns FALSE if suspension is required.
1099  *
1100  * This is called by the entropy decoder after it has read an appropriate
1101  * number of MCUs. cinfo->unread_marker may be nonzero if the entropy decoder
1102  * has already read a marker from the data source. Under normal conditions
1103  * cinfo->unread_marker will be reset to 0 before returning; if not reset,
1104  * it holds a marker which the decoder will be unable to read past.
1105  */
1106 
1107 METHODDEF(boolean)
1109 {
1110  /* Obtain a marker unless we already did. */
1111  /* Note that next_marker will complain if it skips any data. */
1112  if (cinfo->unread_marker == 0) {
1113  if (! next_marker(cinfo))
1114  return FALSE;
1115  }
1116 
1117  if (cinfo->unread_marker ==
1118  ((int) M_RST0 + cinfo->marker->next_restart_num)) {
1119  /* Normal case --- swallow the marker and let entropy decoder continue */
1120  TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
1121  cinfo->unread_marker = 0;
1122  } else {
1123  /* Uh-oh, the restart markers have been messed up. */
1124  /* Let the data source manager determine how to resync. */
1125  if (! (*cinfo->src->resync_to_restart) (cinfo,
1126  cinfo->marker->next_restart_num))
1127  return FALSE;
1128  }
1129 
1130  /* Update next-restart state */
1131  cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
1132 
1133  return TRUE;
1134 }
1135 
1136 
1137 /*
1138  * This is the default resync_to_restart method for data source managers
1139  * to use if they don't have any better approach. Some data source managers
1140  * may be able to back up, or may have additional knowledge about the data
1141  * which permits a more intelligent recovery strategy; such managers would
1142  * presumably supply their own resync method.
1143  *
1144  * read_restart_marker calls resync_to_restart if it finds a marker other than
1145  * the restart marker it was expecting. (This code is *not* used unless
1146  * a nonzero restart interval has been declared.) cinfo->unread_marker is
1147  * the marker code actually found (might be anything, except 0 or FF).
1148  * The desired restart marker number (0..7) is passed as a parameter.
1149  * This routine is supposed to apply whatever error recovery strategy seems
1150  * appropriate in order to position the input stream to the next data segment.
1151  * Note that cinfo->unread_marker is treated as a marker appearing before
1152  * the current data-source input point; usually it should be reset to zero
1153  * before returning.
1154  * Returns FALSE if suspension is required.
1155  *
1156  * This implementation is substantially constrained by wanting to treat the
1157  * input as a data stream; this means we can't back up. Therefore, we have
1158  * only the following actions to work with:
1159  * 1. Simply discard the marker and let the entropy decoder resume at next
1160  * byte of file.
1161  * 2. Read forward until we find another marker, discarding intervening
1162  * data. (In theory we could look ahead within the current bufferload,
1163  * without having to discard data if we don't find the desired marker.
1164  * This idea is not implemented here, in part because it makes behavior
1165  * dependent on buffer size and chance buffer-boundary positions.)
1166  * 3. Leave the marker unread (by failing to zero cinfo->unread_marker).
1167  * This will cause the entropy decoder to process an empty data segment,
1168  * inserting dummy zeroes, and then we will reprocess the marker.
1169  *
1170  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
1171  * appropriate if the found marker is a future restart marker (indicating
1172  * that we have missed the desired restart marker, probably because it got
1173  * corrupted).
1174  * We apply #2 or #3 if the found marker is a restart marker no more than
1175  * two counts behind or ahead of the expected one. We also apply #2 if the
1176  * found marker is not a legal JPEG marker code (it's certainly bogus data).
1177  * If the found marker is a restart marker more than 2 counts away, we do #1
1178  * (too much risk that the marker is erroneous; with luck we will be able to
1179  * resync at some future point).
1180  * For any valid non-restart JPEG marker, we apply #3. This keeps us from
1181  * overrunning the end of a scan. An implementation limited to single-scan
1182  * files might find it better to apply #2 for markers other than EOI, since
1183  * any other marker would have to be bogus data in that case.
1184  */
1185 
1186 GLOBAL(boolean)
1188 {
1189  int marker = cinfo->unread_marker;
1190  int action = 1;
1191 
1192  /* Always put up a warning. */
1193  WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
1194 
1195  /* Outer loop handles repeated decision after scanning forward. */
1196  for (;;) {
1197  if (marker < (int) M_SOF0)
1198  action = 2; /* invalid marker */
1199  else if (marker < (int) M_RST0 || marker > (int) M_RST7)
1200  action = 3; /* valid non-restart marker */
1201  else {
1202  if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
1203  marker == ((int) M_RST0 + ((desired+2) & 7)))
1204  action = 3; /* one of the next two expected restarts */
1205  else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
1206  marker == ((int) M_RST0 + ((desired-2) & 7)))
1207  action = 2; /* a prior restart, so advance */
1208  else
1209  action = 1; /* desired restart or too far away */
1210  }
1211  TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
1212  switch (action) {
1213  case 1:
1214  /* Discard marker and let entropy decoder resume processing. */
1215  cinfo->unread_marker = 0;
1216  return TRUE;
1217  case 2:
1218  /* Scan to the next marker, and repeat the decision loop. */
1219  if (! next_marker(cinfo))
1220  return FALSE;
1221  marker = cinfo->unread_marker;
1222  break;
1223  case 3:
1224  /* Return without advancing past this marker. */
1225  /* Entropy decoder will be forced to process an empty segment. */
1226  return TRUE;
1227  }
1228  } /* end loop */
1229 }
1230 
1231 
1232 /*
1233  * Reset marker processing state to begin a fresh datastream.
1234  */
1235 
1236 METHODDEF(void)
1238 {
1239  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
1240 
1241  cinfo->comp_info = NULL; /* until allocated by get_sof */
1242  cinfo->input_scan_number = 0; /* no SOS seen yet */
1243  cinfo->unread_marker = 0; /* no pending marker */
1244  marker->pub.saw_SOI = FALSE; /* set internal state too */
1245  marker->pub.saw_SOF = FALSE;
1246  marker->pub.discarded_bytes = 0;
1247  marker->cur_marker = NULL;
1248 }
1249 
1250 
1251 /*
1252  * Initialize the marker reader module.
1253  * This is called only once, when the decompression object is created.
1254  */
1255 
1256 GLOBAL(void)
1258 {
1260  int i;
1261 
1262  /* Create subobject in permanent pool */
1263  marker = (my_marker_ptr)
1264  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
1266  cinfo->marker = (struct jpeg_marker_reader *) marker;
1267  /* Initialize public method pointers */
1268  marker->pub.reset_marker_reader = reset_marker_reader;
1269  marker->pub.read_markers = read_markers;
1270  marker->pub.read_restart_marker = read_restart_marker;
1271  /* Initialize COM/APPn processing.
1272  * By default, we examine and then discard APP0 and APP14,
1273  * but simply discard COM and all other APPn.
1274  */
1275  marker->process_COM = skip_variable;
1276  marker->length_limit_COM = 0;
1277  for (i = 0; i < 16; i++) {
1278  marker->process_APPn[i] = skip_variable;
1279  marker->length_limit_APPn[i] = 0;
1280  }
1281  marker->process_APPn[0] = get_interesting_appn;
1282  marker->process_APPn[14] = get_interesting_appn;
1283  /* Reset marker processing state */
1284  reset_marker_reader(cinfo);
1285 }
1286 
1287 
1288 /*
1289  * Control saving of COM and APPn markers into marker_list.
1290  */
1291 
1292 #ifdef SAVE_MARKERS_SUPPORTED
1293 
1294 GLOBAL(void)
1296  unsigned int length_limit)
1297 {
1298  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
1299  long maxlength;
1300  jpeg_marker_parser_method processor;
1301 
1302  /* Length limit mustn't be larger than what we can allocate
1303  * (should only be a concern in a 16-bit environment).
1304  */
1305  maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
1306  if (((long) length_limit) > maxlength)
1307  length_limit = (unsigned int) maxlength;
1308 
1309  /* Choose processor routine to use.
1310  * APP0/APP14 have special requirements.
1311  */
1312  if (length_limit) {
1313  processor = save_marker;
1314  /* If saving APP0/APP14, save at least enough for our internal use. */
1315  if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
1317  else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
1319  } else {
1320  processor = skip_variable;
1321  /* If discarding APP0/APP14, use our regular on-the-fly processor. */
1322  if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
1323  processor = get_interesting_appn;
1324  }
1325 
1326  if (marker_code == (int) M_COM) {
1327  marker->process_COM = processor;
1328  marker->length_limit_COM = length_limit;
1329  } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
1330  marker->process_APPn[marker_code - (int) M_APP0] = processor;
1331  marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
1332  } else
1333  ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
1334 }
1335 
1336 #endif /* SAVE_MARKERS_SUPPORTED */
1337 
1338 
1339 /*
1340  * Install a special processing method for COM or APPn markers.
1341  */
1342 
1343 GLOBAL(void)
1345  jpeg_marker_parser_method routine)
1346 {
1347  my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
1348 
1349  if (marker_code == (int) M_COM)
1350  marker->process_COM = routine;
1351  else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
1352  marker->process_APPn[marker_code - (int) M_APP0] = routine;
1353  else
1354  ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
1355 }
#define INPUT_VARS(cinfo)
Definition: jdmarker.cpp:114
jinit_marker_reader(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:1257
#define INPUT_2BYTES(cinfo, V, action)
Definition: jdmarker.cpp:151
GLuint GLuint GLsizei count
Definition: glext.h:3512
UINT16 quantval[DCTSIZE2]
Definition: mrpt_jpeglib.h:85
get_sof(j_decompress_ptr cinfo, boolean is_prog, boolean is_arith)
Definition: jdmarker.cpp:232
first_marker(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:916
skip_variable(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:840
#define GETJOCTET(value)
Definition: jmorecfg.h:116
#define MAX_COMPS_IN_SCAN
Definition: mrpt_jpeglib.h:43
#define JPOOL_PERMANENT
Definition: mrpt_jpeglib.h:745
int jpeg_marker_parser_method routine
UINT8 arith_dc_U[NUM_ARITH_TBLS]
Definition: mrpt_jpeglib.h:539
const int jpeg_natural_order[]
Definition: jutils.cpp:49
int desired
GLenum GLsizei n
Definition: glext.h:4618
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define ERREXIT(cinfo, code)
Definition: jerror.h:199
#define TRACEMS2(cinfo, lvl, code, p1, p2)
Definition: jerror.h:253
#define SIZEOF(object)
Definition: jinclude.h:73
#define NUM_ARITH_TBLS
Definition: mrpt_jpeglib.h:42
struct jpeg_marker_struct FAR * jpeg_saved_marker_ptr
Definition: mrpt_jpeglib.h:190
read_markers(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:947
my_marker_reader * my_marker_ptr
Definition: jdmarker.cpp:102
long INT32
Definition: jmorecfg.h:158
#define INPUT_BYTE(cinfo, V, action)
Definition: jdmarker.cpp:143
GLint limit
Definition: glext.h:7011
jpeg_component_info * compptr
Definition: jdct.h:97
UINT8 arith_dc_L[NUM_ARITH_TBLS]
Definition: mrpt_jpeglib.h:538
int marker
Definition: mrpt_jpeglib.h:947
#define TRACEMS(cinfo, lvl, code)
Definition: jerror.h:246
get_sos(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:296
#define WARNMS2(cinfo, code, p1, p2)
Definition: jerror.h:239
jpeg_resync_to_restart(j_decompress_ptr cinfo, int desired)
Definition: jdmarker.cpp:1187
#define APP14_DATA_LEN
Definition: jdmarker.cpp:574
UINT8 arith_ac_K[NUM_ARITH_TBLS]
Definition: mrpt_jpeglib.h:540
#define TRACEMS1(cinfo, lvl, code, p1)
Definition: jerror.h:249
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:61
int marker_code
read_restart_marker(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:1108
GLuint index
Definition: glext.h:3891
const GLubyte * c
Definition: glext.h:5590
#define FALSE
Definition: jmorecfg.h:227
short UINT8
Definition: jmorecfg.h:137
#define TRACEMS5(cinfo, lvl, code, p1, p2, p3, p4, p5)
Definition: jerror.h:268
#define JPEG_REACHED_EOI
Definition: mrpt_jpeglib.h:994
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:746
#define LOCAL(type)
Definition: jmorecfg.h:183
int unsigned int length_limit
save_marker(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:734
int const JOCTET unsigned int datalen
Definition: mrpt_jpeglib.h:947
#define APP0_DATA_LEN
Definition: jdmarker.cpp:573
int val
Definition: mrpt_jpeglib.h:953
GLubyte GLubyte b
Definition: glext.h:5575
#define APPN_DATA_LEN
Definition: jdmarker.cpp:575
jpeg_alloc_huff_table(j_common_ptr cinfo)
Definition: jcomapi.cpp:96
examine_app0(j_decompress_ptr cinfo, JOCTET FAR *data, unsigned int datalen, INT32 remaining)
Definition: jdmarker.cpp:579
#define DCTSIZE2
Definition: mrpt_jpeglib.h:39
int version
Definition: mrpt_jpeglib.h:898
jpeg_marker_parser_method process_COM
Definition: jdmarker.cpp:89
reset_marker_reader(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:1237
#define NUM_HUFF_TBLS
Definition: mrpt_jpeglib.h:41
get_dht(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:414
#define JPEG_SUSPENDED
Definition: mrpt_jpeglib.h:962
#define TRUE
Definition: jmorecfg.h:230
jpeg_alloc_quant_table(j_common_ptr cinfo)
Definition: jcomapi.cpp:84
unsigned int UINT16
Definition: jmorecfg.h:146
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:202
get_dqt(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:485
JPEG_MARKER
Definition: jdmarker.cpp:15
jpeg_save_markers(j_decompress_ptr cinfo, int marker_code, unsigned int length_limit)
Definition: jdmarker.cpp:1295
#define GLOBAL(type)
Definition: jmorecfg.h:185
next_marker(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:869
#define METHODDEF(type)
Definition: jmorecfg.h:181
GLuint GLsizei GLsizei * length
Definition: glext.h:3900
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:40
#define MAKE_BYTE_AVAIL(cinfo, action)
Definition: jdmarker.cpp:133
#define JPEG_REACHED_SOS
Definition: mrpt_jpeglib.h:993
get_dri(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:543
#define TRACEMS8(cinfo, lvl, code, p1, p2, p3, p4, p5, p6, p7, p8)
Definition: jerror.h:274
get_soi(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:192
#define get_dac(cinfo)
Definition: jdmarker.cpp:408
#define TRACEMS3(cinfo, lvl, code, p1, p2, p3)
Definition: jerror.h:258
jpeg_saved_marker_ptr cur_marker
Definition: jdmarker.cpp:97
#define TRACEMS4(cinfo, lvl, code, p1, p2, p3, p4)
Definition: jerror.h:263
#define FAR
Definition: zconf.h:261
jpeg_set_marker_processor(j_decompress_ptr cinfo, int marker_code, jpeg_marker_parser_method routine)
Definition: jdmarker.cpp:1344
char JOCTET
Definition: jmorecfg.h:112
GLuint GLenum GLenum transform
Definition: glext.h:6092
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:206
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
unsigned int length_limit_COM
Definition: jdmarker.cpp:93
#define INPUT_SYNC(cinfo)
Definition: jdmarker.cpp:120
examine_app14(j_decompress_ptr cinfo, JOCTET FAR *data, unsigned int datalen, INT32 remaining)
Definition: jdmarker.cpp:655
unsigned int bytes_read
Definition: jdmarker.cpp:98
get_interesting_appn(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:686



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019