home *** CD-ROM | disk | FTP | other *** search
/ Windows Graphics Programming / Feng_Yuan_Win32_GDI_DirectX.iso / Samples / include / jlib / jccoefct.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-16  |  16.8 KB  |  452 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.  * jccoefct.c
  12.  *
  13.  * Copyright (C) 1994-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 the coefficient buffer controller for compression.
  18.  * This controller is the top level of the JPEG compressor proper.
  19.  * The coefficient buffer lies between forward-DCT and entropy encoding steps.
  20.  */
  21.  
  22. #define JPEG_INTERNALS
  23. #include "jinclude.h"
  24. #include "jpeglib.h"
  25.  
  26.  
  27. /* We use a full-image coefficient buffer when doing Huffman optimization,
  28.  * and also for writing multiple-scan JPEG files.  In all cases, the DCT
  29.  * step is run during the first pass, and subsequent passes need only read
  30.  * the buffered coefficients.
  31.  */
  32. #ifdef ENTROPY_OPT_SUPPORTED
  33. #define FULL_COEF_BUFFER_SUPPORTED
  34. #else
  35. #ifdef C_MULTISCAN_FILES_SUPPORTED
  36. #define FULL_COEF_BUFFER_SUPPORTED
  37. #endif
  38. #endif
  39.  
  40.  
  41. /* Private buffer controller object */
  42.  
  43. typedef struct {
  44.   struct jpeg_c_coef_controller pub; /* public fields */
  45.  
  46.   JDIMENSION iMCU_row_num;    /* iMCU row # within image */
  47.   JDIMENSION mcu_ctr;        /* counts MCUs processed in current row */
  48.   int MCU_vert_offset;        /* counts MCU rows within iMCU row */
  49.   int MCU_rows_per_iMCU_row;    /* number of such rows needed */
  50.  
  51.   /* For single-pass compression, it's sufficient to buffer just one MCU
  52.    * (although this may prove a bit slow in practice).  We allocate a
  53.    * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
  54.    * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
  55.    * it's not really very big; this is to keep the module interfaces unchanged
  56.    * when a large coefficient buffer is necessary.)
  57.    * In multi-pass modes, this array points to the current MCU's blocks
  58.    * within the virtual arrays.
  59.    */
  60.   JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
  61.  
  62.   /* In multi-pass modes, we need a virtual block array for each component. */
  63.   jvirt_barray_ptr whole_image[MAX_COMPONENTS];
  64. } my_coef_controller;
  65.  
  66. typedef my_coef_controller * my_coef_ptr;
  67.  
  68.  
  69. /* Forward declarations */
  70. boolean compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  71.  
  72. #ifdef FULL_COEF_BUFFER_SUPPORTED
  73. boolean compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  74. boolean compress_output(j_compress_ptr cinfo, JSAMPIMAGE input_buf);
  75. #endif
  76.  
  77.  
  78. LOCAL(void)
  79. start_iMCU_row (j_compress_ptr cinfo)
  80. /* Reset within-iMCU-row counters for a new row */
  81. {
  82.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  83.  
  84.   /* In an interleaved scan, an MCU row is the same as an iMCU row.
  85.    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
  86.    * But at the bottom of the image, process only what's left.
  87.    */
  88.   if (cinfo->comps_in_scan > 1) {
  89.     coef->MCU_rows_per_iMCU_row = 1;
  90.   } else {
  91.     if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
  92.       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
  93.     else
  94.       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
  95.   }
  96.  
  97.   coef->mcu_ctr = 0;
  98.   coef->MCU_vert_offset = 0;
  99. }
  100.  
  101.  
  102. /*
  103.  * Initialize for a processing pass.
  104.  */
  105.  
  106. LOCAL(void) start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
  107. {
  108.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  109.  
  110.   coef->iMCU_row_num = 0;
  111.   start_iMCU_row(cinfo);
  112.  
  113.   switch (pass_mode) {
  114.   case JBUF_PASS_THRU:
  115.     if (coef->whole_image[0] != NULL)
  116.       cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  117.     coef->pub.compress_data = compress_data;
  118.     break;
  119. #ifdef FULL_COEF_BUFFER_SUPPORTED
  120.   case JBUF_SAVE_AND_PASS:
  121.     if (coef->whole_image[0] == NULL)
  122.       cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  123.     coef->pub.compress_data = compress_first_pass;
  124.     break;
  125.   case JBUF_CRANK_DEST:
  126.     if (coef->whole_image[0] == NULL)
  127.       cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  128.     coef->pub.compress_data = compress_output;
  129.     break;
  130. #endif
  131.   default:
  132.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  133.     break;
  134.   }
  135. }
  136.  
  137.  
  138. /*
  139.  * Process some data in the single-pass case.
  140.  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  141.  * per call, ie, v_samp_factor block rows for each component in the image.
  142.  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  143.  *
  144.  * NB: input_buf contains a plane for each component in image,
  145.  * which we index according to the component's SOF position.
  146.  */
  147.  
  148. boolean compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  149. {
  150.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  151.   JDIMENSION MCU_col_num;    /* index of current MCU within row */
  152.   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
  153.   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  154.   int blkn, bi, ci, yindex, yoffset, blockcnt;
  155.   JDIMENSION ypos, xpos;
  156.   jpeg_component_info *compptr;
  157.  
  158.   /* Loop to write as much as one whole iMCU row */
  159.   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  160.        yoffset++) {
  161.     for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
  162.      MCU_col_num++) {
  163.       /* Determine where data comes from in input_buf and do the DCT thing.
  164.        * Each call on forward_DCT processes a horizontal row of DCT blocks
  165.        * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
  166.        * sequentially.  Dummy blocks at the right or bottom edge are filled in
  167.        * specially.  The data in them does not matter for image reconstruction,
  168.        * so we fill them with values that will encode to the smallest amount of
  169.        * data, viz: all zeroes in the AC entries, DC entries equal to previous
  170.        * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
  171.        */
  172.       blkn = 0;
  173.       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  174.     compptr = cinfo->cur_comp_info[ci];
  175.     blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
  176.                         : compptr->last_col_width;
  177.     xpos = MCU_col_num * compptr->MCU_sample_width;
  178.     ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
  179.     for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  180.       if (coef->iMCU_row_num < last_iMCU_row ||
  181.           yoffset+yindex < compptr->last_row_height) {
  182.         (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  183.                      input_buf[compptr->component_index],
  184.                      coef->MCU_buffer[blkn],
  185.                      ypos, xpos, (JDIMENSION) blockcnt);
  186.         if (blockcnt < compptr->MCU_width) {
  187.           /* Create some dummy blocks at the right edge of the image. */
  188.           jzero_far((void *) coef->MCU_buffer[blkn + blockcnt],
  189.             (compptr->MCU_width - blockcnt) * sizeof(JBLOCK));
  190.           for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
  191.         coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
  192.           }
  193.         }
  194.       } else {
  195.         /* Create a row of dummy blocks at the bottom of the image. */
  196.         jzero_far((void *) coef->MCU_buffer[blkn],
  197.               compptr->MCU_width * sizeof(JBLOCK));
  198.         for (bi = 0; bi < compptr->MCU_width; bi++) {
  199.           coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
  200.         }
  201.       }
  202.       blkn += compptr->MCU_width;
  203.       ypos += DCTSIZE;
  204.     }
  205.       }
  206.       /* Try to write the MCU.  In event of a suspension failure, we will
  207.        * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
  208.        */
  209.       if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  210.     /* Suspension forced; update state counters and exit */
  211.     coef->MCU_vert_offset = yoffset;
  212.     coef->mcu_ctr = MCU_col_num;
  213.     return FALSE;
  214.       }
  215.     }
  216.     /* Completed an MCU row, but perhaps not an iMCU row */
  217.     coef->mcu_ctr = 0;
  218.   }
  219.   /* Completed the iMCU row, advance counters for next one */
  220.   coef->iMCU_row_num++;
  221.   start_iMCU_row(cinfo);
  222.   return TRUE;
  223. }
  224.  
  225.  
  226. #ifdef FULL_COEF_BUFFER_SUPPORTED
  227.  
  228. /*
  229.  * Process some data in the first pass of a multi-pass case.
  230.  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  231.  * per call, ie, v_samp_factor block rows for each component in the image.
  232.  * This amount of data is read from the source buffer, DCT'd and quantized,
  233.  * and saved into the virtual arrays.  We also generate suitable dummy blocks
  234.  * as needed at the right and lower edges.  (The dummy blocks are constructed
  235.  * in the virtual arrays, which have been padded appropriately.)  This makes
  236.  * it possible for subsequent passes not to worry about real vs. dummy blocks.
  237.  *
  238.  * We must also emit the data to the entropy encoder.  This is conveniently
  239.  * done by calling compress_output() after we've loaded the current strip
  240.  * of the virtual arrays.
  241.  *
  242.  * NB: input_buf contains a plane for each component in image.  All
  243.  * components are DCT'd and loaded into the virtual arrays in this pass.
  244.  * However, it may be that only a subset of the components are emitted to
  245.  * the entropy encoder during this first pass; be careful about looking
  246.  * at the scan-dependent variables (MCU dimensions, etc).
  247.  */
  248.  
  249. boolean compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  250. {
  251.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  252.   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
  253.   JDIMENSION blocks_across, MCUs_across, MCUindex;
  254.   int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
  255.   JCOEF lastDC;
  256.   jpeg_component_info *compptr;
  257.   JBLOCKARRAY buffer;
  258.   JBLOCKROW thisblockrow, lastblockrow;
  259.  
  260.   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  261.        ci++, compptr++) {
  262.     /* Align the virtual buffer for this component. */
  263.     buffer = cinfo->mem->access_virt_barray
  264.       (coef->whole_image[ci],
  265.        coef->iMCU_row_num * compptr->v_samp_factor,
  266.        (JDIMENSION) compptr->v_samp_factor, TRUE);
  267.     /* Count non-dummy DCT block rows in this iMCU row. */
  268.     if (coef->iMCU_row_num < last_iMCU_row)
  269.       block_rows = compptr->v_samp_factor;
  270.     else {
  271.       /* NB: can't use last_row_height here, since may not be set! */
  272.       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
  273.       if (block_rows == 0) block_rows = compptr->v_samp_factor;
  274.     }
  275.     blocks_across = compptr->width_in_blocks;
  276.     h_samp_factor = compptr->h_samp_factor;
  277.     /* Count number of dummy blocks to be added at the right margin. */
  278.     ndummy = (int) (blocks_across % h_samp_factor);
  279.     if (ndummy > 0)
  280.       ndummy = h_samp_factor - ndummy;
  281.     /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
  282.      * on forward_DCT processes a complete horizontal row of DCT blocks.
  283.      */
  284.     for (block_row = 0; block_row < block_rows; block_row++) {
  285.       thisblockrow = buffer[block_row];
  286.       (*cinfo->fdct->forward_DCT) (cinfo, compptr,
  287.                    input_buf[ci], thisblockrow,
  288.                    (JDIMENSION) (block_row * DCTSIZE),
  289.                    (JDIMENSION) 0, blocks_across);
  290.       if (ndummy > 0) {
  291.     /* Create dummy blocks at the right edge of the image. */
  292.     thisblockrow += blocks_across; /* => first dummy block */
  293.     jzero_far((void *) thisblockrow, ndummy * sizeof(JBLOCK));
  294.     lastDC = thisblockrow[-1][0];
  295.     for (bi = 0; bi < ndummy; bi++) {
  296.       thisblockrow[bi][0] = lastDC;
  297.     }
  298.       }
  299.     }
  300.     /* If at end of image, create dummy block rows as needed.
  301.      * The tricky part here is that within each MCU, we want the DC values
  302.      * of the dummy blocks to match the last real block's DC value.
  303.      * This squeezes a few more bytes out of the resulting file...
  304.      */
  305.     if (coef->iMCU_row_num == last_iMCU_row) {
  306.       blocks_across += ndummy;    /* include lower right corner */
  307.       MCUs_across = blocks_across / h_samp_factor;
  308.       for (block_row = block_rows; block_row < compptr->v_samp_factor;
  309.        block_row++) {
  310.     thisblockrow = buffer[block_row];
  311.     lastblockrow = buffer[block_row-1];
  312.     jzero_far((void *) thisblockrow,
  313.           (size_t) (blocks_across * sizeof(JBLOCK)));
  314.     for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
  315.       lastDC = lastblockrow[h_samp_factor-1][0];
  316.       for (bi = 0; bi < h_samp_factor; bi++) {
  317.         thisblockrow[bi][0] = lastDC;
  318.       }
  319.       thisblockrow += h_samp_factor; /* advance to next MCU in row */
  320.       lastblockrow += h_samp_factor;
  321.     }
  322.       }
  323.     }
  324.   }
  325.   /* NB: compress_output will increment iMCU_row_num if successful.
  326.    * A suspension return will result in redoing all the work above next time.
  327.    */
  328.  
  329.   /* Emit data to the entropy encoder, sharing code with subsequent passes */
  330.   return compress_output(cinfo, input_buf);
  331. }
  332.  
  333.  
  334. /*
  335.  * Process some data in subsequent passes of a multi-pass case.
  336.  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
  337.  * per call, ie, v_samp_factor block rows for each component in the scan.
  338.  * The data is obtained from the virtual arrays and fed to the entropy coder.
  339.  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
  340.  *
  341.  * NB: input_buf is ignored; it is likely to be a NULL pointer.
  342.  */
  343.  
  344. boolean compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
  345. {
  346.   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
  347.   JDIMENSION MCU_col_num;    /* index of current MCU within row */
  348.   int blkn, ci, xindex, yindex, yoffset;
  349.   JDIMENSION start_col;
  350.   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
  351.   JBLOCKROW buffer_ptr;
  352.   jpeg_component_info *compptr;
  353.  
  354.   /* Align the virtual buffers for the components used in this scan.
  355.    * NB: during first pass, this is safe only because the buffers will
  356.    * already be aligned properly, so jmemmgr.c won't need to do any I/O.
  357.    */
  358.   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  359.     compptr = cinfo->cur_comp_info[ci];
  360.     buffer[ci] = cinfo->mem->access_virt_barray
  361.       (coef->whole_image[compptr->component_index],
  362.        coef->iMCU_row_num * compptr->v_samp_factor,
  363.        (JDIMENSION) compptr->v_samp_factor, FALSE);
  364.   }
  365.  
  366.   /* Loop to process one whole iMCU row */
  367.   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
  368.        yoffset++) {
  369.     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
  370.      MCU_col_num++) {
  371.       /* Construct list of pointers to DCT blocks belonging to this MCU */
  372.       blkn = 0;            /* index of current DCT block within MCU */
  373.       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
  374.     compptr = cinfo->cur_comp_info[ci];
  375.     start_col = MCU_col_num * compptr->MCU_width;
  376.     for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
  377.       buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
  378.       for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
  379.         coef->MCU_buffer[blkn++] = buffer_ptr++;
  380.       }
  381.     }
  382.       }
  383.       /* Try to write the MCU. */
  384.       if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
  385.     /* Suspension forced; update state counters and exit */
  386.     coef->MCU_vert_offset = yoffset;
  387.     coef->mcu_ctr = MCU_col_num;
  388.     return FALSE;
  389.       }
  390.     }
  391.     /* Completed an MCU row, but perhaps not an iMCU row */
  392.     coef->mcu_ctr = 0;
  393.   }
  394.   /* Completed the iMCU row, advance counters for next one */
  395.   coef->iMCU_row_num++;
  396.   start_iMCU_row(cinfo);
  397.   return TRUE;
  398. }
  399.  
  400. #endif /* FULL_COEF_BUFFER_SUPPORTED */
  401.  
  402.  
  403. /*
  404.  * Initialize coefficient buffer controller.
  405.  */
  406.  
  407. GLOBAL(void)
  408. jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
  409. {
  410.   my_coef_ptr coef;
  411.  
  412.   coef = (my_coef_ptr)
  413.     cinfo->mem->alloc_small (JPOOL_IMAGE,
  414.                 sizeof(my_coef_controller));
  415.   cinfo->coef = (struct jpeg_c_coef_controller *) coef;
  416.   coef->pub.start_pass = start_pass_coef;
  417.  
  418.   /* Create the coefficient buffer. */
  419.   if (need_full_buffer) {
  420. #ifdef FULL_COEF_BUFFER_SUPPORTED
  421.     /* Allocate a full-image virtual array for each component, */
  422.     /* padded to a multiple of samp_factor DCT blocks in each direction. */
  423.     int ci;
  424.     jpeg_component_info *compptr;
  425.  
  426.     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  427.      ci++, compptr++) {
  428.       coef->whole_image[ci] = cinfo->mem->request_virt_barray
  429.     (JPOOL_IMAGE, FALSE,
  430.      (JDIMENSION) jround_up((long) compptr->width_in_blocks,
  431.                 (long) compptr->h_samp_factor),
  432.      (JDIMENSION) jround_up((long) compptr->height_in_blocks,
  433.                 (long) compptr->v_samp_factor),
  434.      (JDIMENSION) compptr->v_samp_factor);
  435.     }
  436. #else
  437.     cinfo->ERREXIT(JERR_BAD_BUFFER_MODE);
  438. #endif
  439.   } else {
  440.     /* We only need a single-MCU buffer. */
  441.     JBLOCKROW buffer;
  442.     int i;
  443.  
  444.     buffer = (JBLOCKROW)
  445.       cinfo->mem->alloc_large (JPOOL_IMAGE, C_MAX_BLOCKS_IN_MCU * sizeof(JBLOCK));
  446.     for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
  447.       coef->MCU_buffer[i] = buffer + i;
  448.     }
  449.     coef->whole_image[0] = NULL; /* flag for no virtual arrays */
  450.   }
  451. }
  452.