home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jdmarker.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  31.1 KB  |  1,055 lines

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