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