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