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