media/libjpeg/jdmaster.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/media/libjpeg/jdmaster.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,733 @@
     1.4 +/*
     1.5 + * jdmaster.c
     1.6 + *
     1.7 + * This file was part of the Independent JPEG Group's software:
     1.8 + * Copyright (C) 1991-1997, Thomas G. Lane.
     1.9 + * Modified 2002-2009 by Guido Vollbeding.
    1.10 + * libjpeg-turbo Modifications:
    1.11 + * Copyright (C) 2009-2011, D. R. Commander.
    1.12 + * For conditions of distribution and use, see the accompanying README file.
    1.13 + *
    1.14 + * This file contains master control logic for the JPEG decompressor.
    1.15 + * These routines are concerned with selecting the modules to be executed
    1.16 + * and with determining the number of passes and the work to be done in each
    1.17 + * pass.
    1.18 + */
    1.19 +
    1.20 +#define JPEG_INTERNALS
    1.21 +#include "jinclude.h"
    1.22 +#include "jpeglib.h"
    1.23 +#include "jpegcomp.h"
    1.24 +
    1.25 +
    1.26 +/* Private state */
    1.27 +
    1.28 +typedef struct {
    1.29 +  struct jpeg_decomp_master pub; /* public fields */
    1.30 +
    1.31 +  int pass_number;		/* # of passes completed */
    1.32 +
    1.33 +  boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
    1.34 +
    1.35 +  /* Saved references to initialized quantizer modules,
    1.36 +   * in case we need to switch modes.
    1.37 +   */
    1.38 +  struct jpeg_color_quantizer * quantizer_1pass;
    1.39 +  struct jpeg_color_quantizer * quantizer_2pass;
    1.40 +} my_decomp_master;
    1.41 +
    1.42 +typedef my_decomp_master * my_master_ptr;
    1.43 +
    1.44 +
    1.45 +/*
    1.46 + * Determine whether merged upsample/color conversion should be used.
    1.47 + * CRUCIAL: this must match the actual capabilities of jdmerge.c!
    1.48 + */
    1.49 +
    1.50 +LOCAL(boolean)
    1.51 +use_merged_upsample (j_decompress_ptr cinfo)
    1.52 +{
    1.53 +#ifdef UPSAMPLE_MERGING_SUPPORTED
    1.54 +  /* Merging is the equivalent of plain box-filter upsampling */
    1.55 +  if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
    1.56 +    return FALSE;
    1.57 +  /* jdmerge.c only supports YCC=>RGB color conversion */
    1.58 +  if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
    1.59 +      (cinfo->out_color_space != JCS_RGB &&
    1.60 +      cinfo->out_color_space != JCS_EXT_RGB &&
    1.61 +      cinfo->out_color_space != JCS_EXT_RGBX &&
    1.62 +      cinfo->out_color_space != JCS_EXT_BGR &&
    1.63 +      cinfo->out_color_space != JCS_EXT_BGRX &&
    1.64 +      cinfo->out_color_space != JCS_EXT_XBGR &&
    1.65 +      cinfo->out_color_space != JCS_EXT_XRGB &&
    1.66 +      cinfo->out_color_space != JCS_EXT_RGBA &&
    1.67 +      cinfo->out_color_space != JCS_EXT_BGRA &&
    1.68 +      cinfo->out_color_space != JCS_EXT_ABGR &&
    1.69 +      cinfo->out_color_space != JCS_EXT_ARGB) ||
    1.70 +      cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
    1.71 +    return FALSE;
    1.72 +  /* and it only handles 2h1v or 2h2v sampling ratios */
    1.73 +  if (cinfo->comp_info[0].h_samp_factor != 2 ||
    1.74 +      cinfo->comp_info[1].h_samp_factor != 1 ||
    1.75 +      cinfo->comp_info[2].h_samp_factor != 1 ||
    1.76 +      cinfo->comp_info[0].v_samp_factor >  2 ||
    1.77 +      cinfo->comp_info[1].v_samp_factor != 1 ||
    1.78 +      cinfo->comp_info[2].v_samp_factor != 1)
    1.79 +    return FALSE;
    1.80 +  /* furthermore, it doesn't work if we've scaled the IDCTs differently */
    1.81 +  if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
    1.82 +      cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
    1.83 +      cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
    1.84 +    return FALSE;
    1.85 +  /* ??? also need to test for upsample-time rescaling, when & if supported */
    1.86 +  return TRUE;			/* by golly, it'll work... */
    1.87 +#else
    1.88 +  return FALSE;
    1.89 +#endif
    1.90 +}
    1.91 +
    1.92 +
    1.93 +/*
    1.94 + * Compute output image dimensions and related values.
    1.95 + * NOTE: this is exported for possible use by application.
    1.96 + * Hence it mustn't do anything that can't be done twice.
    1.97 + */
    1.98 +
    1.99 +#if JPEG_LIB_VERSION >= 80
   1.100 +GLOBAL(void)
   1.101 +#else
   1.102 +LOCAL(void)
   1.103 +#endif
   1.104 +jpeg_core_output_dimensions (j_decompress_ptr cinfo)
   1.105 +/* Do computations that are needed before master selection phase.
   1.106 + * This function is used for transcoding and full decompression.
   1.107 + */
   1.108 +{
   1.109 +#ifdef IDCT_SCALING_SUPPORTED
   1.110 +  int ci;
   1.111 +  jpeg_component_info *compptr;
   1.112 +
   1.113 +  /* Compute actual output image dimensions and DCT scaling choices. */
   1.114 +  if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
   1.115 +    /* Provide 1/block_size scaling */
   1.116 +    cinfo->output_width = (JDIMENSION)
   1.117 +      jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
   1.118 +    cinfo->output_height = (JDIMENSION)
   1.119 +      jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
   1.120 +    cinfo->_min_DCT_h_scaled_size = 1;
   1.121 +    cinfo->_min_DCT_v_scaled_size = 1;
   1.122 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
   1.123 +    /* Provide 2/block_size scaling */
   1.124 +    cinfo->output_width = (JDIMENSION)
   1.125 +      jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
   1.126 +    cinfo->output_height = (JDIMENSION)
   1.127 +      jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
   1.128 +    cinfo->_min_DCT_h_scaled_size = 2;
   1.129 +    cinfo->_min_DCT_v_scaled_size = 2;
   1.130 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
   1.131 +    /* Provide 3/block_size scaling */
   1.132 +    cinfo->output_width = (JDIMENSION)
   1.133 +      jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
   1.134 +    cinfo->output_height = (JDIMENSION)
   1.135 +      jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
   1.136 +    cinfo->_min_DCT_h_scaled_size = 3;
   1.137 +    cinfo->_min_DCT_v_scaled_size = 3;
   1.138 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
   1.139 +    /* Provide 4/block_size scaling */
   1.140 +    cinfo->output_width = (JDIMENSION)
   1.141 +      jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
   1.142 +    cinfo->output_height = (JDIMENSION)
   1.143 +      jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
   1.144 +    cinfo->_min_DCT_h_scaled_size = 4;
   1.145 +    cinfo->_min_DCT_v_scaled_size = 4;
   1.146 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
   1.147 +    /* Provide 5/block_size scaling */
   1.148 +    cinfo->output_width = (JDIMENSION)
   1.149 +      jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
   1.150 +    cinfo->output_height = (JDIMENSION)
   1.151 +      jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
   1.152 +    cinfo->_min_DCT_h_scaled_size = 5;
   1.153 +    cinfo->_min_DCT_v_scaled_size = 5;
   1.154 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
   1.155 +    /* Provide 6/block_size scaling */
   1.156 +    cinfo->output_width = (JDIMENSION)
   1.157 +      jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
   1.158 +    cinfo->output_height = (JDIMENSION)
   1.159 +      jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
   1.160 +    cinfo->_min_DCT_h_scaled_size = 6;
   1.161 +    cinfo->_min_DCT_v_scaled_size = 6;
   1.162 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
   1.163 +    /* Provide 7/block_size scaling */
   1.164 +    cinfo->output_width = (JDIMENSION)
   1.165 +      jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
   1.166 +    cinfo->output_height = (JDIMENSION)
   1.167 +      jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
   1.168 +    cinfo->_min_DCT_h_scaled_size = 7;
   1.169 +    cinfo->_min_DCT_v_scaled_size = 7;
   1.170 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
   1.171 +    /* Provide 8/block_size scaling */
   1.172 +    cinfo->output_width = (JDIMENSION)
   1.173 +      jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
   1.174 +    cinfo->output_height = (JDIMENSION)
   1.175 +      jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
   1.176 +    cinfo->_min_DCT_h_scaled_size = 8;
   1.177 +    cinfo->_min_DCT_v_scaled_size = 8;
   1.178 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
   1.179 +    /* Provide 9/block_size scaling */
   1.180 +    cinfo->output_width = (JDIMENSION)
   1.181 +      jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
   1.182 +    cinfo->output_height = (JDIMENSION)
   1.183 +      jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
   1.184 +    cinfo->_min_DCT_h_scaled_size = 9;
   1.185 +    cinfo->_min_DCT_v_scaled_size = 9;
   1.186 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
   1.187 +    /* Provide 10/block_size scaling */
   1.188 +    cinfo->output_width = (JDIMENSION)
   1.189 +      jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
   1.190 +    cinfo->output_height = (JDIMENSION)
   1.191 +      jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
   1.192 +    cinfo->_min_DCT_h_scaled_size = 10;
   1.193 +    cinfo->_min_DCT_v_scaled_size = 10;
   1.194 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
   1.195 +    /* Provide 11/block_size scaling */
   1.196 +    cinfo->output_width = (JDIMENSION)
   1.197 +      jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
   1.198 +    cinfo->output_height = (JDIMENSION)
   1.199 +      jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
   1.200 +    cinfo->_min_DCT_h_scaled_size = 11;
   1.201 +    cinfo->_min_DCT_v_scaled_size = 11;
   1.202 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
   1.203 +    /* Provide 12/block_size scaling */
   1.204 +    cinfo->output_width = (JDIMENSION)
   1.205 +      jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
   1.206 +    cinfo->output_height = (JDIMENSION)
   1.207 +      jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
   1.208 +    cinfo->_min_DCT_h_scaled_size = 12;
   1.209 +    cinfo->_min_DCT_v_scaled_size = 12;
   1.210 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
   1.211 +    /* Provide 13/block_size scaling */
   1.212 +    cinfo->output_width = (JDIMENSION)
   1.213 +      jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
   1.214 +    cinfo->output_height = (JDIMENSION)
   1.215 +      jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
   1.216 +    cinfo->_min_DCT_h_scaled_size = 13;
   1.217 +    cinfo->_min_DCT_v_scaled_size = 13;
   1.218 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
   1.219 +    /* Provide 14/block_size scaling */
   1.220 +    cinfo->output_width = (JDIMENSION)
   1.221 +      jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
   1.222 +    cinfo->output_height = (JDIMENSION)
   1.223 +      jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
   1.224 +    cinfo->_min_DCT_h_scaled_size = 14;
   1.225 +    cinfo->_min_DCT_v_scaled_size = 14;
   1.226 +  } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
   1.227 +    /* Provide 15/block_size scaling */
   1.228 +    cinfo->output_width = (JDIMENSION)
   1.229 +      jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
   1.230 +    cinfo->output_height = (JDIMENSION)
   1.231 +      jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
   1.232 +    cinfo->_min_DCT_h_scaled_size = 15;
   1.233 +    cinfo->_min_DCT_v_scaled_size = 15;
   1.234 +  } else {
   1.235 +    /* Provide 16/block_size scaling */
   1.236 +    cinfo->output_width = (JDIMENSION)
   1.237 +      jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
   1.238 +    cinfo->output_height = (JDIMENSION)
   1.239 +      jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
   1.240 +    cinfo->_min_DCT_h_scaled_size = 16;
   1.241 +    cinfo->_min_DCT_v_scaled_size = 16;
   1.242 +  }
   1.243 +
   1.244 +  /* Recompute dimensions of components */
   1.245 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.246 +       ci++, compptr++) {
   1.247 +    compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
   1.248 +    compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
   1.249 +  }
   1.250 +
   1.251 +#else /* !IDCT_SCALING_SUPPORTED */
   1.252 +
   1.253 +  /* Hardwire it to "no scaling" */
   1.254 +  cinfo->output_width = cinfo->image_width;
   1.255 +  cinfo->output_height = cinfo->image_height;
   1.256 +  /* jdinput.c has already initialized DCT_scaled_size,
   1.257 +   * and has computed unscaled downsampled_width and downsampled_height.
   1.258 +   */
   1.259 +
   1.260 +#endif /* IDCT_SCALING_SUPPORTED */
   1.261 +}
   1.262 +
   1.263 +
   1.264 +/*
   1.265 + * Compute output image dimensions and related values.
   1.266 + * NOTE: this is exported for possible use by application.
   1.267 + * Hence it mustn't do anything that can't be done twice.
   1.268 + * Also note that it may be called before the master module is initialized!
   1.269 + */
   1.270 +
   1.271 +GLOBAL(void)
   1.272 +jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
   1.273 +/* Do computations that are needed before master selection phase */
   1.274 +{
   1.275 +#ifdef IDCT_SCALING_SUPPORTED
   1.276 +  int ci;
   1.277 +  jpeg_component_info *compptr;
   1.278 +#endif
   1.279 +
   1.280 +  /* Prevent application from calling me at wrong times */
   1.281 +  if (cinfo->global_state != DSTATE_READY)
   1.282 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.283 +
   1.284 +  /* Compute core output image dimensions and DCT scaling choices. */
   1.285 +  jpeg_core_output_dimensions(cinfo);
   1.286 +
   1.287 +#ifdef IDCT_SCALING_SUPPORTED
   1.288 +
   1.289 +  /* In selecting the actual DCT scaling for each component, we try to
   1.290 +   * scale up the chroma components via IDCT scaling rather than upsampling.
   1.291 +   * This saves time if the upsampler gets to use 1:1 scaling.
   1.292 +   * Note this code adapts subsampling ratios which are powers of 2.
   1.293 +   */
   1.294 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.295 +       ci++, compptr++) {
   1.296 +    int ssize = cinfo->_min_DCT_scaled_size;
   1.297 +    while (ssize < DCTSIZE &&
   1.298 +	   ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
   1.299 +	    (compptr->h_samp_factor * ssize * 2) == 0) &&
   1.300 +	   ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
   1.301 +	    (compptr->v_samp_factor * ssize * 2) == 0)) {
   1.302 +      ssize = ssize * 2;
   1.303 +    }
   1.304 +#if JPEG_LIB_VERSION >= 70
   1.305 +    compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
   1.306 +#else
   1.307 +    compptr->DCT_scaled_size = ssize;
   1.308 +#endif
   1.309 +  }
   1.310 +
   1.311 +  /* Recompute downsampled dimensions of components;
   1.312 +   * application needs to know these if using raw downsampled data.
   1.313 +   */
   1.314 +  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
   1.315 +       ci++, compptr++) {
   1.316 +    /* Size in samples, after IDCT scaling */
   1.317 +    compptr->downsampled_width = (JDIMENSION)
   1.318 +      jdiv_round_up((long) cinfo->image_width *
   1.319 +		    (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
   1.320 +		    (long) (cinfo->max_h_samp_factor * DCTSIZE));
   1.321 +    compptr->downsampled_height = (JDIMENSION)
   1.322 +      jdiv_round_up((long) cinfo->image_height *
   1.323 +		    (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
   1.324 +		    (long) (cinfo->max_v_samp_factor * DCTSIZE));
   1.325 +  }
   1.326 +
   1.327 +#else /* !IDCT_SCALING_SUPPORTED */
   1.328 +
   1.329 +  /* Hardwire it to "no scaling" */
   1.330 +  cinfo->output_width = cinfo->image_width;
   1.331 +  cinfo->output_height = cinfo->image_height;
   1.332 +  /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
   1.333 +   * and has computed unscaled downsampled_width and downsampled_height.
   1.334 +   */
   1.335 +
   1.336 +#endif /* IDCT_SCALING_SUPPORTED */
   1.337 +
   1.338 +  /* Report number of components in selected colorspace. */
   1.339 +  /* Probably this should be in the color conversion module... */
   1.340 +  switch (cinfo->out_color_space) {
   1.341 +  case JCS_GRAYSCALE:
   1.342 +    cinfo->out_color_components = 1;
   1.343 +    break;
   1.344 +  case JCS_RGB:
   1.345 +  case JCS_EXT_RGB:
   1.346 +  case JCS_EXT_RGBX:
   1.347 +  case JCS_EXT_BGR:
   1.348 +  case JCS_EXT_BGRX:
   1.349 +  case JCS_EXT_XBGR:
   1.350 +  case JCS_EXT_XRGB:
   1.351 +  case JCS_EXT_RGBA:
   1.352 +  case JCS_EXT_BGRA:
   1.353 +  case JCS_EXT_ABGR:
   1.354 +  case JCS_EXT_ARGB:
   1.355 +    cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
   1.356 +    break;
   1.357 +  case JCS_YCbCr:
   1.358 +    cinfo->out_color_components = 3;
   1.359 +    break;
   1.360 +  case JCS_CMYK:
   1.361 +  case JCS_YCCK:
   1.362 +    cinfo->out_color_components = 4;
   1.363 +    break;
   1.364 +  default:			/* else must be same colorspace as in file */
   1.365 +    cinfo->out_color_components = cinfo->num_components;
   1.366 +    break;
   1.367 +  }
   1.368 +  cinfo->output_components = (cinfo->quantize_colors ? 1 :
   1.369 +			      cinfo->out_color_components);
   1.370 +
   1.371 +  /* See if upsampler will want to emit more than one row at a time */
   1.372 +  if (use_merged_upsample(cinfo))
   1.373 +    cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
   1.374 +  else
   1.375 +    cinfo->rec_outbuf_height = 1;
   1.376 +}
   1.377 +
   1.378 +
   1.379 +/*
   1.380 + * Several decompression processes need to range-limit values to the range
   1.381 + * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
   1.382 + * due to noise introduced by quantization, roundoff error, etc.  These
   1.383 + * processes are inner loops and need to be as fast as possible.  On most
   1.384 + * machines, particularly CPUs with pipelines or instruction prefetch,
   1.385 + * a (subscript-check-less) C table lookup
   1.386 + *		x = sample_range_limit[x];
   1.387 + * is faster than explicit tests
   1.388 + *		if (x < 0)  x = 0;
   1.389 + *		else if (x > MAXJSAMPLE)  x = MAXJSAMPLE;
   1.390 + * These processes all use a common table prepared by the routine below.
   1.391 + *
   1.392 + * For most steps we can mathematically guarantee that the initial value
   1.393 + * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
   1.394 + * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
   1.395 + * limiting step (just after the IDCT), a wildly out-of-range value is 
   1.396 + * possible if the input data is corrupt.  To avoid any chance of indexing
   1.397 + * off the end of memory and getting a bad-pointer trap, we perform the
   1.398 + * post-IDCT limiting thus:
   1.399 + *		x = range_limit[x & MASK];
   1.400 + * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
   1.401 + * samples.  Under normal circumstances this is more than enough range and
   1.402 + * a correct output will be generated; with bogus input data the mask will
   1.403 + * cause wraparound, and we will safely generate a bogus-but-in-range output.
   1.404 + * For the post-IDCT step, we want to convert the data from signed to unsigned
   1.405 + * representation by adding CENTERJSAMPLE at the same time that we limit it.
   1.406 + * So the post-IDCT limiting table ends up looking like this:
   1.407 + *   CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
   1.408 + *   MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
   1.409 + *   0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
   1.410 + *   0,1,...,CENTERJSAMPLE-1
   1.411 + * Negative inputs select values from the upper half of the table after
   1.412 + * masking.
   1.413 + *
   1.414 + * We can save some space by overlapping the start of the post-IDCT table
   1.415 + * with the simpler range limiting table.  The post-IDCT table begins at
   1.416 + * sample_range_limit + CENTERJSAMPLE.
   1.417 + *
   1.418 + * Note that the table is allocated in near data space on PCs; it's small
   1.419 + * enough and used often enough to justify this.
   1.420 + */
   1.421 +
   1.422 +LOCAL(void)
   1.423 +prepare_range_limit_table (j_decompress_ptr cinfo)
   1.424 +/* Allocate and fill in the sample_range_limit table */
   1.425 +{
   1.426 +  JSAMPLE * table;
   1.427 +  int i;
   1.428 +
   1.429 +  table = (JSAMPLE *)
   1.430 +    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.431 +		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
   1.432 +  table += (MAXJSAMPLE+1);	/* allow negative subscripts of simple table */
   1.433 +  cinfo->sample_range_limit = table;
   1.434 +  /* First segment of "simple" table: limit[x] = 0 for x < 0 */
   1.435 +  MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
   1.436 +  /* Main part of "simple" table: limit[x] = x */
   1.437 +  for (i = 0; i <= MAXJSAMPLE; i++)
   1.438 +    table[i] = (JSAMPLE) i;
   1.439 +  table += CENTERJSAMPLE;	/* Point to where post-IDCT table starts */
   1.440 +  /* End of simple table, rest of first half of post-IDCT table */
   1.441 +  for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
   1.442 +    table[i] = MAXJSAMPLE;
   1.443 +  /* Second half of post-IDCT table */
   1.444 +  MEMZERO(table + (2 * (MAXJSAMPLE+1)),
   1.445 +	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
   1.446 +  MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
   1.447 +	  cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
   1.448 +}
   1.449 +
   1.450 +
   1.451 +/*
   1.452 + * Master selection of decompression modules.
   1.453 + * This is done once at jpeg_start_decompress time.  We determine
   1.454 + * which modules will be used and give them appropriate initialization calls.
   1.455 + * We also initialize the decompressor input side to begin consuming data.
   1.456 + *
   1.457 + * Since jpeg_read_header has finished, we know what is in the SOF
   1.458 + * and (first) SOS markers.  We also have all the application parameter
   1.459 + * settings.
   1.460 + */
   1.461 +
   1.462 +LOCAL(void)
   1.463 +master_selection (j_decompress_ptr cinfo)
   1.464 +{
   1.465 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.466 +  boolean use_c_buffer;
   1.467 +  long samplesperrow;
   1.468 +  JDIMENSION jd_samplesperrow;
   1.469 +
   1.470 +  /* Initialize dimensions and other stuff */
   1.471 +  jpeg_calc_output_dimensions(cinfo);
   1.472 +  prepare_range_limit_table(cinfo);
   1.473 +
   1.474 +  /* Width of an output scanline must be representable as JDIMENSION. */
   1.475 +  samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
   1.476 +  jd_samplesperrow = (JDIMENSION) samplesperrow;
   1.477 +  if ((long) jd_samplesperrow != samplesperrow)
   1.478 +    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
   1.479 +
   1.480 +  /* Initialize my private state */
   1.481 +  master->pass_number = 0;
   1.482 +  master->using_merged_upsample = use_merged_upsample(cinfo);
   1.483 +
   1.484 +  /* Color quantizer selection */
   1.485 +  master->quantizer_1pass = NULL;
   1.486 +  master->quantizer_2pass = NULL;
   1.487 +  /* No mode changes if not using buffered-image mode. */
   1.488 +  if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
   1.489 +    cinfo->enable_1pass_quant = FALSE;
   1.490 +    cinfo->enable_external_quant = FALSE;
   1.491 +    cinfo->enable_2pass_quant = FALSE;
   1.492 +  }
   1.493 +  if (cinfo->quantize_colors) {
   1.494 +    if (cinfo->raw_data_out)
   1.495 +      ERREXIT(cinfo, JERR_NOTIMPL);
   1.496 +    /* 2-pass quantizer only works in 3-component color space. */
   1.497 +    if (cinfo->out_color_components != 3) {
   1.498 +      cinfo->enable_1pass_quant = TRUE;
   1.499 +      cinfo->enable_external_quant = FALSE;
   1.500 +      cinfo->enable_2pass_quant = FALSE;
   1.501 +      cinfo->colormap = NULL;
   1.502 +    } else if (cinfo->colormap != NULL) {
   1.503 +      cinfo->enable_external_quant = TRUE;
   1.504 +    } else if (cinfo->two_pass_quantize) {
   1.505 +      cinfo->enable_2pass_quant = TRUE;
   1.506 +    } else {
   1.507 +      cinfo->enable_1pass_quant = TRUE;
   1.508 +    }
   1.509 +
   1.510 +    if (cinfo->enable_1pass_quant) {
   1.511 +#ifdef QUANT_1PASS_SUPPORTED
   1.512 +      jinit_1pass_quantizer(cinfo);
   1.513 +      master->quantizer_1pass = cinfo->cquantize;
   1.514 +#else
   1.515 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.516 +#endif
   1.517 +    }
   1.518 +
   1.519 +    /* We use the 2-pass code to map to external colormaps. */
   1.520 +    if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
   1.521 +#ifdef QUANT_2PASS_SUPPORTED
   1.522 +      jinit_2pass_quantizer(cinfo);
   1.523 +      master->quantizer_2pass = cinfo->cquantize;
   1.524 +#else
   1.525 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.526 +#endif
   1.527 +    }
   1.528 +    /* If both quantizers are initialized, the 2-pass one is left active;
   1.529 +     * this is necessary for starting with quantization to an external map.
   1.530 +     */
   1.531 +  }
   1.532 +
   1.533 +  /* Post-processing: in particular, color conversion first */
   1.534 +  if (! cinfo->raw_data_out) {
   1.535 +    if (master->using_merged_upsample) {
   1.536 +#ifdef UPSAMPLE_MERGING_SUPPORTED
   1.537 +      jinit_merged_upsampler(cinfo); /* does color conversion too */
   1.538 +#else
   1.539 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.540 +#endif
   1.541 +    } else {
   1.542 +      jinit_color_deconverter(cinfo);
   1.543 +      jinit_upsampler(cinfo);
   1.544 +    }
   1.545 +    jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
   1.546 +  }
   1.547 +  /* Inverse DCT */
   1.548 +  jinit_inverse_dct(cinfo);
   1.549 +  /* Entropy decoding: either Huffman or arithmetic coding. */
   1.550 +  if (cinfo->arith_code) {
   1.551 +#ifdef D_ARITH_CODING_SUPPORTED
   1.552 +    jinit_arith_decoder(cinfo);
   1.553 +#else
   1.554 +    ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
   1.555 +#endif
   1.556 +  } else {
   1.557 +    if (cinfo->progressive_mode) {
   1.558 +#ifdef D_PROGRESSIVE_SUPPORTED
   1.559 +      jinit_phuff_decoder(cinfo);
   1.560 +#else
   1.561 +      ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.562 +#endif
   1.563 +    } else
   1.564 +      jinit_huff_decoder(cinfo);
   1.565 +  }
   1.566 +
   1.567 +  /* Initialize principal buffer controllers. */
   1.568 +  use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
   1.569 +  jinit_d_coef_controller(cinfo, use_c_buffer);
   1.570 +
   1.571 +  if (! cinfo->raw_data_out)
   1.572 +    jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
   1.573 +
   1.574 +  /* We can now tell the memory manager to allocate virtual arrays. */
   1.575 +  (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
   1.576 +
   1.577 +  /* Initialize input side of decompressor to consume first scan. */
   1.578 +  (*cinfo->inputctl->start_input_pass) (cinfo);
   1.579 +
   1.580 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.581 +  /* If jpeg_start_decompress will read the whole file, initialize
   1.582 +   * progress monitoring appropriately.  The input step is counted
   1.583 +   * as one pass.
   1.584 +   */
   1.585 +  if (cinfo->progress != NULL && ! cinfo->buffered_image &&
   1.586 +      cinfo->inputctl->has_multiple_scans) {
   1.587 +    int nscans;
   1.588 +    /* Estimate number of scans to set pass_limit. */
   1.589 +    if (cinfo->progressive_mode) {
   1.590 +      /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
   1.591 +      nscans = 2 + 3 * cinfo->num_components;
   1.592 +    } else {
   1.593 +      /* For a nonprogressive multiscan file, estimate 1 scan per component. */
   1.594 +      nscans = cinfo->num_components;
   1.595 +    }
   1.596 +    cinfo->progress->pass_counter = 0L;
   1.597 +    cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
   1.598 +    cinfo->progress->completed_passes = 0;
   1.599 +    cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
   1.600 +    /* Count the input pass as done */
   1.601 +    master->pass_number++;
   1.602 +  }
   1.603 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   1.604 +}
   1.605 +
   1.606 +
   1.607 +/*
   1.608 + * Per-pass setup.
   1.609 + * This is called at the beginning of each output pass.  We determine which
   1.610 + * modules will be active during this pass and give them appropriate
   1.611 + * start_pass calls.  We also set is_dummy_pass to indicate whether this
   1.612 + * is a "real" output pass or a dummy pass for color quantization.
   1.613 + * (In the latter case, jdapistd.c will crank the pass to completion.)
   1.614 + */
   1.615 +
   1.616 +METHODDEF(void)
   1.617 +prepare_for_output_pass (j_decompress_ptr cinfo)
   1.618 +{
   1.619 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.620 +
   1.621 +  if (master->pub.is_dummy_pass) {
   1.622 +#ifdef QUANT_2PASS_SUPPORTED
   1.623 +    /* Final pass of 2-pass quantization */
   1.624 +    master->pub.is_dummy_pass = FALSE;
   1.625 +    (*cinfo->cquantize->start_pass) (cinfo, FALSE);
   1.626 +    (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.627 +    (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
   1.628 +#else
   1.629 +    ERREXIT(cinfo, JERR_NOT_COMPILED);
   1.630 +#endif /* QUANT_2PASS_SUPPORTED */
   1.631 +  } else {
   1.632 +    if (cinfo->quantize_colors && cinfo->colormap == NULL) {
   1.633 +      /* Select new quantization method */
   1.634 +      if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
   1.635 +	cinfo->cquantize = master->quantizer_2pass;
   1.636 +	master->pub.is_dummy_pass = TRUE;
   1.637 +      } else if (cinfo->enable_1pass_quant) {
   1.638 +	cinfo->cquantize = master->quantizer_1pass;
   1.639 +      } else {
   1.640 +	ERREXIT(cinfo, JERR_MODE_CHANGE);
   1.641 +      }
   1.642 +    }
   1.643 +    (*cinfo->idct->start_pass) (cinfo);
   1.644 +    (*cinfo->coef->start_output_pass) (cinfo);
   1.645 +    if (! cinfo->raw_data_out) {
   1.646 +      if (! master->using_merged_upsample)
   1.647 +	(*cinfo->cconvert->start_pass) (cinfo);
   1.648 +      (*cinfo->upsample->start_pass) (cinfo);
   1.649 +      if (cinfo->quantize_colors)
   1.650 +	(*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
   1.651 +      (*cinfo->post->start_pass) (cinfo,
   1.652 +	    (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
   1.653 +      (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
   1.654 +    }
   1.655 +  }
   1.656 +
   1.657 +  /* Set up progress monitor's pass info if present */
   1.658 +  if (cinfo->progress != NULL) {
   1.659 +    cinfo->progress->completed_passes = master->pass_number;
   1.660 +    cinfo->progress->total_passes = master->pass_number +
   1.661 +				    (master->pub.is_dummy_pass ? 2 : 1);
   1.662 +    /* In buffered-image mode, we assume one more output pass if EOI not
   1.663 +     * yet reached, but no more passes if EOI has been reached.
   1.664 +     */
   1.665 +    if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
   1.666 +      cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
   1.667 +    }
   1.668 +  }
   1.669 +}
   1.670 +
   1.671 +
   1.672 +/*
   1.673 + * Finish up at end of an output pass.
   1.674 + */
   1.675 +
   1.676 +METHODDEF(void)
   1.677 +finish_output_pass (j_decompress_ptr cinfo)
   1.678 +{
   1.679 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.680 +
   1.681 +  if (cinfo->quantize_colors)
   1.682 +    (*cinfo->cquantize->finish_pass) (cinfo);
   1.683 +  master->pass_number++;
   1.684 +}
   1.685 +
   1.686 +
   1.687 +#ifdef D_MULTISCAN_FILES_SUPPORTED
   1.688 +
   1.689 +/*
   1.690 + * Switch to a new external colormap between output passes.
   1.691 + */
   1.692 +
   1.693 +GLOBAL(void)
   1.694 +jpeg_new_colormap (j_decompress_ptr cinfo)
   1.695 +{
   1.696 +  my_master_ptr master = (my_master_ptr) cinfo->master;
   1.697 +
   1.698 +  /* Prevent application from calling me at wrong times */
   1.699 +  if (cinfo->global_state != DSTATE_BUFIMAGE)
   1.700 +    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
   1.701 +
   1.702 +  if (cinfo->quantize_colors && cinfo->enable_external_quant &&
   1.703 +      cinfo->colormap != NULL) {
   1.704 +    /* Select 2-pass quantizer for external colormap use */
   1.705 +    cinfo->cquantize = master->quantizer_2pass;
   1.706 +    /* Notify quantizer of colormap change */
   1.707 +    (*cinfo->cquantize->new_color_map) (cinfo);
   1.708 +    master->pub.is_dummy_pass = FALSE; /* just in case */
   1.709 +  } else
   1.710 +    ERREXIT(cinfo, JERR_MODE_CHANGE);
   1.711 +}
   1.712 +
   1.713 +#endif /* D_MULTISCAN_FILES_SUPPORTED */
   1.714 +
   1.715 +
   1.716 +/*
   1.717 + * Initialize master decompression control and select active modules.
   1.718 + * This is performed at the start of jpeg_start_decompress.
   1.719 + */
   1.720 +
   1.721 +GLOBAL(void)
   1.722 +jinit_master_decompress (j_decompress_ptr cinfo)
   1.723 +{
   1.724 +  my_master_ptr master;
   1.725 +
   1.726 +  master = (my_master_ptr)
   1.727 +      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
   1.728 +				  SIZEOF(my_decomp_master));
   1.729 +  cinfo->master = (struct jpeg_decomp_master *) master;
   1.730 +  master->pub.prepare_for_output_pass = prepare_for_output_pass;
   1.731 +  master->pub.finish_output_pass = finish_output_pass;
   1.732 +
   1.733 +  master->pub.is_dummy_pass = FALSE;
   1.734 +
   1.735 +  master_selection(cinfo);
   1.736 +}

mercurial