src/3rdparty/libjpeg/jdmaster.c
branchGCC_SURGE
changeset 31 5daf16870df6
parent 30 5dc02b23752f
equal deleted inserted replaced
27:93b982ccede2 31:5daf16870df6
     1 /*
     1 /*
     2  * jdmaster.c
     2  * jdmaster.c
     3  *
     3  *
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
     4  * Copyright (C) 1991-1997, Thomas G. Lane.
       
     5  * Modified 2002-2009 by Guido Vollbeding.
     5  * This file is part of the Independent JPEG Group's software.
     6  * This file is part of the Independent JPEG Group's software.
     6  * For conditions of distribution and use, see the accompanying README file.
     7  * For conditions of distribution and use, see the accompanying README file.
     7  *
     8  *
     8  * This file contains master control logic for the JPEG decompressor.
     9  * This file contains master control logic for the JPEG decompressor.
     9  * These routines are concerned with selecting the modules to be executed
    10  * These routines are concerned with selecting the modules to be executed
    59       cinfo->comp_info[0].v_samp_factor >  2 ||
    60       cinfo->comp_info[0].v_samp_factor >  2 ||
    60       cinfo->comp_info[1].v_samp_factor != 1 ||
    61       cinfo->comp_info[1].v_samp_factor != 1 ||
    61       cinfo->comp_info[2].v_samp_factor != 1)
    62       cinfo->comp_info[2].v_samp_factor != 1)
    62     return FALSE;
    63     return FALSE;
    63   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
    64   /* furthermore, it doesn't work if we've scaled the IDCTs differently */
    64   if (cinfo->comp_info[0].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
    65   if (cinfo->comp_info[0].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
    65       cinfo->comp_info[1].DCT_scaled_size != cinfo->min_DCT_scaled_size ||
    66       cinfo->comp_info[1].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
    66       cinfo->comp_info[2].DCT_scaled_size != cinfo->min_DCT_scaled_size)
    67       cinfo->comp_info[2].DCT_h_scaled_size != cinfo->min_DCT_h_scaled_size ||
       
    68       cinfo->comp_info[0].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
       
    69       cinfo->comp_info[1].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size ||
       
    70       cinfo->comp_info[2].DCT_v_scaled_size != cinfo->min_DCT_v_scaled_size)
    67     return FALSE;
    71     return FALSE;
    68   /* ??? also need to test for upsample-time rescaling, when & if supported */
    72   /* ??? also need to test for upsample-time rescaling, when & if supported */
    69   return TRUE;			/* by golly, it'll work... */
    73   return TRUE;			/* by golly, it'll work... */
    70 #else
    74 #else
    71   return FALSE;
    75   return FALSE;
    80  * Also note that it may be called before the master module is initialized!
    84  * Also note that it may be called before the master module is initialized!
    81  */
    85  */
    82 
    86 
    83 GLOBAL(void)
    87 GLOBAL(void)
    84 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
    88 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
    85 /* Do computations that are needed before master selection phase */
    89 /* Do computations that are needed before master selection phase.
       
    90  * This function is used for full decompression.
       
    91  */
    86 {
    92 {
    87 #ifdef IDCT_SCALING_SUPPORTED
    93 #ifdef IDCT_SCALING_SUPPORTED
    88   int ci;
    94   int ci;
    89   jpeg_component_info *compptr;
    95   jpeg_component_info *compptr;
    90 #endif
    96 #endif
    91 
    97 
    92   /* Prevent application from calling me at wrong times */
    98   /* Prevent application from calling me at wrong times */
    93   if (cinfo->global_state != DSTATE_READY)
    99   if (cinfo->global_state != DSTATE_READY)
    94     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   100     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
    95 
   101 
       
   102   /* Compute core output image dimensions and DCT scaling choices. */
       
   103   jpeg_core_output_dimensions(cinfo);
       
   104 
    96 #ifdef IDCT_SCALING_SUPPORTED
   105 #ifdef IDCT_SCALING_SUPPORTED
    97 
   106 
    98   /* Compute actual output image dimensions and DCT scaling choices. */
       
    99   if (cinfo->scale_num * 8 <= cinfo->scale_denom) {
       
   100     /* Provide 1/8 scaling */
       
   101     cinfo->output_width = (JDIMENSION)
       
   102       jdiv_round_up((long) cinfo->image_width, 8L);
       
   103     cinfo->output_height = (JDIMENSION)
       
   104       jdiv_round_up((long) cinfo->image_height, 8L);
       
   105     cinfo->min_DCT_scaled_size = 1;
       
   106   } else if (cinfo->scale_num * 4 <= cinfo->scale_denom) {
       
   107     /* Provide 1/4 scaling */
       
   108     cinfo->output_width = (JDIMENSION)
       
   109       jdiv_round_up((long) cinfo->image_width, 4L);
       
   110     cinfo->output_height = (JDIMENSION)
       
   111       jdiv_round_up((long) cinfo->image_height, 4L);
       
   112     cinfo->min_DCT_scaled_size = 2;
       
   113   } else if (cinfo->scale_num * 2 <= cinfo->scale_denom) {
       
   114     /* Provide 1/2 scaling */
       
   115     cinfo->output_width = (JDIMENSION)
       
   116       jdiv_round_up((long) cinfo->image_width, 2L);
       
   117     cinfo->output_height = (JDIMENSION)
       
   118       jdiv_round_up((long) cinfo->image_height, 2L);
       
   119     cinfo->min_DCT_scaled_size = 4;
       
   120   } else {
       
   121     /* Provide 1/1 scaling */
       
   122     cinfo->output_width = cinfo->image_width;
       
   123     cinfo->output_height = cinfo->image_height;
       
   124     cinfo->min_DCT_scaled_size = DCTSIZE;
       
   125   }
       
   126   /* In selecting the actual DCT scaling for each component, we try to
   107   /* In selecting the actual DCT scaling for each component, we try to
   127    * scale up the chroma components via IDCT scaling rather than upsampling.
   108    * scale up the chroma components via IDCT scaling rather than upsampling.
   128    * This saves time if the upsampler gets to use 1:1 scaling.
   109    * This saves time if the upsampler gets to use 1:1 scaling.
   129    * Note this code assumes that the supported DCT scalings are powers of 2.
   110    * Note this code adapts subsampling ratios which are powers of 2.
   130    */
   111    */
   131   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   112   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   132        ci++, compptr++) {
   113        ci++, compptr++) {
   133     int ssize = cinfo->min_DCT_scaled_size;
   114     int ssize = 1;
   134     while (ssize < DCTSIZE &&
   115     while (cinfo->min_DCT_h_scaled_size * ssize <=
   135 	   (compptr->h_samp_factor * ssize * 2 <=
   116 	   (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
   136 	    cinfo->max_h_samp_factor * cinfo->min_DCT_scaled_size) &&
   117 	   (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) {
   137 	   (compptr->v_samp_factor * ssize * 2 <=
       
   138 	    cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size)) {
       
   139       ssize = ssize * 2;
   118       ssize = ssize * 2;
   140     }
   119     }
   141     compptr->DCT_scaled_size = ssize;
   120     compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize;
       
   121     ssize = 1;
       
   122     while (cinfo->min_DCT_v_scaled_size * ssize <=
       
   123 	   (cinfo->do_fancy_upsampling ? DCTSIZE : DCTSIZE / 2) &&
       
   124 	   (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) {
       
   125       ssize = ssize * 2;
       
   126     }
       
   127     compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize;
       
   128 
       
   129     /* We don't support IDCT ratios larger than 2. */
       
   130     if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2)
       
   131 	compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2;
       
   132     else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2)
       
   133 	compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2;
   142   }
   134   }
   143 
   135 
   144   /* Recompute downsampled dimensions of components;
   136   /* Recompute downsampled dimensions of components;
   145    * application needs to know these if using raw downsampled data.
   137    * application needs to know these if using raw downsampled data.
   146    */
   138    */
   147   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   139   for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   148        ci++, compptr++) {
   140        ci++, compptr++) {
   149     /* Size in samples, after IDCT scaling */
   141     /* Size in samples, after IDCT scaling */
   150     compptr->downsampled_width = (JDIMENSION)
   142     compptr->downsampled_width = (JDIMENSION)
   151       jdiv_round_up((long) cinfo->image_width *
   143       jdiv_round_up((long) cinfo->image_width *
   152 		    (long) (compptr->h_samp_factor * compptr->DCT_scaled_size),
   144 		    (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size),
   153 		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   145 		    (long) (cinfo->max_h_samp_factor * cinfo->block_size));
   154     compptr->downsampled_height = (JDIMENSION)
   146     compptr->downsampled_height = (JDIMENSION)
   155       jdiv_round_up((long) cinfo->image_height *
   147       jdiv_round_up((long) cinfo->image_height *
   156 		    (long) (compptr->v_samp_factor * compptr->DCT_scaled_size),
   148 		    (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size),
   157 		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   149 		    (long) (cinfo->max_v_samp_factor * cinfo->block_size));
   158   }
   150   }
   159 
       
   160 #else /* !IDCT_SCALING_SUPPORTED */
       
   161 
       
   162   /* Hardwire it to "no scaling" */
       
   163   cinfo->output_width = cinfo->image_width;
       
   164   cinfo->output_height = cinfo->image_height;
       
   165   /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
       
   166    * and has computed unscaled downsampled_width and downsampled_height.
       
   167    */
       
   168 
   151 
   169 #endif /* IDCT_SCALING_SUPPORTED */
   152 #endif /* IDCT_SCALING_SUPPORTED */
   170 
   153 
   171   /* Report number of components in selected colorspace. */
   154   /* Report number of components in selected colorspace. */
   172   /* Probably this should be in the color conversion module... */
   155   /* Probably this should be in the color conversion module... */
   370     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
   353     jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
   371   }
   354   }
   372   /* Inverse DCT */
   355   /* Inverse DCT */
   373   jinit_inverse_dct(cinfo);
   356   jinit_inverse_dct(cinfo);
   374   /* Entropy decoding: either Huffman or arithmetic coding. */
   357   /* Entropy decoding: either Huffman or arithmetic coding. */
   375   if (cinfo->arith_code) {
   358   if (cinfo->arith_code)
   376     ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   359     jinit_arith_decoder(cinfo);
   377   } else {
   360   else {
   378     if (cinfo->progressive_mode) {
   361     jinit_huff_decoder(cinfo);
   379 #ifdef D_PROGRESSIVE_SUPPORTED
       
   380       jinit_phuff_decoder(cinfo);
       
   381 #else
       
   382       ERREXIT(cinfo, JERR_NOT_COMPILED);
       
   383 #endif
       
   384     } else
       
   385       jinit_huff_decoder(cinfo);
       
   386   }
   362   }
   387 
   363 
   388   /* Initialize principal buffer controllers. */
   364   /* Initialize principal buffer controllers. */
   389   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
   365   use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
   390   jinit_d_coef_controller(cinfo, use_c_buffer);
   366   jinit_d_coef_controller(cinfo, use_c_buffer);