| 1 | /* | 
|---|
| 2 | * jcmaster.c | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright (C) 1991-1997, Thomas G. Lane. | 
|---|
| 5 | * Modified 2003-2010 by Guido Vollbeding. | 
|---|
| 6 | * This file is part of the Independent JPEG Group's software. | 
|---|
| 7 | * For conditions of distribution and use, see the accompanying README file. | 
|---|
| 8 | * | 
|---|
| 9 | * This file contains master control logic for the JPEG compressor. | 
|---|
| 10 | * These routines are concerned with parameter validation, initial setup, | 
|---|
| 11 | * and inter-pass control (determining the number of passes and the work | 
|---|
| 12 | * to be done in each pass). | 
|---|
| 13 | */ | 
|---|
| 14 |  | 
|---|
| 15 | #define JPEG_INTERNALS | 
|---|
| 16 | #include "jinclude.h" | 
|---|
| 17 | #include "jpeglib.h" | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | /* Private state */ | 
|---|
| 21 |  | 
|---|
| 22 | typedef enum { | 
|---|
| 23 | main_pass,              /* input data, also do first output step */ | 
|---|
| 24 | huff_opt_pass,          /* Huffman code optimization pass */ | 
|---|
| 25 | output_pass             /* data output pass */ | 
|---|
| 26 | } c_pass_type; | 
|---|
| 27 |  | 
|---|
| 28 | typedef struct { | 
|---|
| 29 | struct jpeg_comp_master pub;  /* public fields */ | 
|---|
| 30 |  | 
|---|
| 31 | c_pass_type pass_type;        /* the type of the current pass */ | 
|---|
| 32 |  | 
|---|
| 33 | int pass_number;              /* # of passes completed */ | 
|---|
| 34 | int total_passes;             /* total # of passes needed */ | 
|---|
| 35 |  | 
|---|
| 36 | int scan_number;              /* current index in scan_info[] */ | 
|---|
| 37 | } my_comp_master; | 
|---|
| 38 |  | 
|---|
| 39 | typedef my_comp_master * my_master_ptr; | 
|---|
| 40 |  | 
|---|
| 41 |  | 
|---|
| 42 | /* | 
|---|
| 43 | * Support routines that do various essential calculations. | 
|---|
| 44 | */ | 
|---|
| 45 |  | 
|---|
| 46 | /* | 
|---|
| 47 | * Compute JPEG image dimensions and related values. | 
|---|
| 48 | * NOTE: this is exported for possible use by application. | 
|---|
| 49 | * Hence it mustn't do anything that can't be done twice. | 
|---|
| 50 | */ | 
|---|
| 51 |  | 
|---|
| 52 | GLOBAL(void) | 
|---|
| 53 | jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) | 
|---|
| 54 | /* Do computations that are needed before master selection phase */ | 
|---|
| 55 | { | 
|---|
| 56 | #ifdef DCT_SCALING_SUPPORTED | 
|---|
| 57 |  | 
|---|
| 58 | /* Compute actual JPEG image dimensions and DCT scaling choices. */ | 
|---|
| 59 | if (cinfo->scale_num >= cinfo->scale_denom * 8) { | 
|---|
| 60 | /* Provide 8/1 scaling */ | 
|---|
| 61 | cinfo->jpeg_width = cinfo->image_width << 3; | 
|---|
| 62 | cinfo->jpeg_height = cinfo->image_height << 3; | 
|---|
| 63 | cinfo->min_DCT_h_scaled_size = 1; | 
|---|
| 64 | cinfo->min_DCT_v_scaled_size = 1; | 
|---|
| 65 | } else if (cinfo->scale_num >= cinfo->scale_denom * 4) { | 
|---|
| 66 | /* Provide 4/1 scaling */ | 
|---|
| 67 | cinfo->jpeg_width = cinfo->image_width << 2; | 
|---|
| 68 | cinfo->jpeg_height = cinfo->image_height << 2; | 
|---|
| 69 | cinfo->min_DCT_h_scaled_size = 2; | 
|---|
| 70 | cinfo->min_DCT_v_scaled_size = 2; | 
|---|
| 71 | } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 8) { | 
|---|
| 72 | /* Provide 8/3 scaling */ | 
|---|
| 73 | cinfo->jpeg_width = (cinfo->image_width << 1) + (JDIMENSION) | 
|---|
| 74 | jdiv_round_up((long) cinfo->image_width * 2, 3L); | 
|---|
| 75 | cinfo->jpeg_height = (cinfo->image_height << 1) + (JDIMENSION) | 
|---|
| 76 | jdiv_round_up((long) cinfo->image_height * 2, 3L); | 
|---|
| 77 | cinfo->min_DCT_h_scaled_size = 3; | 
|---|
| 78 | cinfo->min_DCT_v_scaled_size = 3; | 
|---|
| 79 | } else if (cinfo->scale_num >= cinfo->scale_denom * 2) { | 
|---|
| 80 | /* Provide 2/1 scaling */ | 
|---|
| 81 | cinfo->jpeg_width = cinfo->image_width << 1; | 
|---|
| 82 | cinfo->jpeg_height = cinfo->image_height << 1; | 
|---|
| 83 | cinfo->min_DCT_h_scaled_size = 4; | 
|---|
| 84 | cinfo->min_DCT_v_scaled_size = 4; | 
|---|
| 85 | } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 8) { | 
|---|
| 86 | /* Provide 8/5 scaling */ | 
|---|
| 87 | cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) | 
|---|
| 88 | jdiv_round_up((long) cinfo->image_width * 3, 5L); | 
|---|
| 89 | cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) | 
|---|
| 90 | jdiv_round_up((long) cinfo->image_height * 3, 5L); | 
|---|
| 91 | cinfo->min_DCT_h_scaled_size = 5; | 
|---|
| 92 | cinfo->min_DCT_v_scaled_size = 5; | 
|---|
| 93 | } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 4) { | 
|---|
| 94 | /* Provide 4/3 scaling */ | 
|---|
| 95 | cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) | 
|---|
| 96 | jdiv_round_up((long) cinfo->image_width, 3L); | 
|---|
| 97 | cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) | 
|---|
| 98 | jdiv_round_up((long) cinfo->image_height, 3L); | 
|---|
| 99 | cinfo->min_DCT_h_scaled_size = 6; | 
|---|
| 100 | cinfo->min_DCT_v_scaled_size = 6; | 
|---|
| 101 | } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 8) { | 
|---|
| 102 | /* Provide 8/7 scaling */ | 
|---|
| 103 | cinfo->jpeg_width = cinfo->image_width + (JDIMENSION) | 
|---|
| 104 | jdiv_round_up((long) cinfo->image_width, 7L); | 
|---|
| 105 | cinfo->jpeg_height = cinfo->image_height + (JDIMENSION) | 
|---|
| 106 | jdiv_round_up((long) cinfo->image_height, 7L); | 
|---|
| 107 | cinfo->min_DCT_h_scaled_size = 7; | 
|---|
| 108 | cinfo->min_DCT_v_scaled_size = 7; | 
|---|
| 109 | } else if (cinfo->scale_num >= cinfo->scale_denom) { | 
|---|
| 110 | /* Provide 1/1 scaling */ | 
|---|
| 111 | cinfo->jpeg_width = cinfo->image_width; | 
|---|
| 112 | cinfo->jpeg_height = cinfo->image_height; | 
|---|
| 113 | cinfo->min_DCT_h_scaled_size = 8; | 
|---|
| 114 | cinfo->min_DCT_v_scaled_size = 8; | 
|---|
| 115 | } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * 8) { | 
|---|
| 116 | /* Provide 8/9 scaling */ | 
|---|
| 117 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 118 | jdiv_round_up((long) cinfo->image_width * 8, 9L); | 
|---|
| 119 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 120 | jdiv_round_up((long) cinfo->image_height * 8, 9L); | 
|---|
| 121 | cinfo->min_DCT_h_scaled_size = 9; | 
|---|
| 122 | cinfo->min_DCT_v_scaled_size = 9; | 
|---|
| 123 | } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * 4) { | 
|---|
| 124 | /* Provide 4/5 scaling */ | 
|---|
| 125 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 126 | jdiv_round_up((long) cinfo->image_width * 4, 5L); | 
|---|
| 127 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 128 | jdiv_round_up((long) cinfo->image_height * 4, 5L); | 
|---|
| 129 | cinfo->min_DCT_h_scaled_size = 10; | 
|---|
| 130 | cinfo->min_DCT_v_scaled_size = 10; | 
|---|
| 131 | } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * 8) { | 
|---|
| 132 | /* Provide 8/11 scaling */ | 
|---|
| 133 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 134 | jdiv_round_up((long) cinfo->image_width * 8, 11L); | 
|---|
| 135 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 136 | jdiv_round_up((long) cinfo->image_height * 8, 11L); | 
|---|
| 137 | cinfo->min_DCT_h_scaled_size = 11; | 
|---|
| 138 | cinfo->min_DCT_v_scaled_size = 11; | 
|---|
| 139 | } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * 2) { | 
|---|
| 140 | /* Provide 2/3 scaling */ | 
|---|
| 141 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 142 | jdiv_round_up((long) cinfo->image_width * 2, 3L); | 
|---|
| 143 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 144 | jdiv_round_up((long) cinfo->image_height * 2, 3L); | 
|---|
| 145 | cinfo->min_DCT_h_scaled_size = 12; | 
|---|
| 146 | cinfo->min_DCT_v_scaled_size = 12; | 
|---|
| 147 | } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * 8) { | 
|---|
| 148 | /* Provide 8/13 scaling */ | 
|---|
| 149 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 150 | jdiv_round_up((long) cinfo->image_width * 8, 13L); | 
|---|
| 151 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 152 | jdiv_round_up((long) cinfo->image_height * 8, 13L); | 
|---|
| 153 | cinfo->min_DCT_h_scaled_size = 13; | 
|---|
| 154 | cinfo->min_DCT_v_scaled_size = 13; | 
|---|
| 155 | } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * 4) { | 
|---|
| 156 | /* Provide 4/7 scaling */ | 
|---|
| 157 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 158 | jdiv_round_up((long) cinfo->image_width * 4, 7L); | 
|---|
| 159 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 160 | jdiv_round_up((long) cinfo->image_height * 4, 7L); | 
|---|
| 161 | cinfo->min_DCT_h_scaled_size = 14; | 
|---|
| 162 | cinfo->min_DCT_v_scaled_size = 14; | 
|---|
| 163 | } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * 8) { | 
|---|
| 164 | /* Provide 8/15 scaling */ | 
|---|
| 165 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 166 | jdiv_round_up((long) cinfo->image_width * 8, 15L); | 
|---|
| 167 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 168 | jdiv_round_up((long) cinfo->image_height * 8, 15L); | 
|---|
| 169 | cinfo->min_DCT_h_scaled_size = 15; | 
|---|
| 170 | cinfo->min_DCT_v_scaled_size = 15; | 
|---|
| 171 | } else { | 
|---|
| 172 | /* Provide 1/2 scaling */ | 
|---|
| 173 | cinfo->jpeg_width = (JDIMENSION) | 
|---|
| 174 | jdiv_round_up((long) cinfo->image_width, 2L); | 
|---|
| 175 | cinfo->jpeg_height = (JDIMENSION) | 
|---|
| 176 | jdiv_round_up((long) cinfo->image_height, 2L); | 
|---|
| 177 | cinfo->min_DCT_h_scaled_size = 16; | 
|---|
| 178 | cinfo->min_DCT_v_scaled_size = 16; | 
|---|
| 179 | } | 
|---|
| 180 |  | 
|---|
| 181 | #else /* !DCT_SCALING_SUPPORTED */ | 
|---|
| 182 |  | 
|---|
| 183 | /* Hardwire it to "no scaling" */ | 
|---|
| 184 | cinfo->jpeg_width = cinfo->image_width; | 
|---|
| 185 | cinfo->jpeg_height = cinfo->image_height; | 
|---|
| 186 | cinfo->min_DCT_h_scaled_size = DCTSIZE; | 
|---|
| 187 | cinfo->min_DCT_v_scaled_size = DCTSIZE; | 
|---|
| 188 |  | 
|---|
| 189 | #endif /* DCT_SCALING_SUPPORTED */ | 
|---|
| 190 |  | 
|---|
| 191 | cinfo->block_size = DCTSIZE; | 
|---|
| 192 | cinfo->natural_order = jpeg_natural_order; | 
|---|
| 193 | cinfo->lim_Se = DCTSIZE2-1; | 
|---|
| 194 | } | 
|---|
| 195 |  | 
|---|
| 196 |  | 
|---|
| 197 | LOCAL(void) | 
|---|
| 198 | jpeg_calc_trans_dimensions (j_compress_ptr cinfo) | 
|---|
| 199 | { | 
|---|
| 200 | if (cinfo->min_DCT_h_scaled_size < 1 || cinfo->min_DCT_h_scaled_size > 16 | 
|---|
| 201 | || cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size) | 
|---|
| 202 | ERREXIT2(cinfo, JERR_BAD_DCTSIZE, | 
|---|
| 203 | cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size); | 
|---|
| 204 |  | 
|---|
| 205 | cinfo->block_size = cinfo->min_DCT_h_scaled_size; | 
|---|
| 206 |  | 
|---|
| 207 | switch (cinfo->block_size) { | 
|---|
| 208 | case 2: cinfo->natural_order = jpeg_natural_order2; break; | 
|---|
| 209 | case 3: cinfo->natural_order = jpeg_natural_order3; break; | 
|---|
| 210 | case 4: cinfo->natural_order = jpeg_natural_order4; break; | 
|---|
| 211 | case 5: cinfo->natural_order = jpeg_natural_order5; break; | 
|---|
| 212 | case 6: cinfo->natural_order = jpeg_natural_order6; break; | 
|---|
| 213 | case 7: cinfo->natural_order = jpeg_natural_order7; break; | 
|---|
| 214 | default: cinfo->natural_order = jpeg_natural_order; break; | 
|---|
| 215 | } | 
|---|
| 216 |  | 
|---|
| 217 | cinfo->lim_Se = cinfo->block_size < DCTSIZE ? | 
|---|
| 218 | cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 |  | 
|---|
| 222 | LOCAL(void) | 
|---|
| 223 | initial_setup (j_compress_ptr cinfo, boolean transcode_only) | 
|---|
| 224 | /* Do computations that are needed before master selection phase */ | 
|---|
| 225 | { | 
|---|
| 226 | int ci, ssize; | 
|---|
| 227 | jpeg_component_info *compptr; | 
|---|
| 228 | long samplesperrow; | 
|---|
| 229 | JDIMENSION jd_samplesperrow; | 
|---|
| 230 |  | 
|---|
| 231 | if (transcode_only) | 
|---|
| 232 | jpeg_calc_trans_dimensions(cinfo); | 
|---|
| 233 | else | 
|---|
| 234 | jpeg_calc_jpeg_dimensions(cinfo); | 
|---|
| 235 |  | 
|---|
| 236 | /* Sanity check on image dimensions */ | 
|---|
| 237 | if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || | 
|---|
| 238 | cinfo->num_components <= 0 || cinfo->input_components <= 0) | 
|---|
| 239 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); | 
|---|
| 240 |  | 
|---|
| 241 | /* Make sure image isn't bigger than I can handle */ | 
|---|
| 242 | if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || | 
|---|
| 243 | (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) | 
|---|
| 244 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); | 
|---|
| 245 |  | 
|---|
| 246 | /* Width of an input scanline must be representable as JDIMENSION. */ | 
|---|
| 247 | samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; | 
|---|
| 248 | jd_samplesperrow = (JDIMENSION) samplesperrow; | 
|---|
| 249 | if ((long) jd_samplesperrow != samplesperrow) | 
|---|
| 250 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); | 
|---|
| 251 |  | 
|---|
| 252 | /* For now, precision must match compiled-in value... */ | 
|---|
| 253 | if (cinfo->data_precision != BITS_IN_JSAMPLE) | 
|---|
| 254 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | 
|---|
| 255 |  | 
|---|
| 256 | /* Check that number of components won't exceed internal array sizes */ | 
|---|
| 257 | if (cinfo->num_components > MAX_COMPONENTS) | 
|---|
| 258 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, | 
|---|
| 259 | MAX_COMPONENTS); | 
|---|
| 260 |  | 
|---|
| 261 | /* Compute maximum sampling factors; check factor validity */ | 
|---|
| 262 | cinfo->max_h_samp_factor = 1; | 
|---|
| 263 | cinfo->max_v_samp_factor = 1; | 
|---|
| 264 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | 
|---|
| 265 | ci++, compptr++) { | 
|---|
| 266 | if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || | 
|---|
| 267 | compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) | 
|---|
| 268 | ERREXIT(cinfo, JERR_BAD_SAMPLING); | 
|---|
| 269 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, | 
|---|
| 270 | compptr->h_samp_factor); | 
|---|
| 271 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, | 
|---|
| 272 | compptr->v_samp_factor); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | /* Compute dimensions of components */ | 
|---|
| 276 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | 
|---|
| 277 | ci++, compptr++) { | 
|---|
| 278 | /* Fill in the correct component_index value; don't rely on application */ | 
|---|
| 279 | compptr->component_index = ci; | 
|---|
| 280 | /* In selecting the actual DCT scaling for each component, we try to | 
|---|
| 281 | * scale down the chroma components via DCT scaling rather than downsampling. | 
|---|
| 282 | * This saves time if the downsampler gets to use 1:1 scaling. | 
|---|
| 283 | * Note this code adapts subsampling ratios which are powers of 2. | 
|---|
| 284 | */ | 
|---|
| 285 | ssize = 1; | 
|---|
| 286 | #ifdef DCT_SCALING_SUPPORTED | 
|---|
| 287 | while (cinfo->min_DCT_h_scaled_size * ssize <= | 
|---|
| 288 | (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && | 
|---|
| 289 | (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { | 
|---|
| 290 | ssize = ssize * 2; | 
|---|
| 291 | } | 
|---|
| 292 | #endif | 
|---|
| 293 | compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; | 
|---|
| 294 | ssize = 1; | 
|---|
| 295 | #ifdef DCT_SCALING_SUPPORTED | 
|---|
| 296 | while (cinfo->min_DCT_v_scaled_size * ssize <= | 
|---|
| 297 | (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && | 
|---|
| 298 | (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { | 
|---|
| 299 | ssize = ssize * 2; | 
|---|
| 300 | } | 
|---|
| 301 | #endif | 
|---|
| 302 | compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; | 
|---|
| 303 |  | 
|---|
| 304 | /* We don't support DCT ratios larger than 2. */ | 
|---|
| 305 | if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) | 
|---|
| 306 | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; | 
|---|
| 307 | else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) | 
|---|
| 308 | compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; | 
|---|
| 309 |  | 
|---|
| 310 | /* Size in DCT blocks */ | 
|---|
| 311 | compptr->width_in_blocks = (JDIMENSION) | 
|---|
| 312 | jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, | 
|---|
| 313 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); | 
|---|
| 314 | compptr->height_in_blocks = (JDIMENSION) | 
|---|
| 315 | jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, | 
|---|
| 316 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); | 
|---|
| 317 | /* Size in samples */ | 
|---|
| 318 | compptr->downsampled_width = (JDIMENSION) | 
|---|
| 319 | jdiv_round_up((long) cinfo->jpeg_width * | 
|---|
| 320 | (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), | 
|---|
| 321 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); | 
|---|
| 322 | compptr->downsampled_height = (JDIMENSION) | 
|---|
| 323 | jdiv_round_up((long) cinfo->jpeg_height * | 
|---|
| 324 | (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), | 
|---|
| 325 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); | 
|---|
| 326 | /* Mark component needed (this flag isn't actually used for compression) */ | 
|---|
| 327 | compptr->component_needed = TRUE; | 
|---|
| 328 | } | 
|---|
| 329 |  | 
|---|
| 330 | /* Compute number of fully interleaved MCU rows (number of times that | 
|---|
| 331 | * main controller will call coefficient controller). | 
|---|
| 332 | */ | 
|---|
| 333 | cinfo->total_iMCU_rows = (JDIMENSION) | 
|---|
| 334 | jdiv_round_up((long) cinfo->jpeg_height, | 
|---|
| 335 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); | 
|---|
| 336 | } | 
|---|
| 337 |  | 
|---|
| 338 |  | 
|---|
| 339 | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
|---|
| 340 |  | 
|---|
| 341 | LOCAL(void) | 
|---|
| 342 | validate_script (j_compress_ptr cinfo) | 
|---|
| 343 | /* Verify that the scan script in cinfo->scan_info[] is valid; also | 
|---|
| 344 | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. | 
|---|
| 345 | */ | 
|---|
| 346 | { | 
|---|
| 347 | const jpeg_scan_info * scanptr; | 
|---|
| 348 | int scanno, ncomps, ci, coefi, thisi; | 
|---|
| 349 | int Ss, Se, Ah, Al; | 
|---|
| 350 | boolean component_sent[MAX_COMPONENTS]; | 
|---|
| 351 | #ifdef C_PROGRESSIVE_SUPPORTED | 
|---|
| 352 | int * last_bitpos_ptr; | 
|---|
| 353 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; | 
|---|
| 354 | /* -1 until that coefficient has been seen; then last Al for it */ | 
|---|
| 355 | #endif | 
|---|
| 356 |  | 
|---|
| 357 | if (cinfo->num_scans <= 0) | 
|---|
| 358 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); | 
|---|
| 359 |  | 
|---|
| 360 | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; | 
|---|
| 361 | * for progressive JPEG, no scan can have this. | 
|---|
| 362 | */ | 
|---|
| 363 | scanptr = cinfo->scan_info; | 
|---|
| 364 | if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { | 
|---|
| 365 | #ifdef C_PROGRESSIVE_SUPPORTED | 
|---|
| 366 | cinfo->progressive_mode = TRUE; | 
|---|
| 367 | last_bitpos_ptr = & last_bitpos[0][0]; | 
|---|
| 368 | for (ci = 0; ci < cinfo->num_components; ci++) | 
|---|
| 369 | for (coefi = 0; coefi < DCTSIZE2; coefi++) | 
|---|
| 370 | *last_bitpos_ptr++ = -1; | 
|---|
| 371 | #else | 
|---|
| 372 | ERREXIT(cinfo, JERR_NOT_COMPILED); | 
|---|
| 373 | #endif | 
|---|
| 374 | } else { | 
|---|
| 375 | cinfo->progressive_mode = FALSE; | 
|---|
| 376 | for (ci = 0; ci < cinfo->num_components; ci++) | 
|---|
| 377 | component_sent[ci] = FALSE; | 
|---|
| 378 | } | 
|---|
| 379 |  | 
|---|
| 380 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { | 
|---|
| 381 | /* Validate component indexes */ | 
|---|
| 382 | ncomps = scanptr->comps_in_scan; | 
|---|
| 383 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) | 
|---|
| 384 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); | 
|---|
| 385 | for (ci = 0; ci < ncomps; ci++) { | 
|---|
| 386 | thisi = scanptr->component_index[ci]; | 
|---|
| 387 | if (thisi < 0 || thisi >= cinfo->num_components) | 
|---|
| 388 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
|---|
| 389 | /* Components must appear in SOF order within each scan */ | 
|---|
| 390 | if (ci > 0 && thisi <= scanptr->component_index[ci-1]) | 
|---|
| 391 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
|---|
| 392 | } | 
|---|
| 393 | /* Validate progression parameters */ | 
|---|
| 394 | Ss = scanptr->Ss; | 
|---|
| 395 | Se = scanptr->Se; | 
|---|
| 396 | Ah = scanptr->Ah; | 
|---|
| 397 | Al = scanptr->Al; | 
|---|
| 398 | if (cinfo->progressive_mode) { | 
|---|
| 399 | #ifdef C_PROGRESSIVE_SUPPORTED | 
|---|
| 400 | /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that | 
|---|
| 401 | * seems wrong: the upper bound ought to depend on data precision. | 
|---|
| 402 | * Perhaps they really meant 0..N+1 for N-bit precision. | 
|---|
| 403 | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in | 
|---|
| 404 | * out-of-range reconstructed DC values during the first DC scan, | 
|---|
| 405 | * which might cause problems for some decoders. | 
|---|
| 406 | */ | 
|---|
| 407 | #if BITS_IN_JSAMPLE == 8 | 
|---|
| 408 | #define MAX_AH_AL 10 | 
|---|
| 409 | #else | 
|---|
| 410 | #define MAX_AH_AL 13 | 
|---|
| 411 | #endif | 
|---|
| 412 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || | 
|---|
| 413 | Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) | 
|---|
| 414 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 415 | if (Ss == 0) { | 
|---|
| 416 | if (Se != 0)            /* DC and AC together not OK */ | 
|---|
| 417 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 418 | } else { | 
|---|
| 419 | if (ncomps != 1)        /* AC scans must be for only one component */ | 
|---|
| 420 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 421 | } | 
|---|
| 422 | for (ci = 0; ci < ncomps; ci++) { | 
|---|
| 423 | last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; | 
|---|
| 424 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ | 
|---|
| 425 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 426 | for (coefi = Ss; coefi <= Se; coefi++) { | 
|---|
| 427 | if (last_bitpos_ptr[coefi] < 0) { | 
|---|
| 428 | /* first scan of this coefficient */ | 
|---|
| 429 | if (Ah != 0) | 
|---|
| 430 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 431 | } else { | 
|---|
| 432 | /* not first scan */ | 
|---|
| 433 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) | 
|---|
| 434 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 435 | } | 
|---|
| 436 | last_bitpos_ptr[coefi] = Al; | 
|---|
| 437 | } | 
|---|
| 438 | } | 
|---|
| 439 | #endif | 
|---|
| 440 | } else { | 
|---|
| 441 | /* For sequential JPEG, all progression parameters must be these: */ | 
|---|
| 442 | if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) | 
|---|
| 443 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); | 
|---|
| 444 | /* Make sure components are not sent twice */ | 
|---|
| 445 | for (ci = 0; ci < ncomps; ci++) { | 
|---|
| 446 | thisi = scanptr->component_index[ci]; | 
|---|
| 447 | if (component_sent[thisi]) | 
|---|
| 448 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); | 
|---|
| 449 | component_sent[thisi] = TRUE; | 
|---|
| 450 | } | 
|---|
| 451 | } | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 | /* Now verify that everything got sent. */ | 
|---|
| 455 | if (cinfo->progressive_mode) { | 
|---|
| 456 | #ifdef C_PROGRESSIVE_SUPPORTED | 
|---|
| 457 | /* For progressive mode, we only check that at least some DC data | 
|---|
| 458 | * got sent for each component; the spec does not require that all bits | 
|---|
| 459 | * of all coefficients be transmitted.  Would it be wiser to enforce | 
|---|
| 460 | * transmission of all coefficient bits?? | 
|---|
| 461 | */ | 
|---|
| 462 | for (ci = 0; ci < cinfo->num_components; ci++) { | 
|---|
| 463 | if (last_bitpos[ci][0] < 0) | 
|---|
| 464 | ERREXIT(cinfo, JERR_MISSING_DATA); | 
|---|
| 465 | } | 
|---|
| 466 | #endif | 
|---|
| 467 | } else { | 
|---|
| 468 | for (ci = 0; ci < cinfo->num_components; ci++) { | 
|---|
| 469 | if (! component_sent[ci]) | 
|---|
| 470 | ERREXIT(cinfo, JERR_MISSING_DATA); | 
|---|
| 471 | } | 
|---|
| 472 | } | 
|---|
| 473 | } | 
|---|
| 474 |  | 
|---|
| 475 |  | 
|---|
| 476 | LOCAL(void) | 
|---|
| 477 | reduce_script (j_compress_ptr cinfo) | 
|---|
| 478 | /* Adapt scan script for use with reduced block size; | 
|---|
| 479 | * assume that script has been validated before. | 
|---|
| 480 | */ | 
|---|
| 481 | { | 
|---|
| 482 | jpeg_scan_info * scanptr; | 
|---|
| 483 | int idxout, idxin; | 
|---|
| 484 |  | 
|---|
| 485 | /* Circumvent const declaration for this function */ | 
|---|
| 486 | scanptr = (jpeg_scan_info *) cinfo->scan_info; | 
|---|
| 487 | idxout = 0; | 
|---|
| 488 |  | 
|---|
| 489 | for (idxin = 0; idxin < cinfo->num_scans; idxin++) { | 
|---|
| 490 | /* After skipping, idxout becomes smaller than idxin */ | 
|---|
| 491 | if (idxin != idxout) | 
|---|
| 492 | /* Copy rest of data; | 
|---|
| 493 | * note we stay in given chunk of allocated memory. | 
|---|
| 494 | */ | 
|---|
| 495 | scanptr[idxout] = scanptr[idxin]; | 
|---|
| 496 | if (scanptr[idxout].Ss > cinfo->lim_Se) | 
|---|
| 497 | /* Entire scan out of range - skip this entry */ | 
|---|
| 498 | continue; | 
|---|
| 499 | if (scanptr[idxout].Se > cinfo->lim_Se) | 
|---|
| 500 | /* Limit scan to end of block */ | 
|---|
| 501 | scanptr[idxout].Se = cinfo->lim_Se; | 
|---|
| 502 | idxout++; | 
|---|
| 503 | } | 
|---|
| 504 |  | 
|---|
| 505 | cinfo->num_scans = idxout; | 
|---|
| 506 | } | 
|---|
| 507 |  | 
|---|
| 508 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ | 
|---|
| 509 |  | 
|---|
| 510 |  | 
|---|
| 511 | LOCAL(void) | 
|---|
| 512 | select_scan_parameters (j_compress_ptr cinfo) | 
|---|
| 513 | /* Set up the scan parameters for the current scan */ | 
|---|
| 514 | { | 
|---|
| 515 | int ci; | 
|---|
| 516 |  | 
|---|
| 517 | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
|---|
| 518 | if (cinfo->scan_info != NULL) { | 
|---|
| 519 | /* Prepare for current scan --- the script is already validated */ | 
|---|
| 520 | my_master_ptr master = (my_master_ptr) cinfo->master; | 
|---|
| 521 | const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; | 
|---|
| 522 |  | 
|---|
| 523 | cinfo->comps_in_scan = scanptr->comps_in_scan; | 
|---|
| 524 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { | 
|---|
| 525 | cinfo->cur_comp_info[ci] = | 
|---|
| 526 | &cinfo->comp_info[scanptr->component_index[ci]]; | 
|---|
| 527 | } | 
|---|
| 528 | if (cinfo->progressive_mode) { | 
|---|
| 529 | cinfo->Ss = scanptr->Ss; | 
|---|
| 530 | cinfo->Se = scanptr->Se; | 
|---|
| 531 | cinfo->Ah = scanptr->Ah; | 
|---|
| 532 | cinfo->Al = scanptr->Al; | 
|---|
| 533 | return; | 
|---|
| 534 | } | 
|---|
| 535 | } | 
|---|
| 536 | else | 
|---|
| 537 | #endif | 
|---|
| 538 | { | 
|---|
| 539 | /* Prepare for single sequential-JPEG scan containing all components */ | 
|---|
| 540 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) | 
|---|
| 541 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, | 
|---|
| 542 | MAX_COMPS_IN_SCAN); | 
|---|
| 543 | cinfo->comps_in_scan = cinfo->num_components; | 
|---|
| 544 | for (ci = 0; ci < cinfo->num_components; ci++) { | 
|---|
| 545 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; | 
|---|
| 546 | } | 
|---|
| 547 | } | 
|---|
| 548 | cinfo->Ss = 0; | 
|---|
| 549 | cinfo->Se = cinfo->block_size * cinfo->block_size - 1; | 
|---|
| 550 | cinfo->Ah = 0; | 
|---|
| 551 | cinfo->Al = 0; | 
|---|
| 552 | } | 
|---|
| 553 |  | 
|---|
| 554 |  | 
|---|
| 555 | LOCAL(void) | 
|---|
| 556 | per_scan_setup (j_compress_ptr cinfo) | 
|---|
| 557 | /* Do computations that are needed before processing a JPEG scan */ | 
|---|
| 558 | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ | 
|---|
| 559 | { | 
|---|
| 560 | int ci, mcublks, tmp; | 
|---|
| 561 | jpeg_component_info *compptr; | 
|---|
| 562 |  | 
|---|
| 563 | if (cinfo->comps_in_scan == 1) { | 
|---|
| 564 |  | 
|---|
| 565 | /* Noninterleaved (single-component) scan */ | 
|---|
| 566 | compptr = cinfo->cur_comp_info[0]; | 
|---|
| 567 |  | 
|---|
| 568 | /* Overall image size in MCUs */ | 
|---|
| 569 | cinfo->MCUs_per_row = compptr->width_in_blocks; | 
|---|
| 570 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; | 
|---|
| 571 |  | 
|---|
| 572 | /* For noninterleaved scan, always one block per MCU */ | 
|---|
| 573 | compptr->MCU_width = 1; | 
|---|
| 574 | compptr->MCU_height = 1; | 
|---|
| 575 | compptr->MCU_blocks = 1; | 
|---|
| 576 | compptr->MCU_sample_width = compptr->DCT_h_scaled_size; | 
|---|
| 577 | compptr->last_col_width = 1; | 
|---|
| 578 | /* For noninterleaved scans, it is convenient to define last_row_height | 
|---|
| 579 | * as the number of block rows present in the last iMCU row. | 
|---|
| 580 | */ | 
|---|
| 581 | tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); | 
|---|
| 582 | if (tmp == 0) tmp = compptr->v_samp_factor; | 
|---|
| 583 | compptr->last_row_height = tmp; | 
|---|
| 584 |  | 
|---|
| 585 | /* Prepare array describing MCU composition */ | 
|---|
| 586 | cinfo->blocks_in_MCU = 1; | 
|---|
| 587 | cinfo->MCU_membership[0] = 0; | 
|---|
| 588 |  | 
|---|
| 589 | } else { | 
|---|
| 590 |  | 
|---|
| 591 | /* Interleaved (multi-component) scan */ | 
|---|
| 592 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) | 
|---|
| 593 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, | 
|---|
| 594 | MAX_COMPS_IN_SCAN); | 
|---|
| 595 |  | 
|---|
| 596 | /* Overall image size in MCUs */ | 
|---|
| 597 | cinfo->MCUs_per_row = (JDIMENSION) | 
|---|
| 598 | jdiv_round_up((long) cinfo->jpeg_width, | 
|---|
| 599 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); | 
|---|
| 600 | cinfo->MCU_rows_in_scan = (JDIMENSION) | 
|---|
| 601 | jdiv_round_up((long) cinfo->jpeg_height, | 
|---|
| 602 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); | 
|---|
| 603 |  | 
|---|
| 604 | cinfo->blocks_in_MCU = 0; | 
|---|
| 605 |  | 
|---|
| 606 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | 
|---|
| 607 | compptr = cinfo->cur_comp_info[ci]; | 
|---|
| 608 | /* Sampling factors give # of blocks of component in each MCU */ | 
|---|
| 609 | compptr->MCU_width = compptr->h_samp_factor; | 
|---|
| 610 | compptr->MCU_height = compptr->v_samp_factor; | 
|---|
| 611 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; | 
|---|
| 612 | compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; | 
|---|
| 613 | /* Figure number of non-dummy blocks in last MCU column & row */ | 
|---|
| 614 | tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); | 
|---|
| 615 | if (tmp == 0) tmp = compptr->MCU_width; | 
|---|
| 616 | compptr->last_col_width = tmp; | 
|---|
| 617 | tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); | 
|---|
| 618 | if (tmp == 0) tmp = compptr->MCU_height; | 
|---|
| 619 | compptr->last_row_height = tmp; | 
|---|
| 620 | /* Prepare array describing MCU composition */ | 
|---|
| 621 | mcublks = compptr->MCU_blocks; | 
|---|
| 622 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) | 
|---|
| 623 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); | 
|---|
| 624 | while (mcublks-- > 0) { | 
|---|
| 625 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; | 
|---|
| 626 | } | 
|---|
| 627 | } | 
|---|
| 628 |  | 
|---|
| 629 | } | 
|---|
| 630 |  | 
|---|
| 631 | /* Convert restart specified in rows to actual MCU count. */ | 
|---|
| 632 | /* Note that count must fit in 16 bits, so we provide limiting. */ | 
|---|
| 633 | if (cinfo->restart_in_rows > 0) { | 
|---|
| 634 | long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; | 
|---|
| 635 | cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); | 
|---|
| 636 | } | 
|---|
| 637 | } | 
|---|
| 638 |  | 
|---|
| 639 |  | 
|---|
| 640 | /* | 
|---|
| 641 | * Per-pass setup. | 
|---|
| 642 | * This is called at the beginning of each pass.  We determine which modules | 
|---|
| 643 | * will be active during this pass and give them appropriate start_pass calls. | 
|---|
| 644 | * We also set is_last_pass to indicate whether any more passes will be | 
|---|
| 645 | * required. | 
|---|
| 646 | */ | 
|---|
| 647 |  | 
|---|
| 648 | METHODDEF(void) | 
|---|
| 649 | prepare_for_pass (j_compress_ptr cinfo) | 
|---|
| 650 | { | 
|---|
| 651 | my_master_ptr master = (my_master_ptr) cinfo->master; | 
|---|
| 652 |  | 
|---|
| 653 | switch (master->pass_type) { | 
|---|
| 654 | case main_pass: | 
|---|
| 655 | /* Initial pass: will collect input data, and do either Huffman | 
|---|
| 656 | * optimization or data output for the first scan. | 
|---|
| 657 | */ | 
|---|
| 658 | select_scan_parameters(cinfo); | 
|---|
| 659 | per_scan_setup(cinfo); | 
|---|
| 660 | if (! cinfo->raw_data_in) { | 
|---|
| 661 | (*cinfo->cconvert->start_pass) (cinfo); | 
|---|
| 662 | (*cinfo->downsample->start_pass) (cinfo); | 
|---|
| 663 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); | 
|---|
| 664 | } | 
|---|
| 665 | (*cinfo->fdct->start_pass) (cinfo); | 
|---|
| 666 | (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); | 
|---|
| 667 | (*cinfo->coef->start_pass) (cinfo, | 
|---|
| 668 | (master->total_passes > 1 ? | 
|---|
| 669 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); | 
|---|
| 670 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); | 
|---|
| 671 | if (cinfo->optimize_coding) { | 
|---|
| 672 | /* No immediate data output; postpone writing frame/scan headers */ | 
|---|
| 673 | master->pub.call_pass_startup = FALSE; | 
|---|
| 674 | } else { | 
|---|
| 675 | /* Will write frame/scan headers at first jpeg_write_scanlines call */ | 
|---|
| 676 | master->pub.call_pass_startup = TRUE; | 
|---|
| 677 | } | 
|---|
| 678 | break; | 
|---|
| 679 | #ifdef ENTROPY_OPT_SUPPORTED | 
|---|
| 680 | case huff_opt_pass: | 
|---|
| 681 | /* Do Huffman optimization for a scan after the first one. */ | 
|---|
| 682 | select_scan_parameters(cinfo); | 
|---|
| 683 | per_scan_setup(cinfo); | 
|---|
| 684 | if (cinfo->Ss != 0 || cinfo->Ah == 0) { | 
|---|
| 685 | (*cinfo->entropy->start_pass) (cinfo, TRUE); | 
|---|
| 686 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); | 
|---|
| 687 | master->pub.call_pass_startup = FALSE; | 
|---|
| 688 | break; | 
|---|
| 689 | } | 
|---|
| 690 | /* Special case: Huffman DC refinement scans need no Huffman table | 
|---|
| 691 | * and therefore we can skip the optimization pass for them. | 
|---|
| 692 | */ | 
|---|
| 693 | master->pass_type = output_pass; | 
|---|
| 694 | master->pass_number++; | 
|---|
| 695 | /*FALLTHROUGH*/ | 
|---|
| 696 | #endif | 
|---|
| 697 | case output_pass: | 
|---|
| 698 | /* Do a data-output pass. */ | 
|---|
| 699 | /* We need not repeat per-scan setup if prior optimization pass did it. */ | 
|---|
| 700 | if (! cinfo->optimize_coding) { | 
|---|
| 701 | select_scan_parameters(cinfo); | 
|---|
| 702 | per_scan_setup(cinfo); | 
|---|
| 703 | } | 
|---|
| 704 | (*cinfo->entropy->start_pass) (cinfo, FALSE); | 
|---|
| 705 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); | 
|---|
| 706 | /* We emit frame/scan headers now */ | 
|---|
| 707 | if (master->scan_number == 0) | 
|---|
| 708 | (*cinfo->marker->write_frame_header) (cinfo); | 
|---|
| 709 | (*cinfo->marker->write_scan_header) (cinfo); | 
|---|
| 710 | master->pub.call_pass_startup = FALSE; | 
|---|
| 711 | break; | 
|---|
| 712 | default: | 
|---|
| 713 | ERREXIT(cinfo, JERR_NOT_COMPILED); | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | master->pub.is_last_pass = (master->pass_number == master->total_passes-1); | 
|---|
| 717 |  | 
|---|
| 718 | /* Set up progress monitor's pass info if present */ | 
|---|
| 719 | if (cinfo->progress != NULL) { | 
|---|
| 720 | cinfo->progress->completed_passes = master->pass_number; | 
|---|
| 721 | cinfo->progress->total_passes = master->total_passes; | 
|---|
| 722 | } | 
|---|
| 723 | } | 
|---|
| 724 |  | 
|---|
| 725 |  | 
|---|
| 726 | /* | 
|---|
| 727 | * Special start-of-pass hook. | 
|---|
| 728 | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. | 
|---|
| 729 | * In single-pass processing, we need this hook because we don't want to | 
|---|
| 730 | * write frame/scan headers during jpeg_start_compress; we want to let the | 
|---|
| 731 | * application write COM markers etc. between jpeg_start_compress and the | 
|---|
| 732 | * jpeg_write_scanlines loop. | 
|---|
| 733 | * In multi-pass processing, this routine is not used. | 
|---|
| 734 | */ | 
|---|
| 735 |  | 
|---|
| 736 | METHODDEF(void) | 
|---|
| 737 | pass_startup (j_compress_ptr cinfo) | 
|---|
| 738 | { | 
|---|
| 739 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ | 
|---|
| 740 |  | 
|---|
| 741 | (*cinfo->marker->write_frame_header) (cinfo); | 
|---|
| 742 | (*cinfo->marker->write_scan_header) (cinfo); | 
|---|
| 743 | } | 
|---|
| 744 |  | 
|---|
| 745 |  | 
|---|
| 746 | /* | 
|---|
| 747 | * Finish up at end of pass. | 
|---|
| 748 | */ | 
|---|
| 749 |  | 
|---|
| 750 | METHODDEF(void) | 
|---|
| 751 | finish_pass_master (j_compress_ptr cinfo) | 
|---|
| 752 | { | 
|---|
| 753 | my_master_ptr master = (my_master_ptr) cinfo->master; | 
|---|
| 754 |  | 
|---|
| 755 | /* The entropy coder always needs an end-of-pass call, | 
|---|
| 756 | * either to analyze statistics or to flush its output buffer. | 
|---|
| 757 | */ | 
|---|
| 758 | (*cinfo->entropy->finish_pass) (cinfo); | 
|---|
| 759 |  | 
|---|
| 760 | /* Update state for next pass */ | 
|---|
| 761 | switch (master->pass_type) { | 
|---|
| 762 | case main_pass: | 
|---|
| 763 | /* next pass is either output of scan 0 (after optimization) | 
|---|
| 764 | * or output of scan 1 (if no optimization). | 
|---|
| 765 | */ | 
|---|
| 766 | master->pass_type = output_pass; | 
|---|
| 767 | if (! cinfo->optimize_coding) | 
|---|
| 768 | master->scan_number++; | 
|---|
| 769 | break; | 
|---|
| 770 | case huff_opt_pass: | 
|---|
| 771 | /* next pass is always output of current scan */ | 
|---|
| 772 | master->pass_type = output_pass; | 
|---|
| 773 | break; | 
|---|
| 774 | case output_pass: | 
|---|
| 775 | /* next pass is either optimization or output of next scan */ | 
|---|
| 776 | if (cinfo->optimize_coding) | 
|---|
| 777 | master->pass_type = huff_opt_pass; | 
|---|
| 778 | master->scan_number++; | 
|---|
| 779 | break; | 
|---|
| 780 | } | 
|---|
| 781 |  | 
|---|
| 782 | master->pass_number++; | 
|---|
| 783 | } | 
|---|
| 784 |  | 
|---|
| 785 |  | 
|---|
| 786 | /* | 
|---|
| 787 | * Initialize master compression control. | 
|---|
| 788 | */ | 
|---|
| 789 |  | 
|---|
| 790 | GLOBAL(void) | 
|---|
| 791 | jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) | 
|---|
| 792 | { | 
|---|
| 793 | my_master_ptr master; | 
|---|
| 794 |  | 
|---|
| 795 | master = (my_master_ptr) | 
|---|
| 796 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | 
|---|
| 797 | SIZEOF(my_comp_master)); | 
|---|
| 798 | cinfo->master = (struct jpeg_comp_master *) master; | 
|---|
| 799 | master->pub.prepare_for_pass = prepare_for_pass; | 
|---|
| 800 | master->pub.pass_startup = pass_startup; | 
|---|
| 801 | master->pub.finish_pass = finish_pass_master; | 
|---|
| 802 | master->pub.is_last_pass = FALSE; | 
|---|
| 803 |  | 
|---|
| 804 | /* Validate parameters, determine derived values */ | 
|---|
| 805 | initial_setup(cinfo, transcode_only); | 
|---|
| 806 |  | 
|---|
| 807 | if (cinfo->scan_info != NULL) { | 
|---|
| 808 | #ifdef C_MULTISCAN_FILES_SUPPORTED | 
|---|
| 809 | validate_script(cinfo); | 
|---|
| 810 | if (cinfo->block_size < DCTSIZE) | 
|---|
| 811 | reduce_script(cinfo); | 
|---|
| 812 | #else | 
|---|
| 813 | ERREXIT(cinfo, JERR_NOT_COMPILED); | 
|---|
| 814 | #endif | 
|---|
| 815 | } else { | 
|---|
| 816 | cinfo->progressive_mode = FALSE; | 
|---|
| 817 | cinfo->num_scans = 1; | 
|---|
| 818 | } | 
|---|
| 819 |  | 
|---|
| 820 | if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) && | 
|---|
| 821 | !cinfo->arith_code)                       /*  TEMPORARY HACK ??? */ | 
|---|
| 822 | /* assume default tables no good for progressive or downscale mode */ | 
|---|
| 823 | cinfo->optimize_coding = TRUE; | 
|---|
| 824 |  | 
|---|
| 825 | /* Initialize my private state */ | 
|---|
| 826 | if (transcode_only) { | 
|---|
| 827 | /* no main pass in transcoding */ | 
|---|
| 828 | if (cinfo->optimize_coding) | 
|---|
| 829 | master->pass_type = huff_opt_pass; | 
|---|
| 830 | else | 
|---|
| 831 | master->pass_type = output_pass; | 
|---|
| 832 | } else { | 
|---|
| 833 | /* for normal compression, first pass is always this type: */ | 
|---|
| 834 | master->pass_type = main_pass; | 
|---|
| 835 | } | 
|---|
| 836 | master->scan_number = 0; | 
|---|
| 837 | master->pass_number = 0; | 
|---|
| 838 | if (cinfo->optimize_coding) | 
|---|
| 839 | master->total_passes = cinfo->num_scans * 2; | 
|---|
| 840 | else | 
|---|
| 841 | master->total_passes = cinfo->num_scans; | 
|---|
| 842 | } | 
|---|