Blame gtkmm-osx/jpeg-6b/jctrans.c

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