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