michael@0: /* michael@0: * jctrans.c michael@0: * michael@0: * Copyright (C) 1995-1998, Thomas G. Lane. michael@0: * Modified 2000-2009 by Guido Vollbeding. michael@0: * This file is part of the Independent JPEG Group's software. michael@0: * For conditions of distribution and use, see the accompanying README file. michael@0: * michael@0: * This file contains library routines for transcoding compression, michael@0: * that is, writing raw DCT coefficient arrays to an output JPEG file. michael@0: * The routines in jcapimin.c will also be needed by a transcoder. michael@0: */ michael@0: michael@0: #define JPEG_INTERNALS michael@0: #include "jinclude.h" michael@0: #include "jpeglib.h" michael@0: michael@0: michael@0: /* Forward declarations */ michael@0: LOCAL(void) transencode_master_selection michael@0: JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); michael@0: LOCAL(void) transencode_coef_controller michael@0: JPP((j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays)); michael@0: michael@0: michael@0: /* michael@0: * Compression initialization for writing raw-coefficient data. michael@0: * Before calling this, all parameters and a data destination must be set up. michael@0: * Call jpeg_finish_compress() to actually write the data. michael@0: * michael@0: * The number of passed virtual arrays must match cinfo->num_components. michael@0: * Note that the virtual arrays need not be filled or even realized at michael@0: * the time write_coefficients is called; indeed, if the virtual arrays michael@0: * were requested from this compression object's memory manager, they michael@0: * typically will be realized during this routine and filled afterwards. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jpeg_write_coefficients (j_compress_ptr cinfo, jvirt_barray_ptr * coef_arrays) michael@0: { michael@0: if (cinfo->global_state != CSTATE_START) michael@0: ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state); michael@0: /* Mark all tables to be written */ michael@0: jpeg_suppress_tables(cinfo, FALSE); michael@0: /* (Re)initialize error mgr and destination modules */ michael@0: (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); michael@0: (*cinfo->dest->init_destination) (cinfo); michael@0: /* Perform master selection of active modules */ michael@0: transencode_master_selection(cinfo, coef_arrays); michael@0: /* Wait for jpeg_finish_compress() call */ michael@0: cinfo->next_scanline = 0; /* so jpeg_write_marker works */ michael@0: cinfo->global_state = CSTATE_WRCOEFS; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize the compression object with default parameters, michael@0: * then copy from the source object all parameters needed for lossless michael@0: * transcoding. Parameters that can be varied without loss (such as michael@0: * scan script and Huffman optimization) are left in their default states. michael@0: */ michael@0: michael@0: GLOBAL(void) michael@0: jpeg_copy_critical_parameters (j_decompress_ptr srcinfo, michael@0: j_compress_ptr dstinfo) michael@0: { michael@0: JQUANT_TBL ** qtblptr; michael@0: jpeg_component_info *incomp, *outcomp; michael@0: JQUANT_TBL *c_quant, *slot_quant; michael@0: int tblno, ci, coefi; michael@0: michael@0: /* Safety check to ensure start_compress not called yet. */ michael@0: if (dstinfo->global_state != CSTATE_START) michael@0: ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state); michael@0: /* Copy fundamental image dimensions */ michael@0: dstinfo->image_width = srcinfo->image_width; michael@0: dstinfo->image_height = srcinfo->image_height; michael@0: dstinfo->input_components = srcinfo->num_components; michael@0: dstinfo->in_color_space = srcinfo->jpeg_color_space; michael@0: #if JPEG_LIB_VERSION >= 70 michael@0: dstinfo->jpeg_width = srcinfo->output_width; michael@0: dstinfo->jpeg_height = srcinfo->output_height; michael@0: dstinfo->min_DCT_h_scaled_size = srcinfo->min_DCT_h_scaled_size; michael@0: dstinfo->min_DCT_v_scaled_size = srcinfo->min_DCT_v_scaled_size; michael@0: #endif michael@0: /* Initialize all parameters to default values */ michael@0: jpeg_set_defaults(dstinfo); michael@0: /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB. michael@0: * Fix it to get the right header markers for the image colorspace. michael@0: */ michael@0: jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space); michael@0: dstinfo->data_precision = srcinfo->data_precision; michael@0: dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling; michael@0: /* Copy the source's quantization tables. */ michael@0: for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++) { michael@0: if (srcinfo->quant_tbl_ptrs[tblno] != NULL) { michael@0: qtblptr = & dstinfo->quant_tbl_ptrs[tblno]; michael@0: if (*qtblptr == NULL) michael@0: *qtblptr = jpeg_alloc_quant_table((j_common_ptr) dstinfo); michael@0: MEMCOPY((*qtblptr)->quantval, michael@0: srcinfo->quant_tbl_ptrs[tblno]->quantval, michael@0: SIZEOF((*qtblptr)->quantval)); michael@0: (*qtblptr)->sent_table = FALSE; michael@0: } michael@0: } michael@0: /* Copy the source's per-component info. michael@0: * Note we assume jpeg_set_defaults has allocated the dest comp_info array. michael@0: */ michael@0: dstinfo->num_components = srcinfo->num_components; michael@0: if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS) michael@0: ERREXIT2(dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components, michael@0: MAX_COMPONENTS); michael@0: for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info; michael@0: ci < dstinfo->num_components; ci++, incomp++, outcomp++) { michael@0: outcomp->component_id = incomp->component_id; michael@0: outcomp->h_samp_factor = incomp->h_samp_factor; michael@0: outcomp->v_samp_factor = incomp->v_samp_factor; michael@0: outcomp->quant_tbl_no = incomp->quant_tbl_no; michael@0: /* Make sure saved quantization table for component matches the qtable michael@0: * slot. If not, the input file re-used this qtable slot. michael@0: * IJG encoder currently cannot duplicate this. michael@0: */ michael@0: tblno = outcomp->quant_tbl_no; michael@0: if (tblno < 0 || tblno >= NUM_QUANT_TBLS || michael@0: srcinfo->quant_tbl_ptrs[tblno] == NULL) michael@0: ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno); michael@0: slot_quant = srcinfo->quant_tbl_ptrs[tblno]; michael@0: c_quant = incomp->quant_table; michael@0: if (c_quant != NULL) { michael@0: for (coefi = 0; coefi < DCTSIZE2; coefi++) { michael@0: if (c_quant->quantval[coefi] != slot_quant->quantval[coefi]) michael@0: ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno); michael@0: } michael@0: } michael@0: /* Note: we do not copy the source's Huffman table assignments; michael@0: * instead we rely on jpeg_set_colorspace to have made a suitable choice. michael@0: */ michael@0: } michael@0: /* Also copy JFIF version and resolution information, if available. michael@0: * Strictly speaking this isn't "critical" info, but it's nearly michael@0: * always appropriate to copy it if available. In particular, michael@0: * if the application chooses to copy JFIF 1.02 extension markers from michael@0: * the source file, we need to copy the version to make sure we don't michael@0: * emit a file that has 1.02 extensions but a claimed version of 1.01. michael@0: * We will *not*, however, copy version info from mislabeled "2.01" files. michael@0: */ michael@0: if (srcinfo->saw_JFIF_marker) { michael@0: if (srcinfo->JFIF_major_version == 1) { michael@0: dstinfo->JFIF_major_version = srcinfo->JFIF_major_version; michael@0: dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version; michael@0: } michael@0: dstinfo->density_unit = srcinfo->density_unit; michael@0: dstinfo->X_density = srcinfo->X_density; michael@0: dstinfo->Y_density = srcinfo->Y_density; michael@0: } michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Master selection of compression modules for transcoding. michael@0: * This substitutes for jcinit.c's initialization of the full compressor. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: transencode_master_selection (j_compress_ptr cinfo, michael@0: jvirt_barray_ptr * coef_arrays) michael@0: { michael@0: /* Although we don't actually use input_components for transcoding, michael@0: * jcmaster.c's initial_setup will complain if input_components is 0. michael@0: */ michael@0: cinfo->input_components = 1; michael@0: /* Initialize master control (includes parameter checking/processing) */ michael@0: jinit_c_master_control(cinfo, TRUE /* transcode only */); michael@0: michael@0: /* Entropy encoding: either Huffman or arithmetic coding. */ michael@0: if (cinfo->arith_code) { michael@0: #ifdef C_ARITH_CODING_SUPPORTED michael@0: jinit_arith_encoder(cinfo); michael@0: #else michael@0: ERREXIT(cinfo, JERR_ARITH_NOTIMPL); michael@0: #endif michael@0: } else { michael@0: if (cinfo->progressive_mode) { michael@0: #ifdef C_PROGRESSIVE_SUPPORTED michael@0: jinit_phuff_encoder(cinfo); michael@0: #else michael@0: ERREXIT(cinfo, JERR_NOT_COMPILED); michael@0: #endif michael@0: } else michael@0: jinit_huff_encoder(cinfo); michael@0: } michael@0: michael@0: /* We need a special coefficient buffer controller. */ michael@0: transencode_coef_controller(cinfo, coef_arrays); michael@0: michael@0: jinit_marker_writer(cinfo); michael@0: michael@0: /* We can now tell the memory manager to allocate virtual arrays. */ michael@0: (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo); michael@0: michael@0: /* Write the datastream header (SOI, JFIF) immediately. michael@0: * Frame and scan headers are postponed till later. michael@0: * This lets application insert special markers after the SOI. michael@0: */ michael@0: (*cinfo->marker->write_file_header) (cinfo); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * The rest of this file is a special implementation of the coefficient michael@0: * buffer controller. This is similar to jccoefct.c, but it handles only michael@0: * output from presupplied virtual arrays. Furthermore, we generate any michael@0: * dummy padding blocks on-the-fly rather than expecting them to be present michael@0: * in the arrays. michael@0: */ michael@0: michael@0: /* Private buffer controller object */ michael@0: michael@0: typedef struct { michael@0: struct jpeg_c_coef_controller pub; /* public fields */ michael@0: michael@0: JDIMENSION iMCU_row_num; /* iMCU row # within image */ michael@0: JDIMENSION mcu_ctr; /* counts MCUs processed in current row */ michael@0: int MCU_vert_offset; /* counts MCU rows within iMCU row */ michael@0: int MCU_rows_per_iMCU_row; /* number of such rows needed */ michael@0: michael@0: /* Virtual block array for each component. */ michael@0: jvirt_barray_ptr * whole_image; michael@0: michael@0: /* Workspace for constructing dummy blocks at right/bottom edges. */ michael@0: JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]; michael@0: } my_coef_controller; michael@0: michael@0: typedef my_coef_controller * my_coef_ptr; michael@0: michael@0: michael@0: LOCAL(void) michael@0: start_iMCU_row (j_compress_ptr cinfo) michael@0: /* Reset within-iMCU-row counters for a new row */ michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: michael@0: /* In an interleaved scan, an MCU row is the same as an iMCU row. michael@0: * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows. michael@0: * But at the bottom of the image, process only what's left. michael@0: */ michael@0: if (cinfo->comps_in_scan > 1) { michael@0: coef->MCU_rows_per_iMCU_row = 1; michael@0: } else { michael@0: if (coef->iMCU_row_num < (cinfo->total_iMCU_rows-1)) michael@0: coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->v_samp_factor; michael@0: else michael@0: coef->MCU_rows_per_iMCU_row = cinfo->cur_comp_info[0]->last_row_height; michael@0: } michael@0: michael@0: coef->mcu_ctr = 0; michael@0: coef->MCU_vert_offset = 0; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize for a processing pass. michael@0: */ michael@0: michael@0: METHODDEF(void) michael@0: start_pass_coef (j_compress_ptr cinfo, J_BUF_MODE pass_mode) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: michael@0: if (pass_mode != JBUF_CRANK_DEST) michael@0: ERREXIT(cinfo, JERR_BAD_BUFFER_MODE); michael@0: michael@0: coef->iMCU_row_num = 0; michael@0: start_iMCU_row(cinfo); michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Process some data. michael@0: * We process the equivalent of one fully interleaved MCU row ("iMCU" row) michael@0: * per call, ie, v_samp_factor block rows for each component in the scan. michael@0: * The data is obtained from the virtual arrays and fed to the entropy coder. michael@0: * Returns TRUE if the iMCU row is completed, FALSE if suspended. michael@0: * michael@0: * NB: input_buf is ignored; it is likely to be a NULL pointer. michael@0: */ michael@0: michael@0: METHODDEF(boolean) michael@0: compress_output (j_compress_ptr cinfo, JSAMPIMAGE input_buf) michael@0: { michael@0: my_coef_ptr coef = (my_coef_ptr) cinfo->coef; michael@0: JDIMENSION MCU_col_num; /* index of current MCU within row */ michael@0: JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1; michael@0: JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1; michael@0: int blkn, ci, xindex, yindex, yoffset, blockcnt; michael@0: JDIMENSION start_col; michael@0: JBLOCKARRAY buffer[MAX_COMPS_IN_SCAN]; michael@0: JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]; michael@0: JBLOCKROW buffer_ptr; michael@0: jpeg_component_info *compptr; michael@0: michael@0: /* Align the virtual buffers for the components used in this scan. */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: buffer[ci] = (*cinfo->mem->access_virt_barray) michael@0: ((j_common_ptr) cinfo, coef->whole_image[compptr->component_index], michael@0: coef->iMCU_row_num * compptr->v_samp_factor, michael@0: (JDIMENSION) compptr->v_samp_factor, FALSE); michael@0: } michael@0: michael@0: /* Loop to process one whole iMCU row */ michael@0: for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row; michael@0: yoffset++) { michael@0: for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row; michael@0: MCU_col_num++) { michael@0: /* Construct list of pointers to DCT blocks belonging to this MCU */ michael@0: blkn = 0; /* index of current DCT block within MCU */ michael@0: for (ci = 0; ci < cinfo->comps_in_scan; ci++) { michael@0: compptr = cinfo->cur_comp_info[ci]; michael@0: start_col = MCU_col_num * compptr->MCU_width; michael@0: blockcnt = (MCU_col_num < last_MCU_col) ? compptr->MCU_width michael@0: : compptr->last_col_width; michael@0: for (yindex = 0; yindex < compptr->MCU_height; yindex++) { michael@0: if (coef->iMCU_row_num < last_iMCU_row || michael@0: yindex+yoffset < compptr->last_row_height) { michael@0: /* Fill in pointers to real blocks in this row */ michael@0: buffer_ptr = buffer[ci][yindex+yoffset] + start_col; michael@0: for (xindex = 0; xindex < blockcnt; xindex++) michael@0: MCU_buffer[blkn++] = buffer_ptr++; michael@0: } else { michael@0: /* At bottom of image, need a whole row of dummy blocks */ michael@0: xindex = 0; michael@0: } michael@0: /* Fill in any dummy blocks needed in this row. michael@0: * Dummy blocks are filled in the same way as in jccoefct.c: michael@0: * all zeroes in the AC entries, DC entries equal to previous michael@0: * block's DC value. The init routine has already zeroed the michael@0: * AC entries, so we need only set the DC entries correctly. michael@0: */ michael@0: for (; xindex < compptr->MCU_width; xindex++) { michael@0: MCU_buffer[blkn] = coef->dummy_buffer[blkn]; michael@0: MCU_buffer[blkn][0][0] = MCU_buffer[blkn-1][0][0]; michael@0: blkn++; michael@0: } michael@0: } michael@0: } michael@0: /* Try to write the MCU. */ michael@0: if (! (*cinfo->entropy->encode_mcu) (cinfo, MCU_buffer)) { michael@0: /* Suspension forced; update state counters and exit */ michael@0: coef->MCU_vert_offset = yoffset; michael@0: coef->mcu_ctr = MCU_col_num; michael@0: return FALSE; michael@0: } michael@0: } michael@0: /* Completed an MCU row, but perhaps not an iMCU row */ michael@0: coef->mcu_ctr = 0; michael@0: } michael@0: /* Completed the iMCU row, advance counters for next one */ michael@0: coef->iMCU_row_num++; michael@0: start_iMCU_row(cinfo); michael@0: return TRUE; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * Initialize coefficient buffer controller. michael@0: * michael@0: * Each passed coefficient array must be the right size for that michael@0: * coefficient: width_in_blocks wide and height_in_blocks high, michael@0: * with unitheight at least v_samp_factor. michael@0: */ michael@0: michael@0: LOCAL(void) michael@0: transencode_coef_controller (j_compress_ptr cinfo, michael@0: jvirt_barray_ptr * coef_arrays) michael@0: { michael@0: my_coef_ptr coef; michael@0: JBLOCKROW buffer; michael@0: int i; michael@0: michael@0: coef = (my_coef_ptr) michael@0: (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: SIZEOF(my_coef_controller)); michael@0: cinfo->coef = (struct jpeg_c_coef_controller *) coef; michael@0: coef->pub.start_pass = start_pass_coef; michael@0: coef->pub.compress_data = compress_output; michael@0: michael@0: /* Save pointer to virtual arrays */ michael@0: coef->whole_image = coef_arrays; michael@0: michael@0: /* Allocate and pre-zero space for dummy DCT blocks. */ michael@0: buffer = (JBLOCKROW) michael@0: (*cinfo->mem->alloc_large) ((j_common_ptr) cinfo, JPOOL_IMAGE, michael@0: C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); michael@0: jzero_far((void FAR *) buffer, C_MAX_BLOCKS_IN_MCU * SIZEOF(JBLOCK)); michael@0: for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++) { michael@0: coef->dummy_buffer[i] = buffer + i; michael@0: } michael@0: }