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

darco 56a656
/*
darco 56a656
 * jcapistd.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1994-1996, 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 application interface code for the compression half
darco 56a656
 * of the JPEG library.  These are the "standard" API routines that are
darco 56a656
 * used in the normal full-compression case.  They are not used by a
darco 56a656
 * transcoding-only application.  Note that if an application links in
darco 56a656
 * jpeg_start_compress, it will end up linking in the entire compressor.
darco 56a656
 * We thus must separate this file from jcapimin.c to avoid linking the
darco 56a656
 * whole compression library into 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
/*
darco 56a656
 * Compression initialization.
darco 56a656
 * Before calling this, all parameters and a data destination must be set up.
darco 56a656
 *
darco 56a656
 * We require a write_all_tables parameter as a failsafe check when writing
darco 56a656
 * multiple datastreams from the same compression object.  Since prior runs
darco 56a656
 * will have left all the tables marked sent_table=TRUE, a subsequent run
darco 56a656
 * would emit an abbreviated stream (no tables) by default.  This may be what
darco 56a656
 * is wanted, but for safety's sake it should not be the default behavior:
darco 56a656
 * programmers should have to make a deliberate choice to emit abbreviated
darco 56a656
 * images.  Therefore the documentation and examples should encourage people
darco 56a656
 * to pass write_all_tables=TRUE; then it will take active thought to do the
darco 56a656
 * wrong thing.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
darco 56a656
{
darco 56a656
  if (cinfo->global_state != CSTATE_START)
darco 56a656
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
darco 56a656
darco 56a656
  if (write_all_tables)
darco 56a656
    jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
darco 56a656
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
  jinit_compress_master(cinfo);
darco 56a656
  /* Set up for the first pass */
darco 56a656
  (*cinfo->master->prepare_for_pass) (cinfo);
darco 56a656
  /* Ready for application to drive first pass through jpeg_write_scanlines
darco 56a656
   * or jpeg_write_raw_data.
darco 56a656
   */
darco 56a656
  cinfo->next_scanline = 0;
darco 56a656
  cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Write some scanlines of data to the JPEG compressor.
darco 56a656
 *
darco 56a656
 * The return value will be the number of lines actually written.
darco 56a656
 * This should be less than the supplied num_lines only in case that
darco 56a656
 * the data destination module has requested suspension of the compressor,
darco 56a656
 * or if more than image_height scanlines are passed in.
darco 56a656
 *
darco 56a656
 * Note: we warn about excess calls to jpeg_write_scanlines() since
darco 56a656
 * this likely signals an application programmer error.  However,
darco 56a656
 * excess scanlines passed in the last valid call are *silently* ignored,
darco 56a656
 * so that the application need not adjust num_lines for end-of-image
darco 56a656
 * when using a multiple-scanline buffer.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(JDIMENSION)
darco 56a656
jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
darco 56a656
		      JDIMENSION num_lines)
darco 56a656
{
darco 56a656
  JDIMENSION row_ctr, rows_left;
darco 56a656
darco 56a656
  if (cinfo->global_state != CSTATE_SCANNING)
darco 56a656
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
darco 56a656
  if (cinfo->next_scanline >= cinfo->image_height)
darco 56a656
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
darco 56a656
darco 56a656
  /* Call progress monitor hook if present */
darco 56a656
  if (cinfo->progress != NULL) {
darco 56a656
    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
darco 56a656
    cinfo->progress->pass_limit = (long) cinfo->image_height;
darco 56a656
    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
darco 56a656
  }
darco 56a656
darco 56a656
  /* Give master control module another chance if this is first call to
darco 56a656
   * jpeg_write_scanlines.  This lets output of the frame/scan headers be
darco 56a656
   * delayed so that application can write COM, etc, markers between
darco 56a656
   * jpeg_start_compress and jpeg_write_scanlines.
darco 56a656
   */
darco 56a656
  if (cinfo->master->call_pass_startup)
darco 56a656
    (*cinfo->master->pass_startup) (cinfo);
darco 56a656
darco 56a656
  /* Ignore any extra scanlines at bottom of image. */
darco 56a656
  rows_left = cinfo->image_height - cinfo->next_scanline;
darco 56a656
  if (num_lines > rows_left)
darco 56a656
    num_lines = rows_left;
darco 56a656
darco 56a656
  row_ctr = 0;
darco 56a656
  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
darco 56a656
  cinfo->next_scanline += row_ctr;
darco 56a656
  return row_ctr;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Alternate entry point to write raw data.
darco 56a656
 * Processes exactly one iMCU row per call, unless suspended.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(JDIMENSION)
darco 56a656
jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
darco 56a656
		     JDIMENSION num_lines)
darco 56a656
{
darco 56a656
  JDIMENSION lines_per_iMCU_row;
darco 56a656
darco 56a656
  if (cinfo->global_state != CSTATE_RAW_OK)
darco 56a656
    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
darco 56a656
  if (cinfo->next_scanline >= cinfo->image_height) {
darco 56a656
    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
darco 56a656
    return 0;
darco 56a656
  }
darco 56a656
darco 56a656
  /* Call progress monitor hook if present */
darco 56a656
  if (cinfo->progress != NULL) {
darco 56a656
    cinfo->progress->pass_counter = (long) cinfo->next_scanline;
darco 56a656
    cinfo->progress->pass_limit = (long) cinfo->image_height;
darco 56a656
    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
darco 56a656
  }
darco 56a656
darco 56a656
  /* Give master control module another chance if this is first call to
darco 56a656
   * jpeg_write_raw_data.  This lets output of the frame/scan headers be
darco 56a656
   * delayed so that application can write COM, etc, markers between
darco 56a656
   * jpeg_start_compress and jpeg_write_raw_data.
darco 56a656
   */
darco 56a656
  if (cinfo->master->call_pass_startup)
darco 56a656
    (*cinfo->master->pass_startup) (cinfo);
darco 56a656
darco 56a656
  /* Verify that at least one iMCU row has been passed. */
darco 56a656
  lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
darco 56a656
  if (num_lines < lines_per_iMCU_row)
darco 56a656
    ERREXIT(cinfo, JERR_BUFFER_SIZE);
darco 56a656
darco 56a656
  /* Directly compress the row. */
darco 56a656
  if (! (*cinfo->coef->compress_data) (cinfo, data)) {
darco 56a656
    /* If compressor did not consume the whole row, suspend processing. */
darco 56a656
    return 0;
darco 56a656
  }
darco 56a656
darco 56a656
  /* OK, we processed one iMCU row. */
darco 56a656
  cinfo->next_scanline += lines_per_iMCU_row;
darco 56a656
  return lines_per_iMCU_row;
darco 56a656
}