00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015 #define JPEG_INTERNALS
00016 #include "jinclude.h"
00017 #include "jpeglib.h"
00018
00019
00020
00021
00022 typedef struct {
00023 struct jpeg_decomp_master pub;
00024
00025 int pass_number;
00026
00027 boolean using_merged_upsample;
00028
00029
00030
00031
00032 struct jpeg_color_quantizer * quantizer_1pass;
00033 struct jpeg_color_quantizer * quantizer_2pass;
00034 } my_decomp_master;
00035
00036 typedef my_decomp_master * my_master_ptr;
00037
00038
00039
00040
00041
00042
00043
00044 LOCAL(boolean)
00045 use_merged_upsample (j_decompress_ptr cinfo)
00046 {
00047 #ifdef UPSAMPLE_MERGING_SUPPORTED
00048
00049 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
00050 return FALSE;
00051
00052 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
00053 cinfo->out_color_space != JCS_RGB ||
00054 cinfo->out_color_components != RGB_PIXELSIZE)
00055 return FALSE;
00056
00057 if (cinfo->comp_info[0].h_samp_factor != 2 ||
00058 cinfo->comp_info[1].h_samp_factor != 1 ||
00059 cinfo->comp_info[2].h_samp_factor != 1 ||
00060 cinfo->comp_info[0].v_samp_factor > 2 ||
00061 cinfo->comp_info[1].v_samp_factor != 1 ||
00062 cinfo->comp_info[2].v_samp_factor != 1)
00063 return FALSE;
00064
00065 if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
00066 cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
00067 cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
00068 cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
00069 cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
00070 cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
00071 return FALSE;
00072
00073 return TRUE;
00074 #else
00075 return FALSE;
00076 #endif
00077 }
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087 GLOBAL(void)
00088 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
00089
00090
00091
00092 {
00093 #ifdef IDCT_SCALING_SUPPORTED
00094 int ci;
00095 jpeg_component_info *compptr;
00096 #endif
00097
00098
00099 if (cinfo->global_state != DSTATE_READY)
00100 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00101
00102
00103 jpeg_core_output_dimensions(cinfo);
00104
00105 #ifdef IDCT_SCALING_SUPPORTED
00106
00107
00108
00109
00110
00111
00112 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00113 ci++, compptr++) {
00114 int ssize = 1;
00115 while (cinfo->min_DCT_h_scaled_size * ssize <=
00116 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
00117 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
00118 ssize = ssize * 2;
00119 }
00120 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
00121 ssize = 1;
00122 while (cinfo->min_DCT_v_scaled_size * ssize <=
00123 (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
00124 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
00125 ssize = ssize * 2;
00126 }
00127 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
00128
00129
00130 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
00131 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
00132 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
00133 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
00134 }
00135
00136
00137
00138
00139 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
00140 ci++, compptr++) {
00141
00142 compptr->downsampled_width = (JDIMENSION)
00143 jdiv_round_up((long) cinfo->image_width *
00144 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
00145 (long) (cinfo->max_h_samp_factor * cinfo->block_size));
00146 compptr->downsampled_height = (JDIMENSION)
00147 jdiv_round_up((long) cinfo->image_height *
00148 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
00149 (long) (cinfo->max_v_samp_factor * cinfo->block_size));
00150 }
00151
00152 #endif
00153
00154
00155
00156 switch (cinfo->out_color_space) {
00157 case JCS_GRAYSCALE:
00158 cinfo->out_color_components = 1;
00159 break;
00160 case JCS_RGB:
00161 #if RGB_PIXELSIZE != 3
00162 cinfo->out_color_components = RGB_PIXELSIZE;
00163 break;
00164 #endif
00165 case JCS_YCbCr:
00166 cinfo->out_color_components = 3;
00167 break;
00168 case JCS_CMYK:
00169 case JCS_YCCK:
00170 cinfo->out_color_components = 4;
00171 break;
00172 default:
00173 cinfo->out_color_components = cinfo->num_components;
00174 break;
00175 }
00176 cinfo->output_components = (cinfo->quantize_colors ? 1 :
00177 cinfo->out_color_components);
00178
00179
00180 if (use_merged_upsample(cinfo))
00181 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
00182 else
00183 cinfo->rec_outbuf_height = 1;
00184 }
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230 LOCAL(void)
00231 prepare_range_limit_table (j_decompress_ptr cinfo)
00232
00233 {
00234 JSAMPLE * table;
00235 int i;
00236
00237 table = (JSAMPLE *)
00238 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00239 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
00240 table += (MAXJSAMPLE+1);
00241 cinfo->sample_range_limit = table;
00242
00243 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
00244
00245 for (i = 0; i <= MAXJSAMPLE; i++)
00246 table[i] = (JSAMPLE) i;
00247 table += CENTERJSAMPLE;
00248
00249 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
00250 table[i] = MAXJSAMPLE;
00251
00252 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
00253 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
00254 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
00255 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270 LOCAL(void)
00271 master_selection (j_decompress_ptr cinfo)
00272 {
00273 my_master_ptr master = (my_master_ptr) cinfo->master;
00274 boolean use_c_buffer;
00275 long samplesperrow;
00276 JDIMENSION jd_samplesperrow;
00277
00278
00279 jpeg_calc_output_dimensions(cinfo);
00280 prepare_range_limit_table(cinfo);
00281
00282
00283 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
00284 jd_samplesperrow = (JDIMENSION) samplesperrow;
00285 if ((long) jd_samplesperrow != samplesperrow)
00286 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00287
00288
00289 master->pass_number = 0;
00290 master->using_merged_upsample = use_merged_upsample(cinfo);
00291
00292
00293 master->quantizer_1pass = NULL;
00294 master->quantizer_2pass = NULL;
00295
00296 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
00297 cinfo->enable_1pass_quant = FALSE;
00298 cinfo->enable_external_quant = FALSE;
00299 cinfo->enable_2pass_quant = FALSE;
00300 }
00301 if (cinfo->quantize_colors) {
00302 if (cinfo->raw_data_out)
00303 ERREXIT(cinfo, JERR_NOTIMPL);
00304
00305 if (cinfo->out_color_components != 3) {
00306 cinfo->enable_1pass_quant = TRUE;
00307 cinfo->enable_external_quant = FALSE;
00308 cinfo->enable_2pass_quant = FALSE;
00309 cinfo->colormap = NULL;
00310 } else if (cinfo->colormap != NULL) {
00311 cinfo->enable_external_quant = TRUE;
00312 } else if (cinfo->two_pass_quantize) {
00313 cinfo->enable_2pass_quant = TRUE;
00314 } else {
00315 cinfo->enable_1pass_quant = TRUE;
00316 }
00317
00318 if (cinfo->enable_1pass_quant) {
00319 #ifdef QUANT_1PASS_SUPPORTED
00320 jinit_1pass_quantizer(cinfo);
00321 master->quantizer_1pass = cinfo->cquantize;
00322 #else
00323 ERREXIT(cinfo, JERR_NOT_COMPILED);
00324 #endif
00325 }
00326
00327
00328 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
00329 #ifdef QUANT_2PASS_SUPPORTED
00330 jinit_2pass_quantizer(cinfo);
00331 master->quantizer_2pass = cinfo->cquantize;
00332 #else
00333 ERREXIT(cinfo, JERR_NOT_COMPILED);
00334 #endif
00335 }
00336
00337
00338
00339 }
00340
00341
00342 if (! cinfo->raw_data_out) {
00343 if (master->using_merged_upsample) {
00344 #ifdef UPSAMPLE_MERGING_SUPPORTED
00345 jinit_merged_upsampler(cinfo);
00346 #else
00347 ERREXIT(cinfo, JERR_NOT_COMPILED);
00348 #endif
00349 } else {
00350 jinit_color_deconverter(cinfo);
00351 jinit_upsampler(cinfo);
00352 }
00353 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
00354 }
00355
00356 jinit_inverse_dct(cinfo);
00357
00358 if (cinfo->arith_code)
00359 jinit_arith_decoder(cinfo);
00360 else {
00361 jinit_huff_decoder(cinfo);
00362 }
00363
00364
00365 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
00366 jinit_d_coef_controller(cinfo, use_c_buffer);
00367
00368 if (! cinfo->raw_data_out)
00369 jinit_d_main_controller(cinfo, FALSE );
00370
00371
00372 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
00373
00374
00375 (*cinfo->inputctl->start_input_pass) (cinfo);
00376
00377 #ifdef D_MULTISCAN_FILES_SUPPORTED
00378
00379
00380
00381
00382 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
00383 cinfo->inputctl->has_multiple_scans) {
00384 int nscans;
00385
00386 if (cinfo->progressive_mode) {
00387
00388 nscans = 2 + 3 * cinfo->num_components;
00389 } else {
00390
00391 nscans = cinfo->num_components;
00392 }
00393 cinfo->progress->pass_counter = 0L;
00394 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
00395 cinfo->progress->completed_passes = 0;
00396 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
00397
00398 master->pass_number++;
00399 }
00400 #endif
00401 }
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413 METHODDEF(void)
00414 prepare_for_output_pass (j_decompress_ptr cinfo)
00415 {
00416 my_master_ptr master = (my_master_ptr) cinfo->master;
00417
00418 if (master->pub.is_dummy_pass) {
00419 #ifdef QUANT_2PASS_SUPPORTED
00420
00421 master->pub.is_dummy_pass = FALSE;
00422 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
00423 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
00424 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
00425 #else
00426 ERREXIT(cinfo, JERR_NOT_COMPILED);
00427 #endif
00428 } else {
00429 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
00430
00431 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
00432 cinfo->cquantize = master->quantizer_2pass;
00433 master->pub.is_dummy_pass = TRUE;
00434 } else if (cinfo->enable_1pass_quant) {
00435 cinfo->cquantize = master->quantizer_1pass;
00436 } else {
00437 ERREXIT(cinfo, JERR_MODE_CHANGE);
00438 }
00439 }
00440 (*cinfo->idct->start_pass) (cinfo);
00441 (*cinfo->coef->start_output_pass) (cinfo);
00442 if (! cinfo->raw_data_out) {
00443 if (! master->using_merged_upsample)
00444 (*cinfo->cconvert->start_pass) (cinfo);
00445 (*cinfo->upsample->start_pass) (cinfo);
00446 if (cinfo->quantize_colors)
00447 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
00448 (*cinfo->post->start_pass) (cinfo,
00449 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
00450 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
00451 }
00452 }
00453
00454
00455 if (cinfo->progress != NULL) {
00456 cinfo->progress->completed_passes = master->pass_number;
00457 cinfo->progress->total_passes = master->pass_number +
00458 (master->pub.is_dummy_pass ? 2 : 1);
00459
00460
00461
00462 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
00463 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
00464 }
00465 }
00466 }
00467
00468
00469
00470
00471
00472
00473 METHODDEF(void)
00474 finish_output_pass (j_decompress_ptr cinfo)
00475 {
00476 my_master_ptr master = (my_master_ptr) cinfo->master;
00477
00478 if (cinfo->quantize_colors)
00479 (*cinfo->cquantize->finish_pass) (cinfo);
00480 master->pass_number++;
00481 }
00482
00483
00484 #ifdef D_MULTISCAN_FILES_SUPPORTED
00485
00486
00487
00488
00489
00490 GLOBAL(void)
00491 jpeg_new_colormap (j_decompress_ptr cinfo)
00492 {
00493 my_master_ptr master = (my_master_ptr) cinfo->master;
00494
00495
00496 if (cinfo->global_state != DSTATE_BUFIMAGE)
00497 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
00498
00499 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
00500 cinfo->colormap != NULL) {
00501
00502 cinfo->cquantize = master->quantizer_2pass;
00503
00504 (*cinfo->cquantize->new_color_map) (cinfo);
00505 master->pub.is_dummy_pass = FALSE;
00506 } else
00507 ERREXIT(cinfo, JERR_MODE_CHANGE);
00508 }
00509
00510 #endif
00511
00512
00513
00514
00515
00516
00517
00518 GLOBAL(void)
00519 jinit_master_decompress (j_decompress_ptr cinfo)
00520 {
00521 my_master_ptr master;
00522
00523 master = (my_master_ptr)
00524 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
00525 SIZEOF(my_decomp_master));
00526 cinfo->master = (struct jpeg_decomp_master *) master;
00527 master->pub.prepare_for_output_pass = prepare_for_output_pass;
00528 master->pub.finish_output_pass = finish_output_pass;
00529
00530 master->pub.is_dummy_pass = FALSE;
00531
00532 master_selection(cinfo);
00533 }