Home | History | Annotate | Download | only in jpeg
      1 /*
      2  * jcapistd.c
      3  *
      4  * Copyright (C) 1994-1996, Thomas G. Lane.
      5  * This file is part of the Independent JPEG Group's software.
      6  * For conditions of distribution and use, see the accompanying README file.
      7  *
      8  * This file contains application interface code for the compression half
      9  * of the JPEG library.  These are the "standard" API routines that are
     10  * used in the normal full-compression case.  They are not used by a
     11  * transcoding-only application.  Note that if an application links in
     12  * jpeg_start_compress, it will end up linking in the entire compressor.
     13  * We thus must separate this file from jcapimin.c to avoid linking the
     14  * whole compression library into a transcoder.
     15  */
     16 
     17 #define JPEG_INTERNALS
     18 #include "jinclude.h"
     19 #include "jpeglib.h"
     20 
     21 
     22 /*
     23  * Compression initialization.
     24  * Before calling this, all parameters and a data destination must be set up.
     25  *
     26  * We require a write_all_tables parameter as a failsafe check when writing
     27  * multiple datastreams from the same compression object.  Since prior runs
     28  * will have left all the tables marked sent_table=TRUE, a subsequent run
     29  * would emit an abbreviated stream (no tables) by default.  This may be what
     30  * is wanted, but for safety's sake it should not be the default behavior:
     31  * programmers should have to make a deliberate choice to emit abbreviated
     32  * images.  Therefore the documentation and examples should encourage people
     33  * to pass write_all_tables=TRUE; then it will take active thought to do the
     34  * wrong thing.
     35  */
     36 
     37 GLOBAL(void)
     38 jpeg_start_compress (j_compress_ptr cinfo, boolean write_all_tables)
     39 {
     40   if (cinfo->global_state != CSTATE_START)
     41     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     42 
     43   if (write_all_tables)
     44     jpeg_suppress_tables(cinfo, FALSE);	/* mark all tables to be written */
     45 
     46   /* (Re)initialize error mgr and destination modules */
     47   (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
     48   (*cinfo->dest->init_destination) (cinfo);
     49   /* Perform master selection of active modules */
     50   jinit_compress_master(cinfo);
     51   /* Set up for the first pass */
     52   (*cinfo->master->prepare_for_pass) (cinfo);
     53   /* Ready for application to drive first pass through jpeg_write_scanlines
     54    * or jpeg_write_raw_data.
     55    */
     56   cinfo->next_scanline = 0;
     57   cinfo->global_state = (cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING);
     58 }
     59 
     60 
     61 /*
     62  * Write some scanlines of data to the JPEG compressor.
     63  *
     64  * The return value will be the number of lines actually written.
     65  * This should be less than the supplied num_lines only in case that
     66  * the data destination module has requested suspension of the compressor,
     67  * or if more than image_height scanlines are passed in.
     68  *
     69  * Note: we warn about excess calls to jpeg_write_scanlines() since
     70  * this likely signals an application programmer error.  However,
     71  * excess scanlines passed in the last valid call are *silently* ignored,
     72  * so that the application need not adjust num_lines for end-of-image
     73  * when using a multiple-scanline buffer.
     74  */
     75 
     76 GLOBAL(JDIMENSION)
     77 jpeg_write_scanlines (j_compress_ptr cinfo, JSAMPARRAY scanlines,
     78 		      JDIMENSION num_lines)
     79 {
     80   JDIMENSION row_ctr, rows_left;
     81 
     82   if (cinfo->global_state != CSTATE_SCANNING)
     83     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
     84   if (cinfo->next_scanline >= cinfo->image_height)
     85     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
     86 
     87   /* Call progress monitor hook if present */
     88   if (cinfo->progress != NULL) {
     89     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
     90     cinfo->progress->pass_limit = (long) cinfo->image_height;
     91     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
     92   }
     93 
     94   /* Give master control module another chance if this is first call to
     95    * jpeg_write_scanlines.  This lets output of the frame/scan headers be
     96    * delayed so that application can write COM, etc, markers between
     97    * jpeg_start_compress and jpeg_write_scanlines.
     98    */
     99   if (cinfo->master->call_pass_startup)
    100     (*cinfo->master->pass_startup) (cinfo);
    101 
    102   /* Ignore any extra scanlines at bottom of image. */
    103   rows_left = cinfo->image_height - cinfo->next_scanline;
    104   if (num_lines > rows_left)
    105     num_lines = rows_left;
    106 
    107   row_ctr = 0;
    108   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, num_lines);
    109   cinfo->next_scanline += row_ctr;
    110   return row_ctr;
    111 }
    112 
    113 
    114 /*
    115  * Alternate entry point to write raw data.
    116  * Processes exactly one iMCU row per call, unless suspended.
    117  */
    118 
    119 GLOBAL(JDIMENSION)
    120 jpeg_write_raw_data (j_compress_ptr cinfo, JSAMPIMAGE data,
    121 		     JDIMENSION num_lines)
    122 {
    123   JDIMENSION lines_per_iMCU_row;
    124 
    125   if (cinfo->global_state != CSTATE_RAW_OK)
    126     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    127   if (cinfo->next_scanline >= cinfo->image_height) {
    128     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
    129     return 0;
    130   }
    131 
    132   /* Call progress monitor hook if present */
    133   if (cinfo->progress != NULL) {
    134     cinfo->progress->pass_counter = (long) cinfo->next_scanline;
    135     cinfo->progress->pass_limit = (long) cinfo->image_height;
    136     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
    137   }
    138 
    139   /* Give master control module another chance if this is first call to
    140    * jpeg_write_raw_data.  This lets output of the frame/scan headers be
    141    * delayed so that application can write COM, etc, markers between
    142    * jpeg_start_compress and jpeg_write_raw_data.
    143    */
    144   if (cinfo->master->call_pass_startup)
    145     (*cinfo->master->pass_startup) (cinfo);
    146 
    147   /* Verify that at least one iMCU row has been passed. */
    148   lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
    149   if (num_lines < lines_per_iMCU_row)
    150     ERREXIT(cinfo, JERR_BUFFER_SIZE);
    151 
    152   /* Directly compress the row. */
    153   if (! (*cinfo->coef->compress_data) (cinfo, data)) {
    154     /* If compressor did not consume the whole row, suspend processing. */
    155     return 0;
    156   }
    157 
    158   /* OK, we processed one iMCU row. */
    159   cinfo->next_scanline += lines_per_iMCU_row;
    160   return lines_per_iMCU_row;
    161 }
    162