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