Back to index

lightning-sunbird  0.9+nobinonly
jcapistd.c
Go to the documentation of this file.
00001 /*
00002  * jcapistd.c
00003  *
00004  * Copyright (C) 1994-1996, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains application interface code for the compression half
00009  * of the JPEG library.  These are the "standard" API routines that are
00010  * used in the normal full-compression case.  They are not used by a
00011  * transcoding-only application.  Note that if an application links in
00012  * jpeg_start_compress, it will end up linking in the entire compressor.
00013  * We thus must separate this file from jcapimin.c to avoid linking the
00014  * whole compression library into a transcoder.
00015  */
00016 
00017 #define JPEG_INTERNALS
00018 #include "jinclude.h"
00019 #include "jpeglib.h"
00020 
00021 
00022 /*
00023  * Compression initialization.
00024  * Before calling this, all parameters and a data destination must be set up.
00025  *
00026  * We require a write_all_tables parameter as a failsafe check when writing
00027  * multiple datastreams from the same compression object.  Since prior runs
00028  * will have left all the tables marked sent_table=TRUE, a subsequent run
00029  * would emit an abbreviated stream (no tables) by default.  This may be what
00030  * is wanted, but for safety's sake it should not be the default behavior:
00031  * programmers should have to make a deliberate choice to emit abbreviated
00032  * images.  Therefore the documentation and examples should encourage people
00033  * to pass write_all_tables=TRUE; then it will take active thought to do the
00034  * wrong thing.
00035  */
00036 
00037 GLOBAL(void)
00038 jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
00039 {
00040   if (cinfo->global_state != CSTATE_START)
00041     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00042 
00043   if (write_all_tables)
00044     jpeg_suppress_tables(cinfo, FALSE);   /* mark all tables to be written */
00045 
00046   /* (Re)initialize error mgr and destination modules */
00047   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
00048   (*cinfo->dest->init_destination) (cinfo);
00049   /* Perform master selection of active modules */
00050   jinit_compress_master(cinfo);
00051   /* Set up for the first pass */
00052   (*cinfo->master->prepare_for_pass) (cinfo);
00053   /* Ready for application to drive first pass through jpeg_write_scanlines
00054    * or jpeg_write_raw_data.
00055    */
00056   cinfo->next_scanline = 0;
00057   cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
00058 }
00059 
00060 
00061 /*
00062  * Write some scanlines of data to the JPEG compressor.
00063  *
00064  * The return value will be the number of lines actually written.
00065  * This should be less than the supplied num_lines only in case that
00066  * the data destination module has requested suspension of the compressor,
00067  * or if more than image_height scanlines are passed in.
00068  *
00069  * Note: we warn about excess calls to jpeg_write_scanlines() since
00070  * this likely signals an application programmer error.  However,
00071  * excess scanlines passed in the last valid call are *silently* ignored,
00072  * so that the application need not adjust num_lines for end-of-image
00073  * when using a multiple-scanline buffer.
00074  */
00075 
00076 GLOBAL(JDIMENSION)
00077 jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
00078                     JDIMENSION num_lines)
00079 {
00080   JDIMENSION row_ctr, rows_left;
00081 
00082   if (cinfo->global_state != CSTATE_SCANNING)
00083     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00084   if (cinfo->next_scanline >= cinfo->image_height)
00085     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
00086 
00087   /* Call progress monitor hook if present */
00088   if (cinfo->progress != NULL) {
00089     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
00090     cinfo->progress->pass_limit = (long) cinfo->image_height;
00091     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00092   }
00093 
00094   /* Give master control module another chance if this is first call to
00095    * jpeg_write_scanlines.  This lets output of the frame/scan headers be
00096    * delayed so that application can write COM, etc, markers between
00097    * jpeg_start_compress and jpeg_write_scanlines.
00098    */
00099   if (cinfo->master->call_pass_startup)
00100     (*cinfo->master->pass_startup) (cinfo);
00101 
00102   /* Ignore any extra scanlines at bottom of image. */
00103   rows_left = cinfo->image_height - cinfo->next_scanline;
00104   if (num_lines > rows_left)
00105     num_lines = rows_left;
00106 
00107   row_ctr = 0;
00108   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
00109   cinfo->next_scanline += row_ctr;
00110   return row_ctr;
00111 }
00112 
00113 
00114 /*
00115  * Alternate entry point to write raw data.
00116  * Processes exactly one iMCU row per call, unless suspended.
00117  */
00118 
00119 GLOBAL(JDIMENSION)
00120 jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
00121                    JDIMENSION num_lines)
00122 {
00123   JDIMENSION lines_per_iMCU_row;
00124 
00125   if (cinfo->global_state != CSTATE_RAW_OK)
00126     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00127   if (cinfo->next_scanline >= cinfo->image_height) {
00128     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
00129     return 0;
00130   }
00131 
00132   /* Call progress monitor hook if present */
00133   if (cinfo->progress != NULL) {
00134     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
00135     cinfo->progress->pass_limit = (long) cinfo->image_height;
00136     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
00137   }
00138 
00139   /* Give master control module another chance if this is first call to
00140    * jpeg_write_raw_data.  This lets output of the frame/scan headers be
00141    * delayed so that application can write COM, etc, markers between
00142    * jpeg_start_compress and jpeg_write_raw_data.
00143    */
00144   if (cinfo->master->call_pass_startup)
00145     (*cinfo->master->pass_startup) (cinfo);
00146 
00147   /* Verify that at least one iMCU row has been passed. */
00148   lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
00149   if (num_lines < lines_per_iMCU_row)
00150     ERREXIT(cinfo, JERR_BUFFER_SIZE);
00151 
00152   /* Directly compress the row. */
00153   if (! (*cinfo->coef->compress_data) (cinfo, data)) {
00154     /* If compressor did not consume the whole row, suspend processing. */
00155     return 0;
00156   }
00157 
00158   /* OK, we processed one iMCU row. */
00159   cinfo->next_scanline += lines_per_iMCU_row;
00160   return lines_per_iMCU_row;
00161 }