media/libjpeg/jdmaster.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /*
michael@0 2 * jdmaster.c
michael@0 3 *
michael@0 4 * This file was part of the Independent JPEG Group's software:
michael@0 5 * Copyright (C) 1991-1997, Thomas G. Lane.
michael@0 6 * Modified 2002-2009 by Guido Vollbeding.
michael@0 7 * libjpeg-turbo Modifications:
michael@0 8 * Copyright (C) 2009-2011, D. R. Commander.
michael@0 9 * For conditions of distribution and use, see the accompanying README file.
michael@0 10 *
michael@0 11 * This file contains master control logic for the JPEG decompressor.
michael@0 12 * These routines are concerned with selecting the modules to be executed
michael@0 13 * and with determining the number of passes and the work to be done in each
michael@0 14 * pass.
michael@0 15 */
michael@0 16
michael@0 17 #define JPEG_INTERNALS
michael@0 18 #include "jinclude.h"
michael@0 19 #include "jpeglib.h"
michael@0 20 #include "jpegcomp.h"
michael@0 21
michael@0 22
michael@0 23 /* Private state */
michael@0 24
michael@0 25 typedef struct {
michael@0 26 struct jpeg_decomp_master pub; /* public fields */
michael@0 27
michael@0 28 int pass_number; /* # of passes completed */
michael@0 29
michael@0 30 boolean using_merged_upsample; /* TRUE if using merged upsample/cconvert */
michael@0 31
michael@0 32 /* Saved references to initialized quantizer modules,
michael@0 33 * in case we need to switch modes.
michael@0 34 */
michael@0 35 struct jpeg_color_quantizer * quantizer_1pass;
michael@0 36 struct jpeg_color_quantizer * quantizer_2pass;
michael@0 37 } my_decomp_master;
michael@0 38
michael@0 39 typedef my_decomp_master * my_master_ptr;
michael@0 40
michael@0 41
michael@0 42 /*
michael@0 43 * Determine whether merged upsample/color conversion should be used.
michael@0 44 * CRUCIAL: this must match the actual capabilities of jdmerge.c!
michael@0 45 */
michael@0 46
michael@0 47 LOCAL(boolean)
michael@0 48 use_merged_upsample (j_decompress_ptr cinfo)
michael@0 49 {
michael@0 50 #ifdef UPSAMPLE_MERGING_SUPPORTED
michael@0 51 /* Merging is the equivalent of plain box-filter upsampling */
michael@0 52 if (cinfo->do_fancy_upsampling || cinfo->CCIR601_sampling)
michael@0 53 return FALSE;
michael@0 54 /* jdmerge.c only supports YCC=>RGB color conversion */
michael@0 55 if (cinfo->jpeg_color_space != JCS_YCbCr || cinfo->num_components != 3 ||
michael@0 56 (cinfo->out_color_space != JCS_RGB &&
michael@0 57 cinfo->out_color_space != JCS_EXT_RGB &&
michael@0 58 cinfo->out_color_space != JCS_EXT_RGBX &&
michael@0 59 cinfo->out_color_space != JCS_EXT_BGR &&
michael@0 60 cinfo->out_color_space != JCS_EXT_BGRX &&
michael@0 61 cinfo->out_color_space != JCS_EXT_XBGR &&
michael@0 62 cinfo->out_color_space != JCS_EXT_XRGB &&
michael@0 63 cinfo->out_color_space != JCS_EXT_RGBA &&
michael@0 64 cinfo->out_color_space != JCS_EXT_BGRA &&
michael@0 65 cinfo->out_color_space != JCS_EXT_ABGR &&
michael@0 66 cinfo->out_color_space != JCS_EXT_ARGB) ||
michael@0 67 cinfo->out_color_components != rgb_pixelsize[cinfo->out_color_space])
michael@0 68 return FALSE;
michael@0 69 /* and it only handles 2h1v or 2h2v sampling ratios */
michael@0 70 if (cinfo->comp_info[0].h_samp_factor != 2 ||
michael@0 71 cinfo->comp_info[1].h_samp_factor != 1 ||
michael@0 72 cinfo->comp_info[2].h_samp_factor != 1 ||
michael@0 73 cinfo->comp_info[0].v_samp_factor > 2 ||
michael@0 74 cinfo->comp_info[1].v_samp_factor != 1 ||
michael@0 75 cinfo->comp_info[2].v_samp_factor != 1)
michael@0 76 return FALSE;
michael@0 77 /* furthermore, it doesn't work if we've scaled the IDCTs differently */
michael@0 78 if (cinfo->comp_info[0]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
michael@0 79 cinfo->comp_info[1]._DCT_scaled_size != cinfo->_min_DCT_scaled_size ||
michael@0 80 cinfo->comp_info[2]._DCT_scaled_size != cinfo->_min_DCT_scaled_size)
michael@0 81 return FALSE;
michael@0 82 /* ??? also need to test for upsample-time rescaling, when & if supported */
michael@0 83 return TRUE; /* by golly, it'll work... */
michael@0 84 #else
michael@0 85 return FALSE;
michael@0 86 #endif
michael@0 87 }
michael@0 88
michael@0 89
michael@0 90 /*
michael@0 91 * Compute output image dimensions and related values.
michael@0 92 * NOTE: this is exported for possible use by application.
michael@0 93 * Hence it mustn't do anything that can't be done twice.
michael@0 94 */
michael@0 95
michael@0 96 #if JPEG_LIB_VERSION >= 80
michael@0 97 GLOBAL(void)
michael@0 98 #else
michael@0 99 LOCAL(void)
michael@0 100 #endif
michael@0 101 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
michael@0 102 /* Do computations that are needed before master selection phase.
michael@0 103 * This function is used for transcoding and full decompression.
michael@0 104 */
michael@0 105 {
michael@0 106 #ifdef IDCT_SCALING_SUPPORTED
michael@0 107 int ci;
michael@0 108 jpeg_component_info *compptr;
michael@0 109
michael@0 110 /* Compute actual output image dimensions and DCT scaling choices. */
michael@0 111 if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom) {
michael@0 112 /* Provide 1/block_size scaling */
michael@0 113 cinfo->output_width = (JDIMENSION)
michael@0 114 jdiv_round_up((long) cinfo->image_width, (long) DCTSIZE);
michael@0 115 cinfo->output_height = (JDIMENSION)
michael@0 116 jdiv_round_up((long) cinfo->image_height, (long) DCTSIZE);
michael@0 117 cinfo->_min_DCT_h_scaled_size = 1;
michael@0 118 cinfo->_min_DCT_v_scaled_size = 1;
michael@0 119 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 2) {
michael@0 120 /* Provide 2/block_size scaling */
michael@0 121 cinfo->output_width = (JDIMENSION)
michael@0 122 jdiv_round_up((long) cinfo->image_width * 2L, (long) DCTSIZE);
michael@0 123 cinfo->output_height = (JDIMENSION)
michael@0 124 jdiv_round_up((long) cinfo->image_height * 2L, (long) DCTSIZE);
michael@0 125 cinfo->_min_DCT_h_scaled_size = 2;
michael@0 126 cinfo->_min_DCT_v_scaled_size = 2;
michael@0 127 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 3) {
michael@0 128 /* Provide 3/block_size scaling */
michael@0 129 cinfo->output_width = (JDIMENSION)
michael@0 130 jdiv_round_up((long) cinfo->image_width * 3L, (long) DCTSIZE);
michael@0 131 cinfo->output_height = (JDIMENSION)
michael@0 132 jdiv_round_up((long) cinfo->image_height * 3L, (long) DCTSIZE);
michael@0 133 cinfo->_min_DCT_h_scaled_size = 3;
michael@0 134 cinfo->_min_DCT_v_scaled_size = 3;
michael@0 135 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 4) {
michael@0 136 /* Provide 4/block_size scaling */
michael@0 137 cinfo->output_width = (JDIMENSION)
michael@0 138 jdiv_round_up((long) cinfo->image_width * 4L, (long) DCTSIZE);
michael@0 139 cinfo->output_height = (JDIMENSION)
michael@0 140 jdiv_round_up((long) cinfo->image_height * 4L, (long) DCTSIZE);
michael@0 141 cinfo->_min_DCT_h_scaled_size = 4;
michael@0 142 cinfo->_min_DCT_v_scaled_size = 4;
michael@0 143 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 5) {
michael@0 144 /* Provide 5/block_size scaling */
michael@0 145 cinfo->output_width = (JDIMENSION)
michael@0 146 jdiv_round_up((long) cinfo->image_width * 5L, (long) DCTSIZE);
michael@0 147 cinfo->output_height = (JDIMENSION)
michael@0 148 jdiv_round_up((long) cinfo->image_height * 5L, (long) DCTSIZE);
michael@0 149 cinfo->_min_DCT_h_scaled_size = 5;
michael@0 150 cinfo->_min_DCT_v_scaled_size = 5;
michael@0 151 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 6) {
michael@0 152 /* Provide 6/block_size scaling */
michael@0 153 cinfo->output_width = (JDIMENSION)
michael@0 154 jdiv_round_up((long) cinfo->image_width * 6L, (long) DCTSIZE);
michael@0 155 cinfo->output_height = (JDIMENSION)
michael@0 156 jdiv_round_up((long) cinfo->image_height * 6L, (long) DCTSIZE);
michael@0 157 cinfo->_min_DCT_h_scaled_size = 6;
michael@0 158 cinfo->_min_DCT_v_scaled_size = 6;
michael@0 159 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 7) {
michael@0 160 /* Provide 7/block_size scaling */
michael@0 161 cinfo->output_width = (JDIMENSION)
michael@0 162 jdiv_round_up((long) cinfo->image_width * 7L, (long) DCTSIZE);
michael@0 163 cinfo->output_height = (JDIMENSION)
michael@0 164 jdiv_round_up((long) cinfo->image_height * 7L, (long) DCTSIZE);
michael@0 165 cinfo->_min_DCT_h_scaled_size = 7;
michael@0 166 cinfo->_min_DCT_v_scaled_size = 7;
michael@0 167 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 8) {
michael@0 168 /* Provide 8/block_size scaling */
michael@0 169 cinfo->output_width = (JDIMENSION)
michael@0 170 jdiv_round_up((long) cinfo->image_width * 8L, (long) DCTSIZE);
michael@0 171 cinfo->output_height = (JDIMENSION)
michael@0 172 jdiv_round_up((long) cinfo->image_height * 8L, (long) DCTSIZE);
michael@0 173 cinfo->_min_DCT_h_scaled_size = 8;
michael@0 174 cinfo->_min_DCT_v_scaled_size = 8;
michael@0 175 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 9) {
michael@0 176 /* Provide 9/block_size scaling */
michael@0 177 cinfo->output_width = (JDIMENSION)
michael@0 178 jdiv_round_up((long) cinfo->image_width * 9L, (long) DCTSIZE);
michael@0 179 cinfo->output_height = (JDIMENSION)
michael@0 180 jdiv_round_up((long) cinfo->image_height * 9L, (long) DCTSIZE);
michael@0 181 cinfo->_min_DCT_h_scaled_size = 9;
michael@0 182 cinfo->_min_DCT_v_scaled_size = 9;
michael@0 183 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 10) {
michael@0 184 /* Provide 10/block_size scaling */
michael@0 185 cinfo->output_width = (JDIMENSION)
michael@0 186 jdiv_round_up((long) cinfo->image_width * 10L, (long) DCTSIZE);
michael@0 187 cinfo->output_height = (JDIMENSION)
michael@0 188 jdiv_round_up((long) cinfo->image_height * 10L, (long) DCTSIZE);
michael@0 189 cinfo->_min_DCT_h_scaled_size = 10;
michael@0 190 cinfo->_min_DCT_v_scaled_size = 10;
michael@0 191 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 11) {
michael@0 192 /* Provide 11/block_size scaling */
michael@0 193 cinfo->output_width = (JDIMENSION)
michael@0 194 jdiv_round_up((long) cinfo->image_width * 11L, (long) DCTSIZE);
michael@0 195 cinfo->output_height = (JDIMENSION)
michael@0 196 jdiv_round_up((long) cinfo->image_height * 11L, (long) DCTSIZE);
michael@0 197 cinfo->_min_DCT_h_scaled_size = 11;
michael@0 198 cinfo->_min_DCT_v_scaled_size = 11;
michael@0 199 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 12) {
michael@0 200 /* Provide 12/block_size scaling */
michael@0 201 cinfo->output_width = (JDIMENSION)
michael@0 202 jdiv_round_up((long) cinfo->image_width * 12L, (long) DCTSIZE);
michael@0 203 cinfo->output_height = (JDIMENSION)
michael@0 204 jdiv_round_up((long) cinfo->image_height * 12L, (long) DCTSIZE);
michael@0 205 cinfo->_min_DCT_h_scaled_size = 12;
michael@0 206 cinfo->_min_DCT_v_scaled_size = 12;
michael@0 207 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 13) {
michael@0 208 /* Provide 13/block_size scaling */
michael@0 209 cinfo->output_width = (JDIMENSION)
michael@0 210 jdiv_round_up((long) cinfo->image_width * 13L, (long) DCTSIZE);
michael@0 211 cinfo->output_height = (JDIMENSION)
michael@0 212 jdiv_round_up((long) cinfo->image_height * 13L, (long) DCTSIZE);
michael@0 213 cinfo->_min_DCT_h_scaled_size = 13;
michael@0 214 cinfo->_min_DCT_v_scaled_size = 13;
michael@0 215 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 14) {
michael@0 216 /* Provide 14/block_size scaling */
michael@0 217 cinfo->output_width = (JDIMENSION)
michael@0 218 jdiv_round_up((long) cinfo->image_width * 14L, (long) DCTSIZE);
michael@0 219 cinfo->output_height = (JDIMENSION)
michael@0 220 jdiv_round_up((long) cinfo->image_height * 14L, (long) DCTSIZE);
michael@0 221 cinfo->_min_DCT_h_scaled_size = 14;
michael@0 222 cinfo->_min_DCT_v_scaled_size = 14;
michael@0 223 } else if (cinfo->scale_num * DCTSIZE <= cinfo->scale_denom * 15) {
michael@0 224 /* Provide 15/block_size scaling */
michael@0 225 cinfo->output_width = (JDIMENSION)
michael@0 226 jdiv_round_up((long) cinfo->image_width * 15L, (long) DCTSIZE);
michael@0 227 cinfo->output_height = (JDIMENSION)
michael@0 228 jdiv_round_up((long) cinfo->image_height * 15L, (long) DCTSIZE);
michael@0 229 cinfo->_min_DCT_h_scaled_size = 15;
michael@0 230 cinfo->_min_DCT_v_scaled_size = 15;
michael@0 231 } else {
michael@0 232 /* Provide 16/block_size scaling */
michael@0 233 cinfo->output_width = (JDIMENSION)
michael@0 234 jdiv_round_up((long) cinfo->image_width * 16L, (long) DCTSIZE);
michael@0 235 cinfo->output_height = (JDIMENSION)
michael@0 236 jdiv_round_up((long) cinfo->image_height * 16L, (long) DCTSIZE);
michael@0 237 cinfo->_min_DCT_h_scaled_size = 16;
michael@0 238 cinfo->_min_DCT_v_scaled_size = 16;
michael@0 239 }
michael@0 240
michael@0 241 /* Recompute dimensions of components */
michael@0 242 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 243 ci++, compptr++) {
michael@0 244 compptr->_DCT_h_scaled_size = cinfo->_min_DCT_h_scaled_size;
michael@0 245 compptr->_DCT_v_scaled_size = cinfo->_min_DCT_v_scaled_size;
michael@0 246 }
michael@0 247
michael@0 248 #else /* !IDCT_SCALING_SUPPORTED */
michael@0 249
michael@0 250 /* Hardwire it to "no scaling" */
michael@0 251 cinfo->output_width = cinfo->image_width;
michael@0 252 cinfo->output_height = cinfo->image_height;
michael@0 253 /* jdinput.c has already initialized DCT_scaled_size,
michael@0 254 * and has computed unscaled downsampled_width and downsampled_height.
michael@0 255 */
michael@0 256
michael@0 257 #endif /* IDCT_SCALING_SUPPORTED */
michael@0 258 }
michael@0 259
michael@0 260
michael@0 261 /*
michael@0 262 * Compute output image dimensions and related values.
michael@0 263 * NOTE: this is exported for possible use by application.
michael@0 264 * Hence it mustn't do anything that can't be done twice.
michael@0 265 * Also note that it may be called before the master module is initialized!
michael@0 266 */
michael@0 267
michael@0 268 GLOBAL(void)
michael@0 269 jpeg_calc_output_dimensions (j_decompress_ptr cinfo)
michael@0 270 /* Do computations that are needed before master selection phase */
michael@0 271 {
michael@0 272 #ifdef IDCT_SCALING_SUPPORTED
michael@0 273 int ci;
michael@0 274 jpeg_component_info *compptr;
michael@0 275 #endif
michael@0 276
michael@0 277 /* Prevent application from calling me at wrong times */
michael@0 278 if (cinfo->global_state != DSTATE_READY)
michael@0 279 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 280
michael@0 281 /* Compute core output image dimensions and DCT scaling choices. */
michael@0 282 jpeg_core_output_dimensions(cinfo);
michael@0 283
michael@0 284 #ifdef IDCT_SCALING_SUPPORTED
michael@0 285
michael@0 286 /* In selecting the actual DCT scaling for each component, we try to
michael@0 287 * scale up the chroma components via IDCT scaling rather than upsampling.
michael@0 288 * This saves time if the upsampler gets to use 1:1 scaling.
michael@0 289 * Note this code adapts subsampling ratios which are powers of 2.
michael@0 290 */
michael@0 291 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 292 ci++, compptr++) {
michael@0 293 int ssize = cinfo->_min_DCT_scaled_size;
michael@0 294 while (ssize < DCTSIZE &&
michael@0 295 ((cinfo->max_h_samp_factor * cinfo->_min_DCT_scaled_size) %
michael@0 296 (compptr->h_samp_factor * ssize * 2) == 0) &&
michael@0 297 ((cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size) %
michael@0 298 (compptr->v_samp_factor * ssize * 2) == 0)) {
michael@0 299 ssize = ssize * 2;
michael@0 300 }
michael@0 301 #if JPEG_LIB_VERSION >= 70
michael@0 302 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = ssize;
michael@0 303 #else
michael@0 304 compptr->DCT_scaled_size = ssize;
michael@0 305 #endif
michael@0 306 }
michael@0 307
michael@0 308 /* Recompute downsampled dimensions of components;
michael@0 309 * application needs to know these if using raw downsampled data.
michael@0 310 */
michael@0 311 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
michael@0 312 ci++, compptr++) {
michael@0 313 /* Size in samples, after IDCT scaling */
michael@0 314 compptr->downsampled_width = (JDIMENSION)
michael@0 315 jdiv_round_up((long) cinfo->image_width *
michael@0 316 (long) (compptr->h_samp_factor * compptr->_DCT_scaled_size),
michael@0 317 (long) (cinfo->max_h_samp_factor * DCTSIZE));
michael@0 318 compptr->downsampled_height = (JDIMENSION)
michael@0 319 jdiv_round_up((long) cinfo->image_height *
michael@0 320 (long) (compptr->v_samp_factor * compptr->_DCT_scaled_size),
michael@0 321 (long) (cinfo->max_v_samp_factor * DCTSIZE));
michael@0 322 }
michael@0 323
michael@0 324 #else /* !IDCT_SCALING_SUPPORTED */
michael@0 325
michael@0 326 /* Hardwire it to "no scaling" */
michael@0 327 cinfo->output_width = cinfo->image_width;
michael@0 328 cinfo->output_height = cinfo->image_height;
michael@0 329 /* jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
michael@0 330 * and has computed unscaled downsampled_width and downsampled_height.
michael@0 331 */
michael@0 332
michael@0 333 #endif /* IDCT_SCALING_SUPPORTED */
michael@0 334
michael@0 335 /* Report number of components in selected colorspace. */
michael@0 336 /* Probably this should be in the color conversion module... */
michael@0 337 switch (cinfo->out_color_space) {
michael@0 338 case JCS_GRAYSCALE:
michael@0 339 cinfo->out_color_components = 1;
michael@0 340 break;
michael@0 341 case JCS_RGB:
michael@0 342 case JCS_EXT_RGB:
michael@0 343 case JCS_EXT_RGBX:
michael@0 344 case JCS_EXT_BGR:
michael@0 345 case JCS_EXT_BGRX:
michael@0 346 case JCS_EXT_XBGR:
michael@0 347 case JCS_EXT_XRGB:
michael@0 348 case JCS_EXT_RGBA:
michael@0 349 case JCS_EXT_BGRA:
michael@0 350 case JCS_EXT_ABGR:
michael@0 351 case JCS_EXT_ARGB:
michael@0 352 cinfo->out_color_components = rgb_pixelsize[cinfo->out_color_space];
michael@0 353 break;
michael@0 354 case JCS_YCbCr:
michael@0 355 cinfo->out_color_components = 3;
michael@0 356 break;
michael@0 357 case JCS_CMYK:
michael@0 358 case JCS_YCCK:
michael@0 359 cinfo->out_color_components = 4;
michael@0 360 break;
michael@0 361 default: /* else must be same colorspace as in file */
michael@0 362 cinfo->out_color_components = cinfo->num_components;
michael@0 363 break;
michael@0 364 }
michael@0 365 cinfo->output_components = (cinfo->quantize_colors ? 1 :
michael@0 366 cinfo->out_color_components);
michael@0 367
michael@0 368 /* See if upsampler will want to emit more than one row at a time */
michael@0 369 if (use_merged_upsample(cinfo))
michael@0 370 cinfo->rec_outbuf_height = cinfo->max_v_samp_factor;
michael@0 371 else
michael@0 372 cinfo->rec_outbuf_height = 1;
michael@0 373 }
michael@0 374
michael@0 375
michael@0 376 /*
michael@0 377 * Several decompression processes need to range-limit values to the range
michael@0 378 * 0..MAXJSAMPLE; the input value may fall somewhat outside this range
michael@0 379 * due to noise introduced by quantization, roundoff error, etc. These
michael@0 380 * processes are inner loops and need to be as fast as possible. On most
michael@0 381 * machines, particularly CPUs with pipelines or instruction prefetch,
michael@0 382 * a (subscript-check-less) C table lookup
michael@0 383 * x = sample_range_limit[x];
michael@0 384 * is faster than explicit tests
michael@0 385 * if (x < 0) x = 0;
michael@0 386 * else if (x > MAXJSAMPLE) x = MAXJSAMPLE;
michael@0 387 * These processes all use a common table prepared by the routine below.
michael@0 388 *
michael@0 389 * For most steps we can mathematically guarantee that the initial value
michael@0 390 * of x is within MAXJSAMPLE+1 of the legal range, so a table running from
michael@0 391 * -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient. But for the initial
michael@0 392 * limiting step (just after the IDCT), a wildly out-of-range value is
michael@0 393 * possible if the input data is corrupt. To avoid any chance of indexing
michael@0 394 * off the end of memory and getting a bad-pointer trap, we perform the
michael@0 395 * post-IDCT limiting thus:
michael@0 396 * x = range_limit[x & MASK];
michael@0 397 * where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
michael@0 398 * samples. Under normal circumstances this is more than enough range and
michael@0 399 * a correct output will be generated; with bogus input data the mask will
michael@0 400 * cause wraparound, and we will safely generate a bogus-but-in-range output.
michael@0 401 * For the post-IDCT step, we want to convert the data from signed to unsigned
michael@0 402 * representation by adding CENTERJSAMPLE at the same time that we limit it.
michael@0 403 * So the post-IDCT limiting table ends up looking like this:
michael@0 404 * CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
michael@0 405 * MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
michael@0 406 * 0 (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
michael@0 407 * 0,1,...,CENTERJSAMPLE-1
michael@0 408 * Negative inputs select values from the upper half of the table after
michael@0 409 * masking.
michael@0 410 *
michael@0 411 * We can save some space by overlapping the start of the post-IDCT table
michael@0 412 * with the simpler range limiting table. The post-IDCT table begins at
michael@0 413 * sample_range_limit + CENTERJSAMPLE.
michael@0 414 *
michael@0 415 * Note that the table is allocated in near data space on PCs; it's small
michael@0 416 * enough and used often enough to justify this.
michael@0 417 */
michael@0 418
michael@0 419 LOCAL(void)
michael@0 420 prepare_range_limit_table (j_decompress_ptr cinfo)
michael@0 421 /* Allocate and fill in the sample_range_limit table */
michael@0 422 {
michael@0 423 JSAMPLE * table;
michael@0 424 int i;
michael@0 425
michael@0 426 table = (JSAMPLE *)
michael@0 427 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 428 (5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE));
michael@0 429 table += (MAXJSAMPLE+1); /* allow negative subscripts of simple table */
michael@0 430 cinfo->sample_range_limit = table;
michael@0 431 /* First segment of "simple" table: limit[x] = 0 for x < 0 */
michael@0 432 MEMZERO(table - (MAXJSAMPLE+1), (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));
michael@0 433 /* Main part of "simple" table: limit[x] = x */
michael@0 434 for (i = 0; i <= MAXJSAMPLE; i++)
michael@0 435 table[i] = (JSAMPLE) i;
michael@0 436 table += CENTERJSAMPLE; /* Point to where post-IDCT table starts */
michael@0 437 /* End of simple table, rest of first half of post-IDCT table */
michael@0 438 for (i = CENTERJSAMPLE; i < 2*(MAXJSAMPLE+1); i++)
michael@0 439 table[i] = MAXJSAMPLE;
michael@0 440 /* Second half of post-IDCT table */
michael@0 441 MEMZERO(table + (2 * (MAXJSAMPLE+1)),
michael@0 442 (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
michael@0 443 MEMCOPY(table + (4 * (MAXJSAMPLE+1) - CENTERJSAMPLE),
michael@0 444 cinfo->sample_range_limit, CENTERJSAMPLE * SIZEOF(JSAMPLE));
michael@0 445 }
michael@0 446
michael@0 447
michael@0 448 /*
michael@0 449 * Master selection of decompression modules.
michael@0 450 * This is done once at jpeg_start_decompress time. We determine
michael@0 451 * which modules will be used and give them appropriate initialization calls.
michael@0 452 * We also initialize the decompressor input side to begin consuming data.
michael@0 453 *
michael@0 454 * Since jpeg_read_header has finished, we know what is in the SOF
michael@0 455 * and (first) SOS markers. We also have all the application parameter
michael@0 456 * settings.
michael@0 457 */
michael@0 458
michael@0 459 LOCAL(void)
michael@0 460 master_selection (j_decompress_ptr cinfo)
michael@0 461 {
michael@0 462 my_master_ptr master = (my_master_ptr) cinfo->master;
michael@0 463 boolean use_c_buffer;
michael@0 464 long samplesperrow;
michael@0 465 JDIMENSION jd_samplesperrow;
michael@0 466
michael@0 467 /* Initialize dimensions and other stuff */
michael@0 468 jpeg_calc_output_dimensions(cinfo);
michael@0 469 prepare_range_limit_table(cinfo);
michael@0 470
michael@0 471 /* Width of an output scanline must be representable as JDIMENSION. */
michael@0 472 samplesperrow = (long) cinfo->output_width * (long) cinfo->out_color_components;
michael@0 473 jd_samplesperrow = (JDIMENSION) samplesperrow;
michael@0 474 if ((long) jd_samplesperrow != samplesperrow)
michael@0 475 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
michael@0 476
michael@0 477 /* Initialize my private state */
michael@0 478 master->pass_number = 0;
michael@0 479 master->using_merged_upsample = use_merged_upsample(cinfo);
michael@0 480
michael@0 481 /* Color quantizer selection */
michael@0 482 master->quantizer_1pass = NULL;
michael@0 483 master->quantizer_2pass = NULL;
michael@0 484 /* No mode changes if not using buffered-image mode. */
michael@0 485 if (! cinfo->quantize_colors || ! cinfo->buffered_image) {
michael@0 486 cinfo->enable_1pass_quant = FALSE;
michael@0 487 cinfo->enable_external_quant = FALSE;
michael@0 488 cinfo->enable_2pass_quant = FALSE;
michael@0 489 }
michael@0 490 if (cinfo->quantize_colors) {
michael@0 491 if (cinfo->raw_data_out)
michael@0 492 ERREXIT(cinfo, JERR_NOTIMPL);
michael@0 493 /* 2-pass quantizer only works in 3-component color space. */
michael@0 494 if (cinfo->out_color_components != 3) {
michael@0 495 cinfo->enable_1pass_quant = TRUE;
michael@0 496 cinfo->enable_external_quant = FALSE;
michael@0 497 cinfo->enable_2pass_quant = FALSE;
michael@0 498 cinfo->colormap = NULL;
michael@0 499 } else if (cinfo->colormap != NULL) {
michael@0 500 cinfo->enable_external_quant = TRUE;
michael@0 501 } else if (cinfo->two_pass_quantize) {
michael@0 502 cinfo->enable_2pass_quant = TRUE;
michael@0 503 } else {
michael@0 504 cinfo->enable_1pass_quant = TRUE;
michael@0 505 }
michael@0 506
michael@0 507 if (cinfo->enable_1pass_quant) {
michael@0 508 #ifdef QUANT_1PASS_SUPPORTED
michael@0 509 jinit_1pass_quantizer(cinfo);
michael@0 510 master->quantizer_1pass = cinfo->cquantize;
michael@0 511 #else
michael@0 512 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 513 #endif
michael@0 514 }
michael@0 515
michael@0 516 /* We use the 2-pass code to map to external colormaps. */
michael@0 517 if (cinfo->enable_2pass_quant || cinfo->enable_external_quant) {
michael@0 518 #ifdef QUANT_2PASS_SUPPORTED
michael@0 519 jinit_2pass_quantizer(cinfo);
michael@0 520 master->quantizer_2pass = cinfo->cquantize;
michael@0 521 #else
michael@0 522 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 523 #endif
michael@0 524 }
michael@0 525 /* If both quantizers are initialized, the 2-pass one is left active;
michael@0 526 * this is necessary for starting with quantization to an external map.
michael@0 527 */
michael@0 528 }
michael@0 529
michael@0 530 /* Post-processing: in particular, color conversion first */
michael@0 531 if (! cinfo->raw_data_out) {
michael@0 532 if (master->using_merged_upsample) {
michael@0 533 #ifdef UPSAMPLE_MERGING_SUPPORTED
michael@0 534 jinit_merged_upsampler(cinfo); /* does color conversion too */
michael@0 535 #else
michael@0 536 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 537 #endif
michael@0 538 } else {
michael@0 539 jinit_color_deconverter(cinfo);
michael@0 540 jinit_upsampler(cinfo);
michael@0 541 }
michael@0 542 jinit_d_post_controller(cinfo, cinfo->enable_2pass_quant);
michael@0 543 }
michael@0 544 /* Inverse DCT */
michael@0 545 jinit_inverse_dct(cinfo);
michael@0 546 /* Entropy decoding: either Huffman or arithmetic coding. */
michael@0 547 if (cinfo->arith_code) {
michael@0 548 #ifdef D_ARITH_CODING_SUPPORTED
michael@0 549 jinit_arith_decoder(cinfo);
michael@0 550 #else
michael@0 551 ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
michael@0 552 #endif
michael@0 553 } else {
michael@0 554 if (cinfo->progressive_mode) {
michael@0 555 #ifdef D_PROGRESSIVE_SUPPORTED
michael@0 556 jinit_phuff_decoder(cinfo);
michael@0 557 #else
michael@0 558 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 559 #endif
michael@0 560 } else
michael@0 561 jinit_huff_decoder(cinfo);
michael@0 562 }
michael@0 563
michael@0 564 /* Initialize principal buffer controllers. */
michael@0 565 use_c_buffer = cinfo->inputctl->has_multiple_scans || cinfo->buffered_image;
michael@0 566 jinit_d_coef_controller(cinfo, use_c_buffer);
michael@0 567
michael@0 568 if (! cinfo->raw_data_out)
michael@0 569 jinit_d_main_controller(cinfo, FALSE /* never need full buffer here */);
michael@0 570
michael@0 571 /* We can now tell the memory manager to allocate virtual arrays. */
michael@0 572 (*cinfo->mem->realize_virt_arrays) ((j_common_ptr) cinfo);
michael@0 573
michael@0 574 /* Initialize input side of decompressor to consume first scan. */
michael@0 575 (*cinfo->inputctl->start_input_pass) (cinfo);
michael@0 576
michael@0 577 #ifdef D_MULTISCAN_FILES_SUPPORTED
michael@0 578 /* If jpeg_start_decompress will read the whole file, initialize
michael@0 579 * progress monitoring appropriately. The input step is counted
michael@0 580 * as one pass.
michael@0 581 */
michael@0 582 if (cinfo->progress != NULL && ! cinfo->buffered_image &&
michael@0 583 cinfo->inputctl->has_multiple_scans) {
michael@0 584 int nscans;
michael@0 585 /* Estimate number of scans to set pass_limit. */
michael@0 586 if (cinfo->progressive_mode) {
michael@0 587 /* Arbitrarily estimate 2 interleaved DC scans + 3 AC scans/component. */
michael@0 588 nscans = 2 + 3 * cinfo->num_components;
michael@0 589 } else {
michael@0 590 /* For a nonprogressive multiscan file, estimate 1 scan per component. */
michael@0 591 nscans = cinfo->num_components;
michael@0 592 }
michael@0 593 cinfo->progress->pass_counter = 0L;
michael@0 594 cinfo->progress->pass_limit = (long) cinfo->total_iMCU_rows * nscans;
michael@0 595 cinfo->progress->completed_passes = 0;
michael@0 596 cinfo->progress->total_passes = (cinfo->enable_2pass_quant ? 3 : 2);
michael@0 597 /* Count the input pass as done */
michael@0 598 master->pass_number++;
michael@0 599 }
michael@0 600 #endif /* D_MULTISCAN_FILES_SUPPORTED */
michael@0 601 }
michael@0 602
michael@0 603
michael@0 604 /*
michael@0 605 * Per-pass setup.
michael@0 606 * This is called at the beginning of each output pass. We determine which
michael@0 607 * modules will be active during this pass and give them appropriate
michael@0 608 * start_pass calls. We also set is_dummy_pass to indicate whether this
michael@0 609 * is a "real" output pass or a dummy pass for color quantization.
michael@0 610 * (In the latter case, jdapistd.c will crank the pass to completion.)
michael@0 611 */
michael@0 612
michael@0 613 METHODDEF(void)
michael@0 614 prepare_for_output_pass (j_decompress_ptr cinfo)
michael@0 615 {
michael@0 616 my_master_ptr master = (my_master_ptr) cinfo->master;
michael@0 617
michael@0 618 if (master->pub.is_dummy_pass) {
michael@0 619 #ifdef QUANT_2PASS_SUPPORTED
michael@0 620 /* Final pass of 2-pass quantization */
michael@0 621 master->pub.is_dummy_pass = FALSE;
michael@0 622 (*cinfo->cquantize->start_pass) (cinfo, FALSE);
michael@0 623 (*cinfo->post->start_pass) (cinfo, JBUF_CRANK_DEST);
michael@0 624 (*cinfo->main->start_pass) (cinfo, JBUF_CRANK_DEST);
michael@0 625 #else
michael@0 626 ERREXIT(cinfo, JERR_NOT_COMPILED);
michael@0 627 #endif /* QUANT_2PASS_SUPPORTED */
michael@0 628 } else {
michael@0 629 if (cinfo->quantize_colors && cinfo->colormap == NULL) {
michael@0 630 /* Select new quantization method */
michael@0 631 if (cinfo->two_pass_quantize && cinfo->enable_2pass_quant) {
michael@0 632 cinfo->cquantize = master->quantizer_2pass;
michael@0 633 master->pub.is_dummy_pass = TRUE;
michael@0 634 } else if (cinfo->enable_1pass_quant) {
michael@0 635 cinfo->cquantize = master->quantizer_1pass;
michael@0 636 } else {
michael@0 637 ERREXIT(cinfo, JERR_MODE_CHANGE);
michael@0 638 }
michael@0 639 }
michael@0 640 (*cinfo->idct->start_pass) (cinfo);
michael@0 641 (*cinfo->coef->start_output_pass) (cinfo);
michael@0 642 if (! cinfo->raw_data_out) {
michael@0 643 if (! master->using_merged_upsample)
michael@0 644 (*cinfo->cconvert->start_pass) (cinfo);
michael@0 645 (*cinfo->upsample->start_pass) (cinfo);
michael@0 646 if (cinfo->quantize_colors)
michael@0 647 (*cinfo->cquantize->start_pass) (cinfo, master->pub.is_dummy_pass);
michael@0 648 (*cinfo->post->start_pass) (cinfo,
michael@0 649 (master->pub.is_dummy_pass ? JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
michael@0 650 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
michael@0 651 }
michael@0 652 }
michael@0 653
michael@0 654 /* Set up progress monitor's pass info if present */
michael@0 655 if (cinfo->progress != NULL) {
michael@0 656 cinfo->progress->completed_passes = master->pass_number;
michael@0 657 cinfo->progress->total_passes = master->pass_number +
michael@0 658 (master->pub.is_dummy_pass ? 2 : 1);
michael@0 659 /* In buffered-image mode, we assume one more output pass if EOI not
michael@0 660 * yet reached, but no more passes if EOI has been reached.
michael@0 661 */
michael@0 662 if (cinfo->buffered_image && ! cinfo->inputctl->eoi_reached) {
michael@0 663 cinfo->progress->total_passes += (cinfo->enable_2pass_quant ? 2 : 1);
michael@0 664 }
michael@0 665 }
michael@0 666 }
michael@0 667
michael@0 668
michael@0 669 /*
michael@0 670 * Finish up at end of an output pass.
michael@0 671 */
michael@0 672
michael@0 673 METHODDEF(void)
michael@0 674 finish_output_pass (j_decompress_ptr cinfo)
michael@0 675 {
michael@0 676 my_master_ptr master = (my_master_ptr) cinfo->master;
michael@0 677
michael@0 678 if (cinfo->quantize_colors)
michael@0 679 (*cinfo->cquantize->finish_pass) (cinfo);
michael@0 680 master->pass_number++;
michael@0 681 }
michael@0 682
michael@0 683
michael@0 684 #ifdef D_MULTISCAN_FILES_SUPPORTED
michael@0 685
michael@0 686 /*
michael@0 687 * Switch to a new external colormap between output passes.
michael@0 688 */
michael@0 689
michael@0 690 GLOBAL(void)
michael@0 691 jpeg_new_colormap (j_decompress_ptr cinfo)
michael@0 692 {
michael@0 693 my_master_ptr master = (my_master_ptr) cinfo->master;
michael@0 694
michael@0 695 /* Prevent application from calling me at wrong times */
michael@0 696 if (cinfo->global_state != DSTATE_BUFIMAGE)
michael@0 697 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
michael@0 698
michael@0 699 if (cinfo->quantize_colors && cinfo->enable_external_quant &&
michael@0 700 cinfo->colormap != NULL) {
michael@0 701 /* Select 2-pass quantizer for external colormap use */
michael@0 702 cinfo->cquantize = master->quantizer_2pass;
michael@0 703 /* Notify quantizer of colormap change */
michael@0 704 (*cinfo->cquantize->new_color_map) (cinfo);
michael@0 705 master->pub.is_dummy_pass = FALSE; /* just in case */
michael@0 706 } else
michael@0 707 ERREXIT(cinfo, JERR_MODE_CHANGE);
michael@0 708 }
michael@0 709
michael@0 710 #endif /* D_MULTISCAN_FILES_SUPPORTED */
michael@0 711
michael@0 712
michael@0 713 /*
michael@0 714 * Initialize master decompression control and select active modules.
michael@0 715 * This is performed at the start of jpeg_start_decompress.
michael@0 716 */
michael@0 717
michael@0 718 GLOBAL(void)
michael@0 719 jinit_master_decompress (j_decompress_ptr cinfo)
michael@0 720 {
michael@0 721 my_master_ptr master;
michael@0 722
michael@0 723 master = (my_master_ptr)
michael@0 724 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
michael@0 725 SIZEOF(my_decomp_master));
michael@0 726 cinfo->master = (struct jpeg_decomp_master *) master;
michael@0 727 master->pub.prepare_for_output_pass = prepare_for_output_pass;
michael@0 728 master->pub.finish_output_pass = finish_output_pass;
michael@0 729
michael@0 730 master->pub.is_dummy_pass = FALSE;
michael@0 731
michael@0 732 master_selection(cinfo);
michael@0 733 }

mercurial