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