jccoefct.c
Go to the documentation of this file.
00001 /*
00002  * jccoefct.c
00003  *
00004  * Copyright (C) 1994-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the coefficient buffer controller for compression.
00009  * This controller is the top level of the JPEG compressor proper.
00010  * The coefficient buffer lies between forward-DCT and entropy encoding steps.
00011  */
00012 
00013 #define JPEG_INTERNALS
00014 #include "jinclude.h"
00015 #include "jpeglib.h"
00016 
00017 
00018 /* We use a full-image coefficient buffer when doing Huffman optimization,
00019  * and also for writing multiple-scan JPEG files.  In all cases, the DCT
00020  * step is run during the first pass, and subsequent passes need only read
00021  * the buffered coefficients.
00022  */
00023 #ifdef ENTROPY_OPT_SUPPORTED
00024 #define FULL_COEF_BUFFER_SUPPORTED
00025 #else
00026 #ifdef C_MULTISCAN_FILES_SUPPORTED
00027 #define FULL_COEF_BUFFER_SUPPORTED
00028 #endif
00029 #endif
00030 
00031 
00032 /* Private buffer controller object */
00033 
00034 typedef struct {
00035   struct jpeg_c_coef_controller pub; /* public fields */
00036 
00037   JDIMENSION iMCU_row_num;      /* iMCU row # within image */
00038   JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */
00039   int MCU_vert_offset;          /* counts MCU rows within iMCU row */
00040   int MCU_rows_per_iMCU_row;    /* number of such rows needed */
00041 
00042   /* For single-pass compression, it's sufficient to buffer just one MCU
00043    * (although this may prove a bit slow in practice).  We allocate a
00044    * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for each
00045    * MCU constructed and sent.  (On 80x86, the workspace is FAR even though
00046    * it's not really very big; this is to keep the module interfaces unchanged
00047    * when a large coefficient buffer is necessary.)
00048    * In multi-pass modes, this array points to the current MCU's blocks
00049    * within the virtual arrays.
00050    */
00051   JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
00052 
00053   /* In multi-pass modes, we need a virtual block array for each component. */
00054   jvirt_barray_ptr whole_image[MAX_COMPONENTS];
00055 } my_coef_controller;
00056 
00057 typedef my_coef_controller * my_coef_ptr;
00058 
00059 
00060 /* Forward declarations */
00061 METHODDEF(boolean) compress_data
00062     JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
00063 #ifdef FULL_COEF_BUFFER_SUPPORTED
00064 METHODDEF(boolean) compress_first_pass
00065     JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
00066 METHODDEF(boolean) compress_output
00067     JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
00068 #endif
00069 
00070 
00071 LOCAL(void)
00072 start_iMCU_row (j_compress_ptr cinfo)
00073 /* Reset within-iMCU-row counters for a new row */
00074 {
00075   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00076 
00077   /* In an interleaved scan, an MCU row is the same as an iMCU row.
00078    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
00079    * But at the bottom of the image, process only what's left.
00080    */
00081   if (cinfo->comps_in_scan > 1) {
00082     coef->MCU_rows_per_iMCU_row = 1;
00083   } else {
00084     if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
00085       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
00086     else
00087       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
00088   }
00089 
00090   coef->mcu_ctr = 0;
00091   coef->MCU_vert_offset = 0;
00092 }
00093 
00094 
00095 /*
00096  * Initialize for a processing pass.
00097  */
00098 
00099 METHODDEF(void)
00100 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00101 {
00102   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00103 
00104   coef->iMCU_row_num = 0;
00105   start_iMCU_row(cinfo);
00106 
00107   switch (pass_mode) {
00108   case JBUF_PASS_THRU:
00109     if (coef->whole_image[0] != NULL)
00110       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00111     coef->pub.compress_data = compress_data;
00112     break;
00113 #ifdef FULL_COEF_BUFFER_SUPPORTED
00114   case JBUF_SAVE_AND_PASS:
00115     if (coef->whole_image[0] == NULL)
00116       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00117     coef->pub.compress_data = compress_first_pass;
00118     break;
00119   case JBUF_CRANK_DEST:
00120     if (coef->whole_image[0] == NULL)
00121       ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00122     coef->pub.compress_data = compress_output;
00123     break;
00124 #endif
00125   default:
00126     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00127     break;
00128   }
00129 }
00130 
00131 
00132 /*
00133  * Process some data in the single-pass case.
00134  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
00135  * per call, ie, v_samp_factor block rows for each component in the image.
00136  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
00137  *
00138  * NB: input_buf contains a plane for each component in image,
00139  * which we index according to the component's SOF position.
00140  */
00141 
00142 METHODDEF(boolean)
00143 compress_data (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
00144 {
00145   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00146   JDIMENSION MCU_col_num;       /* index of current MCU within row */
00147   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
00148   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00149   int blkn, bi, ci, yindex, yoffset, blockcnt;
00150   JDIMENSION ypos, xpos;
00151   jpeg_component_info *compptr;
00152 
00153   /* Loop to write as much as one whole iMCU row */
00154   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00155        yoffset++) {
00156     for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
00157          MCU_col_num++) {
00158       /* Determine where data comes from in input_buf and do the DCT thing.
00159        * Each call on forward_DCT processes a horizontal row of DCT blocks
00160        * as wide as an MCU; we rely on having allocated the MCU_buffer[] blocks
00161        * sequentially.  Dummy blocks at the right or bottom edge are filled in
00162        * specially.  The data in them does not matter for image reconstruction,
00163        * so we fill them with values that will encode to the smallest amount of
00164        * data, viz: all zeroes in the AC entries, DC entries equal to previous
00165        * block's DC value.  (Thanks to Thomas Kinsman for this idea.)
00166        */
00167       blkn = 0;
00168       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00169         compptr = cinfo->cur_comp_info[ci];
00170         blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
00171                                                 : compptr->last_col_width;
00172         xpos = MCU_col_num * compptr->MCU_sample_width;
00173         ypos = yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
00174         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00175           if (coef->iMCU_row_num < last_iMCU_row ||
00176               yoffset+yindex < compptr->last_row_height) {
00177             (*cinfo->fdct->forward_DCT) (cinfo, compptr,
00178                                          input_buf[compptr->component_index],
00179                                          coef->MCU_buffer[blkn],
00180                                          ypos, xpos, (JDIMENSION) blockcnt);
00181             if (blockcnt < compptr->MCU_width) {
00182               /* Create some dummy blocks at the right edge of the image. */
00183               jzero_far((void FAR *) coef->MCU_buffer[blkn + blockcnt],
00184                         (compptr->MCU_width - blockcnt) * SIZEOF(JBLOCK));
00185               for (bi = blockcnt; bi < compptr->MCU_width; bi++) {
00186                 coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn+bi-1][0][0];
00187               }
00188             }
00189           } else {
00190             /* Create a row of dummy blocks at the bottom of the image. */
00191             jzero_far((void FAR *) coef->MCU_buffer[blkn],
00192                       compptr->MCU_width * SIZEOF(JBLOCK));
00193             for (bi = 0; bi < compptr->MCU_width; bi++) {
00194               coef->MCU_buffer[blkn+bi][0][0] = coef->MCU_buffer[blkn-1][0][0];
00195             }
00196           }
00197           blkn += compptr->MCU_width;
00198           ypos += DCTSIZE;
00199         }
00200       }
00201       /* Try to write the MCU.  In event of a suspension failure, we will
00202        * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
00203        */
00204       if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
00205         /* Suspension forced; update state counters and exit */
00206         coef->MCU_vert_offset = yoffset;
00207         coef->mcu_ctr = MCU_col_num;
00208         return FALSE;
00209       }
00210     }
00211     /* Completed an MCU row, but perhaps not an iMCU row */
00212     coef->mcu_ctr = 0;
00213   }
00214   /* Completed the iMCU row, advance counters for next one */
00215   coef->iMCU_row_num++;
00216   start_iMCU_row(cinfo);
00217   return TRUE;
00218 }
00219 
00220 
00221 #ifdef FULL_COEF_BUFFER_SUPPORTED
00222 
00223 /*
00224  * Process some data in the first pass of a multi-pass case.
00225  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
00226  * per call, ie, v_samp_factor block rows for each component in the image.
00227  * This amount of data is read from the source buffer, DCT'd and quantized,
00228  * and saved into the virtual arrays.  We also generate suitable dummy blocks
00229  * as needed at the right and lower edges.  (The dummy blocks are constructed
00230  * in the virtual arrays, which have been padded appropriately.)  This makes
00231  * it possible for subsequent passes not to worry about real vs. dummy blocks.
00232  *
00233  * We must also emit the data to the entropy encoder.  This is conveniently
00234  * done by calling compress_output() after we've loaded the current strip
00235  * of the virtual arrays.
00236  *
00237  * NB: input_buf contains a plane for each component in image.  All
00238  * components are DCT'd and loaded into the virtual arrays in this pass.
00239  * However, it may be that only a subset of the components are emitted to
00240  * the entropy encoder during this first pass; be careful about looking
00241  * at the scan-dependent variables (MCU dimensions, etc).
00242  */
00243 
00244 METHODDEF(boolean)
00245 compress_first_pass (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
00246 {
00247   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00248   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00249   JDIMENSION blocks_across, MCUs_across, MCUindex;
00250   int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
00251   JCOEF lastDC;
00252   jpeg_component_info *compptr;
00253   JBLOCKARRAY buffer;
00254   JBLOCKROW thisblockrow, lastblockrow;
00255 
00256   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00257        ci++, compptr++) {
00258     /* Align the virtual buffer for this component. */
00259     buffer = (*cinfo->mem->access_virt_barray)
00260       ((j_common_ptr) cinfo, coef->whole_image[ci],
00261        coef->iMCU_row_num * compptr->v_samp_factor,
00262        (JDIMENSION) compptr->v_samp_factor, TRUE);
00263     /* Count non-dummy DCT block rows in this iMCU row. */
00264     if (coef->iMCU_row_num < last_iMCU_row)
00265       block_rows = compptr->v_samp_factor;
00266     else {
00267       /* NB: can't use last_row_height here, since may not be set! */
00268       block_rows = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
00269       if (block_rows == 0) block_rows = compptr->v_samp_factor;
00270     }
00271     blocks_across = compptr->width_in_blocks;
00272     h_samp_factor = compptr->h_samp_factor;
00273     /* Count number of dummy blocks to be added at the right margin. */
00274     ndummy = (int) (blocks_across % h_samp_factor);
00275     if (ndummy > 0)
00276       ndummy = h_samp_factor - ndummy;
00277     /* Perform DCT for all non-dummy blocks in this iMCU row.  Each call
00278      * on forward_DCT processes a complete horizontal row of DCT blocks.
00279      */
00280     for (block_row = 0; block_row < block_rows; block_row++) {
00281       thisblockrow = buffer[block_row];
00282       (*cinfo->fdct->forward_DCT) (cinfo, compptr,
00283                                    input_buf[ci], thisblockrow,
00284                                    (JDIMENSION) (block_row * DCTSIZE),
00285                                    (JDIMENSION) 0, blocks_across);
00286       if (ndummy > 0) {
00287         /* Create dummy blocks at the right edge of the image. */
00288         thisblockrow += blocks_across; /* => first dummy block */
00289         jzero_far((void FAR *) thisblockrow, ndummy * SIZEOF(JBLOCK));
00290         lastDC = thisblockrow[-1][0];
00291         for (bi = 0; bi < ndummy; bi++) {
00292           thisblockrow[bi][0] = lastDC;
00293         }
00294       }
00295     }
00296     /* If at end of image, create dummy block rows as needed.
00297      * The tricky part here is that within each MCU, we want the DC values
00298      * of the dummy blocks to match the last real block's DC value.
00299      * This squeezes a few more bytes out of the resulting file...
00300      */
00301     if (coef->iMCU_row_num == last_iMCU_row) {
00302       blocks_across += ndummy;  /* include lower right corner */
00303       MCUs_across = blocks_across / h_samp_factor;
00304       for (block_row = block_rows; block_row < compptr->v_samp_factor;
00305            block_row++) {
00306         thisblockrow = buffer[block_row];
00307         lastblockrow = buffer[block_row-1];
00308         jzero_far((void FAR *) thisblockrow,
00309                   (size_t) (blocks_across * SIZEOF(JBLOCK)));
00310         for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++) {
00311           lastDC = lastblockrow[h_samp_factor-1][0];
00312           for (bi = 0; bi < h_samp_factor; bi++) {
00313             thisblockrow[bi][0] = lastDC;
00314           }
00315           thisblockrow += h_samp_factor; /* advance to next MCU in row */
00316           lastblockrow += h_samp_factor;
00317         }
00318       }
00319     }
00320   }
00321   /* NB: compress_output will increment iMCU_row_num if successful.
00322    * A suspension return will result in redoing all the work above next time.
00323    */
00324 
00325   /* Emit data to the entropy encoder, sharing code with subsequent passes */
00326   return compress_output(cinfo, input_buf);
00327 }
00328 
00329 
00330 /*
00331  * Process some data in subsequent passes of a multi-pass case.
00332  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
00333  * per call, ie, v_samp_factor block rows for each component in the scan.
00334  * The data is obtained from the virtual arrays and fed to the entropy coder.
00335  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
00336  *
00337  * NB: input_buf is ignored; it is likely to be a NULL pointer.
00338  */
00339 
00340 METHODDEF(boolean)
00341 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
00342 {
00343   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00344   JDIMENSION MCU_col_num;       /* index of current MCU within row */
00345   int blkn, ci, xindex, yindex, yoffset;
00346   JDIMENSION start_col;
00347   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
00348   JBLOCKROW buffer_ptr;
00349   jpeg_component_info *compptr;
00350 
00351   /* Align the virtual buffers for the components used in this scan.
00352    * NB: during first pass, this is safe only because the buffers will
00353    * already be aligned properly, so jmemmgr.c won't need to do any I/O.
00354    */
00355   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00356     compptr = cinfo->cur_comp_info[ci];
00357     buffer[ci] = (*cinfo->mem->access_virt_barray)
00358       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
00359        coef->iMCU_row_num * compptr->v_samp_factor,
00360        (JDIMENSION) compptr->v_samp_factor, FALSE);
00361   }
00362 
00363   /* Loop to process one whole iMCU row */
00364   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00365        yoffset++) {
00366     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
00367          MCU_col_num++) {
00368       /* Construct list of pointers to DCT blocks belonging to this MCU */
00369       blkn = 0;                 /* index of current DCT block within MCU */
00370       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00371         compptr = cinfo->cur_comp_info[ci];
00372         start_col = MCU_col_num * compptr->MCU_width;
00373         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00374           buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
00375           for (xindex = 0; xindex < compptr->MCU_width; xindex++) {
00376             coef->MCU_buffer[blkn++] = buffer_ptr++;
00377           }
00378         }
00379       }
00380       /* Try to write the MCU. */
00381       if (! (*cinfo->entropy->encode_mcu) (cinfo, coef->MCU_buffer)) {
00382         /* Suspension forced; update state counters and exit */
00383         coef->MCU_vert_offset = yoffset;
00384         coef->mcu_ctr = MCU_col_num;
00385         return FALSE;
00386       }
00387     }
00388     /* Completed an MCU row, but perhaps not an iMCU row */
00389     coef->mcu_ctr = 0;
00390   }
00391   /* Completed the iMCU row, advance counters for next one */
00392   coef->iMCU_row_num++;
00393   start_iMCU_row(cinfo);
00394   return TRUE;
00395 }
00396 
00397 #endif /* FULL_COEF_BUFFER_SUPPORTED */
00398 
00399 
00400 /*
00401  * Initialize coefficient buffer controller.
00402  */
00403 
00404 GLOBAL(void)
00405 jinit_c_coef_controller (j_compress_ptr cinfo, boolean need_full_buffer)
00406 {
00407   my_coef_ptr coef;
00408 
00409   coef = (my_coef_ptr)
00410     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00411                                 SIZEOF(my_coef_controller));
00412   cinfo->coef = (struct jpeg_c_coef_controller *) coef;
00413   coef->pub.start_pass = start_pass_coef;
00414 
00415   /* Create the coefficient buffer. */
00416   if (need_full_buffer) {
00417 #ifdef FULL_COEF_BUFFER_SUPPORTED
00418     /* Allocate a full-image virtual array for each component, */
00419     /* padded to a multiple of samp_factor DCT blocks in each direction. */
00420     int ci;
00421     jpeg_component_info *compptr;
00422 
00423     for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00424          ci++, compptr++) {
00425       coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)
00426         ((j_common_ptr) cinfo, JPOOL_IMAGE, FALSE,
00427          (JDIMENSION) jround_up((long) compptr->width_in_blocks,
00428                                 (long) compptr->h_samp_factor),
00429          (JDIMENSION) jround_up((long) compptr->height_in_blocks,
00430                                 (long) compptr->v_samp_factor),
00431          (JDIMENSION) compptr->v_samp_factor);
00432     }
00433 #else
00434     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00435 #endif
00436   } else {
00437     /* We only need a single-MCU buffer. */
00438     JBLOCKROW buffer;
00439     int i;
00440 
00441     buffer = (JBLOCKROW)
00442       (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00443                                   C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00444     for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
00445       coef->MCU_buffer[i] = buffer + i;
00446     }
00447     coef->whole_image[0] = NULL; /* flag for no virtual arrays */
00448   }
00449 }


openhrp3
Author(s): AIST, General Robotix Inc., Nakamura Lab of Dept. of Mechano Informatics at University of Tokyo
autogenerated on Thu Apr 11 2019 03:30:17