jctrans.c
Go to the documentation of this file.
00001 /*
00002  * jctrans.c
00003  *
00004  * Copyright (C) 1995-1998, 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 library routines for transcoding compression,
00009  * that is, writing raw DCT coefficient arrays to an output JPEG file.
00010  * The routines in jcapimin.c will also be needed by a transcoder.
00011  */
00012 
00013 #define JPEG_INTERNALS
00014 #include "jinclude.h"
00015 #include "jpeglib.h"
00016 
00017 
00018 /* Forward declarations */
00019 LOCAL(void) transencode_master_selection
00020         JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
00021 LOCAL(void) transencode_coef_controller
00022         JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays));
00023 
00024 
00025 /*
00026  * Compression initialization for writing raw-coefficient data.
00027  * Before calling this, all parameters and a data destination must be set up.
00028  * Call jpeg_finish_compress() to actually write the data.
00029  *
00030  * The number of passed virtual arrays must match cinfo->num_components.
00031  * Note that the virtual arrays need not be filled or even realized at
00032  * the time write_coefficients is called; indeed, if the virtual arrays
00033  * were requested from this compression object's memory manager, they
00034  * typically will be realized during this routine and filled afterwards.
00035  */
00036 
00037 GLOBAL(void)
00038 jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)
00039 {
00040   if (cinfo->global_state != CSTATE_START)
00041     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00042   /* Mark all tables to be written */
00043   jpeg_suppress_tables(cinfo, FALSE);
00044   /* (Re)initialize error mgr and destination modules */
00045   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00046   (*cinfo->dest->init_destination) (cinfo);
00047   /* Perform master selection of active modules */
00048   transencode_master_selection(cinfo, coef_arrays);
00049   /* Wait for jpeg_finish_compress() call */
00050   cinfo->next_scanline = 0;     /* so jpeg_write_marker works */
00051   cinfo->global_state = CSTATE_WRCOEFS;
00052 }
00053 
00054 
00055 /*
00056  * Initialize the compression object with default parameters,
00057  * then copy from the source object all parameters needed for lossless
00058  * transcoding.  Parameters that can be varied without loss (such as
00059  * scan script and Huffman optimization) are left in their default states.
00060  */
00061 
00062 GLOBAL(void)
00063 jpeg_copy_critical_parameters (j_decompress_ptr srcinfo,
00064                                j_compress_ptr dstinfo)
00065 {
00066   JQUANT_TBL ** qtblptr;
00067   jpeg_component_info *incomp, *outcomp;
00068   JQUANT_TBL *c_quant, *slot_quant;
00069   int tblno, ci, coefi;
00070 
00071   /* Safety check to ensure start_compress not called yet. */
00072   if (dstinfo->global_state != CSTATE_START)
00073     ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
00074   /* Copy fundamental image dimensions */
00075   dstinfo->image_width = srcinfo->image_width;
00076   dstinfo->image_height = srcinfo->image_height;
00077   dstinfo->input_components = srcinfo->num_components;
00078   dstinfo->in_color_space = srcinfo->jpeg_color_space;
00079   /* Initialize all parameters to default values */
00080   jpeg_set_defaults(dstinfo);
00081   /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
00082    * Fix it to get the right header markers for the image colorspace.
00083    */
00084   jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
00085   dstinfo->data_precision = srcinfo->data_precision;
00086   dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
00087   /* Copy the source's quantization tables. */
00088   for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) {
00089     if (srcinfo->quant_tbl_ptrs[tblno] != NULL) {
00090       qtblptr = & dstinfo->quant_tbl_ptrs[tblno];
00091       if (*qtblptr == NULL)
00092         *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo);
00093       MEMCOPY((*qtblptr)->quantval,
00094               srcinfo->quant_tbl_ptrs[tblno]->quantval,
00095               SIZEOF((*qtblptr)->quantval));
00096       (*qtblptr)->sent_table = FALSE;
00097     }
00098   }
00099   /* Copy the source's per-component info.
00100    * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
00101    */
00102   dstinfo->num_components = srcinfo->num_components;
00103   if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
00104     ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
00105              MAX_COMPONENTS);
00106   for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
00107        ci < dstinfo->num_components; ci++, incomp++, outcomp++) {
00108     outcomp->component_id = incomp->component_id;
00109     outcomp->h_samp_factor = incomp->h_samp_factor;
00110     outcomp->v_samp_factor = incomp->v_samp_factor;
00111     outcomp->quant_tbl_no = incomp->quant_tbl_no;
00112     /* Make sure saved quantization table for component matches the qtable
00113      * slot.  If not, the input file re-used this qtable slot.
00114      * IJG encoder currently cannot duplicate this.
00115      */
00116     tblno = outcomp->quant_tbl_no;
00117     if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
00118         srcinfo->quant_tbl_ptrs[tblno] == NULL)
00119       ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
00120     slot_quant = srcinfo->quant_tbl_ptrs[tblno];
00121     c_quant = incomp->quant_table;
00122     if (c_quant != NULL) {
00123       for (coefi = 0; coefi < DCTSIZE2; coefi++) {
00124         if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
00125           ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
00126       }
00127     }
00128     /* Note: we do not copy the source's Huffman table assignments;
00129      * instead we rely on jpeg_set_colorspace to have made a suitable choice.
00130      */
00131   }
00132   /* Also copy JFIF version and resolution information, if available.
00133    * Strictly speaking this isn't "critical" info, but it's nearly
00134    * always appropriate to copy it if available.  In particular,
00135    * if the application chooses to copy JFIF 1.02 extension markers from
00136    * the source file, we need to copy the version to make sure we don't
00137    * emit a file that has 1.02 extensions but a claimed version of 1.01.
00138    * We will *not*, however, copy version info from mislabeled "2.01" files.
00139    */
00140   if (srcinfo->saw_JFIF_marker) {
00141     if (srcinfo->JFIF_major_version == 1) {
00142       dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
00143       dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
00144     }
00145     dstinfo->density_unit = srcinfo->density_unit;
00146     dstinfo->X_density = srcinfo->X_density;
00147     dstinfo->Y_density = srcinfo->Y_density;
00148   }
00149 }
00150 
00151 
00152 /*
00153  * Master selection of compression modules for transcoding.
00154  * This substitutes for jcinit.c's initialization of the full compressor.
00155  */
00156 
00157 LOCAL(void)
00158 transencode_master_selection (j_compress_ptr cinfo,
00159                               jvirt_barray_ptr * coef_arrays)
00160 {
00161   /* Although we don't actually use input_components for transcoding,
00162    * jcmaster.c's initial_setup will complain if input_components is 0.
00163    */
00164   cinfo->input_components = 1;
00165   /* Initialize master control (includes parameter checking/processing) */
00166   jinit_c_master_control(cinfo, TRUE /* transcode only */);
00167 
00168   /* Entropy encoding: either Huffman or arithmetic coding. */
00169   if (cinfo->arith_code) {
00170     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
00171   } else {
00172     if (cinfo->progressive_mode) {
00173 #ifdef C_PROGRESSIVE_SUPPORTED
00174       jinit_phuff_encoder(cinfo);
00175 #else
00176       ERREXIT(cinfo, JERR_NOT_COMPILED);
00177 #endif
00178     } else
00179       jinit_huff_encoder(cinfo);
00180   }
00181 
00182   /* We need a special coefficient buffer controller. */
00183   transencode_coef_controller(cinfo, coef_arrays);
00184 
00185   jinit_marker_writer(cinfo);
00186 
00187   /* We can now tell the memory manager to allocate virtual arrays. */
00188   (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
00189 
00190   /* Write the datastream header (SOI, JFIF) immediately.
00191    * Frame and scan headers are postponed till later.
00192    * This lets application insert special markers after the SOI.
00193    */
00194   (*cinfo->marker->write_file_header) (cinfo);
00195 }
00196 
00197 
00198 /*
00199  * The rest of this file is a special implementation of the coefficient
00200  * buffer controller.  This is similar to jccoefct.c, but it handles only
00201  * output from presupplied virtual arrays.  Furthermore, we generate any
00202  * dummy padding blocks on-the-fly rather than expecting them to be present
00203  * in the arrays.
00204  */
00205 
00206 /* Private buffer controller object */
00207 
00208 typedef struct {
00209   struct jpeg_c_coef_controller pub; /* public fields */
00210 
00211   JDIMENSION iMCU_row_num;      /* iMCU row # within image */
00212   JDIMENSION mcu_ctr;           /* counts MCUs processed in current row */
00213   int MCU_vert_offset;          /* counts MCU rows within iMCU row */
00214   int MCU_rows_per_iMCU_row;    /* number of such rows needed */
00215 
00216   /* Virtual block array for each component. */
00217   jvirt_barray_ptr * whole_image;
00218 
00219   /* Workspace for constructing dummy blocks at right/bottom edges. */
00220   JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU];
00221 } my_coef_controller;
00222 
00223 typedef my_coef_controller * my_coef_ptr;
00224 
00225 
00226 LOCAL(void)
00227 start_iMCU_row (j_compress_ptr cinfo)
00228 /* Reset within-iMCU-row counters for a new row */
00229 {
00230   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00231 
00232   /* In an interleaved scan, an MCU row is the same as an iMCU row.
00233    * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
00234    * But at the bottom of the image, process only what's left.
00235    */
00236   if (cinfo->comps_in_scan > 1) {
00237     coef->MCU_rows_per_iMCU_row = 1;
00238   } else {
00239     if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1))
00240       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor;
00241     else
00242       coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height;
00243   }
00244 
00245   coef->mcu_ctr = 0;
00246   coef->MCU_vert_offset = 0;
00247 }
00248 
00249 
00250 /*
00251  * Initialize for a processing pass.
00252  */
00253 
00254 METHODDEF(void)
00255 start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode)
00256 {
00257   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00258 
00259   if (pass_mode != JBUF_CRANK_DEST)
00260     ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
00261 
00262   coef->iMCU_row_num = 0;
00263   start_iMCU_row(cinfo);
00264 }
00265 
00266 
00267 /*
00268  * Process some data.
00269  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
00270  * per call, ie, v_samp_factor block rows for each component in the scan.
00271  * The data is obtained from the virtual arrays and fed to the entropy coder.
00272  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
00273  *
00274  * NB: input_buf is ignored; it is likely to be a NULL pointer.
00275  */
00276 
00277 METHODDEF(boolean)
00278 compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf)
00279 {
00280   my_coef_ptr coef = (my_coef_ptr) cinfo->coef;
00281   JDIMENSION MCU_col_num;       /* index of current MCU within row */
00282   JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
00283   JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
00284   int blkn, ci, xindex, yindex, yoffset, blockcnt;
00285   JDIMENSION start_col;
00286   JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN];
00287   JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
00288   JBLOCKROW buffer_ptr;
00289   jpeg_component_info *compptr;
00290 
00291   /* Align the virtual buffers for the components used in this scan. */
00292   for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00293     compptr = cinfo->cur_comp_info[ci];
00294     buffer[ci] = (*cinfo->mem->access_virt_barray)
00295       ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index],
00296        coef->iMCU_row_num * compptr->v_samp_factor,
00297        (JDIMENSION) compptr->v_samp_factor, FALSE);
00298   }
00299 
00300   /* Loop to process one whole iMCU row */
00301   for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
00302        yoffset++) {
00303     for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
00304          MCU_col_num++) {
00305       /* Construct list of pointers to DCT blocks belonging to this MCU */
00306       blkn = 0;                 /* index of current DCT block within MCU */
00307       for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
00308         compptr = cinfo->cur_comp_info[ci];
00309         start_col = MCU_col_num * compptr->MCU_width;
00310         blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width
00311                                                 : compptr->last_col_width;
00312         for (yindex = 0; yindex < compptr->MCU_height; yindex++) {
00313           if (coef->iMCU_row_num < last_iMCU_row ||
00314               yindex+yoffset < compptr->last_row_height) {
00315             /* Fill in pointers to real blocks in this row */
00316             buffer_ptr = buffer[ci][yindex+yoffset] + start_col;
00317             for (xindex = 0; xindex < blockcnt; xindex++)
00318               MCU_buffer[blkn++] = buffer_ptr++;
00319           } else {
00320             /* At bottom of image, need a whole row of dummy blocks */
00321             xindex = 0;
00322           }
00323           /* Fill in any dummy blocks needed in this row.
00324            * Dummy blocks are filled in the same way as in jccoefct.c:
00325            * all zeroes in the AC entries, DC entries equal to previous
00326            * block's DC value.  The init routine has already zeroed the
00327            * AC entries, so we need only set the DC entries correctly.
00328            */
00329           for (; xindex < compptr->MCU_width; xindex++) {
00330             MCU_buffer[blkn] = coef->dummy_buffer[blkn];
00331             MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0];
00332             blkn++;
00333           }
00334         }
00335       }
00336       /* Try to write the MCU. */
00337       if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) {
00338         /* Suspension forced; update state counters and exit */
00339         coef->MCU_vert_offset = yoffset;
00340         coef->mcu_ctr = MCU_col_num;
00341         return FALSE;
00342       }
00343     }
00344     /* Completed an MCU row, but perhaps not an iMCU row */
00345     coef->mcu_ctr = 0;
00346   }
00347   /* Completed the iMCU row, advance counters for next one */
00348   coef->iMCU_row_num++;
00349   start_iMCU_row(cinfo);
00350   return TRUE;
00351 }
00352 
00353 
00354 /*
00355  * Initialize coefficient buffer controller.
00356  *
00357  * Each passed coefficient array must be the right size for that
00358  * coefficient: width_in_blocks wide and height_in_blocks high,
00359  * with unitheight at least v_samp_factor.
00360  */
00361 
00362 LOCAL(void)
00363 transencode_coef_controller (j_compress_ptr cinfo,
00364                              jvirt_barray_ptr * coef_arrays)
00365 {
00366   my_coef_ptr coef;
00367   JBLOCKROW buffer;
00368   int i;
00369 
00370   coef = (my_coef_ptr)
00371     (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00372                                 SIZEOF(my_coef_controller));
00373   cinfo->coef = (struct jpeg_c_coef_controller *) coef;
00374   coef->pub.start_pass = start_pass_coef;
00375   coef->pub.compress_data = compress_output;
00376 
00377   /* Save pointer to virtual arrays */
00378   coef->whole_image = coef_arrays;
00379 
00380   /* Allocate and pre-zero space for dummy DCT blocks. */
00381   buffer = (JBLOCKROW)
00382     (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00383                                 C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00384   jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK));
00385   for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) {
00386     coef->dummy_buffer[i] = buffer + i;
00387   }
00388 }


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