Shoaib Ahmed / Mbed 2 deprecated uzairkhan

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jdmarker.c Source File

jdmarker.c

00001 /*
00002  * jdmarker.c
00003  *
00004  * Copyright (C) 1991-1998, Thomas G. Lane.
00005  * Modified 2009-2013 by Guido Vollbeding.
00006  * This file is part of the Independent JPEG Group's software.
00007  * For conditions of distribution and use, see the accompanying README file.
00008  *
00009  * This file contains routines to decode JPEG datastream markers.
00010  * Most of the complexity arises from our desire to support input
00011  * suspension: if not all of the data for a marker is available,
00012  * we must exit back to the application.  On resumption, we reprocess
00013  * the marker.
00014  */
00015 
00016 #define JPEG_INTERNALS
00017 #include "jinclude.h"
00018 #include "jpeglib.h"
00019 
00020 
00021 typedef enum {          /* JPEG marker codes */
00022   M_SOF0  = 0xc0,
00023   M_SOF1  = 0xc1,
00024   M_SOF2  = 0xc2,
00025   M_SOF3  = 0xc3,
00026 
00027   M_SOF5  = 0xc5,
00028   M_SOF6  = 0xc6,
00029   M_SOF7  = 0xc7,
00030 
00031   M_JPG   = 0xc8,
00032   M_SOF9  = 0xc9,
00033   M_SOF10 = 0xca,
00034   M_SOF11 = 0xcb,
00035 
00036   M_SOF13 = 0xcd,
00037   M_SOF14 = 0xce,
00038   M_SOF15 = 0xcf,
00039 
00040   M_DHT   = 0xc4,
00041 
00042   M_DAC   = 0xcc,
00043 
00044   M_RST0  = 0xd0,
00045   M_RST1  = 0xd1,
00046   M_RST2  = 0xd2,
00047   M_RST3  = 0xd3,
00048   M_RST4  = 0xd4,
00049   M_RST5  = 0xd5,
00050   M_RST6  = 0xd6,
00051   M_RST7  = 0xd7,
00052 
00053   M_SOI   = 0xd8,
00054   M_EOI   = 0xd9,
00055   M_SOS   = 0xda,
00056   M_DQT   = 0xdb,
00057   M_DNL   = 0xdc,
00058   M_DRI   = 0xdd,
00059   M_DHP   = 0xde,
00060   M_EXP   = 0xdf,
00061 
00062   M_APP0  = 0xe0,
00063   M_APP1  = 0xe1,
00064   M_APP2  = 0xe2,
00065   M_APP3  = 0xe3,
00066   M_APP4  = 0xe4,
00067   M_APP5  = 0xe5,
00068   M_APP6  = 0xe6,
00069   M_APP7  = 0xe7,
00070   M_APP8  = 0xe8,
00071   M_APP9  = 0xe9,
00072   M_APP10 = 0xea,
00073   M_APP11 = 0xeb,
00074   M_APP12 = 0xec,
00075   M_APP13 = 0xed,
00076   M_APP14 = 0xee,
00077   M_APP15 = 0xef,
00078 
00079   M_JPG0  = 0xf0,
00080   M_JPG8  = 0xf8,
00081   M_JPG13 = 0xfd,
00082   M_COM   = 0xfe,
00083 
00084   M_TEM   = 0x01,
00085 
00086   M_ERROR = 0x100
00087 } JPEG_MARKER;
00088 
00089 
00090 /* Private state */
00091 
00092 typedef struct {
00093   struct jpeg_marker_reader pub; /* public fields */
00094 
00095   /* Application-overridable marker processing methods */
00096   jpeg_marker_parser_method process_COM;
00097   jpeg_marker_parser_method process_APPn[16];
00098 
00099   /* Limit on marker data length to save for each marker type */
00100   unsigned int length_limit_COM;
00101   unsigned int length_limit_APPn[16];
00102 
00103   /* Status of COM/APPn marker saving */
00104   jpeg_saved_marker_ptr cur_marker; /* NULL if not processing a marker */
00105   unsigned int bytes_read;      /* data bytes read so far in marker */
00106   /* Note: cur_marker is not linked into marker_list until it's all read. */
00107 } my_marker_reader;
00108 
00109 typedef my_marker_reader * my_marker_ptr;
00110 
00111 
00112 /*
00113  * Macros for fetching data from the data source module.
00114  *
00115  * At all times, cinfo->src->next_input_byte and ->bytes_in_buffer reflect
00116  * the current restart point; we update them only when we have reached a
00117  * suitable place to restart if a suspension occurs.
00118  */
00119 
00120 /* Declare and initialize local copies of input pointer/count */
00121 #define INPUT_VARS(cinfo)  \
00122     struct jpeg_source_mgr * datasrc = (cinfo)->src;  \
00123     const JOCTET * next_input_byte = datasrc->next_input_byte;  \
00124     size_t bytes_in_buffer = datasrc->bytes_in_buffer
00125 
00126 /* Unload the local copies --- do this only at a restart boundary */
00127 #define INPUT_SYNC(cinfo)  \
00128     ( datasrc->next_input_byte = next_input_byte,  \
00129       datasrc->bytes_in_buffer = bytes_in_buffer )
00130 
00131 /* Reload the local copies --- used only in MAKE_BYTE_AVAIL */
00132 #define INPUT_RELOAD(cinfo)  \
00133     ( next_input_byte = datasrc->next_input_byte,  \
00134       bytes_in_buffer = datasrc->bytes_in_buffer )
00135 
00136 /* Internal macro for INPUT_BYTE and INPUT_2BYTES: make a byte available.
00137  * Note we do *not* do INPUT_SYNC before calling fill_input_buffer,
00138  * but we must reload the local copies after a successful fill.
00139  */
00140 #define MAKE_BYTE_AVAIL(cinfo,action)  \
00141     if (bytes_in_buffer == 0) {  \
00142       if (! (*datasrc->fill_input_buffer) (cinfo))  \
00143         { action; }  \
00144       INPUT_RELOAD(cinfo);  \
00145     }
00146 
00147 /* Read a byte into variable V.
00148  * If must suspend, take the specified action (typically "return FALSE").
00149  */
00150 #define INPUT_BYTE(cinfo,V,action)  \
00151     MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
00152           bytes_in_buffer--; \
00153           V = GETJOCTET(*next_input_byte++); )
00154 
00155 /* As above, but read two bytes interpreted as an unsigned 16-bit integer.
00156  * V should be declared unsigned int or perhaps INT32.
00157  */
00158 #define INPUT_2BYTES(cinfo,V,action)  \
00159     MAKESTMT( MAKE_BYTE_AVAIL(cinfo,action); \
00160           bytes_in_buffer--; \
00161           V = ((unsigned int) GETJOCTET(*next_input_byte++)) << 8; \
00162           MAKE_BYTE_AVAIL(cinfo,action); \
00163           bytes_in_buffer--; \
00164           V += GETJOCTET(*next_input_byte++); )
00165 
00166 
00167 /*
00168  * Routines to process JPEG markers.
00169  *
00170  * Entry condition: JPEG marker itself has been read and its code saved
00171  *   in cinfo->unread_marker; input restart point is just after the marker.
00172  *
00173  * Exit: if return TRUE, have read and processed any parameters, and have
00174  *   updated the restart point to point after the parameters.
00175  *   If return FALSE, was forced to suspend before reaching end of
00176  *   marker parameters; restart point has not been moved.  Same routine
00177  *   will be called again after application supplies more input data.
00178  *
00179  * This approach to suspension assumes that all of a marker's parameters
00180  * can fit into a single input bufferload.  This should hold for "normal"
00181  * markers.  Some COM/APPn markers might have large parameter segments
00182  * that might not fit.  If we are simply dropping such a marker, we use
00183  * skip_input_data to get past it, and thereby put the problem on the
00184  * source manager's shoulders.  If we are saving the marker's contents
00185  * into memory, we use a slightly different convention: when forced to
00186  * suspend, the marker processor updates the restart point to the end of
00187  * what it's consumed (ie, the end of the buffer) before returning FALSE.
00188  * On resumption, cinfo->unread_marker still contains the marker code,
00189  * but the data source will point to the next chunk of marker data.
00190  * The marker processor must retain internal state to deal with this.
00191  *
00192  * Note that we don't bother to avoid duplicate trace messages if a
00193  * suspension occurs within marker parameters.  Other side effects
00194  * require more care.
00195  */
00196 
00197 
00198 LOCAL(boolean)
00199 get_soi (j_decompress_ptr cinfo)
00200 /* Process an SOI marker */
00201 {
00202   int i;
00203   
00204   TRACEMS(cinfo, 1, JTRC_SOI);
00205 
00206   if (cinfo->marker->saw_SOI)
00207     ERREXIT(cinfo, JERR_SOI_DUPLICATE);
00208 
00209   /* Reset all parameters that are defined to be reset by SOI */
00210 
00211   for (i = 0; i < NUM_ARITH_TBLS; i++) {
00212     cinfo->arith_dc_L[i] = 0;
00213     cinfo->arith_dc_U[i] = 1;
00214     cinfo->arith_ac_K[i] = 5;
00215   }
00216   cinfo->restart_interval = 0;
00217 
00218   /* Set initial assumptions for colorspace etc */
00219 
00220   cinfo->jpeg_color_space = JCS_UNKNOWN;
00221   cinfo->color_transform = JCT_NONE;
00222   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling??? */
00223 
00224   cinfo->saw_JFIF_marker = FALSE;
00225   cinfo->JFIF_major_version = 1; /* set default JFIF APP0 values */
00226   cinfo->JFIF_minor_version = 1;
00227   cinfo->density_unit = 0;
00228   cinfo->X_density = 1;
00229   cinfo->Y_density = 1;
00230   cinfo->saw_Adobe_marker = FALSE;
00231   cinfo->Adobe_transform = 0;
00232 
00233   cinfo->marker->saw_SOI = TRUE;
00234 
00235   return TRUE;
00236 }
00237 
00238 
00239 LOCAL(boolean)
00240 get_sof (j_decompress_ptr cinfo, boolean is_baseline, boolean is_prog,
00241      boolean is_arith)
00242 /* Process a SOFn marker */
00243 {
00244   INT32 length;
00245   int c, ci, i;
00246   jpeg_component_info * compptr;
00247   INPUT_VARS(cinfo);
00248 
00249   cinfo->is_baseline = is_baseline;
00250   cinfo->progressive_mode = is_prog;
00251   cinfo->arith_code = is_arith;
00252 
00253   INPUT_2BYTES(cinfo, length, return FALSE);
00254 
00255   INPUT_BYTE(cinfo, cinfo->data_precision, return FALSE);
00256   INPUT_2BYTES(cinfo, cinfo->image_height, return FALSE);
00257   INPUT_2BYTES(cinfo, cinfo->image_width, return FALSE);
00258   INPUT_BYTE(cinfo, cinfo->num_components, return FALSE);
00259 
00260   length -= 8;
00261 
00262   TRACEMS4(cinfo, 1, JTRC_SOF, cinfo->unread_marker,
00263        (int) cinfo->image_width, (int) cinfo->image_height,
00264        cinfo->num_components);
00265 
00266   if (cinfo->marker->saw_SOF)
00267     ERREXIT(cinfo, JERR_SOF_DUPLICATE);
00268 
00269   /* We don't support files in which the image height is initially specified */
00270   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
00271   /* might as well have a general sanity check. */
00272   if (cinfo->image_height <= 0 || cinfo->image_width <= 0 ||
00273       cinfo->num_components <= 0)
00274     ERREXIT(cinfo, JERR_EMPTY_IMAGE);
00275 
00276   if (length != (cinfo->num_components * 3))
00277     ERREXIT(cinfo, JERR_BAD_LENGTH);
00278 
00279   if (cinfo->comp_info == NULL) /* do only once, even if suspend */
00280     cinfo->comp_info = (jpeg_component_info *) (*cinfo->mem->alloc_small)
00281             ((j_common_ptr) cinfo, JPOOL_IMAGE,
00282              cinfo->num_components * SIZEOF(jpeg_component_info));
00283 
00284   for (ci = 0; ci < cinfo->num_components; ci++) {
00285     INPUT_BYTE(cinfo, c, return FALSE);
00286     /* Check to see whether component id has already been seen   */
00287     /* (in violation of the spec, but unfortunately seen in some */
00288     /* files).  If so, create "fake" component id equal to the   */
00289     /* max id seen so far + 1. */
00290     for (i = 0, compptr = cinfo->comp_info; i < ci; i++, compptr++) {
00291       if (c == compptr->component_id) {
00292     compptr = cinfo->comp_info;
00293     c = compptr->component_id;
00294     compptr++;
00295     for (i = 1; i < ci; i++, compptr++) {
00296       if (compptr->component_id > c) c = compptr->component_id;
00297     }
00298     c++;
00299     break;
00300       }
00301     }
00302     compptr->component_id = c;
00303     compptr->component_index = ci;
00304     INPUT_BYTE(cinfo, c, return FALSE);
00305     compptr->h_samp_factor = (c >> 4) & 15;
00306     compptr->v_samp_factor = (c     ) & 15;
00307     INPUT_BYTE(cinfo, compptr->quant_tbl_no, return FALSE);
00308 
00309     TRACEMS4(cinfo, 1, JTRC_SOF_COMPONENT,
00310          compptr->component_id, compptr->h_samp_factor,
00311          compptr->v_samp_factor, compptr->quant_tbl_no);
00312   }
00313 
00314   cinfo->marker->saw_SOF = TRUE;
00315 
00316   INPUT_SYNC(cinfo);
00317   return TRUE;
00318 }
00319 
00320 
00321 LOCAL(boolean)
00322 get_sos (j_decompress_ptr cinfo)
00323 /* Process a SOS marker */
00324 {
00325   INT32 length;
00326   int c, ci, i, n;
00327   jpeg_component_info * compptr;
00328   INPUT_VARS(cinfo);
00329 
00330   if (! cinfo->marker->saw_SOF)
00331     ERREXITS(cinfo, JERR_SOF_BEFORE, "SOS");
00332 
00333   INPUT_2BYTES(cinfo, length, return FALSE);
00334 
00335   INPUT_BYTE(cinfo, n, return FALSE); /* Number of components */
00336 
00337   TRACEMS1(cinfo, 1, JTRC_SOS, n);
00338 
00339   if (length != (n * 2 + 6) || n > MAX_COMPS_IN_SCAN ||
00340       (n == 0 && !cinfo->progressive_mode))
00341       /* pseudo SOS marker only allowed in progressive mode */
00342     ERREXIT(cinfo, JERR_BAD_LENGTH);
00343 
00344   cinfo->comps_in_scan = n;
00345 
00346   /* Collect the component-spec parameters */
00347 
00348   for (i = 0; i < n; i++) {
00349     INPUT_BYTE(cinfo, c, return FALSE);
00350 
00351     /* Detect the case where component id's are not unique, and, if so, */
00352     /* create a fake component id using the same logic as in get_sof.   */
00353     /* Note:  This also ensures that all of the SOF components are      */
00354     /* referenced in the single scan case, which prevents access to     */
00355     /* uninitialized memory in later decoding stages. */
00356     for (ci = 0; ci < i; ci++) {
00357       if (c == cinfo->cur_comp_info[ci]->component_id) {
00358     c = cinfo->cur_comp_info[0]->component_id;
00359     for (ci = 1; ci < i; ci++) {
00360       compptr = cinfo->cur_comp_info[ci];
00361       if (compptr->component_id > c) c = compptr->component_id;
00362     }
00363     c++;
00364     break;
00365       }
00366     }
00367 
00368     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00369      ci++, compptr++) {
00370       if (c == compptr->component_id)
00371     goto id_found;
00372     }
00373 
00374     ERREXIT1(cinfo, JERR_BAD_COMPONENT_ID, c);
00375 
00376   id_found:
00377 
00378     cinfo->cur_comp_info[i] = compptr;
00379     INPUT_BYTE(cinfo, c, return FALSE);
00380     compptr->dc_tbl_no = (c >> 4) & 15;
00381     compptr->ac_tbl_no = (c     ) & 15;
00382 
00383     TRACEMS3(cinfo, 1, JTRC_SOS_COMPONENT, compptr->component_id,
00384          compptr->dc_tbl_no, compptr->ac_tbl_no);
00385   }
00386 
00387   /* Collect the additional scan parameters Ss, Se, Ah/Al. */
00388   INPUT_BYTE(cinfo, c, return FALSE);
00389   cinfo->Ss = c;
00390   INPUT_BYTE(cinfo, c, return FALSE);
00391   cinfo->Se = c;
00392   INPUT_BYTE(cinfo, c, return FALSE);
00393   cinfo->Ah = (c >> 4) & 15;
00394   cinfo->Al = (c     ) & 15;
00395 
00396   TRACEMS4(cinfo, 1, JTRC_SOS_PARAMS, cinfo->Ss, cinfo->Se,
00397        cinfo->Ah, cinfo->Al);
00398 
00399   /* Prepare to scan data & restart markers */
00400   cinfo->marker->next_restart_num = 0;
00401 
00402   /* Count another (non-pseudo) SOS marker */
00403   if (n) cinfo->input_scan_number++;
00404 
00405   INPUT_SYNC(cinfo);
00406   return TRUE;
00407 }
00408 
00409 
00410 #ifdef D_ARITH_CODING_SUPPORTED
00411 
00412 LOCAL(boolean)
00413 get_dac (j_decompress_ptr cinfo)
00414 /* Process a DAC marker */
00415 {
00416   INT32 length;
00417   int index, val;
00418   INPUT_VARS(cinfo);
00419 
00420   INPUT_2BYTES(cinfo, length, return FALSE);
00421   length -= 2;
00422   
00423   while (length > 0) {
00424     INPUT_BYTE(cinfo, index, return FALSE);
00425     INPUT_BYTE(cinfo, val, return FALSE);
00426 
00427     length -= 2;
00428 
00429     TRACEMS2(cinfo, 1, JTRC_DAC, index, val);
00430 
00431     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
00432       ERREXIT1(cinfo, JERR_DAC_INDEX, index);
00433 
00434     if (index >= NUM_ARITH_TBLS) { /* define AC table */
00435       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
00436     } else {            /* define DC table */
00437       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
00438       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
00439       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
00440     ERREXIT1(cinfo, JERR_DAC_VALUE, val);
00441     }
00442   }
00443 
00444   if (length != 0)
00445     ERREXIT(cinfo, JERR_BAD_LENGTH);
00446 
00447   INPUT_SYNC(cinfo);
00448   return TRUE;
00449 }
00450 
00451 #else /* ! D_ARITH_CODING_SUPPORTED */
00452 
00453 #define get_dac(cinfo)  skip_variable(cinfo)
00454 
00455 #endif /* D_ARITH_CODING_SUPPORTED */
00456 
00457 
00458 LOCAL(boolean)
00459 get_dht (j_decompress_ptr cinfo)
00460 /* Process a DHT marker */
00461 {
00462   INT32 length;
00463   UINT8 bits[17];
00464   UINT8 huffval[256];
00465   int i, index, count;
00466   JHUFF_TBL **htblptr;
00467   INPUT_VARS(cinfo);
00468 
00469   INPUT_2BYTES(cinfo, length, return FALSE);
00470   length -= 2;
00471   
00472   while (length > 16) {
00473     INPUT_BYTE(cinfo, index, return FALSE);
00474 
00475     TRACEMS1(cinfo, 1, JTRC_DHT, index);
00476       
00477     bits[0] = 0;
00478     count = 0;
00479     for (i = 1; i <= 16; i++) {
00480       INPUT_BYTE(cinfo, bits[i], return FALSE);
00481       count += bits[i];
00482     }
00483 
00484     length -= 1 + 16;
00485 
00486     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
00487          bits[1], bits[2], bits[3], bits[4],
00488          bits[5], bits[6], bits[7], bits[8]);
00489     TRACEMS8(cinfo, 2, JTRC_HUFFBITS,
00490          bits[9], bits[10], bits[11], bits[12],
00491          bits[13], bits[14], bits[15], bits[16]);
00492 
00493     /* Here we just do minimal validation of the counts to avoid walking
00494      * off the end of our table space.  jdhuff.c will check more carefully.
00495      */
00496     if (count > 256 || ((INT32) count) > length)
00497       ERREXIT(cinfo, JERR_BAD_HUFF_TABLE);
00498 
00499     MEMZERO(huffval, SIZEOF(huffval)); /* pre-zero array for later copy */
00500 
00501     for (i = 0; i < count; i++)
00502       INPUT_BYTE(cinfo, huffval[i], return FALSE);
00503 
00504     length -= count;
00505 
00506     if (index & 0x10) {     /* AC table definition */
00507       index -= 0x10;
00508       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
00509     } else {            /* DC table definition */
00510       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
00511     }
00512 
00513     if (index < 0 || index >= NUM_HUFF_TBLS)
00514       ERREXIT1(cinfo, JERR_DHT_INDEX, index);
00515 
00516     if (*htblptr == NULL)
00517       *htblptr = jpeg_alloc_huff_table((j_common_ptr) cinfo);
00518   
00519     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
00520     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
00521   }
00522 
00523   if (length != 0)
00524     ERREXIT(cinfo, JERR_BAD_LENGTH);
00525 
00526   INPUT_SYNC(cinfo);
00527   return TRUE;
00528 }
00529 
00530 
00531 LOCAL(boolean)
00532 get_dqt (j_decompress_ptr cinfo)
00533 /* Process a DQT marker */
00534 {
00535   INT32 length, count, i;
00536   int n, prec;
00537   unsigned int tmp;
00538   JQUANT_TBL *quant_ptr;
00539   const int *natural_order;
00540   INPUT_VARS(cinfo);
00541 
00542   INPUT_2BYTES(cinfo, length, return FALSE);
00543   length -= 2;
00544 
00545   while (length > 0) {
00546     length--;
00547     INPUT_BYTE(cinfo, n, return FALSE);
00548     prec = n >> 4;
00549     n &= 0x0F;
00550 
00551     TRACEMS2(cinfo, 1, JTRC_DQT, n, prec);
00552 
00553     if (n >= NUM_QUANT_TBLS)
00554       ERREXIT1(cinfo, JERR_DQT_INDEX, n);
00555       
00556     if (cinfo->quant_tbl_ptrs[n] == NULL)
00557       cinfo->quant_tbl_ptrs[n] = jpeg_alloc_quant_table((j_common_ptr) cinfo);
00558     quant_ptr = cinfo->quant_tbl_ptrs[n];
00559 
00560     if (prec) {
00561       if (length < DCTSIZE2 * 2) {
00562     /* Initialize full table for safety. */
00563     for (i = 0; i < DCTSIZE2; i++) {
00564       quant_ptr->quantval[i] = 1;
00565     }
00566     count = length >> 1;
00567       } else
00568     count = DCTSIZE2;
00569     } else {
00570       if (length < DCTSIZE2) {
00571     /* Initialize full table for safety. */
00572     for (i = 0; i < DCTSIZE2; i++) {
00573       quant_ptr->quantval[i] = 1;
00574     }
00575     count = length;
00576       } else
00577     count = DCTSIZE2;
00578     }
00579 
00580     switch (count) {
00581     case (2*2): natural_order = jpeg_natural_order2; break;
00582     case (3*3): natural_order = jpeg_natural_order3; break;
00583     case (4*4): natural_order = jpeg_natural_order4; break;
00584     case (5*5): natural_order = jpeg_natural_order5; break;
00585     case (6*6): natural_order = jpeg_natural_order6; break;
00586     case (7*7): natural_order = jpeg_natural_order7; break;
00587     default:    natural_order = jpeg_natural_order;  break;
00588     }
00589 
00590     for (i = 0; i < count; i++) {
00591       if (prec)
00592     INPUT_2BYTES(cinfo, tmp, return FALSE);
00593       else
00594     INPUT_BYTE(cinfo, tmp, return FALSE);
00595       /* We convert the zigzag-order table to natural array order. */
00596       quant_ptr->quantval[natural_order[i]] = (UINT16) tmp;
00597     }
00598 
00599     if (cinfo->err->trace_level >= 2) {
00600       for (i = 0; i < DCTSIZE2; i += 8) {
00601     TRACEMS8(cinfo, 2, JTRC_QUANTVALS,
00602          quant_ptr->quantval[i],   quant_ptr->quantval[i+1],
00603          quant_ptr->quantval[i+2], quant_ptr->quantval[i+3],
00604          quant_ptr->quantval[i+4], quant_ptr->quantval[i+5],
00605          quant_ptr->quantval[i+6], quant_ptr->quantval[i+7]);
00606       }
00607     }
00608 
00609     length -= count;
00610     if (prec) length -= count;
00611   }
00612 
00613   if (length != 0)
00614     ERREXIT(cinfo, JERR_BAD_LENGTH);
00615 
00616   INPUT_SYNC(cinfo);
00617   return TRUE;
00618 }
00619 
00620 
00621 LOCAL(boolean)
00622 get_dri (j_decompress_ptr cinfo)
00623 /* Process a DRI marker */
00624 {
00625   INT32 length;
00626   unsigned int tmp;
00627   INPUT_VARS(cinfo);
00628 
00629   INPUT_2BYTES(cinfo, length, return FALSE);
00630   
00631   if (length != 4)
00632     ERREXIT(cinfo, JERR_BAD_LENGTH);
00633 
00634   INPUT_2BYTES(cinfo, tmp, return FALSE);
00635 
00636   TRACEMS1(cinfo, 1, JTRC_DRI, tmp);
00637 
00638   cinfo->restart_interval = tmp;
00639 
00640   INPUT_SYNC(cinfo);
00641   return TRUE;
00642 }
00643 
00644 
00645 LOCAL(boolean)
00646 get_lse (j_decompress_ptr cinfo)
00647 /* Process an LSE marker */
00648 {
00649   INT32 length;
00650   unsigned int tmp;
00651   int cid;
00652   INPUT_VARS(cinfo);
00653 
00654   if (! cinfo->marker->saw_SOF)
00655     ERREXITS(cinfo, JERR_SOF_BEFORE, "LSE");
00656 
00657   if (cinfo->num_components < 3) goto bad;
00658 
00659   INPUT_2BYTES(cinfo, length, return FALSE);
00660 
00661   if (length != 24)
00662     ERREXIT(cinfo, JERR_BAD_LENGTH);
00663 
00664   INPUT_BYTE(cinfo, tmp, return FALSE);
00665   if (tmp != 0x0D)  /* ID inverse transform specification */
00666     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
00667   INPUT_2BYTES(cinfo, tmp, return FALSE);
00668   if (tmp != MAXJSAMPLE) goto bad;      /* MAXTRANS */
00669   INPUT_BYTE(cinfo, tmp, return FALSE);
00670   if (tmp != 3) goto bad;           /* Nt=3 */
00671   INPUT_BYTE(cinfo, cid, return FALSE);
00672   if (cid != cinfo->comp_info[1].component_id) goto bad;
00673   INPUT_BYTE(cinfo, cid, return FALSE);
00674   if (cid != cinfo->comp_info[0].component_id) goto bad;
00675   INPUT_BYTE(cinfo, cid, return FALSE);
00676   if (cid != cinfo->comp_info[2].component_id) goto bad;
00677   INPUT_BYTE(cinfo, tmp, return FALSE);
00678   if (tmp != 0x80) goto bad;        /* F1: CENTER1=1, NORM1=0 */
00679   INPUT_2BYTES(cinfo, tmp, return FALSE);
00680   if (tmp != 0) goto bad;           /* A(1,1)=0 */
00681   INPUT_2BYTES(cinfo, tmp, return FALSE);
00682   if (tmp != 0) goto bad;           /* A(1,2)=0 */
00683   INPUT_BYTE(cinfo, tmp, return FALSE);
00684   if (tmp != 0) goto bad;       /* F2: CENTER2=0, NORM2=0 */
00685   INPUT_2BYTES(cinfo, tmp, return FALSE);
00686   if (tmp != 1) goto bad;           /* A(2,1)=1 */
00687   INPUT_2BYTES(cinfo, tmp, return FALSE);
00688   if (tmp != 0) goto bad;           /* A(2,2)=0 */
00689   INPUT_BYTE(cinfo, tmp, return FALSE);
00690   if (tmp != 0) goto bad;       /* F3: CENTER3=0, NORM3=0 */
00691   INPUT_2BYTES(cinfo, tmp, return FALSE);
00692   if (tmp != 1) goto bad;           /* A(3,1)=1 */
00693   INPUT_2BYTES(cinfo, tmp, return FALSE);
00694   if (tmp != 0) {               /* A(3,2)=0 */
00695     bad:
00696     ERREXIT(cinfo, JERR_CONVERSION_NOTIMPL);
00697   }
00698 
00699   /* OK, valid transform that we can handle. */
00700   cinfo->color_transform = JCT_SUBTRACT_GREEN;
00701 
00702   INPUT_SYNC(cinfo);
00703   return TRUE;
00704 }
00705 
00706 
00707 /*
00708  * Routines for processing APPn and COM markers.
00709  * These are either saved in memory or discarded, per application request.
00710  * APP0 and APP14 are specially checked to see if they are
00711  * JFIF and Adobe markers, respectively.
00712  */
00713 
00714 #define APP0_DATA_LEN   14  /* Length of interesting data in APP0 */
00715 #define APP14_DATA_LEN  12  /* Length of interesting data in APP14 */
00716 #define APPN_DATA_LEN   14  /* Must be the largest of the above!! */
00717 
00718 
00719 LOCAL(void)
00720 examine_app0 (j_decompress_ptr cinfo, JOCTET FAR * data,
00721           unsigned int datalen, INT32 remaining)
00722 /* Examine first few bytes from an APP0.
00723  * Take appropriate action if it is a JFIF marker.
00724  * datalen is # of bytes at data[], remaining is length of rest of marker data.
00725  */
00726 {
00727   INT32 totallen = (INT32) datalen + remaining;
00728 
00729   if (datalen >= APP0_DATA_LEN &&
00730       GETJOCTET(data[0]) == 0x4A &&
00731       GETJOCTET(data[1]) == 0x46 &&
00732       GETJOCTET(data[2]) == 0x49 &&
00733       GETJOCTET(data[3]) == 0x46 &&
00734       GETJOCTET(data[4]) == 0) {
00735     /* Found JFIF APP0 marker: save info */
00736     cinfo->saw_JFIF_marker = TRUE;
00737     cinfo->JFIF_major_version = GETJOCTET(data[5]);
00738     cinfo->JFIF_minor_version = GETJOCTET(data[6]);
00739     cinfo->density_unit = GETJOCTET(data[7]);
00740     cinfo->X_density = (GETJOCTET(data[8]) << 8) + GETJOCTET(data[9]);
00741     cinfo->Y_density = (GETJOCTET(data[10]) << 8) + GETJOCTET(data[11]);
00742     /* Check version.
00743      * Major version must be 1 or 2, anything else signals an incompatible
00744      * change.
00745      * (We used to treat this as an error, but now it's a nonfatal warning,
00746      * because some bozo at Hijaak couldn't read the spec.)
00747      * Minor version should be 0..2, but process anyway if newer.
00748      */
00749     if (cinfo->JFIF_major_version != 1 && cinfo->JFIF_major_version != 2)
00750       WARNMS2(cinfo, JWRN_JFIF_MAJOR,
00751           cinfo->JFIF_major_version, cinfo->JFIF_minor_version);
00752     /* Generate trace messages */
00753     TRACEMS5(cinfo, 1, JTRC_JFIF,
00754          cinfo->JFIF_major_version, cinfo->JFIF_minor_version,
00755          cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
00756     /* Validate thumbnail dimensions and issue appropriate messages */
00757     if (GETJOCTET(data[12]) | GETJOCTET(data[13]))
00758       TRACEMS2(cinfo, 1, JTRC_JFIF_THUMBNAIL,
00759            GETJOCTET(data[12]), GETJOCTET(data[13]));
00760     totallen -= APP0_DATA_LEN;
00761     if (totallen !=
00762     ((INT32)GETJOCTET(data[12]) * (INT32)GETJOCTET(data[13]) * (INT32) 3))
00763       TRACEMS1(cinfo, 1, JTRC_JFIF_BADTHUMBNAILSIZE, (int) totallen);
00764   } else if (datalen >= 6 &&
00765       GETJOCTET(data[0]) == 0x4A &&
00766       GETJOCTET(data[1]) == 0x46 &&
00767       GETJOCTET(data[2]) == 0x58 &&
00768       GETJOCTET(data[3]) == 0x58 &&
00769       GETJOCTET(data[4]) == 0) {
00770     /* Found JFIF "JFXX" extension APP0 marker */
00771     /* The library doesn't actually do anything with these,
00772      * but we try to produce a helpful trace message.
00773      */
00774     switch (GETJOCTET(data[5])) {
00775     case 0x10:
00776       TRACEMS1(cinfo, 1, JTRC_THUMB_JPEG, (int) totallen);
00777       break;
00778     case 0x11:
00779       TRACEMS1(cinfo, 1, JTRC_THUMB_PALETTE, (int) totallen);
00780       break;
00781     case 0x13:
00782       TRACEMS1(cinfo, 1, JTRC_THUMB_RGB, (int) totallen);
00783       break;
00784     default:
00785       TRACEMS2(cinfo, 1, JTRC_JFIF_EXTENSION,
00786            GETJOCTET(data[5]), (int) totallen);
00787       break;
00788     }
00789   } else {
00790     /* Start of APP0 does not match "JFIF" or "JFXX", or too short */
00791     TRACEMS1(cinfo, 1, JTRC_APP0, (int) totallen);
00792   }
00793 }
00794 
00795 
00796 LOCAL(void)
00797 examine_app14 (j_decompress_ptr cinfo, JOCTET FAR * data,
00798            unsigned int datalen, INT32 remaining)
00799 /* Examine first few bytes from an APP14.
00800  * Take appropriate action if it is an Adobe marker.
00801  * datalen is # of bytes at data[], remaining is length of rest of marker data.
00802  */
00803 {
00804   unsigned int version, flags0, flags1, transform;
00805 
00806   if (datalen >= APP14_DATA_LEN &&
00807       GETJOCTET(data[0]) == 0x41 &&
00808       GETJOCTET(data[1]) == 0x64 &&
00809       GETJOCTET(data[2]) == 0x6F &&
00810       GETJOCTET(data[3]) == 0x62 &&
00811       GETJOCTET(data[4]) == 0x65) {
00812     /* Found Adobe APP14 marker */
00813     version = (GETJOCTET(data[5]) << 8) + GETJOCTET(data[6]);
00814     flags0 = (GETJOCTET(data[7]) << 8) + GETJOCTET(data[8]);
00815     flags1 = (GETJOCTET(data[9]) << 8) + GETJOCTET(data[10]);
00816     transform = GETJOCTET(data[11]);
00817     TRACEMS4(cinfo, 1, JTRC_ADOBE, version, flags0, flags1, transform);
00818     cinfo->saw_Adobe_marker = TRUE;
00819     cinfo->Adobe_transform = (UINT8) transform;
00820   } else {
00821     /* Start of APP14 does not match "Adobe", or too short */
00822     TRACEMS1(cinfo, 1, JTRC_APP14, (int) (datalen + remaining));
00823   }
00824 }
00825 
00826 
00827 METHODDEF(boolean)
00828 get_interesting_appn (j_decompress_ptr cinfo)
00829 /* Process an APP0 or APP14 marker without saving it */
00830 {
00831   INT32 length;
00832   JOCTET b[APPN_DATA_LEN];
00833   unsigned int i, numtoread;
00834   INPUT_VARS(cinfo);
00835 
00836   INPUT_2BYTES(cinfo, length, return FALSE);
00837   length -= 2;
00838 
00839   /* get the interesting part of the marker data */
00840   if (length >= APPN_DATA_LEN)
00841     numtoread = APPN_DATA_LEN;
00842   else if (length > 0)
00843     numtoread = (unsigned int) length;
00844   else
00845     numtoread = 0;
00846   for (i = 0; i < numtoread; i++)
00847     INPUT_BYTE(cinfo, b[i], return FALSE);
00848   length -= numtoread;
00849 
00850   /* process it */
00851   switch (cinfo->unread_marker) {
00852   case M_APP0:
00853     examine_app0(cinfo, (JOCTET FAR *) b, numtoread, length);
00854     break;
00855   case M_APP14:
00856     examine_app14(cinfo, (JOCTET FAR *) b, numtoread, length);
00857     break;
00858   default:
00859     /* can't get here unless jpeg_save_markers chooses wrong processor */
00860     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
00861     break;
00862   }
00863 
00864   /* skip any remaining data -- could be lots */
00865   INPUT_SYNC(cinfo);
00866   if (length > 0)
00867     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00868 
00869   return TRUE;
00870 }
00871 
00872 
00873 #ifdef SAVE_MARKERS_SUPPORTED
00874 
00875 METHODDEF(boolean)
00876 save_marker (j_decompress_ptr cinfo)
00877 /* Save an APPn or COM marker into the marker list */
00878 {
00879   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
00880   jpeg_saved_marker_ptr cur_marker = marker->cur_marker;
00881   unsigned int bytes_read, data_length;
00882   JOCTET FAR * data;
00883   INT32 length = 0;
00884   INPUT_VARS(cinfo);
00885 
00886   if (cur_marker == NULL) {
00887     /* begin reading a marker */
00888     INPUT_2BYTES(cinfo, length, return FALSE);
00889     length -= 2;
00890     if (length >= 0) {      /* watch out for bogus length word */
00891       /* figure out how much we want to save */
00892       unsigned int limit;
00893       if (cinfo->unread_marker == (int) M_COM)
00894     limit = marker->length_limit_COM;
00895       else
00896     limit = marker->length_limit_APPn[cinfo->unread_marker - (int) M_APP0];
00897       if ((unsigned int) length < limit)
00898     limit = (unsigned int) length;
00899       /* allocate and initialize the marker item */
00900       cur_marker = (jpeg_saved_marker_ptr)
00901     (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00902                     SIZEOF(struct jpeg_marker_struct) + limit);
00903       cur_marker->next = NULL;
00904       cur_marker->marker = (UINT8) cinfo->unread_marker;
00905       cur_marker->original_length = (unsigned int) length;
00906       cur_marker->data_length = limit;
00907       /* data area is just beyond the jpeg_marker_struct */
00908       data = cur_marker->data = (JOCTET FAR *) (cur_marker + 1);
00909       marker->cur_marker = cur_marker;
00910       marker->bytes_read = 0;
00911       bytes_read = 0;
00912       data_length = limit;
00913     } else {
00914       /* deal with bogus length word */
00915       bytes_read = data_length = 0;
00916       data = NULL;
00917     }
00918   } else {
00919     /* resume reading a marker */
00920     bytes_read = marker->bytes_read;
00921     data_length = cur_marker->data_length;
00922     data = cur_marker->data + bytes_read;
00923   }
00924 
00925   while (bytes_read < data_length) {
00926     INPUT_SYNC(cinfo);      /* move the restart point to here */
00927     marker->bytes_read = bytes_read;
00928     /* If there's not at least one byte in buffer, suspend */
00929     MAKE_BYTE_AVAIL(cinfo, return FALSE);
00930     /* Copy bytes with reasonable rapidity */
00931     while (bytes_read < data_length && bytes_in_buffer > 0) {
00932       *data++ = *next_input_byte++;
00933       bytes_in_buffer--;
00934       bytes_read++;
00935     }
00936   }
00937 
00938   /* Done reading what we want to read */
00939   if (cur_marker != NULL) { /* will be NULL if bogus length word */
00940     /* Add new marker to end of list */
00941     if (cinfo->marker_list == NULL) {
00942       cinfo->marker_list = cur_marker;
00943     } else {
00944       jpeg_saved_marker_ptr prev = cinfo->marker_list;
00945       while (prev->next != NULL)
00946     prev = prev->next;
00947       prev->next = cur_marker;
00948     }
00949     /* Reset pointer & calc remaining data length */
00950     data = cur_marker->data;
00951     length = cur_marker->original_length - data_length;
00952   }
00953   /* Reset to initial state for next marker */
00954   marker->cur_marker = NULL;
00955 
00956   /* Process the marker if interesting; else just make a generic trace msg */
00957   switch (cinfo->unread_marker) {
00958   case M_APP0:
00959     examine_app0(cinfo, data, data_length, length);
00960     break;
00961   case M_APP14:
00962     examine_app14(cinfo, data, data_length, length);
00963     break;
00964   default:
00965     TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker,
00966          (int) (data_length + length));
00967     break;
00968   }
00969 
00970   /* skip any remaining data -- could be lots */
00971   INPUT_SYNC(cinfo);        /* do before skip_input_data */
00972   if (length > 0)
00973     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00974 
00975   return TRUE;
00976 }
00977 
00978 #endif /* SAVE_MARKERS_SUPPORTED */
00979 
00980 
00981 METHODDEF(boolean)
00982 skip_variable (j_decompress_ptr cinfo)
00983 /* Skip over an unknown or uninteresting variable-length marker */
00984 {
00985   INT32 length;
00986   INPUT_VARS(cinfo);
00987 
00988   INPUT_2BYTES(cinfo, length, return FALSE);
00989   length -= 2;
00990   
00991   TRACEMS2(cinfo, 1, JTRC_MISC_MARKER, cinfo->unread_marker, (int) length);
00992 
00993   INPUT_SYNC(cinfo);        /* do before skip_input_data */
00994   if (length > 0)
00995     (*cinfo->src->skip_input_data) (cinfo, (long) length);
00996 
00997   return TRUE;
00998 }
00999 
01000 
01001 /*
01002  * Find the next JPEG marker, save it in cinfo->unread_marker.
01003  * Returns FALSE if had to suspend before reaching a marker;
01004  * in that case cinfo->unread_marker is unchanged.
01005  *
01006  * Note that the result might not be a valid marker code,
01007  * but it will never be 0 or FF.
01008  */
01009 
01010 LOCAL(boolean)
01011 next_marker (j_decompress_ptr cinfo)
01012 {
01013   int c;
01014   INPUT_VARS(cinfo);
01015 
01016   for (;;) {
01017     INPUT_BYTE(cinfo, c, return FALSE);
01018     /* Skip any non-FF bytes.
01019      * This may look a bit inefficient, but it will not occur in a valid file.
01020      * We sync after each discarded byte so that a suspending data source
01021      * can discard the byte from its buffer.
01022      */
01023     while (c != 0xFF) {
01024       cinfo->marker->discarded_bytes++;
01025       INPUT_SYNC(cinfo);
01026       INPUT_BYTE(cinfo, c, return FALSE);
01027     }
01028     /* This loop swallows any duplicate FF bytes.  Extra FFs are legal as
01029      * pad bytes, so don't count them in discarded_bytes.  We assume there
01030      * will not be so many consecutive FF bytes as to overflow a suspending
01031      * data source's input buffer.
01032      */
01033     do {
01034       INPUT_BYTE(cinfo, c, return FALSE);
01035     } while (c == 0xFF);
01036     if (c != 0)
01037       break;            /* found a valid marker, exit loop */
01038     /* Reach here if we found a stuffed-zero data sequence (FF/00).
01039      * Discard it and loop back to try again.
01040      */
01041     cinfo->marker->discarded_bytes += 2;
01042     INPUT_SYNC(cinfo);
01043   }
01044 
01045   if (cinfo->marker->discarded_bytes != 0) {
01046     WARNMS2(cinfo, JWRN_EXTRANEOUS_DATA, cinfo->marker->discarded_bytes, c);
01047     cinfo->marker->discarded_bytes = 0;
01048   }
01049 
01050   cinfo->unread_marker = c;
01051 
01052   INPUT_SYNC(cinfo);
01053   return TRUE;
01054 }
01055 
01056 
01057 LOCAL(boolean)
01058 first_marker (j_decompress_ptr cinfo)
01059 /* Like next_marker, but used to obtain the initial SOI marker. */
01060 /* For this marker, we do not allow preceding garbage or fill; otherwise,
01061  * we might well scan an entire input file before realizing it ain't JPEG.
01062  * If an application wants to process non-JFIF files, it must seek to the
01063  * SOI before calling the JPEG library.
01064  */
01065 {
01066   int c, c2;
01067   INPUT_VARS(cinfo);
01068 
01069   INPUT_BYTE(cinfo, c, return FALSE);
01070   INPUT_BYTE(cinfo, c2, return FALSE);
01071   if (c != 0xFF || c2 != (int) M_SOI)
01072     ERREXIT2(cinfo, JERR_NO_SOI, c, c2);
01073 
01074   cinfo->unread_marker = c2;
01075 
01076   INPUT_SYNC(cinfo);
01077   return TRUE;
01078 }
01079 
01080 
01081 /*
01082  * Read markers until SOS or EOI.
01083  *
01084  * Returns same codes as are defined for jpeg_consume_input:
01085  * JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
01086  *
01087  * Note: This function may return a pseudo SOS marker (with zero
01088  * component number) for treat by input controller's consume_input.
01089  * consume_input itself should filter out (skip) the pseudo marker
01090  * after processing for the caller.
01091  */
01092 
01093 METHODDEF(int)
01094 read_markers (j_decompress_ptr cinfo)
01095 {
01096   /* Outer loop repeats once for each marker. */
01097   for (;;) {
01098     /* Collect the marker proper, unless we already did. */
01099     /* NB: first_marker() enforces the requirement that SOI appear first. */
01100     if (cinfo->unread_marker == 0) {
01101       if (! cinfo->marker->saw_SOI) {
01102     if (! first_marker(cinfo))
01103       return JPEG_SUSPENDED;
01104       } else {
01105     if (! next_marker(cinfo))
01106       return JPEG_SUSPENDED;
01107       }
01108     }
01109     /* At this point cinfo->unread_marker contains the marker code and the
01110      * input point is just past the marker proper, but before any parameters.
01111      * A suspension will cause us to return with this state still true.
01112      */
01113     switch (cinfo->unread_marker) {
01114     case M_SOI:
01115       if (! get_soi(cinfo))
01116     return JPEG_SUSPENDED;
01117       break;
01118 
01119     case M_SOF0:        /* Baseline */
01120       if (! get_sof(cinfo, TRUE, FALSE, FALSE))
01121     return JPEG_SUSPENDED;
01122       break;
01123 
01124     case M_SOF1:        /* Extended sequential, Huffman */
01125       if (! get_sof(cinfo, FALSE, FALSE, FALSE))
01126     return JPEG_SUSPENDED;
01127       break;
01128 
01129     case M_SOF2:        /* Progressive, Huffman */
01130       if (! get_sof(cinfo, FALSE, TRUE, FALSE))
01131     return JPEG_SUSPENDED;
01132       break;
01133 
01134     case M_SOF9:        /* Extended sequential, arithmetic */
01135       if (! get_sof(cinfo, FALSE, FALSE, TRUE))
01136     return JPEG_SUSPENDED;
01137       break;
01138 
01139     case M_SOF10:       /* Progressive, arithmetic */
01140       if (! get_sof(cinfo, FALSE, TRUE, TRUE))
01141     return JPEG_SUSPENDED;
01142       break;
01143 
01144     /* Currently unsupported SOFn types */
01145     case M_SOF3:        /* Lossless, Huffman */
01146     case M_SOF5:        /* Differential sequential, Huffman */
01147     case M_SOF6:        /* Differential progressive, Huffman */
01148     case M_SOF7:        /* Differential lossless, Huffman */
01149     case M_JPG:         /* Reserved for JPEG extensions */
01150     case M_SOF11:       /* Lossless, arithmetic */
01151     case M_SOF13:       /* Differential sequential, arithmetic */
01152     case M_SOF14:       /* Differential progressive, arithmetic */
01153     case M_SOF15:       /* Differential lossless, arithmetic */
01154       ERREXIT1(cinfo, JERR_SOF_UNSUPPORTED, cinfo->unread_marker);
01155       break;
01156 
01157     case M_SOS:
01158       if (! get_sos(cinfo))
01159     return JPEG_SUSPENDED;
01160       cinfo->unread_marker = 0; /* processed the marker */
01161       return JPEG_REACHED_SOS;
01162 
01163     case M_EOI:
01164       TRACEMS(cinfo, 1, JTRC_EOI);
01165       cinfo->unread_marker = 0; /* processed the marker */
01166       return JPEG_REACHED_EOI;
01167 
01168     case M_DAC:
01169       if (! get_dac(cinfo))
01170     return JPEG_SUSPENDED;
01171       break;
01172 
01173     case M_DHT:
01174       if (! get_dht(cinfo))
01175     return JPEG_SUSPENDED;
01176       break;
01177 
01178     case M_DQT:
01179       if (! get_dqt(cinfo))
01180     return JPEG_SUSPENDED;
01181       break;
01182 
01183     case M_DRI:
01184       if (! get_dri(cinfo))
01185     return JPEG_SUSPENDED;
01186       break;
01187 
01188     case M_JPG8:
01189       if (! get_lse(cinfo))
01190     return JPEG_SUSPENDED;
01191       break;
01192 
01193     case M_APP0:
01194     case M_APP1:
01195     case M_APP2:
01196     case M_APP3:
01197     case M_APP4:
01198     case M_APP5:
01199     case M_APP6:
01200     case M_APP7:
01201     case M_APP8:
01202     case M_APP9:
01203     case M_APP10:
01204     case M_APP11:
01205     case M_APP12:
01206     case M_APP13:
01207     case M_APP14:
01208     case M_APP15:
01209       if (! (*((my_marker_ptr) cinfo->marker)->process_APPn[
01210         cinfo->unread_marker - (int) M_APP0]) (cinfo))
01211     return JPEG_SUSPENDED;
01212       break;
01213 
01214     case M_COM:
01215       if (! (*((my_marker_ptr) cinfo->marker)->process_COM) (cinfo))
01216     return JPEG_SUSPENDED;
01217       break;
01218 
01219     case M_RST0:        /* these are all parameterless */
01220     case M_RST1:
01221     case M_RST2:
01222     case M_RST3:
01223     case M_RST4:
01224     case M_RST5:
01225     case M_RST6:
01226     case M_RST7:
01227     case M_TEM:
01228       TRACEMS1(cinfo, 1, JTRC_PARMLESS_MARKER, cinfo->unread_marker);
01229       break;
01230 
01231     case M_DNL:         /* Ignore DNL ... perhaps the wrong thing */
01232       if (! skip_variable(cinfo))
01233     return JPEG_SUSPENDED;
01234       break;
01235 
01236     default:            /* must be DHP, EXP, JPGn, or RESn */
01237       /* For now, we treat the reserved markers as fatal errors since they are
01238        * likely to be used to signal incompatible JPEG Part 3 extensions.
01239        * Once the JPEG 3 version-number marker is well defined, this code
01240        * ought to change!
01241        */
01242       ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, cinfo->unread_marker);
01243       break;
01244     }
01245     /* Successfully processed marker, so reset state variable */
01246     cinfo->unread_marker = 0;
01247   } /* end loop */
01248 }
01249 
01250 
01251 /*
01252  * Read a restart marker, which is expected to appear next in the datastream;
01253  * if the marker is not there, take appropriate recovery action.
01254  * Returns FALSE if suspension is required.
01255  *
01256  * This is called by the entropy decoder after it has read an appropriate
01257  * number of MCUs.  cinfo->unread_marker may be nonzero if the entropy decoder
01258  * has already read a marker from the data source.  Under normal conditions
01259  * cinfo->unread_marker will be reset to 0 before returning; if not reset,
01260  * it holds a marker which the decoder will be unable to read past.
01261  */
01262 
01263 METHODDEF(boolean)
01264 read_restart_marker (j_decompress_ptr cinfo)
01265 {
01266   /* Obtain a marker unless we already did. */
01267   /* Note that next_marker will complain if it skips any data. */
01268   if (cinfo->unread_marker == 0) {
01269     if (! next_marker(cinfo))
01270       return FALSE;
01271   }
01272 
01273   if (cinfo->unread_marker ==
01274       ((int) M_RST0 + cinfo->marker->next_restart_num)) {
01275     /* Normal case --- swallow the marker and let entropy decoder continue */
01276     TRACEMS1(cinfo, 3, JTRC_RST, cinfo->marker->next_restart_num);
01277     cinfo->unread_marker = 0;
01278   } else {
01279     /* Uh-oh, the restart markers have been messed up. */
01280     /* Let the data source manager determine how to resync. */
01281     if (! (*cinfo->src->resync_to_restart) (cinfo,
01282                         cinfo->marker->next_restart_num))
01283       return FALSE;
01284   }
01285 
01286   /* Update next-restart state */
01287   cinfo->marker->next_restart_num = (cinfo->marker->next_restart_num + 1) & 7;
01288 
01289   return TRUE;
01290 }
01291 
01292 
01293 /*
01294  * This is the default resync_to_restart method for data source managers
01295  * to use if they don't have any better approach.  Some data source managers
01296  * may be able to back up, or may have additional knowledge about the data
01297  * which permits a more intelligent recovery strategy; such managers would
01298  * presumably supply their own resync method.
01299  *
01300  * read_restart_marker calls resync_to_restart if it finds a marker other than
01301  * the restart marker it was expecting.  (This code is *not* used unless
01302  * a nonzero restart interval has been declared.)  cinfo->unread_marker is
01303  * the marker code actually found (might be anything, except 0 or FF).
01304  * The desired restart marker number (0..7) is passed as a parameter.
01305  * This routine is supposed to apply whatever error recovery strategy seems
01306  * appropriate in order to position the input stream to the next data segment.
01307  * Note that cinfo->unread_marker is treated as a marker appearing before
01308  * the current data-source input point; usually it should be reset to zero
01309  * before returning.
01310  * Returns FALSE if suspension is required.
01311  *
01312  * This implementation is substantially constrained by wanting to treat the
01313  * input as a data stream; this means we can't back up.  Therefore, we have
01314  * only the following actions to work with:
01315  *   1. Simply discard the marker and let the entropy decoder resume at next
01316  *      byte of file.
01317  *   2. Read forward until we find another marker, discarding intervening
01318  *      data.  (In theory we could look ahead within the current bufferload,
01319  *      without having to discard data if we don't find the desired marker.
01320  *      This idea is not implemented here, in part because it makes behavior
01321  *      dependent on buffer size and chance buffer-boundary positions.)
01322  *   3. Leave the marker unread (by failing to zero cinfo->unread_marker).
01323  *      This will cause the entropy decoder to process an empty data segment,
01324  *      inserting dummy zeroes, and then we will reprocess the marker.
01325  *
01326  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
01327  * appropriate if the found marker is a future restart marker (indicating
01328  * that we have missed the desired restart marker, probably because it got
01329  * corrupted).
01330  * We apply #2 or #3 if the found marker is a restart marker no more than
01331  * two counts behind or ahead of the expected one.  We also apply #2 if the
01332  * found marker is not a legal JPEG marker code (it's certainly bogus data).
01333  * If the found marker is a restart marker more than 2 counts away, we do #1
01334  * (too much risk that the marker is erroneous; with luck we will be able to
01335  * resync at some future point).
01336  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
01337  * overrunning the end of a scan.  An implementation limited to single-scan
01338  * files might find it better to apply #2 for markers other than EOI, since
01339  * any other marker would have to be bogus data in that case.
01340  */
01341 
01342 GLOBAL(boolean)
01343 jpeg_resync_to_restart (j_decompress_ptr cinfo, int desired)
01344 {
01345   int marker = cinfo->unread_marker;
01346   int action = 1;
01347   
01348   /* Always put up a warning. */
01349   WARNMS2(cinfo, JWRN_MUST_RESYNC, marker, desired);
01350   
01351   /* Outer loop handles repeated decision after scanning forward. */
01352   for (;;) {
01353     if (marker < (int) M_SOF0)
01354       action = 2;       /* invalid marker */
01355     else if (marker < (int) M_RST0 || marker > (int) M_RST7)
01356       action = 3;       /* valid non-restart marker */
01357     else {
01358       if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
01359       marker == ((int) M_RST0 + ((desired+2) & 7)))
01360     action = 3;     /* one of the next two expected restarts */
01361       else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
01362            marker == ((int) M_RST0 + ((desired-2) & 7)))
01363     action = 2;     /* a prior restart, so advance */
01364       else
01365     action = 1;     /* desired restart or too far away */
01366     }
01367     TRACEMS2(cinfo, 4, JTRC_RECOVERY_ACTION, marker, action);
01368     switch (action) {
01369     case 1:
01370       /* Discard marker and let entropy decoder resume processing. */
01371       cinfo->unread_marker = 0;
01372       return TRUE;
01373     case 2:
01374       /* Scan to the next marker, and repeat the decision loop. */
01375       if (! next_marker(cinfo))
01376     return FALSE;
01377       marker = cinfo->unread_marker;
01378       break;
01379     case 3:
01380       /* Return without advancing past this marker. */
01381       /* Entropy decoder will be forced to process an empty segment. */
01382       return TRUE;
01383     }
01384   } /* end loop */
01385 }
01386 
01387 
01388 /*
01389  * Reset marker processing state to begin a fresh datastream.
01390  */
01391 
01392 METHODDEF(void)
01393 reset_marker_reader (j_decompress_ptr cinfo)
01394 {
01395   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01396 
01397   cinfo->comp_info = NULL;      /* until allocated by get_sof */
01398   cinfo->input_scan_number = 0;     /* no SOS seen yet */
01399   cinfo->unread_marker = 0;     /* no pending marker */
01400   marker->pub.saw_SOI = FALSE;      /* set internal state too */
01401   marker->pub.saw_SOF = FALSE;
01402   marker->pub.discarded_bytes = 0;
01403   marker->cur_marker = NULL;
01404 }
01405 
01406 
01407 /*
01408  * Initialize the marker reader module.
01409  * This is called only once, when the decompression object is created.
01410  */
01411 
01412 GLOBAL(void)
01413 jinit_marker_reader (j_decompress_ptr cinfo)
01414 {
01415   my_marker_ptr marker;
01416   int i;
01417 
01418   /* Create subobject in permanent pool */
01419   marker = (my_marker_ptr)
01420     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
01421                 SIZEOF(my_marker_reader));
01422   cinfo->marker = &marker->pub;
01423   /* Initialize public method pointers */
01424   marker->pub.reset_marker_reader = reset_marker_reader;
01425   marker->pub.read_markers = read_markers;
01426   marker->pub.read_restart_marker = read_restart_marker;
01427   /* Initialize COM/APPn processing.
01428    * By default, we examine and then discard APP0 and APP14,
01429    * but simply discard COM and all other APPn.
01430    */
01431   marker->process_COM = skip_variable;
01432   marker->length_limit_COM = 0;
01433   for (i = 0; i < 16; i++) {
01434     marker->process_APPn[i] = skip_variable;
01435     marker->length_limit_APPn[i] = 0;
01436   }
01437   marker->process_APPn[0] = get_interesting_appn;
01438   marker->process_APPn[14] = get_interesting_appn;
01439   /* Reset marker processing state */
01440   reset_marker_reader(cinfo);
01441 }
01442 
01443 
01444 /*
01445  * Control saving of COM and APPn markers into marker_list.
01446  */
01447 
01448 #ifdef SAVE_MARKERS_SUPPORTED
01449 
01450 GLOBAL(void)
01451 jpeg_save_markers (j_decompress_ptr cinfo, int marker_code,
01452            unsigned int length_limit)
01453 {
01454   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01455   long maxlength;
01456   jpeg_marker_parser_method processor;
01457 
01458   /* Length limit mustn't be larger than what we can allocate
01459    * (should only be a concern in a 16-bit environment).
01460    */
01461   maxlength = cinfo->mem->max_alloc_chunk - SIZEOF(struct jpeg_marker_struct);
01462   if (((long) length_limit) > maxlength)
01463     length_limit = (unsigned int) maxlength;
01464 
01465   /* Choose processor routine to use.
01466    * APP0/APP14 have special requirements.
01467    */
01468   if (length_limit) {
01469     processor = save_marker;
01470     /* If saving APP0/APP14, save at least enough for our internal use. */
01471     if (marker_code == (int) M_APP0 && length_limit < APP0_DATA_LEN)
01472       length_limit = APP0_DATA_LEN;
01473     else if (marker_code == (int) M_APP14 && length_limit < APP14_DATA_LEN)
01474       length_limit = APP14_DATA_LEN;
01475   } else {
01476     processor = skip_variable;
01477     /* If discarding APP0/APP14, use our regular on-the-fly processor. */
01478     if (marker_code == (int) M_APP0 || marker_code == (int) M_APP14)
01479       processor = get_interesting_appn;
01480   }
01481 
01482   if (marker_code == (int) M_COM) {
01483     marker->process_COM = processor;
01484     marker->length_limit_COM = length_limit;
01485   } else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15) {
01486     marker->process_APPn[marker_code - (int) M_APP0] = processor;
01487     marker->length_limit_APPn[marker_code - (int) M_APP0] = length_limit;
01488   } else
01489     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
01490 }
01491 
01492 #endif /* SAVE_MARKERS_SUPPORTED */
01493 
01494 
01495 /*
01496  * Install a special processing method for COM or APPn markers.
01497  */
01498 
01499 GLOBAL(void)
01500 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
01501                jpeg_marker_parser_method routine)
01502 {
01503   my_marker_ptr marker = (my_marker_ptr) cinfo->marker;
01504 
01505   if (marker_code == (int) M_COM)
01506     marker->process_COM = routine;
01507   else if (marker_code >= (int) M_APP0 && marker_code <= (int) M_APP15)
01508     marker->process_APPn[marker_code - (int) M_APP0] = routine;
01509   else
01510     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
01511 }