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