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