jdapimin.cpp (14127B)
1 /* 2 * jdapimin.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 decompression half 9 * of the JPEG library. These are the "minimum" API routines that may be 10 * needed in either the normal full-decompression case or the 11 * transcoding-only case. 12 * 13 * Most of the routines intended to be called directly by an application 14 * are in this file or in jdapistd.c. But also see jcomapi.c for routines 15 * shared by compression and decompression, and jdtrans.c for the transcoding 16 * case. 17 */ 18 19 #define JPEG_INTERNALS 20 #include "jinclude.h" 21 #include "jpeglib.h" 22 23 24 /* 25 * Initialization of a JPEG decompression object. 26 * The error manager must already be set up (in case memory manager fails). 27 */ 28 29 GLOBAL void 30 jpeg_create_decompress( j_decompress_ptr cinfo ) { 31 int i; 32 33 /* For debugging purposes, zero the whole master structure. 34 * But error manager pointer is already there, so save and restore it. 35 */ 36 { 37 struct jpeg_error_mgr * err = cinfo->err; 38 MEMZERO( cinfo, SIZEOF( struct jpeg_decompress_struct ) ); 39 cinfo->err = err; 40 } 41 cinfo->is_decompressor = TRUE; 42 43 /* Initialize a memory manager instance for this object */ 44 jinit_memory_mgr( (j_common_ptr) cinfo ); 45 46 /* Zero out pointers to permanent structures. */ 47 cinfo->progress = NULL; 48 cinfo->src = NULL; 49 50 for ( i = 0; i < NUM_QUANT_TBLS; i++ ) { 51 cinfo->quant_tbl_ptrs[i] = NULL; 52 } 53 54 for ( i = 0; i < NUM_HUFF_TBLS; i++ ) { 55 cinfo->dc_huff_tbl_ptrs[i] = NULL; 56 cinfo->ac_huff_tbl_ptrs[i] = NULL; 57 } 58 59 /* Initialize marker processor so application can override methods 60 * for COM, APPn markers before calling jpeg_read_header. 61 */ 62 jinit_marker_reader( cinfo ); 63 64 /* And initialize the overall input controller. */ 65 jinit_input_controller( cinfo ); 66 67 /* OK, I'm ready */ 68 cinfo->global_state = DSTATE_START; 69 } 70 71 72 /* 73 * Destruction of a JPEG decompression object 74 */ 75 76 GLOBAL void 77 jpeg_destroy_decompress( j_decompress_ptr cinfo ) { 78 jpeg_destroy( (j_common_ptr) cinfo );/* use common routine */ 79 } 80 81 82 /* 83 * Abort processing of a JPEG decompression operation, 84 * but don't destroy the object itself. 85 */ 86 87 GLOBAL void 88 jpeg_abort_decompress( j_decompress_ptr cinfo ) { 89 jpeg_abort( (j_common_ptr) cinfo );/* use common routine */ 90 } 91 92 93 /* 94 * Install a special processing method for COM or APPn markers. 95 */ 96 97 GLOBAL void 98 jpeg_set_marker_processor( j_decompress_ptr cinfo, int marker_code, 99 jpeg_marker_parser_method routine ) { 100 if ( marker_code == JPEG_COM ) { 101 cinfo->marker->process_COM = routine; 102 } else if ( marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0 + 15 ) { 103 cinfo->marker->process_APPn[marker_code - JPEG_APP0] = routine; 104 } else { 105 ERREXIT1( cinfo, JERR_UNKNOWN_MARKER, marker_code ); 106 } 107 } 108 109 110 /* 111 * Set default decompression parameters. 112 */ 113 114 LOCAL void 115 default_decompress_parms( j_decompress_ptr cinfo ) { 116 /* Guess the input colorspace, and set output colorspace accordingly. */ 117 /* (Wish JPEG committee had provided a real way to specify this...) */ 118 /* Note application may override our guesses. */ 119 switch ( cinfo->num_components ) { 120 case 1: 121 cinfo->jpeg_color_space = JCS_GRAYSCALE; 122 cinfo->out_color_space = JCS_GRAYSCALE; 123 break; 124 125 case 3: 126 if ( cinfo->saw_JFIF_marker ) { 127 cinfo->jpeg_color_space = JCS_YCbCr;/* JFIF implies YCbCr */ 128 } else if ( cinfo->saw_Adobe_marker ) { 129 switch ( cinfo->Adobe_transform ) { 130 case 0: 131 cinfo->jpeg_color_space = JCS_RGB; 132 break; 133 case 1: 134 cinfo->jpeg_color_space = JCS_YCbCr; 135 break; 136 default: 137 WARNMS1( cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform ); 138 cinfo->jpeg_color_space = JCS_YCbCr;/* assume it's YCbCr */ 139 break; 140 } 141 } else { 142 /* Saw no special markers, try to guess from the component IDs */ 143 int cid0 = cinfo->comp_info[0].component_id; 144 int cid1 = cinfo->comp_info[1].component_id; 145 int cid2 = cinfo->comp_info[2].component_id; 146 147 if ( ( cid0 == 1 ) && ( cid1 == 2 ) && ( cid2 == 3 ) ) { 148 cinfo->jpeg_color_space = JCS_YCbCr; 149 } /* assume JFIF w/out marker */ 150 else if ( cid0 == 82 && cid1 == 71 && cid2 == 66 ) { 151 cinfo->jpeg_color_space = JCS_RGB; 152 } /* ASCII 'R', 'G', 'B' */ 153 else { 154 TRACEMS3( cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2 ); 155 cinfo->jpeg_color_space = JCS_YCbCr;/* assume it's YCbCr */ 156 } 157 } 158 /* Always guess RGB is proper output colorspace. */ 159 cinfo->out_color_space = JCS_RGB; 160 break; 161 162 case 4: 163 if ( cinfo->saw_Adobe_marker ) { 164 switch ( cinfo->Adobe_transform ) { 165 case 0: 166 cinfo->jpeg_color_space = JCS_CMYK; 167 break; 168 case 2: 169 cinfo->jpeg_color_space = JCS_YCCK; 170 break; 171 default: 172 WARNMS1( cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform ); 173 cinfo->jpeg_color_space = JCS_YCCK;/* assume it's YCCK */ 174 break; 175 } 176 } else { 177 /* No special markers, assume straight CMYK. */ 178 cinfo->jpeg_color_space = JCS_CMYK; 179 } 180 cinfo->out_color_space = JCS_CMYK; 181 break; 182 183 default: 184 cinfo->jpeg_color_space = JCS_UNKNOWN; 185 cinfo->out_color_space = JCS_UNKNOWN; 186 break; 187 } 188 189 /* Set defaults for other decompression parameters. */ 190 cinfo->scale_num = 1; /* 1:1 scaling */ 191 cinfo->scale_denom = 1; 192 cinfo->output_gamma = 1.0; 193 cinfo->buffered_image = FALSE; 194 cinfo->raw_data_out = FALSE; 195 cinfo->dct_method = JDCT_DEFAULT; 196 cinfo->do_fancy_upsampling = TRUE; 197 cinfo->do_block_smoothing = TRUE; 198 cinfo->quantize_colors = FALSE; 199 /* We set these in case application only sets quantize_colors. */ 200 cinfo->dither_mode = JDITHER_FS; 201 #ifdef QUANT_2PASS_SUPPORTED 202 cinfo->two_pass_quantize = TRUE; 203 #else 204 cinfo->two_pass_quantize = FALSE; 205 #endif 206 cinfo->desired_number_of_colors = 256; 207 cinfo->colormap = NULL; 208 /* Initialize for no mode change in buffered-image mode. */ 209 cinfo->enable_1pass_quant = FALSE; 210 cinfo->enable_external_quant = FALSE; 211 cinfo->enable_2pass_quant = FALSE; 212 } 213 214 215 /* 216 * Decompression startup: read start of JPEG datastream to see what's there. 217 * Need only initialize JPEG object and supply a data source before calling. 218 * 219 * This routine will read as far as the first SOS marker (ie, actual start of 220 * compressed data), and will save all tables and parameters in the JPEG 221 * object. It will also initialize the decompression parameters to default 222 * values, and finally return JPEG_HEADER_OK. On return, the application may 223 * adjust the decompression parameters and then call jpeg_start_decompress. 224 * (Or, if the application only wanted to determine the image parameters, 225 * the data need not be decompressed. In that case, call jpeg_abort or 226 * jpeg_destroy to release any temporary space.) 227 * If an abbreviated (tables only) datastream is presented, the routine will 228 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then 229 * re-use the JPEG object to read the abbreviated image datastream(s). 230 * It is unnecessary (but OK) to call jpeg_abort in this case. 231 * The JPEG_SUSPENDED return code only occurs if the data source module 232 * requests suspension of the decompressor. In this case the application 233 * should load more source data and then re-call jpeg_read_header to resume 234 * processing. 235 * If a non-suspending data source is used and require_image is TRUE, then the 236 * return code need not be inspected since only JPEG_HEADER_OK is possible. 237 * 238 * This routine is now just a front end to jpeg_consume_input, with some 239 * extra error checking. 240 */ 241 242 GLOBAL int 243 jpeg_read_header( j_decompress_ptr cinfo, boolean require_image ) { 244 int retcode; 245 246 if ( ( cinfo->global_state != DSTATE_START ) && 247 ( cinfo->global_state != DSTATE_INHEADER ) ) { 248 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 249 } 250 251 retcode = jpeg_consume_input( cinfo ); 252 253 switch ( retcode ) { 254 case JPEG_REACHED_SOS: 255 retcode = JPEG_HEADER_OK; 256 break; 257 case JPEG_REACHED_EOI: 258 if ( require_image ) {/* Complain if application wanted an image */ 259 ERREXIT( cinfo, JERR_NO_IMAGE ); 260 } 261 /* Reset to start state; it would be safer to require the application to 262 * call jpeg_abort, but we can't change it now for compatibility reasons. 263 * A side effect is to free any temporary memory (there shouldn't be any). 264 */ 265 jpeg_abort( (j_common_ptr) cinfo );/* sets state = DSTATE_START */ 266 retcode = JPEG_HEADER_TABLES_ONLY; 267 break; 268 case JPEG_SUSPENDED: 269 /* no work */ 270 break; 271 } 272 273 return retcode; 274 } 275 276 277 /* 278 * Consume data in advance of what the decompressor requires. 279 * This can be called at any time once the decompressor object has 280 * been created and a data source has been set up. 281 * 282 * This routine is essentially a state machine that handles a couple 283 * of critical state-transition actions, namely initial setup and 284 * transition from header scanning to ready-for-start_decompress. 285 * All the actual input is done via the input controller's consume_input 286 * method. 287 */ 288 289 GLOBAL int 290 jpeg_consume_input( j_decompress_ptr cinfo ) { 291 int retcode = JPEG_SUSPENDED; 292 293 /* NB: every possible DSTATE value should be listed in this switch */ 294 switch ( cinfo->global_state ) { 295 case DSTATE_START: 296 /* Start-of-datastream actions: reset appropriate modules */ 297 ( *cinfo->inputctl->reset_input_controller )( cinfo ); 298 /* Initialize application's data source module */ 299 ( *cinfo->src->init_source )( cinfo ); 300 cinfo->global_state = DSTATE_INHEADER; 301 /*FALLTHROUGH*/ 302 case DSTATE_INHEADER: 303 retcode = ( *cinfo->inputctl->consume_input )( cinfo ); 304 if ( retcode == JPEG_REACHED_SOS ) {/* Found SOS, prepare to decompress */ 305 /* Set up default parameters based on header data */ 306 default_decompress_parms( cinfo ); 307 /* Set global state: ready for start_decompress */ 308 cinfo->global_state = DSTATE_READY; 309 } 310 break; 311 case DSTATE_READY: 312 /* Can't advance past first SOS until start_decompress is called */ 313 retcode = JPEG_REACHED_SOS; 314 break; 315 case DSTATE_PRELOAD: 316 case DSTATE_PRESCAN: 317 case DSTATE_SCANNING: 318 case DSTATE_RAW_OK: 319 case DSTATE_BUFIMAGE: 320 case DSTATE_BUFPOST: 321 case DSTATE_STOPPING: 322 retcode = ( *cinfo->inputctl->consume_input )( cinfo ); 323 break; 324 default: 325 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 326 } 327 return retcode; 328 } 329 330 331 /* 332 * Have we finished reading the input file? 333 */ 334 335 GLOBAL boolean 336 jpeg_input_complete( j_decompress_ptr cinfo ) { 337 /* Check for valid jpeg object */ 338 if ( ( cinfo->global_state < DSTATE_START ) || 339 ( cinfo->global_state > DSTATE_STOPPING ) ) { 340 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 341 } 342 return cinfo->inputctl->eoi_reached; 343 } 344 345 346 /* 347 * Is there more than one scan? 348 */ 349 350 GLOBAL boolean 351 jpeg_has_multiple_scans( j_decompress_ptr cinfo ) { 352 /* Only valid after jpeg_read_header completes */ 353 if ( ( cinfo->global_state < DSTATE_READY ) || 354 ( cinfo->global_state > DSTATE_STOPPING ) ) { 355 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 356 } 357 return cinfo->inputctl->has_multiple_scans; 358 } 359 360 361 /* 362 * Finish JPEG decompression. 363 * 364 * This will normally just verify the file trailer and release temp storage. 365 * 366 * Returns FALSE if suspended. The return value need be inspected only if 367 * a suspending data source is used. 368 */ 369 370 GLOBAL boolean 371 jpeg_finish_decompress( j_decompress_ptr cinfo ) { 372 if ( ( ( cinfo->global_state == DSTATE_SCANNING ) || 373 ( cinfo->global_state == DSTATE_RAW_OK ) && !cinfo->buffered_image ) ) { 374 /* Terminate final pass of non-buffered mode */ 375 if ( cinfo->output_scanline < cinfo->output_height ) { 376 ERREXIT( cinfo, JERR_TOO_LITTLE_DATA ); 377 } 378 ( *cinfo->master->finish_output_pass )( cinfo ); 379 cinfo->global_state = DSTATE_STOPPING; 380 } else if ( cinfo->global_state == DSTATE_BUFIMAGE ) { 381 /* Finishing after a buffered-image operation */ 382 cinfo->global_state = DSTATE_STOPPING; 383 } else if ( cinfo->global_state != DSTATE_STOPPING ) { 384 /* STOPPING = repeat call after a suspension, anything else is error */ 385 ERREXIT1( cinfo, JERR_BAD_STATE, cinfo->global_state ); 386 } 387 /* Read until EOI */ 388 while ( !cinfo->inputctl->eoi_reached ) { 389 if ( ( *cinfo->inputctl->consume_input )( cinfo ) == JPEG_SUSPENDED ) { 390 return FALSE; 391 } /* Suspend, come back later */ 392 } 393 /* Do final cleanup */ 394 ( *cinfo->src->term_source )( cinfo ); 395 /* We can use jpeg_abort to release memory and reset global_state */ 396 jpeg_abort( (j_common_ptr) cinfo ); 397 return TRUE; 398 }