Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 1 | /* |
| 2 | * jcmaster.c |
| 3 | * |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 4 | * Copyright (C) 1991-1997, Thomas G. Lane. |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 5 | * Modified 2003-2010 by Guido Vollbeding. |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 6 | * Copyright (C) 2010, D. R. Commander. |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 7 | * This file is part of the Independent JPEG Group's software. |
| 8 | * For conditions of distribution and use, see the accompanying README file. |
| 9 | * |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 10 | * This file contains master control logic for the JPEG compressor. |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 11 | * These routines are concerned with parameter validation, initial setup, |
| 12 | * and inter-pass control (determining the number of passes and the work |
| 13 | * to be done in each pass). |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 14 | */ |
| 15 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 16 | #define JPEG_INTERNALS |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 17 | #include "jinclude.h" |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 18 | #include "jpeglib.h" |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 19 | #include "jpegcomp.h" |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 20 | |
| 21 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 22 | /* Private state */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 23 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 24 | typedef enum { |
| 25 | main_pass, /* input data, also do first output step */ |
| 26 | huff_opt_pass, /* Huffman code optimization pass */ |
| 27 | output_pass /* data output pass */ |
| 28 | } c_pass_type; |
| 29 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 30 | typedef struct { |
| 31 | struct jpeg_comp_master pub; /* public fields */ |
| 32 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 33 | c_pass_type pass_type; /* the type of the current pass */ |
| 34 | |
| 35 | int pass_number; /* # of passes completed */ |
| 36 | int total_passes; /* total # of passes needed */ |
| 37 | |
| 38 | int scan_number; /* current index in scan_info[] */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 39 | } my_comp_master; |
| 40 | |
| 41 | typedef my_comp_master * my_master_ptr; |
| 42 | |
| 43 | |
| 44 | /* |
| 45 | * Support routines that do various essential calculations. |
| 46 | */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 47 | |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 48 | #if JPEG_LIB_VERSION >= 70 |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 49 | /* |
| 50 | * Compute JPEG image dimensions and related values. |
| 51 | * NOTE: this is exported for possible use by application. |
| 52 | * Hence it mustn't do anything that can't be done twice. |
| 53 | */ |
| 54 | |
| 55 | GLOBAL(void) |
| 56 | jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo) |
| 57 | /* Do computations that are needed before master selection phase */ |
| 58 | { |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 59 | /* Hardwire it to "no scaling" */ |
| 60 | cinfo->jpeg_width = cinfo->image_width; |
| 61 | cinfo->jpeg_height = cinfo->image_height; |
| 62 | cinfo->min_DCT_h_scaled_size = DCTSIZE; |
| 63 | cinfo->min_DCT_v_scaled_size = DCTSIZE; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 64 | } |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 65 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 66 | |
| 67 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 68 | LOCAL(void) |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 69 | initial_setup (j_compress_ptr cinfo, boolean transcode_only) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 70 | /* Do computations that are needed before master selection phase */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 71 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 72 | int ci; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 73 | jpeg_component_info *compptr; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 74 | long samplesperrow; |
| 75 | JDIMENSION jd_samplesperrow; |
| 76 | |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 77 | #if JPEG_LIB_VERSION >= 70 |
DRC | e7fde87 | 2011-04-03 07:09:49 +0000 | [diff] [blame] | 78 | #if JPEG_LIB_VERSION >= 80 |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 79 | if (!transcode_only) |
DRC | e7fde87 | 2011-04-03 07:09:49 +0000 | [diff] [blame] | 80 | #endif |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 81 | jpeg_calc_jpeg_dimensions(cinfo); |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 82 | #endif |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 83 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 84 | /* Sanity check on image dimensions */ |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 85 | if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0 |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 86 | || cinfo->num_components <= 0 || cinfo->input_components <= 0) |
| 87 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
| 88 | |
| 89 | /* Make sure image isn't bigger than I can handle */ |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 90 | if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION || |
| 91 | (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 92 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); |
| 93 | |
| 94 | /* Width of an input scanline must be representable as JDIMENSION. */ |
| 95 | samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components; |
| 96 | jd_samplesperrow = (JDIMENSION) samplesperrow; |
| 97 | if ((long) jd_samplesperrow != samplesperrow) |
| 98 | ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
| 99 | |
| 100 | /* For now, precision must match compiled-in value... */ |
| 101 | if (cinfo->data_precision != BITS_IN_JSAMPLE) |
| 102 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
| 103 | |
| 104 | /* Check that number of components won't exceed internal array sizes */ |
| 105 | if (cinfo->num_components > MAX_COMPONENTS) |
| 106 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
| 107 | MAX_COMPONENTS); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 108 | |
| 109 | /* Compute maximum sampling factors; check factor validity */ |
| 110 | cinfo->max_h_samp_factor = 1; |
| 111 | cinfo->max_v_samp_factor = 1; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 112 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 113 | ci++, compptr++) { |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 114 | if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || |
| 115 | compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 116 | ERREXIT(cinfo, JERR_BAD_SAMPLING); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 117 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, |
| 118 | compptr->h_samp_factor); |
| 119 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, |
| 120 | compptr->v_samp_factor); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 123 | /* Compute dimensions of components */ |
| 124 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
| 125 | ci++, compptr++) { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 126 | /* Fill in the correct component_index value; don't rely on application */ |
| 127 | compptr->component_index = ci; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 128 | /* For compression, we never do DCT scaling. */ |
DRC | 36a6eec | 2010-10-08 08:05:44 +0000 | [diff] [blame] | 129 | #if JPEG_LIB_VERSION >= 70 |
| 130 | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE; |
| 131 | #else |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 132 | compptr->DCT_scaled_size = DCTSIZE; |
Guido Vollbeding | 5996a25 | 2009-06-27 00:00:00 +0000 | [diff] [blame] | 133 | #endif |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 134 | /* Size in DCT blocks */ |
| 135 | compptr->width_in_blocks = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 136 | jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 137 | (long) (cinfo->max_h_samp_factor * DCTSIZE)); |
| 138 | compptr->height_in_blocks = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 139 | jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 140 | (long) (cinfo->max_v_samp_factor * DCTSIZE)); |
| 141 | /* Size in samples */ |
| 142 | compptr->downsampled_width = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 143 | jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 144 | (long) cinfo->max_h_samp_factor); |
| 145 | compptr->downsampled_height = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 146 | jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 147 | (long) cinfo->max_v_samp_factor); |
| 148 | /* Mark component needed (this flag isn't actually used for compression) */ |
| 149 | compptr->component_needed = TRUE; |
| 150 | } |
| 151 | |
| 152 | /* Compute number of fully interleaved MCU rows (number of times that |
| 153 | * main controller will call coefficient controller). |
| 154 | */ |
| 155 | cinfo->total_iMCU_rows = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 156 | jdiv_round_up((long) cinfo->_jpeg_height, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 157 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
| 158 | } |
| 159 | |
| 160 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 161 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 162 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 163 | LOCAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 164 | validate_script (j_compress_ptr cinfo) |
| 165 | /* Verify that the scan script in cinfo->scan_info[] is valid; also |
| 166 | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. |
| 167 | */ |
| 168 | { |
| 169 | const jpeg_scan_info * scanptr; |
| 170 | int scanno, ncomps, ci, coefi, thisi; |
| 171 | int Ss, Se, Ah, Al; |
| 172 | boolean component_sent[MAX_COMPONENTS]; |
| 173 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 174 | int * last_bitpos_ptr; |
| 175 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; |
| 176 | /* -1 until that coefficient has been seen; then last Al for it */ |
| 177 | #endif |
| 178 | |
| 179 | if (cinfo->num_scans <= 0) |
| 180 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); |
| 181 | |
| 182 | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; |
| 183 | * for progressive JPEG, no scan can have this. |
| 184 | */ |
| 185 | scanptr = cinfo->scan_info; |
| 186 | if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { |
| 187 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 188 | cinfo->progressive_mode = TRUE; |
| 189 | last_bitpos_ptr = & last_bitpos[0][0]; |
| 190 | for (ci = 0; ci < cinfo->num_components; ci++) |
| 191 | for (coefi = 0; coefi < DCTSIZE2; coefi++) |
| 192 | *last_bitpos_ptr++ = -1; |
| 193 | #else |
| 194 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 195 | #endif |
| 196 | } else { |
| 197 | cinfo->progressive_mode = FALSE; |
| 198 | for (ci = 0; ci < cinfo->num_components; ci++) |
| 199 | component_sent[ci] = FALSE; |
| 200 | } |
| 201 | |
| 202 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { |
| 203 | /* Validate component indexes */ |
| 204 | ncomps = scanptr->comps_in_scan; |
| 205 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) |
| 206 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); |
| 207 | for (ci = 0; ci < ncomps; ci++) { |
| 208 | thisi = scanptr->component_index[ci]; |
| 209 | if (thisi < 0 || thisi >= cinfo->num_components) |
| 210 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 211 | /* Components must appear in SOF order within each scan */ |
| 212 | if (ci > 0 && thisi <= scanptr->component_index[ci-1]) |
| 213 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 214 | } |
| 215 | /* Validate progression parameters */ |
| 216 | Ss = scanptr->Ss; |
| 217 | Se = scanptr->Se; |
| 218 | Ah = scanptr->Ah; |
| 219 | Al = scanptr->Al; |
| 220 | if (cinfo->progressive_mode) { |
| 221 | #ifdef C_PROGRESSIVE_SUPPORTED |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 222 | /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that |
| 223 | * seems wrong: the upper bound ought to depend on data precision. |
| 224 | * Perhaps they really meant 0..N+1 for N-bit precision. |
| 225 | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in |
| 226 | * out-of-range reconstructed DC values during the first DC scan, |
| 227 | * which might cause problems for some decoders. |
| 228 | */ |
| 229 | #if BITS_IN_JSAMPLE == 8 |
| 230 | #define MAX_AH_AL 10 |
| 231 | #else |
| 232 | #define MAX_AH_AL 13 |
| 233 | #endif |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 234 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || |
Thomas G. Lane | 5ead57a | 1998-03-27 00:00:00 +0000 | [diff] [blame] | 235 | Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 236 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 237 | if (Ss == 0) { |
| 238 | if (Se != 0) /* DC and AC together not OK */ |
| 239 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 240 | } else { |
| 241 | if (ncomps != 1) /* AC scans must be for only one component */ |
| 242 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 243 | } |
| 244 | for (ci = 0; ci < ncomps; ci++) { |
| 245 | last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; |
| 246 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ |
| 247 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 248 | for (coefi = Ss; coefi <= Se; coefi++) { |
| 249 | if (last_bitpos_ptr[coefi] < 0) { |
| 250 | /* first scan of this coefficient */ |
| 251 | if (Ah != 0) |
| 252 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 253 | } else { |
| 254 | /* not first scan */ |
| 255 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) |
| 256 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 257 | } |
| 258 | last_bitpos_ptr[coefi] = Al; |
| 259 | } |
| 260 | } |
| 261 | #endif |
| 262 | } else { |
| 263 | /* For sequential JPEG, all progression parameters must be these: */ |
| 264 | if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) |
| 265 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
| 266 | /* Make sure components are not sent twice */ |
| 267 | for (ci = 0; ci < ncomps; ci++) { |
| 268 | thisi = scanptr->component_index[ci]; |
| 269 | if (component_sent[thisi]) |
| 270 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
| 271 | component_sent[thisi] = TRUE; |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
| 276 | /* Now verify that everything got sent. */ |
| 277 | if (cinfo->progressive_mode) { |
| 278 | #ifdef C_PROGRESSIVE_SUPPORTED |
| 279 | /* For progressive mode, we only check that at least some DC data |
| 280 | * got sent for each component; the spec does not require that all bits |
| 281 | * of all coefficients be transmitted. Would it be wiser to enforce |
| 282 | * transmission of all coefficient bits?? |
| 283 | */ |
| 284 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 285 | if (last_bitpos[ci][0] < 0) |
| 286 | ERREXIT(cinfo, JERR_MISSING_DATA); |
| 287 | } |
| 288 | #endif |
| 289 | } else { |
| 290 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 291 | if (! component_sent[ci]) |
| 292 | ERREXIT(cinfo, JERR_MISSING_DATA); |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
| 298 | |
| 299 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 300 | LOCAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 301 | select_scan_parameters (j_compress_ptr cinfo) |
| 302 | /* Set up the scan parameters for the current scan */ |
| 303 | { |
| 304 | int ci; |
| 305 | |
| 306 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 307 | if (cinfo->scan_info != NULL) { |
| 308 | /* Prepare for current scan --- the script is already validated */ |
| 309 | my_master_ptr master = (my_master_ptr) cinfo->master; |
| 310 | const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; |
| 311 | |
| 312 | cinfo->comps_in_scan = scanptr->comps_in_scan; |
| 313 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { |
| 314 | cinfo->cur_comp_info[ci] = |
| 315 | &cinfo->comp_info[scanptr->component_index[ci]]; |
| 316 | } |
| 317 | cinfo->Ss = scanptr->Ss; |
| 318 | cinfo->Se = scanptr->Se; |
| 319 | cinfo->Ah = scanptr->Ah; |
| 320 | cinfo->Al = scanptr->Al; |
| 321 | } |
| 322 | else |
| 323 | #endif |
| 324 | { |
| 325 | /* Prepare for single sequential-JPEG scan containing all components */ |
| 326 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) |
| 327 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
| 328 | MAX_COMPS_IN_SCAN); |
| 329 | cinfo->comps_in_scan = cinfo->num_components; |
| 330 | for (ci = 0; ci < cinfo->num_components; ci++) { |
| 331 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; |
| 332 | } |
| 333 | cinfo->Ss = 0; |
| 334 | cinfo->Se = DCTSIZE2-1; |
| 335 | cinfo->Ah = 0; |
| 336 | cinfo->Al = 0; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 341 | LOCAL(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 342 | per_scan_setup (j_compress_ptr cinfo) |
| 343 | /* Do computations that are needed before processing a JPEG scan */ |
| 344 | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ |
| 345 | { |
| 346 | int ci, mcublks, tmp; |
| 347 | jpeg_component_info *compptr; |
| 348 | |
| 349 | if (cinfo->comps_in_scan == 1) { |
| 350 | |
| 351 | /* Noninterleaved (single-component) scan */ |
| 352 | compptr = cinfo->cur_comp_info[0]; |
| 353 | |
| 354 | /* Overall image size in MCUs */ |
| 355 | cinfo->MCUs_per_row = compptr->width_in_blocks; |
| 356 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; |
| 357 | |
| 358 | /* For noninterleaved scan, always one block per MCU */ |
| 359 | compptr->MCU_width = 1; |
| 360 | compptr->MCU_height = 1; |
| 361 | compptr->MCU_blocks = 1; |
| 362 | compptr->MCU_sample_width = DCTSIZE; |
| 363 | compptr->last_col_width = 1; |
Thomas G. Lane | a8b67c4 | 1995-03-15 00:00:00 +0000 | [diff] [blame] | 364 | /* For noninterleaved scans, it is convenient to define last_row_height |
| 365 | * as the number of block rows present in the last iMCU row. |
| 366 | */ |
| 367 | tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
| 368 | if (tmp == 0) tmp = compptr->v_samp_factor; |
| 369 | compptr->last_row_height = tmp; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 370 | |
| 371 | /* Prepare array describing MCU composition */ |
| 372 | cinfo->blocks_in_MCU = 1; |
| 373 | cinfo->MCU_membership[0] = 0; |
| 374 | |
| 375 | } else { |
| 376 | |
| 377 | /* Interleaved (multi-component) scan */ |
| 378 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) |
| 379 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
| 380 | MAX_COMPS_IN_SCAN); |
| 381 | |
| 382 | /* Overall image size in MCUs */ |
| 383 | cinfo->MCUs_per_row = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 384 | jdiv_round_up((long) cinfo->_jpeg_width, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 385 | (long) (cinfo->max_h_samp_factor*DCTSIZE)); |
| 386 | cinfo->MCU_rows_in_scan = (JDIMENSION) |
DRC | c04bd3c | 2010-10-10 02:15:56 +0000 | [diff] [blame] | 387 | jdiv_round_up((long) cinfo->_jpeg_height, |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 388 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); |
| 389 | |
| 390 | cinfo->blocks_in_MCU = 0; |
| 391 | |
| 392 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
| 393 | compptr = cinfo->cur_comp_info[ci]; |
| 394 | /* Sampling factors give # of blocks of component in each MCU */ |
| 395 | compptr->MCU_width = compptr->h_samp_factor; |
| 396 | compptr->MCU_height = compptr->v_samp_factor; |
| 397 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
| 398 | compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE; |
| 399 | /* Figure number of non-dummy blocks in last MCU column & row */ |
| 400 | tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); |
| 401 | if (tmp == 0) tmp = compptr->MCU_width; |
| 402 | compptr->last_col_width = tmp; |
| 403 | tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); |
| 404 | if (tmp == 0) tmp = compptr->MCU_height; |
| 405 | compptr->last_row_height = tmp; |
| 406 | /* Prepare array describing MCU composition */ |
| 407 | mcublks = compptr->MCU_blocks; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 408 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 409 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); |
| 410 | while (mcublks-- > 0) { |
| 411 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | } |
| 416 | |
| 417 | /* Convert restart specified in rows to actual MCU count. */ |
| 418 | /* Note that count must fit in 16 bits, so we provide limiting. */ |
| 419 | if (cinfo->restart_in_rows > 0) { |
| 420 | long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; |
| 421 | cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 422 | } |
| 423 | } |
| 424 | |
| 425 | |
| 426 | /* |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 427 | * Per-pass setup. |
| 428 | * This is called at the beginning of each pass. We determine which modules |
| 429 | * will be active during this pass and give them appropriate start_pass calls. |
| 430 | * We also set is_last_pass to indicate whether any more passes will be |
| 431 | * required. |
| 432 | */ |
| 433 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 434 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 435 | prepare_for_pass (j_compress_ptr cinfo) |
| 436 | { |
| 437 | my_master_ptr master = (my_master_ptr) cinfo->master; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 438 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 439 | switch (master->pass_type) { |
| 440 | case main_pass: |
| 441 | /* Initial pass: will collect input data, and do either Huffman |
| 442 | * optimization or data output for the first scan. |
| 443 | */ |
| 444 | select_scan_parameters(cinfo); |
| 445 | per_scan_setup(cinfo); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 446 | if (! cinfo->raw_data_in) { |
| 447 | (*cinfo->cconvert->start_pass) (cinfo); |
| 448 | (*cinfo->downsample->start_pass) (cinfo); |
| 449 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); |
| 450 | } |
| 451 | (*cinfo->fdct->start_pass) (cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 452 | (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); |
| 453 | (*cinfo->coef->start_pass) (cinfo, |
| 454 | (master->total_passes > 1 ? |
| 455 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 456 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 457 | if (cinfo->optimize_coding) { |
| 458 | /* No immediate data output; postpone writing frame/scan headers */ |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 459 | master->pub.call_pass_startup = FALSE; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 460 | } else { |
| 461 | /* Will write frame/scan headers at first jpeg_write_scanlines call */ |
| 462 | master->pub.call_pass_startup = TRUE; |
| 463 | } |
| 464 | break; |
| 465 | #ifdef ENTROPY_OPT_SUPPORTED |
| 466 | case huff_opt_pass: |
| 467 | /* Do Huffman optimization for a scan after the first one. */ |
| 468 | select_scan_parameters(cinfo); |
| 469 | per_scan_setup(cinfo); |
| 470 | if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 471 | (*cinfo->entropy->start_pass) (cinfo, TRUE); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 472 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 473 | master->pub.call_pass_startup = FALSE; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 474 | break; |
| 475 | } |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 476 | /* Special case: Huffman DC refinement scans need no Huffman table |
| 477 | * and therefore we can skip the optimization pass for them. |
| 478 | */ |
| 479 | master->pass_type = output_pass; |
| 480 | master->pass_number++; |
| 481 | /*FALLTHROUGH*/ |
| 482 | #endif |
| 483 | case output_pass: |
| 484 | /* Do a data-output pass. */ |
| 485 | /* We need not repeat per-scan setup if prior optimization pass did it. */ |
| 486 | if (! cinfo->optimize_coding) { |
| 487 | select_scan_parameters(cinfo); |
| 488 | per_scan_setup(cinfo); |
| 489 | } |
| 490 | (*cinfo->entropy->start_pass) (cinfo, FALSE); |
| 491 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
| 492 | /* We emit frame/scan headers now */ |
| 493 | if (master->scan_number == 0) |
| 494 | (*cinfo->marker->write_frame_header) (cinfo); |
| 495 | (*cinfo->marker->write_scan_header) (cinfo); |
| 496 | master->pub.call_pass_startup = FALSE; |
| 497 | break; |
| 498 | default: |
| 499 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 502 | master->pub.is_last_pass = (master->pass_number == master->total_passes-1); |
| 503 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 504 | /* Set up progress monitor's pass info if present */ |
| 505 | if (cinfo->progress != NULL) { |
| 506 | cinfo->progress->completed_passes = master->pass_number; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 507 | cinfo->progress->total_passes = master->total_passes; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 508 | } |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | |
| 512 | /* |
| 513 | * Special start-of-pass hook. |
| 514 | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. |
| 515 | * In single-pass processing, we need this hook because we don't want to |
| 516 | * write frame/scan headers during jpeg_start_compress; we want to let the |
| 517 | * application write COM markers etc. between jpeg_start_compress and the |
| 518 | * jpeg_write_scanlines loop. |
| 519 | * In multi-pass processing, this routine is not used. |
| 520 | */ |
| 521 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 522 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 523 | pass_startup (j_compress_ptr cinfo) |
| 524 | { |
| 525 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ |
| 526 | |
| 527 | (*cinfo->marker->write_frame_header) (cinfo); |
| 528 | (*cinfo->marker->write_scan_header) (cinfo); |
| 529 | } |
| 530 | |
| 531 | |
| 532 | /* |
| 533 | * Finish up at end of pass. |
| 534 | */ |
| 535 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 536 | METHODDEF(void) |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 537 | finish_pass_master (j_compress_ptr cinfo) |
| 538 | { |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 539 | my_master_ptr master = (my_master_ptr) cinfo->master; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 540 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 541 | /* The entropy coder always needs an end-of-pass call, |
| 542 | * either to analyze statistics or to flush its output buffer. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 543 | */ |
| 544 | (*cinfo->entropy->finish_pass) (cinfo); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 545 | |
| 546 | /* Update state for next pass */ |
| 547 | switch (master->pass_type) { |
| 548 | case main_pass: |
| 549 | /* next pass is either output of scan 0 (after optimization) |
| 550 | * or output of scan 1 (if no optimization). |
| 551 | */ |
| 552 | master->pass_type = output_pass; |
| 553 | if (! cinfo->optimize_coding) |
| 554 | master->scan_number++; |
| 555 | break; |
| 556 | case huff_opt_pass: |
| 557 | /* next pass is always output of current scan */ |
| 558 | master->pass_type = output_pass; |
| 559 | break; |
| 560 | case output_pass: |
| 561 | /* next pass is either optimization or output of next scan */ |
| 562 | if (cinfo->optimize_coding) |
| 563 | master->pass_type = huff_opt_pass; |
| 564 | master->scan_number++; |
| 565 | break; |
| 566 | } |
| 567 | |
| 568 | master->pass_number++; |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | |
| 572 | /* |
| 573 | * Initialize master compression control. |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 574 | */ |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 575 | |
Thomas G. Lane | 489583f | 1996-02-07 00:00:00 +0000 | [diff] [blame] | 576 | GLOBAL(void) |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 577 | jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 578 | { |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 579 | my_master_ptr master; |
Thomas G. Lane | 4a6b730 | 1992-03-17 00:00:00 +0000 | [diff] [blame] | 580 | |
Thomas G. Lane | 36a4ccc | 1994-09-24 00:00:00 +0000 | [diff] [blame] | 581 | master = (my_master_ptr) |
| 582 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, |
| 583 | SIZEOF(my_comp_master)); |
| 584 | cinfo->master = (struct jpeg_comp_master *) master; |
| 585 | master->pub.prepare_for_pass = prepare_for_pass; |
| 586 | master->pub.pass_startup = pass_startup; |
| 587 | master->pub.finish_pass = finish_pass_master; |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 588 | master->pub.is_last_pass = FALSE; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 589 | |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 590 | /* Validate parameters, determine derived values */ |
Guido Vollbeding | 989630f | 2010-01-10 00:00:00 +0000 | [diff] [blame] | 591 | initial_setup(cinfo, transcode_only); |
Thomas G. Lane | bc79e06 | 1995-08-02 00:00:00 +0000 | [diff] [blame] | 592 | |
| 593 | if (cinfo->scan_info != NULL) { |
| 594 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
| 595 | validate_script(cinfo); |
| 596 | #else |
| 597 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
| 598 | #endif |
| 599 | } else { |
| 600 | cinfo->progressive_mode = FALSE; |
| 601 | cinfo->num_scans = 1; |
| 602 | } |
| 603 | |
| 604 | if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */ |
| 605 | cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */ |
| 606 | |
| 607 | /* Initialize my private state */ |
| 608 | if (transcode_only) { |
| 609 | /* no main pass in transcoding */ |
| 610 | if (cinfo->optimize_coding) |
| 611 | master->pass_type = huff_opt_pass; |
| 612 | else |
| 613 | master->pass_type = output_pass; |
| 614 | } else { |
| 615 | /* for normal compression, first pass is always this type: */ |
| 616 | master->pass_type = main_pass; |
| 617 | } |
| 618 | master->scan_number = 0; |
| 619 | master->pass_number = 0; |
| 620 | if (cinfo->optimize_coding) |
| 621 | master->total_passes = cinfo->num_scans * 2; |
| 622 | else |
| 623 | master->total_passes = cinfo->num_scans; |
Thomas G. Lane | 2cbeb8a | 1991-10-07 00:00:00 +0000 | [diff] [blame] | 624 | } |