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