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