home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jdinput.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  14.0 KB  |  386 lines

  1. //-------------------------------------------------------------------------//
  2. //          Windows Graphics Programming: Win32 GDI and DirectDraw         //
  3. //                        ISBN  0-13-086985-6                              //
  4. //                                                                         //
  5. //  Modified by: Yuan, Feng                             www.fengyuan.com   //
  6. //  Changes    : C++, exception, in-memory source, BGR byte order          //
  7. //  Version    : 1.00.000, May 31, 2000                                    //
  8. //-------------------------------------------------------------------------//
  9.  
  10. /*
  11.  * jdinput.c
  12.  *
  13.  * Copyright (C) 1991-1997, Thomas G. Lane.
  14.  * This file is part of the Independent JPEG Group's software.
  15.  * For conditions of distribution and use, see the accompanying README file.
  16.  *
  17.  * This file contains input control logic for the JPEG decompressor.
  18.  * These routines are concerned with controlling the decompressor's input
  19.  * processing (marker reading and coefficient decoding).  The actual input
  20.  * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
  21.  */
  22.  
  23. #define JPEG_INTERNALS
  24. #include "jinclude.h"
  25. #include "jpeglib.h"
  26.  
  27.  
  28. /* Private state */
  29.  
  30. typedef struct {
  31.   struct jpeg_input_controller pub; /* public fields */
  32.  
  33.   boolean inheaders;        /* TRUE until first SOS is reached */
  34. } my_input_controller;
  35.  
  36. typedef my_input_controller * my_inputctl_ptr;
  37.  
  38.  
  39. /* Forward declarations */
  40. int consume_markers (j_decompress_ptr cinfo);
  41.  
  42.  
  43. /*
  44.  * Routines to calculate various quantities related to the size of the image.
  45.  */
  46.  
  47. LOCAL(void)
  48. initial_setup (j_decompress_ptr cinfo)
  49. /* Called once, when first SOS marker is reached */
  50. {
  51.   int ci;
  52.   jpeg_component_info *compptr;
  53.  
  54.   /* Make sure image isn't bigger than I can handle */
  55.   if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
  56.       (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
  57.     cinfo->ERREXIT1(JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
  58.  
  59.   /* For now, precision must match compiled-in value... */
  60.   if (cinfo->data_precision != BITS_IN_JSAMPLE)
  61.     cinfo->ERREXIT1(JERR_BAD_PRECISION, cinfo->data_precision);
  62.  
  63.   /* Check that number of components won't exceed internal array sizes */
  64.   if (cinfo->num_components > MAX_COMPONENTS)
  65.     cinfo->ERREXIT2(JERR_COMPONENT_COUNT, cinfo->num_components,
  66.          MAX_COMPONENTS);
  67.  
  68.   /* Compute maximum sampling factors; check factor validity */
  69.   cinfo->max_h_samp_factor = 1;
  70.   cinfo->max_v_samp_factor = 1;
  71.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  72.        ci++, compptr++) {
  73.     if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
  74.     compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
  75.       cinfo->ERREXIT(JERR_BAD_SAMPLING);
  76.     cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
  77.                    compptr->h_samp_factor);
  78.     cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
  79.                    compptr->v_samp_factor);
  80.   }
  81.  
  82.   /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
  83.    * In the full decompressor, this will be overridden by jdmaster.c;
  84.    * but in the transcoder, jdmaster.c is not used, so we must do it here.
  85.    */
  86.   cinfo->min_DCT_scaled_size = DCTSIZE;
  87.  
  88.   /* Compute dimensions of components */
  89.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  90.        ci++, compptr++) {
  91.     compptr->DCT_scaled_size = DCTSIZE;
  92.     /* Size in DCT blocks */
  93.     compptr->width_in_blocks = (JDIMENSION)
  94.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  95.             (long) (cinfo->max_h_samp_factor * DCTSIZE));
  96.     compptr->height_in_blocks = (JDIMENSION)
  97.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  98.             (long) (cinfo->max_v_samp_factor * DCTSIZE));
  99.     /* downsampled_width and downsampled_height will also be overridden by
  100.      * jdmaster.c if we are doing full decompression.  The transcoder library
  101.      * doesn't use these values, but the calling application might.
  102.      */
  103.     /* Size in samples */
  104.     compptr->downsampled_width = (JDIMENSION)
  105.       jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
  106.             (long) cinfo->max_h_samp_factor);
  107.     compptr->downsampled_height = (JDIMENSION)
  108.       jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
  109.             (long) cinfo->max_v_samp_factor);
  110.     /* Mark component needed, until color conversion says otherwise */
  111.     compptr->component_needed = TRUE;
  112.     /* Mark no quantization table yet saved for component */
  113.     compptr->quant_table = NULL;
  114.   }
  115.  
  116.   /* Compute number of fully interleaved MCU rows. */
  117.   cinfo->total_iMCU_rows = (JDIMENSION)
  118.     jdiv_round_up((long) cinfo->image_height,
  119.           (long) (cinfo->max_v_samp_factor*DCTSIZE));
  120.  
  121.   /* Decide whether file contains multiple scans */
  122.   if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
  123.     cinfo->inputctl->has_multiple_scans = TRUE;
  124.   else
  125.     cinfo->inputctl->has_multiple_scans = FALSE;
  126. }
  127.  
  128.  
  129. LOCAL(void)
  130. per_scan_setup (j_decompress_ptr cinfo)
  131. /* Do computations that are needed before processing a JPEG scan */
  132. /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
  133. {
  134.   int ci, mcublks, tmp;
  135.   jpeg_component_info *compptr;
  136.   
  137.   if (cinfo->comps_in_scan == 1) {
  138.     
  139.     /* Noninterleaved (single-component) scan */
  140.     compptr = cinfo->cur_comp_info[0];
  141.     
  142.     /* Overall image size in MCUs */
  143.     cinfo->MCUs_per_row = compptr->width_in_blocks;
  144.     cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
  145.     
  146.     /* For noninterleaved scan, always one block per MCU */
  147.     compptr->MCU_width = 1;
  148.     compptr->MCU_height = 1;
  149.     compptr->MCU_blocks = 1;
  150.     compptr->MCU_sample_width = compptr->DCT_scaled_size;
  151.     compptr->last_col_width = 1;
  152.     /* For noninterleaved scans, it is convenient to define last_row_height
  153.      * as the number of block rows present in the last iMCU row.
  154.      */
  155.     tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  156.     if (tmp == 0) tmp = compptr->v_samp_factor;
  157.     compptr->last_row_height = tmp;
  158.     
  159.     /* Prepare array describing MCU composition */
  160.     cinfo->blocks_in_MCU = 1;
  161.     cinfo->MCU_membership[0] = 0;
  162.     
  163.   } else {
  164.     
  165.     /* Interleaved (multi-component) scan */
  166.     if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
  167.       cinfo->ERREXIT2(JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
  168.            MAX_COMPS_IN_SCAN);
  169.     
  170.     /* Overall image size in MCUs */
  171.     cinfo->MCUs_per_row = (JDIMENSION)
  172.       jdiv_round_up((long) cinfo->image_width,
  173.             (long) (cinfo->max_h_samp_factor*DCTSIZE));
  174.     cinfo->MCU_rows_in_scan = (JDIMENSION)
  175.       jdiv_round_up((long) cinfo->image_height,
  176.             (long) (cinfo->max_v_samp_factor*DCTSIZE));
  177.     
  178.     cinfo->blocks_in_MCU = 0;
  179.     
  180.     for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  181.       compptr = cinfo->cur_comp_info[ci];
  182.       /* Sampling factors give # of blocks of component in each MCU */
  183.       compptr->MCU_width = compptr->h_samp_factor;
  184.       compptr->MCU_height = compptr->v_samp_factor;
  185.       compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
  186.       compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
  187.       /* Figure number of non-dummy blocks in last MCU column & row */
  188.       tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
  189.       if (tmp == 0) tmp = compptr->MCU_width;
  190.       compptr->last_col_width = tmp;
  191.       tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
  192.       if (tmp == 0) tmp = compptr->MCU_height;
  193.       compptr->last_row_height = tmp;
  194.       /* Prepare array describing MCU composition */
  195.       mcublks = compptr->MCU_blocks;
  196.       if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
  197.     cinfo->ERREXIT(JERR_BAD_MCU_SIZE);
  198.       while (mcublks-- > 0) {
  199.     cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
  200.       }
  201.     }
  202.     
  203.   }
  204. }
  205.  
  206.  
  207. /*
  208.  * Save away a copy of the Q-table referenced by each component present
  209.  * in the current scan, unless already saved during a prior scan.
  210.  *
  211.  * In a multiple-scan JPEG file, the encoder could assign different components
  212.  * the same Q-table slot number, but change table definitions between scans
  213.  * so that each component uses a different Q-table.  (The IJG encoder is not
  214.  * currently capable of doing this, but other encoders might.)  Since we want
  215.  * to be able to dequantize all the components at the end of the file, this
  216.  * means that we have to save away the table actually used for each component.
  217.  * We do this by copying the table at the start of the first scan containing
  218.  * the component.
  219.  * The JPEG spec prohibits the encoder from changing the contents of a Q-table
  220.  * slot between scans of a component using that slot.  If the encoder does so
  221.  * anyway, this decoder will simply use the Q-table values that were current
  222.  * at the start of the first scan for the component.
  223.  *
  224.  * The decompressor output side looks only at the saved quant tables,
  225.  * not at the current Q-table slots.
  226.  */
  227.  
  228. LOCAL(void)
  229. latch_quant_tables (j_decompress_ptr cinfo)
  230. {
  231.   int ci, qtblno;
  232.   jpeg_component_info *compptr;
  233.   JQUANT_TBL * qtbl;
  234.  
  235.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  236.     compptr = cinfo->cur_comp_info[ci];
  237.     /* No work if we already saved Q-table for this component */
  238.     if (compptr->quant_table != NULL)
  239.       continue;
  240.     /* Make sure specified quantization table is present */
  241.     qtblno = compptr->quant_tbl_no;
  242.     if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  243.     cinfo->quant_tbl_ptrs[qtblno] == NULL)
  244.       cinfo->ERREXIT1(JERR_NO_QUANT_TABLE, qtblno);
  245.     /* OK, save away the quantization table */
  246.     qtbl = (JQUANT_TBL *)
  247.       cinfo->mem->alloc_small(JPOOL_IMAGE, sizeof(JQUANT_TBL));
  248.     memcpy(qtbl, cinfo->quant_tbl_ptrs[qtblno], sizeof(JQUANT_TBL));
  249.     compptr->quant_table = qtbl;
  250.   }
  251. }
  252.  
  253.  
  254. /*
  255.  * Initialize the input modules to read a scan of compressed data.
  256.  * The first call to this is done by jdmaster.c after initializing
  257.  * the entire decompressor (during jpeg_start_decompress).
  258.  * Subsequent calls come from consume_markers, below.
  259.  */
  260.  
  261. LOCAL(void) start_input_pass (j_decompress_ptr cinfo)
  262. {
  263.   per_scan_setup(cinfo);
  264.   latch_quant_tables(cinfo);
  265.   (*cinfo->entropy->start_pass) (cinfo);
  266.   (*cinfo->coef->start_input_pass) (cinfo);
  267.   cinfo->inputctl->consume_input = cinfo->coef->consume_data;
  268. }
  269.  
  270.  
  271. /*
  272.  * Finish up after inputting a compressed-data scan.
  273.  * This is called by the coefficient controller after it's read all
  274.  * the expected data of the scan.
  275.  */
  276.  
  277. void finish_input_pass (j_decompress_ptr cinfo)
  278. {
  279.   cinfo->inputctl->consume_input = consume_markers;
  280. }
  281.  
  282.  
  283. /*
  284.  * Read JPEG markers before, between, or after compressed-data scans.
  285.  * Change state as necessary when a new scan is reached.
  286.  * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
  287.  *
  288.  * The consume_input method pointer points either here or to the
  289.  * coefficient controller's consume_data routine, depending on whether
  290.  * we are reading a compressed data segment or inter-segment markers.
  291.  */
  292.  
  293. int consume_markers (j_decompress_ptr cinfo)
  294. {
  295.   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  296.   int val;
  297.  
  298.   if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
  299.     return JPEG_REACHED_EOI;
  300.  
  301.   val = (*cinfo->marker->read_markers) (cinfo);
  302.  
  303.   switch (val) {
  304.   case JPEG_REACHED_SOS:    /* Found SOS */
  305.     if (inputctl->inheaders) {    /* 1st SOS */
  306.       initial_setup(cinfo);
  307.       inputctl->inheaders = FALSE;
  308.       /* Note: start_input_pass must be called by jdmaster.c
  309.        * before any more input can be consumed.  jdapimin.c is
  310.        * responsible for enforcing this sequencing.
  311.        */
  312.     } else {            /* 2nd or later SOS marker */
  313.       if (! inputctl->pub.has_multiple_scans)
  314.     cinfo->ERREXIT(JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
  315.       start_input_pass(cinfo);
  316.     }
  317.     break;
  318.   case JPEG_REACHED_EOI:    /* Found EOI */
  319.     inputctl->pub.eoi_reached = TRUE;
  320.     if (inputctl->inheaders) {    /* Tables-only datastream, apparently */
  321.       if (cinfo->marker->saw_SOF)
  322.     cinfo->ERREXIT(JERR_SOF_NO_SOS);
  323.     } else {
  324.       /* Prevent infinite loop in coef ctlr's decompress_data routine
  325.        * if user set output_scan_number larger than number of scans.
  326.        */
  327.       if (cinfo->output_scan_number > cinfo->input_scan_number)
  328.     cinfo->output_scan_number = cinfo->input_scan_number;
  329.     }
  330.     break;
  331.   case JPEG_SUSPENDED:
  332.     break;
  333.   }
  334.  
  335.   return val;
  336. }
  337.  
  338.  
  339. /*
  340.  * Reset state to begin a fresh datastream.
  341.  */
  342.  
  343. void reset_input_controller (j_decompress_ptr cinfo)
  344. {
  345.   my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
  346.  
  347.   inputctl->pub.consume_input = consume_markers;
  348.   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  349.   inputctl->pub.eoi_reached = FALSE;
  350.   inputctl->inheaders = TRUE;
  351.   /* Reset other modules */
  352.   cinfo->err->reset_error_mgr ();
  353.   (*cinfo->marker->reset_marker_reader) (cinfo);
  354.   /* Reset progression state -- would be cleaner if entropy decoder did this */
  355.   cinfo->coef_bits = NULL;
  356. }
  357.  
  358.  
  359. /*
  360.  * Initialize the input controller module.
  361.  * This is called only once, when the decompression object is created.
  362.  */
  363.  
  364. GLOBAL(void)
  365. jinit_input_controller (j_decompress_ptr cinfo)
  366. {
  367.   my_inputctl_ptr inputctl;
  368.  
  369.   /* Create subobject in permanent pool */
  370.   inputctl = (my_inputctl_ptr)
  371.     cinfo->mem->alloc_small(JPOOL_PERMANENT,
  372.                 sizeof(my_input_controller));
  373.   cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
  374.   /* Initialize method pointers */
  375.   inputctl->pub.consume_input = consume_markers;
  376.   inputctl->pub.reset_input_controller = reset_input_controller;
  377.   inputctl->pub.start_input_pass = start_input_pass;
  378.   inputctl->pub.finish_input_pass = finish_input_pass;
  379.   /* Initialize state: can't use reset_input_controller since we don't
  380.    * want to try to reset other modules yet.
  381.    */
  382.   inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
  383.   inputctl->pub.eoi_reached = FALSE;
  384.   inputctl->inheaders = TRUE;
  385. }
  386.