home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / jpeg / jpegv4a.tar / jrdjfif.c < prev    next >
C/C++ Source or Header  |  1993-02-17  |  24KB  |  865 lines

  1. /*
  2.  * jrdjfif.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 1993, 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 standard JPEG file headers/markers.
  9.  * This code will handle "raw JPEG" and JFIF-convention JPEG files.
  10.  *
  11.  * You can also use this module to decode a raw-JPEG or JFIF-standard data
  12.  * stream that is embedded within a larger file.  To do that, you must
  13.  * position the file to the JPEG SOI marker (0xFF/0xD8) that begins the
  14.  * data sequence to be decoded.  If nothing better is possible, you can scan
  15.  * the file until you see the SOI marker, then use JUNGETC to push it back.
  16.  *
  17.  * This module relies on the JGETC macro and the read_jpeg_data method (which
  18.  * is provided by the user interface) to read from the JPEG data stream.
  19.  * Therefore, this module is not dependent on any particular assumption about
  20.  * the data source; it need not be a stdio stream at all.  (This fact does
  21.  * NOT carry over to more complex JPEG file formats such as JPEG-in-TIFF;
  22.  * those format control modules may well need to assume stdio input.)
  23.  *
  24.  * These routines are invoked via the methods read_file_header,
  25.  * read_scan_header, read_jpeg_data, read_scan_trailer, and read_file_trailer.
  26.  */
  27.  
  28. #include "jinclude.h"
  29.  
  30. #ifdef JFIF_SUPPORTED
  31.  
  32.  
  33. typedef enum {            /* JPEG marker codes */
  34.   M_SOF0  = 0xc0,
  35.   M_SOF1  = 0xc1,
  36.   M_SOF2  = 0xc2,
  37.   M_SOF3  = 0xc3,
  38.   
  39.   M_SOF5  = 0xc5,
  40.   M_SOF6  = 0xc6,
  41.   M_SOF7  = 0xc7,
  42.   
  43.   M_JPG   = 0xc8,
  44.   M_SOF9  = 0xc9,
  45.   M_SOF10 = 0xca,
  46.   M_SOF11 = 0xcb,
  47.   
  48.   M_SOF13 = 0xcd,
  49.   M_SOF14 = 0xce,
  50.   M_SOF15 = 0xcf,
  51.   
  52.   M_DHT   = 0xc4,
  53.   
  54.   M_DAC   = 0xcc,
  55.   
  56.   M_RST0  = 0xd0,
  57.   M_RST1  = 0xd1,
  58.   M_RST2  = 0xd2,
  59.   M_RST3  = 0xd3,
  60.   M_RST4  = 0xd4,
  61.   M_RST5  = 0xd5,
  62.   M_RST6  = 0xd6,
  63.   M_RST7  = 0xd7,
  64.   
  65.   M_SOI   = 0xd8,
  66.   M_EOI   = 0xd9,
  67.   M_SOS   = 0xda,
  68.   M_DQT   = 0xdb,
  69.   M_DNL   = 0xdc,
  70.   M_DRI   = 0xdd,
  71.   M_DHP   = 0xde,
  72.   M_EXP   = 0xdf,
  73.   
  74.   M_APP0  = 0xe0,
  75.   M_APP15 = 0xef,
  76.   
  77.   M_JPG0  = 0xf0,
  78.   M_JPG13 = 0xfd,
  79.   M_COM   = 0xfe,
  80.   
  81.   M_TEM   = 0x01,
  82.   
  83.   M_ERROR = 0x100
  84. } JPEG_MARKER;
  85.  
  86.  
  87. /*
  88.  * Reload the input buffer after it's been emptied, and return the next byte.
  89.  * This is exported for direct use by the entropy decoder.
  90.  * See the JGETC macro for calling conditions.  Note in particular that
  91.  * read_jpeg_data may NOT return EOF.  If no more data is available, it must
  92.  * exit via ERREXIT, or perhaps synthesize fake data (such as an RST marker).
  93.  * For error recovery purposes, synthesizing an EOI marker is probably best.
  94.  *
  95.  * For this header control module, read_jpeg_data is supplied by the
  96.  * user interface.  However, header formats that require random access
  97.  * to the input file would need to supply their own code.  This code is
  98.  * left here to indicate what is required.
  99.  */
  100.  
  101. #if 0                /* not needed in this module */
  102.  
  103. METHODDEF int
  104. read_jpeg_data (decompress_info_ptr cinfo)
  105. {
  106.   cinfo->next_input_byte = cinfo->input_buffer + MIN_UNGET;
  107.  
  108.   cinfo->bytes_in_buffer = (int) JFREAD(cinfo->input_file,
  109.                     cinfo->next_input_byte,
  110.                     JPEG_BUF_SIZE);
  111.   
  112.   if (cinfo->bytes_in_buffer <= 0) {
  113.     WARNMS(cinfo->emethods, "Premature EOF in JPEG file");
  114.     cinfo->next_input_byte[0] = (char) 0xFF;
  115.     cinfo->next_input_byte[1] = (char) M_EOI;
  116.     cinfo->bytes_in_buffer = 2;
  117.   }
  118.  
  119.   return JGETC(cinfo);
  120. }
  121.  
  122. #endif
  123.  
  124.  
  125. /*
  126.  * Routines to parse JPEG markers & save away the useful info.
  127.  */
  128.  
  129.  
  130. LOCAL INT32
  131. get_2bytes (decompress_info_ptr cinfo)
  132. /* Get a 2-byte unsigned integer (e.g., a marker parameter length field) */
  133. {
  134.   INT32 a;
  135.   
  136.   a = JGETC(cinfo);
  137.   return (a << 8) + JGETC(cinfo);
  138. }
  139.  
  140.  
  141. LOCAL void
  142. skip_variable (decompress_info_ptr cinfo, int code)
  143. /* Skip over an unknown or uninteresting variable-length marker */
  144. {
  145.   INT32 length;
  146.   
  147.   length = get_2bytes(cinfo);
  148.   
  149.   TRACEMS2(cinfo->emethods, 1,
  150.        "Skipping marker 0x%02x, length %u", code, (int) length);
  151.   
  152.   for (length -= 2; length > 0; length--)
  153.     (void) JGETC(cinfo);
  154. }
  155.  
  156.  
  157. LOCAL void
  158. get_dht (decompress_info_ptr cinfo)
  159. /* Process a DHT marker */
  160. {
  161.   INT32 length;
  162.   UINT8 bits[17];
  163.   UINT8 huffval[256];
  164.   int i, index, count;
  165.   HUFF_TBL **htblptr;
  166.   
  167.   length = get_2bytes(cinfo)-2;
  168.   
  169.   while (length > 0) {
  170.     index = JGETC(cinfo);
  171.  
  172.     TRACEMS1(cinfo->emethods, 1, "Define Huffman Table 0x%02x", index);
  173.       
  174.     bits[0] = 0;
  175.     count = 0;
  176.     for (i = 1; i <= 16; i++) {
  177.       bits[i] = (UINT8) JGETC(cinfo);
  178.       count += bits[i];
  179.     }
  180.  
  181.     length -= 1 + 16;
  182.  
  183.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  184.          bits[1], bits[2], bits[3], bits[4],
  185.          bits[5], bits[6], bits[7], bits[8]);
  186.     TRACEMS8(cinfo->emethods, 2, "        %3d %3d %3d %3d %3d %3d %3d %3d",
  187.          bits[9], bits[10], bits[11], bits[12],
  188.          bits[13], bits[14], bits[15], bits[16]);
  189.  
  190.     if (count > 256 || ((INT32) count) > length)
  191.       ERREXIT(cinfo->emethods, "Bogus DHT counts");
  192.  
  193.     for (i = 0; i < count; i++)
  194.       huffval[i] = (UINT8) JGETC(cinfo);
  195.  
  196.     length -= count;
  197.  
  198.     if (index & 0x10) {        /* AC table definition */
  199.       index -= 0x10;
  200.       htblptr = &cinfo->ac_huff_tbl_ptrs[index];
  201.     } else {            /* DC table definition */
  202.       htblptr = &cinfo->dc_huff_tbl_ptrs[index];
  203.     }
  204.  
  205.     if (index < 0 || index >= NUM_HUFF_TBLS)
  206.       ERREXIT1(cinfo->emethods, "Bogus DHT index %d", index);
  207.  
  208.     if (*htblptr == NULL)
  209.       *htblptr = (HUFF_TBL *) (*cinfo->emethods->alloc_small) (SIZEOF(HUFF_TBL));
  210.   
  211.     MEMCOPY((*htblptr)->bits, bits, SIZEOF((*htblptr)->bits));
  212.     MEMCOPY((*htblptr)->huffval, huffval, SIZEOF((*htblptr)->huffval));
  213.     }
  214. }
  215.  
  216.  
  217. LOCAL void
  218. get_dac (decompress_info_ptr cinfo)
  219. /* Process a DAC marker */
  220. {
  221.   INT32 length;
  222.   int index, val;
  223.  
  224.   length = get_2bytes(cinfo)-2;
  225.   
  226.   while (length > 0) {
  227.     index = JGETC(cinfo);
  228.     val = JGETC(cinfo);
  229.  
  230.     TRACEMS2(cinfo->emethods, 1,
  231.          "Define Arithmetic Table 0x%02x: 0x%02x", index, val);
  232.  
  233.     if (index < 0 || index >= (2*NUM_ARITH_TBLS))
  234.       ERREXIT1(cinfo->emethods, "Bogus DAC index %d", index);
  235.  
  236.     if (index >= NUM_ARITH_TBLS) { /* define AC table */
  237.       cinfo->arith_ac_K[index-NUM_ARITH_TBLS] = (UINT8) val;
  238.     } else {            /* define DC table */
  239.       cinfo->arith_dc_L[index] = (UINT8) (val & 0x0F);
  240.       cinfo->arith_dc_U[index] = (UINT8) (val >> 4);
  241.       if (cinfo->arith_dc_L[index] > cinfo->arith_dc_U[index])
  242.     ERREXIT1(cinfo->emethods, "Bogus DAC value 0x%x", val);
  243.     }
  244.  
  245.     length -= 2;
  246.   }
  247. }
  248.  
  249.  
  250. LOCAL void
  251. get_dqt (decompress_info_ptr cinfo)
  252. /* Process a DQT marker */
  253. {
  254.   INT32 length;
  255.   int n, i, prec;
  256.   UINT16 tmp;
  257.   QUANT_TBL_PTR quant_ptr;
  258.   
  259.   length = get_2bytes(cinfo) - 2;
  260.   
  261.   while (length > 0) {
  262.     n = JGETC(cinfo);
  263.     prec = n >> 4;
  264.     n &= 0x0F;
  265.  
  266.     TRACEMS2(cinfo->emethods, 1,
  267.          "Define Quantization Table %d  precision %d", n, prec);
  268.  
  269.     if (n >= NUM_QUANT_TBLS)
  270.       ERREXIT1(cinfo->emethods, "Bogus table number %d", n);
  271.       
  272.     if (cinfo->quant_tbl_ptrs[n] == NULL)
  273.       cinfo->quant_tbl_ptrs[n] = (QUANT_TBL_PTR)
  274.     (*cinfo->emethods->alloc_small) (SIZEOF(QUANT_TBL));
  275.     quant_ptr = cinfo->quant_tbl_ptrs[n];
  276.  
  277.     for (i = 0; i < DCTSIZE2; i++) {
  278.       tmp = JGETC(cinfo);
  279.       if (prec)
  280.     tmp = (tmp<<8) + JGETC(cinfo);
  281.       quant_ptr[i] = tmp;
  282.     }
  283.  
  284.     for (i = 0; i < DCTSIZE2; i += 8) {
  285.       TRACEMS8(cinfo->emethods, 2, "        %4u %4u %4u %4u %4u %4u %4u %4u",
  286.            quant_ptr[i  ], quant_ptr[i+1], quant_ptr[i+2], quant_ptr[i+3],
  287.            quant_ptr[i+4], quant_ptr[i+5], quant_ptr[i+6], quant_ptr[i+7]);
  288.     }
  289.  
  290.     length -= DCTSIZE2+1;
  291.     if (prec) length -= DCTSIZE2;
  292.   }
  293. }
  294.  
  295.  
  296. LOCAL void
  297. get_dri (decompress_info_ptr cinfo)
  298. /* Process a DRI marker */
  299. {
  300.   if (get_2bytes(cinfo) != 4)
  301.     ERREXIT(cinfo->emethods, "Bogus length in DRI");
  302.  
  303.   cinfo->restart_interval = (UINT16) get_2bytes(cinfo);
  304.  
  305.   TRACEMS1(cinfo->emethods, 1,
  306.        "Define Restart Interval %u", cinfo->restart_interval);
  307. }
  308.  
  309.  
  310. LOCAL void
  311. get_app0 (decompress_info_ptr cinfo)
  312. /* Process an APP0 marker */
  313. {
  314. #define JFIF_LEN 14
  315.   INT32 length;
  316.   UINT8 b[JFIF_LEN];
  317.   int buffp;
  318.  
  319.   length = get_2bytes(cinfo) - 2;
  320.  
  321.   /* See if a JFIF APP0 marker is present */
  322.  
  323.   if (length >= JFIF_LEN) {
  324.     for (buffp = 0; buffp < JFIF_LEN; buffp++)
  325.       b[buffp] = (UINT8) JGETC(cinfo);
  326.     length -= JFIF_LEN;
  327.  
  328.     if (b[0]==0x4A && b[1]==0x46 && b[2]==0x49 && b[3]==0x46 && b[4]==0) {
  329.       /* Found JFIF APP0 marker: check version */
  330.       /* Major version must be 1 */
  331.       if (b[5] != 1)
  332.     ERREXIT2(cinfo->emethods, "Unsupported JFIF revision number %d.%02d",
  333.          b[5], b[6]);
  334.       /* Minor version should be 0..2, but try to process anyway if newer */
  335.       if (b[6] > 2)
  336.     TRACEMS2(cinfo->emethods, 1, "Warning: unknown JFIF revision number %d.%02d",
  337.          b[5], b[6]);
  338.       /* Save info */
  339.       cinfo->density_unit = b[7];
  340.       cinfo->X_density = (b[8] << 8) + b[9];
  341.       cinfo->Y_density = (b[10] << 8) + b[11];
  342.       /* Assume colorspace is YCbCr, unless UI has overridden me */
  343.       if (cinfo->jpeg_color_space == CS_UNKNOWN)
  344.     cinfo->jpeg_color_space = CS_YCbCr;
  345.       TRACEMS3(cinfo->emethods, 1, "JFIF APP0 marker, density %dx%d  %d",
  346.            cinfo->X_density, cinfo->Y_density, cinfo->density_unit);
  347.       if (b[12] | b[13])
  348.     TRACEMS2(cinfo->emethods, 1, "    with %d x %d thumbnail image",
  349.          b[12], b[13]);
  350.       if (length != ((INT32) b[12] * (INT32) b[13] * (INT32) 3))
  351.     TRACEMS1(cinfo->emethods, 1,
  352.          "Warning: thumbnail image size does not match data length %u",
  353.          (int) length);
  354.     } else {
  355.       TRACEMS1(cinfo->emethods, 1, "Unknown APP0 marker (not JFIF), length %u",
  356.            (int) length + JFIF_LEN);
  357.     }
  358.   } else {
  359.     TRACEMS1(cinfo->emethods, 1, "Short APP0 marker, length %u", (int) length);
  360.   }
  361.  
  362.   while (--length >= 0)        /* skip any remaining data */
  363.     (void) JGETC(cinfo);
  364. }
  365.  
  366.  
  367. LOCAL void
  368. get_com (decompress_info_ptr cinfo)
  369. /* Process a COM marker */
  370. /* Actually we just pass this off to an application-supplied routine */
  371. {
  372.   INT32 length;
  373.   
  374.   length = get_2bytes(cinfo) - 2;
  375.   
  376.   TRACEMS1(cinfo->emethods, 1, "Comment, length %u", (int) length);
  377.   
  378.   (*cinfo->methods->process_comment) (cinfo, (long) length);
  379. }
  380.  
  381.  
  382. LOCAL void
  383. get_sof (decompress_info_ptr cinfo, int code)
  384. /* Process a SOFn marker */
  385. {
  386.   INT32 length;
  387.   short ci;
  388.   int c;
  389.   jpeg_component_info * compptr;
  390.   
  391.   length = get_2bytes(cinfo);
  392.   
  393.   cinfo->data_precision = JGETC(cinfo);
  394.   cinfo->image_height   = get_2bytes(cinfo);
  395.   cinfo->image_width    = get_2bytes(cinfo);
  396.   cinfo->num_components = JGETC(cinfo);
  397.  
  398.   TRACEMS4(cinfo->emethods, 1,
  399.        "Start Of Frame 0x%02x: width=%u, height=%u, components=%d",
  400.        code, (int) cinfo->image_width, (int) cinfo->image_height,
  401.        cinfo->num_components);
  402.  
  403.   /* We don't support files in which the image height is initially specified */
  404.   /* as 0 and is later redefined by DNL.  As long as we have to check that,  */
  405.   /* might as well have a general sanity check. */
  406.   if (cinfo->image_height <= 0 || cinfo->image_width <= 0
  407.       || cinfo->num_components <= 0)
  408.     ERREXIT(cinfo->emethods, "Empty JPEG image (DNL not supported)");
  409.  
  410. #ifdef EIGHT_BIT_SAMPLES
  411.   if (cinfo->data_precision != 8)
  412.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  413. #endif
  414. #ifdef TWELVE_BIT_SAMPLES
  415.   if (cinfo->data_precision != 12) /* this needs more thought?? */
  416.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  417. #endif
  418. #ifdef SIXTEEN_BIT_SAMPLES
  419.   if (cinfo->data_precision != 16) /* this needs more thought?? */
  420.     ERREXIT(cinfo->emethods, "Unsupported JPEG data precision");
  421. #endif
  422.  
  423.   if (length != (cinfo->num_components * 3 + 8))
  424.     ERREXIT(cinfo->emethods, "Bogus SOF length");
  425.  
  426.   cinfo->comp_info = (jpeg_component_info *) (*cinfo->emethods->alloc_small)
  427.             (cinfo->num_components * SIZEOF(jpeg_component_info));
  428.   
  429.   for (ci = 0; ci < cinfo->num_components; ci++) {
  430.     compptr = &cinfo->comp_info[ci];
  431.     compptr->component_index = ci;
  432.     compptr->component_id = JGETC(cinfo);
  433.     c = JGETC(cinfo);
  434.     compptr->h_samp_factor = (c >> 4) & 15;
  435.     compptr->v_samp_factor = (c     ) & 15;
  436.     compptr->quant_tbl_no  = JGETC(cinfo);
  437.     compptr->component_needed = TRUE; /* assume all components are wanted */
  438.  
  439.     TRACEMS4(cinfo->emethods, 1, "    Component %d: %dhx%dv q=%d",
  440.          compptr->component_id, compptr->h_samp_factor,
  441.          compptr->v_samp_factor, compptr->quant_tbl_no);
  442.   }
  443. }
  444.  
  445.  
  446. LOCAL void
  447. get_sos (decompress_info_ptr cinfo)
  448. /* Process a SOS marker */
  449. {
  450.   INT32 length;
  451.   int i, ci, n, c, cc;
  452.   jpeg_component_info * compptr;
  453.   
  454.   length = get_2bytes(cinfo);
  455.   
  456.   n = JGETC(cinfo);  /* Number of components */
  457.   cinfo->comps_in_scan = n;
  458.   length -= 3;
  459.   
  460.   if (length != (n * 2 + 3) || n < 1 || n > MAX_COMPS_IN_SCAN)
  461.     ERREXIT(cinfo->emethods, "Bogus SOS length");
  462.  
  463.   TRACEMS1(cinfo->emethods, 1, "Start Of Scan: %d components", n);
  464.   
  465.   for (i = 0; i < n; i++) {
  466.     cc = JGETC(cinfo);
  467.     c = JGETC(cinfo);
  468.     length -= 2;
  469.     
  470.     for (ci = 0; ci < cinfo->num_components; ci++)
  471.       if (cc == cinfo->comp_info[ci].component_id)
  472.     break;
  473.     
  474.     if (ci >= cinfo->num_components)
  475.       ERREXIT(cinfo->emethods, "Invalid component number in SOS");
  476.     
  477.     compptr = &cinfo->comp_info[ci];
  478.     cinfo->cur_comp_info[i] = compptr;
  479.     compptr->dc_tbl_no = (c >> 4) & 15;
  480.     compptr->ac_tbl_no = (c     ) & 15;
  481.     
  482.     TRACEMS3(cinfo->emethods, 1, "    c%d: [dc=%d ac=%d]", cc,
  483.          compptr->dc_tbl_no, compptr->ac_tbl_no);
  484.   }
  485.   
  486.   while (length > 0) {
  487.     (void) JGETC(cinfo);
  488.     length--;
  489.   }
  490. }
  491.  
  492.  
  493. LOCAL void
  494. get_soi (decompress_info_ptr cinfo)
  495. /* Process an SOI marker */
  496. {
  497.   int i;
  498.   
  499.   TRACEMS(cinfo->emethods, 1, "Start of Image");
  500.  
  501.   /* Reset all parameters that are defined to be reset by SOI */
  502.  
  503.   for (i = 0; i < NUM_ARITH_TBLS; i++) {
  504.     cinfo->arith_dc_L[i] = 0;
  505.     cinfo->arith_dc_U[i] = 1;
  506.     cinfo->arith_ac_K[i] = 5;
  507.   }
  508.   cinfo->restart_interval = 0;
  509.  
  510.   cinfo->density_unit = 0;    /* set default JFIF APP0 values */
  511.   cinfo->X_density = 1;
  512.   cinfo->Y_density = 1;
  513.  
  514.   cinfo->CCIR601_sampling = FALSE; /* Assume non-CCIR sampling */
  515. }
  516.  
  517.  
  518. LOCAL int
  519. next_marker (decompress_info_ptr cinfo)
  520. /* Find the next JPEG marker */
  521. /* Note that the output might not be a valid marker code, */
  522. /* but it will never be 0 or FF */
  523. {
  524.   int c, nbytes;
  525.  
  526.   nbytes = 0;
  527.   do {
  528.     do {            /* skip any non-FF bytes */
  529.       nbytes++;
  530.       c = JGETC(cinfo);
  531.     } while (c != 0xFF);
  532.     do {            /* skip any duplicate FFs */
  533.       /* we don't increment nbytes here since extra FFs are legal */
  534.       c = JGETC(cinfo);
  535.     } while (c == 0xFF);
  536.   } while (c == 0);        /* repeat if it was a stuffed FF/00 */
  537.  
  538.   if (nbytes != 1)
  539.     WARNMS2(cinfo->emethods,
  540.         "Corrupt JPEG data: %d extraneous bytes before marker 0x%02x",
  541.         nbytes-1, c);
  542.  
  543.   return c;
  544. }
  545.  
  546.  
  547. LOCAL int
  548. process_tables (decompress_info_ptr cinfo)
  549. /* Scan and process JPEG markers that can appear in any order */
  550. /* Return when an SOI, EOI, SOFn, or SOS is found */
  551. {
  552.   int c;
  553.  
  554.   for (;;) {
  555.     c = next_marker(cinfo);
  556.       
  557.     switch (c) {
  558.     case M_SOF0:
  559.     case M_SOF1:
  560.     case M_SOF2:
  561.     case M_SOF3:
  562.     case M_SOF5:
  563.     case M_SOF6:
  564.     case M_SOF7:
  565.     case M_JPG:
  566.     case M_SOF9:
  567.     case M_SOF10:
  568.     case M_SOF11:
  569.     case M_SOF13:
  570.     case M_SOF14:
  571.     case M_SOF15:
  572.     case M_SOI:
  573.     case M_EOI:
  574.     case M_SOS:
  575.       return c;
  576.       
  577.     case M_DHT:
  578.       get_dht(cinfo);
  579.       break;
  580.       
  581.     case M_DAC:
  582.       get_dac(cinfo);
  583.       break;
  584.       
  585.     case M_DQT:
  586.       get_dqt(cinfo);
  587.       break;
  588.       
  589.     case M_DRI:
  590.       get_dri(cinfo);
  591.       break;
  592.       
  593.     case M_APP0:
  594.       get_app0(cinfo);
  595.       break;
  596.       
  597.     case M_COM:
  598.       get_com(cinfo);
  599.       break;
  600.  
  601.     case M_RST0:        /* these are all parameterless */
  602.     case M_RST1:
  603.     case M_RST2:
  604.     case M_RST3:
  605.     case M_RST4:
  606.     case M_RST5:
  607.     case M_RST6:
  608.     case M_RST7:
  609.     case M_TEM:
  610.       TRACEMS1(cinfo->emethods, 1, "Unexpected marker 0x%02x", c);
  611.       break;
  612.  
  613.     default:    /* must be DNL, DHP, EXP, APPn, JPGn, or RESn */
  614.       skip_variable(cinfo, c);
  615.       break;
  616.     }
  617.   }
  618. }
  619.  
  620.  
  621.  
  622. /*
  623.  * Initialize and read the file header (everything through the SOF marker).
  624.  */
  625.  
  626. METHODDEF void
  627. read_file_header (decompress_info_ptr cinfo)
  628. {
  629.   int c;
  630.  
  631.   /* Demand an SOI marker at the start of the file --- otherwise it's
  632.    * probably not a JPEG file at all.  If the user interface wants to support
  633.    * nonstandard headers in front of the SOI, it must skip over them itself
  634.    * before calling jpeg_decompress().
  635.    */
  636.   if (JGETC(cinfo) != 0xFF  ||  JGETC(cinfo) != (int) M_SOI)
  637.     ERREXIT(cinfo->emethods, "Not a JPEG file");
  638.  
  639.   get_soi(cinfo);        /* OK, process SOI */
  640.  
  641.   /* Process markers until SOF */
  642.   c = process_tables(cinfo);
  643.  
  644.   switch (c) {
  645.   case M_SOF0:
  646.   case M_SOF1:
  647.     get_sof(cinfo, c);
  648.     cinfo->arith_code = FALSE;
  649.     break;
  650.       
  651.   case M_SOF9:
  652.     get_sof(cinfo, c);
  653.     cinfo->arith_code = TRUE;
  654.     break;
  655.  
  656.   default:
  657.     ERREXIT1(cinfo->emethods, "Unsupported SOF marker type 0x%02x", c);
  658.     break;
  659.   }
  660.  
  661.   /* Figure out what colorspace we have */
  662.   /* (too bad the JPEG committee didn't provide a real way to specify this) */
  663.  
  664.   switch (cinfo->num_components) {
  665.   case 1:
  666.     cinfo->jpeg_color_space = CS_GRAYSCALE;
  667.     break;
  668.  
  669.   case 3:
  670.     /* if we saw a JFIF marker, leave it set to YCbCr; */
  671.     /* also leave it alone if UI has provided a value */
  672.     if (cinfo->jpeg_color_space == CS_UNKNOWN) {
  673.       short cid0 = cinfo->comp_info[0].component_id;
  674.       short cid1 = cinfo->comp_info[1].component_id;
  675.       short cid2 = cinfo->comp_info[2].component_id;
  676.  
  677.       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
  678.     cinfo->jpeg_color_space = CS_YCbCr; /* assume it's JFIF w/out marker */
  679.       else if (cid0 == 1 && cid1 == 4 && cid2 == 5)
  680.     cinfo->jpeg_color_space = CS_YIQ; /* prototype's YIQ matrix */
  681.       else {
  682.     TRACEMS3(cinfo->emethods, 1,
  683.          "Unrecognized component IDs %d %d %d, assuming YCbCr",
  684.          cid0, cid1, cid2);
  685.     cinfo->jpeg_color_space = CS_YCbCr;
  686.       }
  687.     }
  688.     break;
  689.  
  690.   case 4:
  691.     cinfo->jpeg_color_space = CS_CMYK;
  692.     break;
  693.  
  694.   default:
  695.     cinfo->jpeg_color_space = CS_UNKNOWN;
  696.     break;
  697.   }
  698. }
  699.  
  700.  
  701. /*
  702.  * Read the start of a scan (everything through the SOS marker).
  703.  * Return TRUE if find SOS, FALSE if find EOI.
  704.  */
  705.  
  706. METHODDEF boolean
  707. read_scan_header (decompress_info_ptr cinfo)
  708. {
  709.   int c;
  710.   
  711.   /* Process markers until SOS or EOI */
  712.   c = process_tables(cinfo);
  713.   
  714.   switch (c) {
  715.   case M_SOS:
  716.     get_sos(cinfo);
  717.     return TRUE;
  718.     
  719.   case M_EOI:
  720.     TRACEMS(cinfo->emethods, 1, "End Of Image");
  721.     return FALSE;
  722.  
  723.   default:
  724.     ERREXIT1(cinfo->emethods, "Unexpected marker 0x%02x", c);
  725.     break;
  726.   }
  727.   return FALSE;            /* keeps lint happy */
  728. }
  729.  
  730.  
  731. /*
  732.  * The entropy decoder calls this routine if it finds a marker other than
  733.  * the restart marker it was expecting.  (This code is *not* used unless
  734.  * a nonzero restart interval has been declared.)  The passed parameter is
  735.  * the marker code actually found (might be anything, except 0 or FF).
  736.  * The desired restart marker is that indicated by cinfo->next_restart_num.
  737.  * This routine is supposed to apply whatever error recovery strategy seems
  738.  * appropriate in order to position the input stream to the next data segment.
  739.  * For some file formats (eg, TIFF) extra information such as tile boundary
  740.  * pointers may be available to help in this decision.
  741.  *
  742.  * This implementation is substantially constrained by wanting to treat the
  743.  * input as a data stream; this means we can't back up.  (For instance, we
  744.  * generally can't fseek() if the input is a Unix pipe.)  Therefore, we have
  745.  * only the following actions to work with:
  746.  *   1. Do nothing, let the entropy decoder resume at next byte of file.
  747.  *   2. Read forward until we find another marker, discarding intervening
  748.  *      data.  (In theory we could look ahead within the current bufferload,
  749.  *      without having to discard data if we don't find the desired marker.
  750.  *      This idea is not implemented here, in part because it makes behavior
  751.  *      dependent on buffer size and chance buffer-boundary positions.)
  752.  *   3. Push back the passed marker (with JUNGETC).  This will cause the
  753.  *      entropy decoder to process an empty data segment, inserting dummy
  754.  *      zeroes, and then re-read the marker we pushed back.
  755.  * #2 is appropriate if we think the desired marker lies ahead, while #3 is
  756.  * appropriate if the found marker is a future restart marker (indicating
  757.  * that we have missed the desired restart marker, probably because it got
  758.  * corrupted).
  759.  
  760.  * We apply #2 or #3 if the found marker is a restart marker no more than
  761.  * two counts behind or ahead of the expected one.  We also apply #2 if the
  762.  * found marker is not a legal JPEG marker code (it's certainly bogus data).
  763.  * If the found marker is a restart marker more than 2 counts away, we do #1
  764.  * (too much risk that the marker is erroneous; with luck we will be able to
  765.  * resync at some future point).
  766.  * For any valid non-restart JPEG marker, we apply #3.  This keeps us from
  767.  * overrunning the end of a scan.  An implementation limited to single-scan
  768.  * files might find it better to apply #2 for markers other than EOI, since
  769.  * any other marker would have to be bogus data in that case.
  770.  */
  771.  
  772. METHODDEF void
  773. resync_to_restart (decompress_info_ptr cinfo, int marker)
  774. {
  775.   int desired = cinfo->next_restart_num;
  776.   int action = 1;
  777.  
  778.   /* Always put up a warning. */
  779.   WARNMS2(cinfo->emethods,
  780.       "Corrupt JPEG data: found 0x%02x marker instead of RST%d",
  781.       marker, desired);
  782.   /* Outer loop handles repeated decision after scanning forward. */
  783.   for (;;) {
  784.     if (marker < (int) M_SOF0)
  785.       action = 2;        /* invalid marker */
  786.     else if (marker < (int) M_RST0 || marker > (int) M_RST7)
  787.       action = 3;        /* valid non-restart marker */
  788.     else {
  789.       if (marker == ((int) M_RST0 + ((desired+1) & 7)) ||
  790.       marker == ((int) M_RST0 + ((desired+2) & 7)))
  791.     action = 3;        /* one of the next two expected restarts */
  792.       else if (marker == ((int) M_RST0 + ((desired-1) & 7)) ||
  793.            marker == ((int) M_RST0 + ((desired-2) & 7)))
  794.     action = 2;        /* a prior restart, so advance */
  795.       else
  796.     action = 1;        /* desired restart or too far away */
  797.     }
  798.     TRACEMS2(cinfo->emethods, 4,
  799.          "At marker 0x%02x, recovery action %d", marker, action);
  800.     switch (action) {
  801.     case 1:
  802.       /* Let entropy decoder resume processing. */
  803.       return;
  804.     case 2:
  805.       /* Scan to the next marker, and repeat the decision loop. */
  806.       marker = next_marker(cinfo);
  807.       break;
  808.     case 3:
  809.       /* Put back this marker & return. */
  810.       /* Entropy decoder will be forced to process an empty segment. */
  811.       JUNGETC(marker, cinfo);
  812.       JUNGETC(0xFF, cinfo);
  813.       return;
  814.     }
  815.   }
  816. }
  817.  
  818.  
  819. /*
  820.  * Finish up after a compressed scan (series of read_jpeg_data calls);
  821.  * prepare for another read_scan_header call.
  822.  */
  823.  
  824. METHODDEF void
  825. read_scan_trailer (decompress_info_ptr cinfo)
  826. {
  827.   /* no work needed */
  828. }
  829.  
  830.  
  831. /*
  832.  * Finish up at the end of the file.
  833.  */
  834.  
  835. METHODDEF void
  836. read_file_trailer (decompress_info_ptr cinfo)
  837. {
  838.   /* no work needed */
  839. }
  840.  
  841.  
  842. /*
  843.  * The method selection routine for standard JPEG header reading.
  844.  * Note that this must be called by the user interface before calling
  845.  * jpeg_decompress.  When a non-JFIF file is to be decompressed (TIFF,
  846.  * perhaps), the user interface must discover the file type and call
  847.  * the appropriate method selection routine.
  848.  */
  849.  
  850. GLOBAL void
  851. jselrjfif (decompress_info_ptr cinfo)
  852. {
  853.   cinfo->methods->read_file_header = read_file_header;
  854.   cinfo->methods->read_scan_header = read_scan_header;
  855.   /* For JFIF/raw-JPEG format, the user interface supplies read_jpeg_data. */
  856. #if 0
  857.   cinfo->methods->read_jpeg_data = read_jpeg_data;
  858. #endif
  859.   cinfo->methods->resync_to_restart = resync_to_restart;
  860.   cinfo->methods->read_scan_trailer = read_scan_trailer;
  861.   cinfo->methods->read_file_trailer = read_file_trailer;
  862. }
  863.  
  864. #endif /* JFIF_SUPPORTED */
  865.