Shoaib Ahmed / Mbed 2 deprecated uzairkhan

Dependencies:   uzair Camera_LS_Y201 F7_Ethernet LCD_DISCO_F746NG NetworkAPI SDFileSystem mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers jctrans.c Source File

jctrans.c

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