Home | Info | Community | Development | myReactOS | Contact Us
ReactOS Development > Doxygenjcmaster.c
Go to the documentation of this file.
00001 /* 00002 * jcmaster.c 00003 * 00004 * Copyright (C) 1991-1997, Thomas G. Lane. 00005 * Modified 2003-2011 by Guido Vollbeding. 00006 * This file is part of the Independent JPEG Group's software. 00007 * For conditions of distribution and use, see the accompanying README file. 00008 * 00009 * This file contains master control logic for the JPEG compressor. 00010 * These routines are concerned with parameter validation, initial setup, 00011 * and inter-pass control (determining the number of passes and the work 00012 * to be done in each pass). 00013 */ 00014 00015 #define JPEG_INTERNALS 00016 #include "jinclude.h" 00017 #include "jpeglib.h" 00018 00019 00020 /* Private state */ 00021 00022 typedef enum { 00023 main_pass, /* input data, also do first output step */ 00024 huff_opt_pass, /* Huffman code optimization pass */ 00025 output_pass /* data output pass */ 00026 } c_pass_type; 00027 00028 typedef struct { 00029 struct jpeg_comp_master pub; /* public fields */ 00030 00031 c_pass_type pass_type; /* the type of the current pass */ 00032 00033 int pass_number; /* # of passes completed */ 00034 int total_passes; /* total # of passes needed */ 00035 00036 int scan_number; /* current index in scan_info[] */ 00037 } my_comp_master; 00038 00039 typedef my_comp_master * my_master_ptr; 00040 00041 00042 /* 00043 * Support routines that do various essential calculations. 00044 */ 00045 00046 /* 00047 * Compute JPEG image dimensions and related values. 00048 * NOTE: this is exported for possible use by application. 00049 * Hence it mustn't do anything that can't be done twice. 00050 */ 00051 00052 GLOBAL(void) 00053 jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) 00054 /* Do computations that are needed before master selection phase */ 00055 { 00056 #ifdef DCT_SCALING_SUPPORTED 00057 00058 /* Sanity check on input image dimensions to prevent overflow in 00059 * following calculation. 00060 * We do check jpeg_width and jpeg_height in initial_setup below, 00061 * but image_width and image_height can come from arbitrary data, 00062 * and we need some space for multiplication by block_size. 00063 */ 00064 if (((long) cinfo->image_width >> 24) || ((long) cinfo->image_height >> 24)) 00065 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 00066 00067 /* Compute actual JPEG image dimensions and DCT scaling choices. */ 00068 if (cinfo->scale_num >= cinfo->scale_denom * cinfo->block_size) { 00069 /* Provide block_size/1 scaling */ 00070 cinfo->jpeg_width = cinfo->image_width * cinfo->block_size; 00071 cinfo->jpeg_height = cinfo->image_height * cinfo->block_size; 00072 cinfo->min_DCT_h_scaled_size = 1; 00073 cinfo->min_DCT_v_scaled_size = 1; 00074 } else if (cinfo->scale_num * 2 >= cinfo->scale_denom * cinfo->block_size) { 00075 /* Provide block_size/2 scaling */ 00076 cinfo->jpeg_width = (JDIMENSION) 00077 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 2L); 00078 cinfo->jpeg_height = (JDIMENSION) 00079 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 2L); 00080 cinfo->min_DCT_h_scaled_size = 2; 00081 cinfo->min_DCT_v_scaled_size = 2; 00082 } else if (cinfo->scale_num * 3 >= cinfo->scale_denom * cinfo->block_size) { 00083 /* Provide block_size/3 scaling */ 00084 cinfo->jpeg_width = (JDIMENSION) 00085 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 3L); 00086 cinfo->jpeg_height = (JDIMENSION) 00087 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 3L); 00088 cinfo->min_DCT_h_scaled_size = 3; 00089 cinfo->min_DCT_v_scaled_size = 3; 00090 } else if (cinfo->scale_num * 4 >= cinfo->scale_denom * cinfo->block_size) { 00091 /* Provide block_size/4 scaling */ 00092 cinfo->jpeg_width = (JDIMENSION) 00093 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 4L); 00094 cinfo->jpeg_height = (JDIMENSION) 00095 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 4L); 00096 cinfo->min_DCT_h_scaled_size = 4; 00097 cinfo->min_DCT_v_scaled_size = 4; 00098 } else if (cinfo->scale_num * 5 >= cinfo->scale_denom * cinfo->block_size) { 00099 /* Provide block_size/5 scaling */ 00100 cinfo->jpeg_width = (JDIMENSION) 00101 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 5L); 00102 cinfo->jpeg_height = (JDIMENSION) 00103 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 5L); 00104 cinfo->min_DCT_h_scaled_size = 5; 00105 cinfo->min_DCT_v_scaled_size = 5; 00106 } else if (cinfo->scale_num * 6 >= cinfo->scale_denom * cinfo->block_size) { 00107 /* Provide block_size/6 scaling */ 00108 cinfo->jpeg_width = (JDIMENSION) 00109 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 6L); 00110 cinfo->jpeg_height = (JDIMENSION) 00111 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 6L); 00112 cinfo->min_DCT_h_scaled_size = 6; 00113 cinfo->min_DCT_v_scaled_size = 6; 00114 } else if (cinfo->scale_num * 7 >= cinfo->scale_denom * cinfo->block_size) { 00115 /* Provide block_size/7 scaling */ 00116 cinfo->jpeg_width = (JDIMENSION) 00117 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 7L); 00118 cinfo->jpeg_height = (JDIMENSION) 00119 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 7L); 00120 cinfo->min_DCT_h_scaled_size = 7; 00121 cinfo->min_DCT_v_scaled_size = 7; 00122 } else if (cinfo->scale_num * 8 >= cinfo->scale_denom * cinfo->block_size) { 00123 /* Provide block_size/8 scaling */ 00124 cinfo->jpeg_width = (JDIMENSION) 00125 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 8L); 00126 cinfo->jpeg_height = (JDIMENSION) 00127 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 8L); 00128 cinfo->min_DCT_h_scaled_size = 8; 00129 cinfo->min_DCT_v_scaled_size = 8; 00130 } else if (cinfo->scale_num * 9 >= cinfo->scale_denom * cinfo->block_size) { 00131 /* Provide block_size/9 scaling */ 00132 cinfo->jpeg_width = (JDIMENSION) 00133 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 9L); 00134 cinfo->jpeg_height = (JDIMENSION) 00135 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 9L); 00136 cinfo->min_DCT_h_scaled_size = 9; 00137 cinfo->min_DCT_v_scaled_size = 9; 00138 } else if (cinfo->scale_num * 10 >= cinfo->scale_denom * cinfo->block_size) { 00139 /* Provide block_size/10 scaling */ 00140 cinfo->jpeg_width = (JDIMENSION) 00141 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 10L); 00142 cinfo->jpeg_height = (JDIMENSION) 00143 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 10L); 00144 cinfo->min_DCT_h_scaled_size = 10; 00145 cinfo->min_DCT_v_scaled_size = 10; 00146 } else if (cinfo->scale_num * 11 >= cinfo->scale_denom * cinfo->block_size) { 00147 /* Provide block_size/11 scaling */ 00148 cinfo->jpeg_width = (JDIMENSION) 00149 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 11L); 00150 cinfo->jpeg_height = (JDIMENSION) 00151 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 11L); 00152 cinfo->min_DCT_h_scaled_size = 11; 00153 cinfo->min_DCT_v_scaled_size = 11; 00154 } else if (cinfo->scale_num * 12 >= cinfo->scale_denom * cinfo->block_size) { 00155 /* Provide block_size/12 scaling */ 00156 cinfo->jpeg_width = (JDIMENSION) 00157 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 12L); 00158 cinfo->jpeg_height = (JDIMENSION) 00159 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 12L); 00160 cinfo->min_DCT_h_scaled_size = 12; 00161 cinfo->min_DCT_v_scaled_size = 12; 00162 } else if (cinfo->scale_num * 13 >= cinfo->scale_denom * cinfo->block_size) { 00163 /* Provide block_size/13 scaling */ 00164 cinfo->jpeg_width = (JDIMENSION) 00165 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 13L); 00166 cinfo->jpeg_height = (JDIMENSION) 00167 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 13L); 00168 cinfo->min_DCT_h_scaled_size = 13; 00169 cinfo->min_DCT_v_scaled_size = 13; 00170 } else if (cinfo->scale_num * 14 >= cinfo->scale_denom * cinfo->block_size) { 00171 /* Provide block_size/14 scaling */ 00172 cinfo->jpeg_width = (JDIMENSION) 00173 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 14L); 00174 cinfo->jpeg_height = (JDIMENSION) 00175 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 14L); 00176 cinfo->min_DCT_h_scaled_size = 14; 00177 cinfo->min_DCT_v_scaled_size = 14; 00178 } else if (cinfo->scale_num * 15 >= cinfo->scale_denom * cinfo->block_size) { 00179 /* Provide block_size/15 scaling */ 00180 cinfo->jpeg_width = (JDIMENSION) 00181 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 15L); 00182 cinfo->jpeg_height = (JDIMENSION) 00183 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 15L); 00184 cinfo->min_DCT_h_scaled_size = 15; 00185 cinfo->min_DCT_v_scaled_size = 15; 00186 } else { 00187 /* Provide block_size/16 scaling */ 00188 cinfo->jpeg_width = (JDIMENSION) 00189 jdiv_round_up((long) cinfo->image_width * cinfo->block_size, 16L); 00190 cinfo->jpeg_height = (JDIMENSION) 00191 jdiv_round_up((long) cinfo->image_height * cinfo->block_size, 16L); 00192 cinfo->min_DCT_h_scaled_size = 16; 00193 cinfo->min_DCT_v_scaled_size = 16; 00194 } 00195 00196 #else /* !DCT_SCALING_SUPPORTED */ 00197 00198 /* Hardwire it to "no scaling" */ 00199 cinfo->jpeg_width = cinfo->image_width; 00200 cinfo->jpeg_height = cinfo->image_height; 00201 cinfo->min_DCT_h_scaled_size = DCTSIZE; 00202 cinfo->min_DCT_v_scaled_size = DCTSIZE; 00203 00204 #endif /* DCT_SCALING_SUPPORTED */ 00205 } 00206 00207 00208 LOCAL(void) 00209 jpeg_calc_trans_dimensions (j_compress_ptr cinfo) 00210 { 00211 if (cinfo->min_DCT_h_scaled_size != cinfo->min_DCT_v_scaled_size) 00212 ERREXIT2(cinfo, JERR_BAD_DCTSIZE, 00213 cinfo->min_DCT_h_scaled_size, cinfo->min_DCT_v_scaled_size); 00214 00215 cinfo->block_size = cinfo->min_DCT_h_scaled_size; 00216 } 00217 00218 00219 LOCAL(void) 00220 initial_setup (j_compress_ptr cinfo, boolean transcode_only) 00221 /* Do computations that are needed before master selection phase */ 00222 { 00223 int ci, ssize; 00224 jpeg_component_info *compptr; 00225 long samplesperrow; 00226 JDIMENSION jd_samplesperrow; 00227 00228 if (transcode_only) 00229 jpeg_calc_trans_dimensions(cinfo); 00230 else 00231 jpeg_calc_jpeg_dimensions(cinfo); 00232 00233 /* Sanity check on block_size */ 00234 if (cinfo->block_size < 1 || cinfo->block_size > 16) 00235 ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size); 00236 00237 /* Derive natural_order from block_size */ 00238 switch (cinfo->block_size) { 00239 case 2: cinfo->natural_order = jpeg_natural_order2; break; 00240 case 3: cinfo->natural_order = jpeg_natural_order3; break; 00241 case 4: cinfo->natural_order = jpeg_natural_order4; break; 00242 case 5: cinfo->natural_order = jpeg_natural_order5; break; 00243 case 6: cinfo->natural_order = jpeg_natural_order6; break; 00244 case 7: cinfo->natural_order = jpeg_natural_order7; break; 00245 default: cinfo->natural_order = jpeg_natural_order; break; 00246 } 00247 00248 /* Derive lim_Se from block_size */ 00249 cinfo->lim_Se = cinfo->block_size < DCTSIZE ? 00250 cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; 00251 00252 /* Sanity check on image dimensions */ 00253 if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || 00254 cinfo->num_components <= 0 || cinfo->input_components <= 0) 00255 ERREXIT(cinfo, JERR_EMPTY_IMAGE); 00256 00257 /* Make sure image isn't bigger than I can handle */ 00258 if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || 00259 (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) 00260 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 00261 00262 /* Width of an input scanline must be representable as JDIMENSION. */ 00263 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; 00264 jd_samplesperrow = (JDIMENSION) samplesperrow; 00265 if ((long) jd_samplesperrow != samplesperrow) 00266 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 00267 00268 /* For now, precision must match compiled-in value... */ 00269 if (cinfo->data_precision != BITS_IN_JSAMPLE) 00270 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 00271 00272 /* Check that number of components won't exceed internal array sizes */ 00273 if (cinfo->num_components > MAX_COMPONENTS) 00274 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 00275 MAX_COMPONENTS); 00276 00277 /* Compute maximum sampling factors; check factor validity */ 00278 cinfo->max_h_samp_factor = 1; 00279 cinfo->max_v_samp_factor = 1; 00280 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 00281 ci++, compptr++) { 00282 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 00283 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 00284 ERREXIT(cinfo, JERR_BAD_SAMPLING); 00285 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 00286 compptr->h_samp_factor); 00287 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 00288 compptr->v_samp_factor); 00289 } 00290 00291 /* Compute dimensions of components */ 00292 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 00293 ci++, compptr++) { 00294 /* Fill in the correct component_index value; don't rely on application */ 00295 compptr->component_index = ci; 00296 /* In selecting the actual DCT scaling for each component, we try to 00297 * scale down the chroma components via DCT scaling rather than downsampling. 00298 * This saves time if the downsampler gets to use 1:1 scaling. 00299 * Note this code adapts subsampling ratios which are powers of 2. 00300 */ 00301 ssize = 1; 00302 #ifdef DCT_SCALING_SUPPORTED 00303 while (cinfo->min_DCT_h_scaled_size * ssize <= 00304 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && 00305 (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == 0) { 00306 ssize = ssize * 2; 00307 } 00308 #endif 00309 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; 00310 ssize = 1; 00311 #ifdef DCT_SCALING_SUPPORTED 00312 while (cinfo->min_DCT_v_scaled_size * ssize <= 00313 (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && 00314 (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == 0) { 00315 ssize = ssize * 2; 00316 } 00317 #endif 00318 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; 00319 00320 /* We don't support DCT ratios larger than 2. */ 00321 if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) 00322 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; 00323 else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) 00324 compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; 00325 00326 /* Size in DCT blocks */ 00327 compptr->width_in_blocks = (JDIMENSION) 00328 jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, 00329 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 00330 compptr->height_in_blocks = (JDIMENSION) 00331 jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, 00332 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 00333 /* Size in samples */ 00334 compptr->downsampled_width = (JDIMENSION) 00335 jdiv_round_up((long) cinfo->jpeg_width * 00336 (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), 00337 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 00338 compptr->downsampled_height = (JDIMENSION) 00339 jdiv_round_up((long) cinfo->jpeg_height * 00340 (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), 00341 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 00342 /* Mark component needed (this flag isn't actually used for compression) */ 00343 compptr->component_needed = TRUE; 00344 } 00345 00346 /* Compute number of fully interleaved MCU rows (number of times that 00347 * main controller will call coefficient controller). 00348 */ 00349 cinfo->total_iMCU_rows = (JDIMENSION) 00350 jdiv_round_up((long) cinfo->jpeg_height, 00351 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 00352 } 00353 00354 00355 #ifdef C_MULTISCAN_FILES_SUPPORTED 00356 00357 LOCAL(void) 00358 validate_script (j_compress_ptr cinfo) 00359 /* Verify that the scan script in cinfo->scan_info[] is valid; also 00360 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. 00361 */ 00362 { 00363 const jpeg_scan_info * scanptr; 00364 int scanno, ncomps, ci, coefi, thisi; 00365 int Ss, Se, Ah, Al; 00366 boolean component_sent[MAX_COMPONENTS]; 00367 #ifdef C_PROGRESSIVE_SUPPORTED 00368 int * last_bitpos_ptr; 00369 int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; 00370 /* -1 until that coefficient has been seen; then last Al for it */ 00371 #endif 00372 00373 if (cinfo->num_scans <= 0) 00374 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); 00375 00376 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; 00377 * for progressive JPEG, no scan can have this. 00378 */ 00379 scanptr = cinfo->scan_info; 00380 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { 00381 #ifdef C_PROGRESSIVE_SUPPORTED 00382 cinfo->progressive_mode = TRUE; 00383 last_bitpos_ptr = & last_bitpos[0][0]; 00384 for (ci = 0; ci < cinfo->num_components; ci++) 00385 for (coefi = 0; coefi < DCTSIZE2; coefi++) 00386 *last_bitpos_ptr++ = -1; 00387 #else 00388 ERREXIT(cinfo, JERR_NOT_COMPILED); 00389 #endif 00390 } else { 00391 cinfo->progressive_mode = FALSE; 00392 for (ci = 0; ci < cinfo->num_components; ci++) 00393 component_sent[ci] = FALSE; 00394 } 00395 00396 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { 00397 /* Validate component indexes */ 00398 ncomps = scanptr->comps_in_scan; 00399 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) 00400 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); 00401 for (ci = 0; ci < ncomps; ci++) { 00402 thisi = scanptr->component_index[ci]; 00403 if (thisi < 0 || thisi >= cinfo->num_components) 00404 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 00405 /* Components must appear in SOF order within each scan */ 00406 if (ci > 0 && thisi <= scanptr->component_index[ci-1]) 00407 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 00408 } 00409 /* Validate progression parameters */ 00410 Ss = scanptr->Ss; 00411 Se = scanptr->Se; 00412 Ah = scanptr->Ah; 00413 Al = scanptr->Al; 00414 if (cinfo->progressive_mode) { 00415 #ifdef C_PROGRESSIVE_SUPPORTED 00416 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that 00417 * seems wrong: the upper bound ought to depend on data precision. 00418 * Perhaps they really meant 0..N+1 for N-bit precision. 00419 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in 00420 * out-of-range reconstructed DC values during the first DC scan, 00421 * which might cause problems for some decoders. 00422 */ 00423 #if BITS_IN_JSAMPLE == 8 00424 #define MAX_AH_AL 10 00425 #else 00426 #define MAX_AH_AL 13 00427 #endif 00428 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || 00429 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) 00430 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00431 if (Ss == 0) { 00432 if (Se != 0) /* DC and AC together not OK */ 00433 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00434 } else { 00435 if (ncomps != 1) /* AC scans must be for only one component */ 00436 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00437 } 00438 for (ci = 0; ci < ncomps; ci++) { 00439 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; 00440 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ 00441 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00442 for (coefi = Ss; coefi <= Se; coefi++) { 00443 if (last_bitpos_ptr[coefi] < 0) { 00444 /* first scan of this coefficient */ 00445 if (Ah != 0) 00446 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00447 } else { 00448 /* not first scan */ 00449 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) 00450 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00451 } 00452 last_bitpos_ptr[coefi] = Al; 00453 } 00454 } 00455 #endif 00456 } else { 00457 /* For sequential JPEG, all progression parameters must be these: */ 00458 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) 00459 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); 00460 /* Make sure components are not sent twice */ 00461 for (ci = 0; ci < ncomps; ci++) { 00462 thisi = scanptr->component_index[ci]; 00463 if (component_sent[thisi]) 00464 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); 00465 component_sent[thisi] = TRUE; 00466 } 00467 } 00468 } 00469 00470 /* Now verify that everything got sent. */ 00471 if (cinfo->progressive_mode) { 00472 #ifdef C_PROGRESSIVE_SUPPORTED 00473 /* For progressive mode, we only check that at least some DC data 00474 * got sent for each component; the spec does not require that all bits 00475 * of all coefficients be transmitted. Would it be wiser to enforce 00476 * transmission of all coefficient bits?? 00477 */ 00478 for (ci = 0; ci < cinfo->num_components; ci++) { 00479 if (last_bitpos[ci][0] < 0) 00480 ERREXIT(cinfo, JERR_MISSING_DATA); 00481 } 00482 #endif 00483 } else { 00484 for (ci = 0; ci < cinfo->num_components; ci++) { 00485 if (! component_sent[ci]) 00486 ERREXIT(cinfo, JERR_MISSING_DATA); 00487 } 00488 } 00489 } 00490 00491 00492 LOCAL(void) 00493 reduce_script (j_compress_ptr cinfo) 00494 /* Adapt scan script for use with reduced block size; 00495 * assume that script has been validated before. 00496 */ 00497 { 00498 jpeg_scan_info * scanptr; 00499 int idxout, idxin; 00500 00501 /* Circumvent const declaration for this function */ 00502 scanptr = (jpeg_scan_info *) cinfo->scan_info; 00503 idxout = 0; 00504 00505 for (idxin = 0; idxin < cinfo->num_scans; idxin++) { 00506 /* After skipping, idxout becomes smaller than idxin */ 00507 if (idxin != idxout) 00508 /* Copy rest of data; 00509 * note we stay in given chunk of allocated memory. 00510 */ 00511 scanptr[idxout] = scanptr[idxin]; 00512 if (scanptr[idxout].Ss > cinfo->lim_Se) 00513 /* Entire scan out of range - skip this entry */ 00514 continue; 00515 if (scanptr[idxout].Se > cinfo->lim_Se) 00516 /* Limit scan to end of block */ 00517 scanptr[idxout].Se = cinfo->lim_Se; 00518 idxout++; 00519 } 00520 00521 cinfo->num_scans = idxout; 00522 } 00523 00524 #endif /* C_MULTISCAN_FILES_SUPPORTED */ 00525 00526 00527 LOCAL(void) 00528 select_scan_parameters (j_compress_ptr cinfo) 00529 /* Set up the scan parameters for the current scan */ 00530 { 00531 int ci; 00532 00533 #ifdef C_MULTISCAN_FILES_SUPPORTED 00534 if (cinfo->scan_info != NULL) { 00535 /* Prepare for current scan --- the script is already validated */ 00536 my_master_ptr master = (my_master_ptr) cinfo->master; 00537 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; 00538 00539 cinfo->comps_in_scan = scanptr->comps_in_scan; 00540 for (ci = 0; ci < scanptr->comps_in_scan; ci++) { 00541 cinfo->cur_comp_info[ci] = 00542 &cinfo->comp_info[scanptr->component_index[ci]]; 00543 } 00544 if (cinfo->progressive_mode) { 00545 cinfo->Ss = scanptr->Ss; 00546 cinfo->Se = scanptr->Se; 00547 cinfo->Ah = scanptr->Ah; 00548 cinfo->Al = scanptr->Al; 00549 return; 00550 } 00551 } 00552 else 00553 #endif 00554 { 00555 /* Prepare for single sequential-JPEG scan containing all components */ 00556 if (cinfo->num_components > MAX_COMPS_IN_SCAN) 00557 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 00558 MAX_COMPS_IN_SCAN); 00559 cinfo->comps_in_scan = cinfo->num_components; 00560 for (ci = 0; ci < cinfo->num_components; ci++) { 00561 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; 00562 } 00563 } 00564 cinfo->Ss = 0; 00565 cinfo->Se = cinfo->block_size * cinfo->block_size - 1; 00566 cinfo->Ah = 0; 00567 cinfo->Al = 0; 00568 } 00569 00570 00571 LOCAL(void) 00572 per_scan_setup (j_compress_ptr cinfo) 00573 /* Do computations that are needed before processing a JPEG scan */ 00574 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ 00575 { 00576 int ci, mcublks, tmp; 00577 jpeg_component_info *compptr; 00578 00579 if (cinfo->comps_in_scan == 1) { 00580 00581 /* Noninterleaved (single-component) scan */ 00582 compptr = cinfo->cur_comp_info[0]; 00583 00584 /* Overall image size in MCUs */ 00585 cinfo->MCUs_per_row = compptr->width_in_blocks; 00586 cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 00587 00588 /* For noninterleaved scan, always one block per MCU */ 00589 compptr->MCU_width = 1; 00590 compptr->MCU_height = 1; 00591 compptr->MCU_blocks = 1; 00592 compptr->MCU_sample_width = compptr->DCT_h_scaled_size; 00593 compptr->last_col_width = 1; 00594 /* For noninterleaved scans, it is convenient to define last_row_height 00595 * as the number of block rows present in the last iMCU row. 00596 */ 00597 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 00598 if (tmp == 0) tmp = compptr->v_samp_factor; 00599 compptr->last_row_height = tmp; 00600 00601 /* Prepare array describing MCU composition */ 00602 cinfo->blocks_in_MCU = 1; 00603 cinfo->MCU_membership[0] = 0; 00604 00605 } else { 00606 00607 /* Interleaved (multi-component) scan */ 00608 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 00609 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 00610 MAX_COMPS_IN_SCAN); 00611 00612 /* Overall image size in MCUs */ 00613 cinfo->MCUs_per_row = (JDIMENSION) 00614 jdiv_round_up((long) cinfo->jpeg_width, 00615 (long) (cinfo->max_h_samp_factor * cinfo->block_size)); 00616 cinfo->MCU_rows_in_scan = (JDIMENSION) 00617 jdiv_round_up((long) cinfo->jpeg_height, 00618 (long) (cinfo->max_v_samp_factor * cinfo->block_size)); 00619 00620 cinfo->blocks_in_MCU = 0; 00621 00622 for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 00623 compptr = cinfo->cur_comp_info[ci]; 00624 /* Sampling factors give # of blocks of component in each MCU */ 00625 compptr->MCU_width = compptr->h_samp_factor; 00626 compptr->MCU_height = compptr->v_samp_factor; 00627 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 00628 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; 00629 /* Figure number of non-dummy blocks in last MCU column & row */ 00630 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 00631 if (tmp == 0) tmp = compptr->MCU_width; 00632 compptr->last_col_width = tmp; 00633 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 00634 if (tmp == 0) tmp = compptr->MCU_height; 00635 compptr->last_row_height = tmp; 00636 /* Prepare array describing MCU composition */ 00637 mcublks = compptr->MCU_blocks; 00638 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) 00639 ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 00640 while (mcublks-- > 0) { 00641 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 00642 } 00643 } 00644 00645 } 00646 00647 /* Convert restart specified in rows to actual MCU count. */ 00648 /* Note that count must fit in 16 bits, so we provide limiting. */ 00649 if (cinfo->restart_in_rows > 0) { 00650 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; 00651 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); 00652 } 00653 } 00654 00655 00656 /* 00657 * Per-pass setup. 00658 * This is called at the beginning of each pass. We determine which modules 00659 * will be active during this pass and give them appropriate start_pass calls. 00660 * We also set is_last_pass to indicate whether any more passes will be 00661 * required. 00662 */ 00663 00664 METHODDEF(void) 00665 prepare_for_pass (j_compress_ptr cinfo) 00666 { 00667 my_master_ptr master = (my_master_ptr) cinfo->master; 00668 00669 switch (master->pass_type) { 00670 case main_pass: 00671 /* Initial pass: will collect input data, and do either Huffman 00672 * optimization or data output for the first scan. 00673 */ 00674 select_scan_parameters(cinfo); 00675 per_scan_setup(cinfo); 00676 if (! cinfo->raw_data_in) { 00677 (*cinfo->cconvert->start_pass) (cinfo); 00678 (*cinfo->downsample->start_pass) (cinfo); 00679 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); 00680 } 00681 (*cinfo->fdct->start_pass) (cinfo); 00682 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); 00683 (*cinfo->coef->start_pass) (cinfo, 00684 (master->total_passes > 1 ? 00685 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); 00686 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); 00687 if (cinfo->optimize_coding) { 00688 /* No immediate data output; postpone writing frame/scan headers */ 00689 master->pub.call_pass_startup = FALSE; 00690 } else { 00691 /* Will write frame/scan headers at first jpeg_write_scanlines call */ 00692 master->pub.call_pass_startup = TRUE; 00693 } 00694 break; 00695 #ifdef ENTROPY_OPT_SUPPORTED 00696 case huff_opt_pass: 00697 /* Do Huffman optimization for a scan after the first one. */ 00698 select_scan_parameters(cinfo); 00699 per_scan_setup(cinfo); 00700 if (cinfo->Ss != 0 || cinfo->Ah == 0) { 00701 (*cinfo->entropy->start_pass) (cinfo, TRUE); 00702 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 00703 master->pub.call_pass_startup = FALSE; 00704 break; 00705 } 00706 /* Special case: Huffman DC refinement scans need no Huffman table 00707 * and therefore we can skip the optimization pass for them. 00708 */ 00709 master->pass_type = output_pass; 00710 master->pass_number++; 00711 /*FALLTHROUGH*/ 00712 #endif 00713 case output_pass: 00714 /* Do a data-output pass. */ 00715 /* We need not repeat per-scan setup if prior optimization pass did it. */ 00716 if (! cinfo->optimize_coding) { 00717 select_scan_parameters(cinfo); 00718 per_scan_setup(cinfo); 00719 } 00720 (*cinfo->entropy->start_pass) (cinfo, FALSE); 00721 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); 00722 /* We emit frame/scan headers now */ 00723 if (master->scan_number == 0) 00724 (*cinfo->marker->write_frame_header) (cinfo); 00725 (*cinfo->marker->write_scan_header) (cinfo); 00726 master->pub.call_pass_startup = FALSE; 00727 break; 00728 default: 00729 ERREXIT(cinfo, JERR_NOT_COMPILED); 00730 } 00731 00732 master->pub.is_last_pass = (master->pass_number == master->total_passes-1); 00733 00734 /* Set up progress monitor's pass info if present */ 00735 if (cinfo->progress != NULL) { 00736 cinfo->progress->completed_passes = master->pass_number; 00737 cinfo->progress->total_passes = master->total_passes; 00738 } 00739 } 00740 00741 00742 /* 00743 * Special start-of-pass hook. 00744 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. 00745 * In single-pass processing, we need this hook because we don't want to 00746 * write frame/scan headers during jpeg_start_compress; we want to let the 00747 * application write COM markers etc. between jpeg_start_compress and the 00748 * jpeg_write_scanlines loop. 00749 * In multi-pass processing, this routine is not used. 00750 */ 00751 00752 METHODDEF(void) 00753 pass_startup (j_compress_ptr cinfo) 00754 { 00755 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ 00756 00757 (*cinfo->marker->write_frame_header) (cinfo); 00758 (*cinfo->marker->write_scan_header) (cinfo); 00759 } 00760 00761 00762 /* 00763 * Finish up at end of pass. 00764 */ 00765 00766 METHODDEF(void) 00767 finish_pass_master (j_compress_ptr cinfo) 00768 { 00769 my_master_ptr master = (my_master_ptr) cinfo->master; 00770 00771 /* The entropy coder always needs an end-of-pass call, 00772 * either to analyze statistics or to flush its output buffer. 00773 */ 00774 (*cinfo->entropy->finish_pass) (cinfo); 00775 00776 /* Update state for next pass */ 00777 switch (master->pass_type) { 00778 case main_pass: 00779 /* next pass is either output of scan 0 (after optimization) 00780 * or output of scan 1 (if no optimization). 00781 */ 00782 master->pass_type = output_pass; 00783 if (! cinfo->optimize_coding) 00784 master->scan_number++; 00785 break; 00786 case huff_opt_pass: 00787 /* next pass is always output of current scan */ 00788 master->pass_type = output_pass; 00789 break; 00790 case output_pass: 00791 /* next pass is either optimization or output of next scan */ 00792 if (cinfo->optimize_coding) 00793 master->pass_type = huff_opt_pass; 00794 master->scan_number++; 00795 break; 00796 } 00797 00798 master->pass_number++; 00799 } 00800 00801 00802 /* 00803 * Initialize master compression control. 00804 */ 00805 00806 GLOBAL(void) 00807 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) 00808 { 00809 my_master_ptr master; 00810 00811 master = (my_master_ptr) 00812 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00813 SIZEOF(my_comp_master)); 00814 cinfo->master = (struct jpeg_comp_master *) master; 00815 master->pub.prepare_for_pass = prepare_for_pass; 00816 master->pub.pass_startup = pass_startup; 00817 master->pub.finish_pass = finish_pass_master; 00818 master->pub.is_last_pass = FALSE; 00819 00820 /* Validate parameters, determine derived values */ 00821 initial_setup(cinfo, transcode_only); 00822 00823 if (cinfo->scan_info != NULL) { 00824 #ifdef C_MULTISCAN_FILES_SUPPORTED 00825 validate_script(cinfo); 00826 if (cinfo->block_size < DCTSIZE) 00827 reduce_script(cinfo); 00828 #else 00829 ERREXIT(cinfo, JERR_NOT_COMPILED); 00830 #endif 00831 } else { 00832 cinfo->progressive_mode = FALSE; 00833 cinfo->num_scans = 1; 00834 } 00835 00836 if ((cinfo->progressive_mode || cinfo->block_size < DCTSIZE) && 00837 !cinfo->arith_code) /* TEMPORARY HACK ??? */ 00838 /* assume default tables no good for progressive or downscale mode */ 00839 cinfo->optimize_coding = TRUE; 00840 00841 /* Initialize my private state */ 00842 if (transcode_only) { 00843 /* no main pass in transcoding */ 00844 if (cinfo->optimize_coding) 00845 master->pass_type = huff_opt_pass; 00846 else 00847 master->pass_type = output_pass; 00848 } else { 00849 /* for normal compression, first pass is always this type: */ 00850 master->pass_type = main_pass; 00851 } 00852 master->scan_number = 0; 00853 master->pass_number = 0; 00854 if (cinfo->optimize_coding) 00855 master->total_passes = cinfo->num_scans * 2; 00856 else 00857 master->total_passes = cinfo->num_scans; 00858 } Generated on Mon May 28 2012 04:19:09 for ReactOS by
1.7.6.1
|