jcapistd.cpp (6271B)
1 /* 2 * jcapistd.c 3 * 4 * Copyright (C) 1994-1995, 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 if ( cinfo->global_state != CSTATE_START ) { 40 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 41 } 42 43 if ( write_all_tables ) { 44 jpeg_suppress_tables( cinfo, FALSE ); 45 } /* mark all tables to be written */ 46 47 /* (Re)initialize error mgr and destination modules */ 48 ( *cinfo->err->reset_error_mgr )( (j_common_ptr) cinfo ); 49 ( *cinfo->dest->init_destination )( cinfo ); 50 /* Perform master selection of active modules */ 51 jinit_compress_master( cinfo ); 52 /* Set up for the first pass */ 53 ( *cinfo->master->prepare_for_pass )( cinfo ); 54 /* Ready for application to drive first pass through jpeg_write_scanlines 55 * or jpeg_write_raw_data. 56 */ 57 cinfo->next_scanline = 0; 58 cinfo->global_state = ( cinfo->raw_data_in ? CSTATE_RAW_OK : CSTATE_SCANNING ); 59 } 60 61 62 /* 63 * Write some scanlines of data to the JPEG compressor. 64 * 65 * The return value will be the number of lines actually written. 66 * This should be less than the supplied num_lines only in case that 67 * the data destination module has requested suspension of the compressor, 68 * or if more than image_height scanlines are passed in. 69 * 70 * Note: we warn about excess calls to jpeg_write_scanlines() since 71 * this likely signals an application programmer error. However, 72 * excess scanlines passed in the last valid call are *silently* ignored, 73 * so that the application need not adjust num_lines for end-of-image 74 * when using a multiple-scanline buffer. 75 */ 76 77 GLOBAL JDIMENSION 78 jpeg_write_scanlines( j_compress_ptr cinfo, JSAMPARRAY scanlines, 79 JDIMENSION num_lines ) { 80 JDIMENSION row_ctr, rows_left; 81 82 if ( cinfo->global_state != CSTATE_SCANNING ) { 83 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 84 } 85 if ( cinfo->next_scanline >= cinfo->image_height ) { 86 WARNMS( cinfo, JWRN_TOO_MUCH_DATA ); 87 } 88 89 /* Call progress monitor hook if present */ 90 if ( cinfo->progress != NULL ) { 91 cinfo->progress->pass_counter = (long) cinfo->next_scanline; 92 cinfo->progress->pass_limit = (long) cinfo->image_height; 93 ( *cinfo->progress->progress_monitor )( (j_common_ptr) cinfo ); 94 } 95 96 /* Give master control module another chance if this is first call to 97 * jpeg_write_scanlines. This lets output of the frame/scan headers be 98 * delayed so that application can write COM, etc, markers between 99 * jpeg_start_compress and jpeg_write_scanlines. 100 */ 101 if ( cinfo->master->call_pass_startup ) { 102 ( *cinfo->master->pass_startup )( cinfo ); 103 } 104 105 /* Ignore any extra scanlines at bottom of image. */ 106 rows_left = cinfo->image_height - cinfo->next_scanline; 107 if ( num_lines > rows_left ) { 108 num_lines = rows_left; 109 } 110 111 row_ctr = 0; 112 ( *cinfo->main->process_data )( cinfo, scanlines, &row_ctr, num_lines ); 113 cinfo->next_scanline += row_ctr; 114 return row_ctr; 115 } 116 117 118 /* 119 * Alternate entry point to write raw data. 120 * Processes exactly one iMCU row per call, unless suspended. 121 */ 122 123 GLOBAL JDIMENSION 124 jpeg_write_raw_data( j_compress_ptr cinfo, JSAMPIMAGE data, 125 JDIMENSION num_lines ) { 126 JDIMENSION lines_per_iMCU_row; 127 128 if ( cinfo->global_state != CSTATE_RAW_OK ) { 129 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 130 } 131 if ( cinfo->next_scanline >= cinfo->image_height ) { 132 WARNMS( cinfo, JWRN_TOO_MUCH_DATA ); 133 return 0; 134 } 135 136 /* Call progress monitor hook if present */ 137 if ( cinfo->progress != NULL ) { 138 cinfo->progress->pass_counter = (long) cinfo->next_scanline; 139 cinfo->progress->pass_limit = (long) cinfo->image_height; 140 ( *cinfo->progress->progress_monitor )( (j_common_ptr) cinfo ); 141 } 142 143 /* Give master control module another chance if this is first call to 144 * jpeg_write_raw_data. This lets output of the frame/scan headers be 145 * delayed so that application can write COM, etc, markers between 146 * jpeg_start_compress and jpeg_write_raw_data. 147 */ 148 if ( cinfo->master->call_pass_startup ) { 149 ( *cinfo->master->pass_startup )( cinfo ); 150 } 151 152 /* Verify that at least one iMCU row has been passed. */ 153 lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE; 154 if ( num_lines < lines_per_iMCU_row ) { 155 ERREXIT( cinfo, JERR_BUFFER_SIZE ); 156 } 157 158 /* Directly compress the row. */ 159 if ( !( *cinfo->coef->compress_data )( cinfo, data ) ) { 160 /* If compressor did not consume the whole row, suspend processing. */ 161 return 0; 162 } 163 164 /* OK, we processed one iMCU row. */ 165 cinfo->next_scanline += lines_per_iMCU_row; 166 return lines_per_iMCU_row; 167 }