/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jcmaster.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * jcmaster.c |
3 | | * |
4 | | * Copyright (C) 1991-1997, Thomas G. Lane. |
5 | | * Modified 2003-2019 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 | | LOCAL(void) |
47 | | initial_setup (j_compress_ptr cinfo) |
48 | | /* Do computations that are needed before master selection phase */ |
49 | 0 | { |
50 | 0 | int ci, ssize; |
51 | 0 | jpeg_component_info *compptr; |
52 | | |
53 | | /* Sanity check on block_size */ |
54 | 0 | if (cinfo->block_size < 1 || cinfo->block_size > 16) |
55 | 0 | ERREXIT2(cinfo, JERR_BAD_DCTSIZE, cinfo->block_size, cinfo->block_size); |
56 | | |
57 | | /* Derive natural_order from block_size */ |
58 | 0 | switch (cinfo->block_size) { |
59 | 0 | case 2: cinfo->natural_order = jpeg_natural_order2; break; |
60 | 0 | case 3: cinfo->natural_order = jpeg_natural_order3; break; |
61 | 0 | case 4: cinfo->natural_order = jpeg_natural_order4; break; |
62 | 0 | case 5: cinfo->natural_order = jpeg_natural_order5; break; |
63 | 0 | case 6: cinfo->natural_order = jpeg_natural_order6; break; |
64 | 0 | case 7: cinfo->natural_order = jpeg_natural_order7; break; |
65 | 0 | default: cinfo->natural_order = jpeg_natural_order; |
66 | 0 | } |
67 | | |
68 | | /* Derive lim_Se from block_size */ |
69 | 0 | cinfo->lim_Se = cinfo->block_size < DCTSIZE ? |
70 | 0 | cinfo->block_size * cinfo->block_size - 1 : DCTSIZE2-1; |
71 | | |
72 | | /* Sanity check on image dimensions */ |
73 | 0 | if (cinfo->jpeg_height <= 0 || cinfo->jpeg_width <= 0 || |
74 | 0 | cinfo->num_components <= 0) |
75 | 0 | ERREXIT(cinfo, JERR_EMPTY_IMAGE); |
76 | | |
77 | | /* Make sure image isn't bigger than I can handle */ |
78 | 0 | if ((long) cinfo->jpeg_height > (long) JPEG_MAX_DIMENSION || |
79 | 0 | (long) cinfo->jpeg_width > (long) JPEG_MAX_DIMENSION) |
80 | 0 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); |
81 | | |
82 | | /* Only 8 to 12 bits data precision are supported for DCT based JPEG */ |
83 | 0 | if (cinfo->data_precision < 8 || cinfo->data_precision > 12) |
84 | 0 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); |
85 | | |
86 | | /* Check that number of components won't exceed internal array sizes */ |
87 | 0 | if (cinfo->num_components > MAX_COMPONENTS) |
88 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
89 | 0 | MAX_COMPONENTS); |
90 | | |
91 | | /* Compute maximum sampling factors; check factor validity */ |
92 | 0 | cinfo->max_h_samp_factor = 1; |
93 | 0 | cinfo->max_v_samp_factor = 1; |
94 | 0 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
95 | 0 | ci++, compptr++) { |
96 | 0 | if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || |
97 | 0 | compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) |
98 | 0 | ERREXIT(cinfo, JERR_BAD_SAMPLING); |
99 | 0 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, |
100 | 0 | compptr->h_samp_factor); |
101 | 0 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, |
102 | 0 | compptr->v_samp_factor); |
103 | 0 | } |
104 | | |
105 | | /* Compute dimensions of components */ |
106 | 0 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; |
107 | 0 | ci++, compptr++) { |
108 | | /* Fill in the correct component_index value; don't rely on application */ |
109 | 0 | compptr->component_index = ci; |
110 | | /* In selecting the actual DCT scaling for each component, we try to |
111 | | * scale down the chroma components via DCT scaling rather than downsampling. |
112 | | * This saves time if the downsampler gets to use 1:1 scaling. |
113 | | * Note this code adapts subsampling ratios which are powers of 2. |
114 | | */ |
115 | 0 | ssize = 1; |
116 | 0 | #ifdef DCT_SCALING_SUPPORTED |
117 | 0 | if (! cinfo->raw_data_in) |
118 | 0 | while (cinfo->min_DCT_h_scaled_size * ssize <= |
119 | 0 | (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && |
120 | 0 | (cinfo->max_h_samp_factor % (compptr->h_samp_factor * ssize * 2)) == |
121 | 0 | 0) { |
122 | 0 | ssize = ssize * 2; |
123 | 0 | } |
124 | 0 | #endif |
125 | 0 | compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size * ssize; |
126 | 0 | ssize = 1; |
127 | 0 | #ifdef DCT_SCALING_SUPPORTED |
128 | 0 | if (! cinfo->raw_data_in) |
129 | 0 | while (cinfo->min_DCT_v_scaled_size * ssize <= |
130 | 0 | (cinfo->do_fancy_downsampling ? DCTSIZE : DCTSIZE / 2) && |
131 | 0 | (cinfo->max_v_samp_factor % (compptr->v_samp_factor * ssize * 2)) == |
132 | 0 | 0) { |
133 | 0 | ssize = ssize * 2; |
134 | 0 | } |
135 | 0 | #endif |
136 | 0 | compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size * ssize; |
137 | | |
138 | | /* We don't support DCT ratios larger than 2. */ |
139 | 0 | if (compptr->DCT_h_scaled_size > compptr->DCT_v_scaled_size * 2) |
140 | 0 | compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size * 2; |
141 | 0 | else if (compptr->DCT_v_scaled_size > compptr->DCT_h_scaled_size * 2) |
142 | 0 | compptr->DCT_v_scaled_size = compptr->DCT_h_scaled_size * 2; |
143 | | |
144 | | /* Size in DCT blocks */ |
145 | 0 | compptr->width_in_blocks = (JDIMENSION) |
146 | 0 | jdiv_round_up((long) cinfo->jpeg_width * (long) compptr->h_samp_factor, |
147 | 0 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
148 | 0 | compptr->height_in_blocks = (JDIMENSION) |
149 | 0 | jdiv_round_up((long) cinfo->jpeg_height * (long) compptr->v_samp_factor, |
150 | 0 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
151 | | /* Size in samples */ |
152 | 0 | compptr->downsampled_width = (JDIMENSION) |
153 | 0 | jdiv_round_up((long) cinfo->jpeg_width * |
154 | 0 | (long) (compptr->h_samp_factor * compptr->DCT_h_scaled_size), |
155 | 0 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
156 | 0 | compptr->downsampled_height = (JDIMENSION) |
157 | 0 | jdiv_round_up((long) cinfo->jpeg_height * |
158 | 0 | (long) (compptr->v_samp_factor * compptr->DCT_v_scaled_size), |
159 | 0 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
160 | | /* Don't need quantization scale after DCT, |
161 | | * until color conversion says otherwise. |
162 | | */ |
163 | 0 | compptr->component_needed = FALSE; |
164 | 0 | } |
165 | | |
166 | | /* Compute number of fully interleaved MCU rows (number of times that |
167 | | * main controller will call coefficient controller). |
168 | | */ |
169 | 0 | cinfo->total_iMCU_rows = (JDIMENSION) |
170 | 0 | jdiv_round_up((long) cinfo->jpeg_height, |
171 | 0 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
172 | 0 | } |
173 | | |
174 | | |
175 | | #ifdef C_MULTISCAN_FILES_SUPPORTED |
176 | | |
177 | | LOCAL(void) |
178 | | validate_script (j_compress_ptr cinfo) |
179 | | /* Verify that the scan script in cinfo->scan_info[] is valid; also |
180 | | * determine whether it uses progressive JPEG, and set cinfo->progressive_mode. |
181 | | */ |
182 | 0 | { |
183 | 0 | const jpeg_scan_info * scanptr; |
184 | 0 | int scanno, ncomps, ci, coefi, thisi; |
185 | 0 | int Ss, Se, Ah, Al; |
186 | 0 | boolean component_sent[MAX_COMPONENTS]; |
187 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
188 | 0 | int * last_bitpos_ptr; |
189 | 0 | int last_bitpos[MAX_COMPONENTS][DCTSIZE2]; |
190 | | /* -1 until that coefficient has been seen; then last Al for it */ |
191 | 0 | #endif |
192 | |
|
193 | 0 | if (cinfo->num_scans <= 0) |
194 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0); |
195 | | |
196 | | /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1; |
197 | | * for progressive JPEG, no scan can have this. |
198 | | */ |
199 | 0 | scanptr = cinfo->scan_info; |
200 | 0 | if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) { |
201 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
202 | 0 | cinfo->progressive_mode = TRUE; |
203 | 0 | last_bitpos_ptr = & last_bitpos[0][0]; |
204 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) |
205 | 0 | for (coefi = 0; coefi < DCTSIZE2; coefi++) |
206 | 0 | *last_bitpos_ptr++ = -1; |
207 | | #else |
208 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
209 | | #endif |
210 | 0 | } else { |
211 | 0 | cinfo->progressive_mode = FALSE; |
212 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) |
213 | 0 | component_sent[ci] = FALSE; |
214 | 0 | } |
215 | |
|
216 | 0 | for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) { |
217 | | /* Validate component indexes */ |
218 | 0 | ncomps = scanptr->comps_in_scan; |
219 | 0 | if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN) |
220 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN); |
221 | 0 | for (ci = 0; ci < ncomps; ci++) { |
222 | 0 | thisi = scanptr->component_index[ci]; |
223 | 0 | if (thisi < 0 || thisi >= cinfo->num_components) |
224 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
225 | | /* Components must appear in SOF order within each scan */ |
226 | 0 | if (ci > 0 && thisi <= scanptr->component_index[ci-1]) |
227 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
228 | 0 | } |
229 | | /* Validate progression parameters */ |
230 | 0 | Ss = scanptr->Ss; |
231 | 0 | Se = scanptr->Se; |
232 | 0 | Ah = scanptr->Ah; |
233 | 0 | Al = scanptr->Al; |
234 | 0 | if (cinfo->progressive_mode) { |
235 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
236 | | /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that |
237 | | * seems wrong: the upper bound ought to depend on data precision. |
238 | | * Perhaps they really meant 0..N+1 for N-bit precision. |
239 | | * Here we allow 0..10 for 8-bit data; Al larger than 10 results in |
240 | | * out-of-range reconstructed DC values during the first DC scan, |
241 | | * which might cause problems for some decoders. |
242 | | */ |
243 | 0 | if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 || |
244 | 0 | Ah < 0 || Ah > (cinfo->data_precision > 8 ? 13 : 10) || |
245 | 0 | Al < 0 || Al > (cinfo->data_precision > 8 ? 13 : 10)) |
246 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
247 | 0 | if (Ss == 0) { |
248 | 0 | if (Se != 0) /* DC and AC together not OK */ |
249 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
250 | 0 | } else { |
251 | 0 | if (ncomps != 1) /* AC scans must be for only one component */ |
252 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
253 | 0 | } |
254 | 0 | for (ci = 0; ci < ncomps; ci++) { |
255 | 0 | last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0]; |
256 | 0 | if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */ |
257 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
258 | 0 | for (coefi = Ss; coefi <= Se; coefi++) { |
259 | 0 | if (last_bitpos_ptr[coefi] < 0) { |
260 | | /* first scan of this coefficient */ |
261 | 0 | if (Ah != 0) |
262 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
263 | 0 | } else { |
264 | | /* not first scan */ |
265 | 0 | if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1) |
266 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
267 | 0 | } |
268 | 0 | last_bitpos_ptr[coefi] = Al; |
269 | 0 | } |
270 | 0 | } |
271 | 0 | #endif |
272 | 0 | } else { |
273 | | /* For sequential JPEG, all progression parameters must be these: */ |
274 | 0 | if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0) |
275 | 0 | ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno); |
276 | | /* Make sure components are not sent twice */ |
277 | 0 | for (ci = 0; ci < ncomps; ci++) { |
278 | 0 | thisi = scanptr->component_index[ci]; |
279 | 0 | if (component_sent[thisi]) |
280 | 0 | ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno); |
281 | 0 | component_sent[thisi] = TRUE; |
282 | 0 | } |
283 | 0 | } |
284 | 0 | } |
285 | | |
286 | | /* Now verify that everything got sent. */ |
287 | 0 | if (cinfo->progressive_mode) { |
288 | 0 | #ifdef C_PROGRESSIVE_SUPPORTED |
289 | | /* For progressive mode, we only check that at least some DC data |
290 | | * got sent for each component; the spec does not require that all bits |
291 | | * of all coefficients be transmitted. Would it be wiser to enforce |
292 | | * transmission of all coefficient bits?? |
293 | | */ |
294 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
295 | 0 | if (last_bitpos[ci][0] < 0) |
296 | 0 | ERREXIT(cinfo, JERR_MISSING_DATA); |
297 | 0 | } |
298 | 0 | #endif |
299 | 0 | } else { |
300 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
301 | 0 | if (! component_sent[ci]) |
302 | 0 | ERREXIT(cinfo, JERR_MISSING_DATA); |
303 | 0 | } |
304 | 0 | } |
305 | 0 | } |
306 | | |
307 | | |
308 | | LOCAL(void) |
309 | | reduce_script (j_compress_ptr cinfo) |
310 | | /* Adapt scan script for use with reduced block size; |
311 | | * assume that script has been validated before. |
312 | | */ |
313 | 0 | { |
314 | 0 | jpeg_scan_info * scanptr; |
315 | 0 | int idxout, idxin; |
316 | | |
317 | | /* Circumvent const declaration for this function */ |
318 | 0 | scanptr = (jpeg_scan_info *) cinfo->scan_info; |
319 | 0 | idxout = 0; |
320 | |
|
321 | 0 | for (idxin = 0; idxin < cinfo->num_scans; idxin++) { |
322 | | /* After skipping, idxout becomes smaller than idxin */ |
323 | 0 | if (idxin != idxout) |
324 | | /* Copy rest of data; |
325 | | * note we stay in given chunk of allocated memory. |
326 | | */ |
327 | 0 | scanptr[idxout] = scanptr[idxin]; |
328 | 0 | if (scanptr[idxout].Ss > cinfo->lim_Se) |
329 | | /* Entire scan out of range - skip this entry */ |
330 | 0 | continue; |
331 | 0 | if (scanptr[idxout].Se > cinfo->lim_Se) |
332 | | /* Limit scan to end of block */ |
333 | 0 | scanptr[idxout].Se = cinfo->lim_Se; |
334 | 0 | idxout++; |
335 | 0 | } |
336 | |
|
337 | 0 | cinfo->num_scans = idxout; |
338 | 0 | } |
339 | | |
340 | | #endif /* C_MULTISCAN_FILES_SUPPORTED */ |
341 | | |
342 | | |
343 | | LOCAL(void) |
344 | | select_scan_parameters (j_compress_ptr cinfo) |
345 | | /* Set up the scan parameters for the current scan */ |
346 | 0 | { |
347 | 0 | int ci; |
348 | |
|
349 | 0 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
350 | 0 | if (cinfo->scan_info != NULL) { |
351 | | /* Prepare for current scan --- the script is already validated */ |
352 | 0 | my_master_ptr master = (my_master_ptr) cinfo->master; |
353 | 0 | const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number; |
354 | |
|
355 | 0 | cinfo->comps_in_scan = scanptr->comps_in_scan; |
356 | 0 | for (ci = 0; ci < scanptr->comps_in_scan; ci++) { |
357 | 0 | cinfo->cur_comp_info[ci] = |
358 | 0 | &cinfo->comp_info[scanptr->component_index[ci]]; |
359 | 0 | } |
360 | 0 | if (cinfo->progressive_mode) { |
361 | 0 | cinfo->Ss = scanptr->Ss; |
362 | 0 | cinfo->Se = scanptr->Se; |
363 | 0 | cinfo->Ah = scanptr->Ah; |
364 | 0 | cinfo->Al = scanptr->Al; |
365 | 0 | return; |
366 | 0 | } |
367 | 0 | } |
368 | 0 | else |
369 | 0 | #endif |
370 | 0 | { |
371 | | /* Prepare for single sequential-JPEG scan containing all components */ |
372 | 0 | if (cinfo->num_components > MAX_COMPS_IN_SCAN) |
373 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, |
374 | 0 | MAX_COMPS_IN_SCAN); |
375 | 0 | cinfo->comps_in_scan = cinfo->num_components; |
376 | 0 | for (ci = 0; ci < cinfo->num_components; ci++) { |
377 | 0 | cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci]; |
378 | 0 | } |
379 | 0 | } |
380 | 0 | cinfo->Ss = 0; |
381 | 0 | cinfo->Se = cinfo->block_size * cinfo->block_size - 1; |
382 | 0 | cinfo->Ah = 0; |
383 | 0 | cinfo->Al = 0; |
384 | 0 | } |
385 | | |
386 | | |
387 | | LOCAL(void) |
388 | | per_scan_setup (j_compress_ptr cinfo) |
389 | | /* Do computations that are needed before processing a JPEG scan */ |
390 | | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */ |
391 | 0 | { |
392 | 0 | int ci, mcublks, tmp; |
393 | 0 | jpeg_component_info *compptr; |
394 | | |
395 | 0 | if (cinfo->comps_in_scan == 1) { |
396 | | |
397 | | /* Noninterleaved (single-component) scan */ |
398 | 0 | compptr = cinfo->cur_comp_info[0]; |
399 | | |
400 | | /* Overall image size in MCUs */ |
401 | 0 | cinfo->MCUs_per_row = compptr->width_in_blocks; |
402 | 0 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; |
403 | | |
404 | | /* For noninterleaved scan, always one block per MCU */ |
405 | 0 | compptr->MCU_width = 1; |
406 | 0 | compptr->MCU_height = 1; |
407 | 0 | compptr->MCU_blocks = 1; |
408 | 0 | compptr->MCU_sample_width = compptr->DCT_h_scaled_size; |
409 | 0 | compptr->last_col_width = 1; |
410 | | /* For noninterleaved scans, it is convenient to define last_row_height |
411 | | * as the number of block rows present in the last iMCU row. |
412 | | */ |
413 | 0 | tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); |
414 | 0 | if (tmp == 0) tmp = compptr->v_samp_factor; |
415 | 0 | compptr->last_row_height = tmp; |
416 | | |
417 | | /* Prepare array describing MCU composition */ |
418 | 0 | cinfo->blocks_in_MCU = 1; |
419 | 0 | cinfo->MCU_membership[0] = 0; |
420 | | |
421 | 0 | } else { |
422 | | |
423 | | /* Interleaved (multi-component) scan */ |
424 | 0 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) |
425 | 0 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, |
426 | 0 | MAX_COMPS_IN_SCAN); |
427 | | |
428 | | /* Overall image size in MCUs */ |
429 | 0 | cinfo->MCUs_per_row = (JDIMENSION) |
430 | 0 | jdiv_round_up((long) cinfo->jpeg_width, |
431 | 0 | (long) (cinfo->max_h_samp_factor * cinfo->block_size)); |
432 | 0 | cinfo->MCU_rows_in_scan = (JDIMENSION) |
433 | 0 | jdiv_round_up((long) cinfo->jpeg_height, |
434 | 0 | (long) (cinfo->max_v_samp_factor * cinfo->block_size)); |
435 | | |
436 | 0 | cinfo->blocks_in_MCU = 0; |
437 | | |
438 | 0 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
439 | 0 | compptr = cinfo->cur_comp_info[ci]; |
440 | | /* Sampling factors give # of blocks of component in each MCU */ |
441 | 0 | compptr->MCU_width = compptr->h_samp_factor; |
442 | 0 | compptr->MCU_height = compptr->v_samp_factor; |
443 | 0 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; |
444 | 0 | compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size; |
445 | | /* Figure number of non-dummy blocks in last MCU column & row */ |
446 | 0 | tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); |
447 | 0 | if (tmp == 0) tmp = compptr->MCU_width; |
448 | 0 | compptr->last_col_width = tmp; |
449 | 0 | tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); |
450 | 0 | if (tmp == 0) tmp = compptr->MCU_height; |
451 | 0 | compptr->last_row_height = tmp; |
452 | | /* Prepare array describing MCU composition */ |
453 | 0 | mcublks = compptr->MCU_blocks; |
454 | 0 | if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU) |
455 | 0 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); |
456 | 0 | while (mcublks-- > 0) { |
457 | 0 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; |
458 | 0 | } |
459 | 0 | } |
460 | | |
461 | 0 | } |
462 | | |
463 | | /* Convert restart specified in rows to actual MCU count. */ |
464 | | /* Note that count must fit in 16 bits, so we provide limiting. */ |
465 | 0 | if (cinfo->restart_in_rows > 0) { |
466 | 0 | long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row; |
467 | 0 | cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L); |
468 | 0 | } |
469 | 0 | } |
470 | | |
471 | | |
472 | | /* |
473 | | * Per-pass setup. |
474 | | * This is called at the beginning of each pass. We determine which modules |
475 | | * will be active during this pass and give them appropriate start_pass calls. |
476 | | * We also set is_last_pass to indicate whether any more passes will be |
477 | | * required. |
478 | | */ |
479 | | |
480 | | METHODDEF(void) |
481 | | prepare_for_pass (j_compress_ptr cinfo) |
482 | 0 | { |
483 | 0 | my_master_ptr master = (my_master_ptr) cinfo->master; |
484 | |
|
485 | 0 | switch (master->pass_type) { |
486 | 0 | case main_pass: |
487 | | /* Initial pass: will collect input data, and do either Huffman |
488 | | * optimization or data output for the first scan. |
489 | | */ |
490 | 0 | select_scan_parameters(cinfo); |
491 | 0 | per_scan_setup(cinfo); |
492 | 0 | if (! cinfo->raw_data_in) { |
493 | 0 | (*cinfo->cconvert->start_pass) (cinfo); |
494 | 0 | (*cinfo->downsample->start_pass) (cinfo); |
495 | 0 | (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU); |
496 | 0 | } |
497 | 0 | (*cinfo->fdct->start_pass) (cinfo); |
498 | 0 | (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding); |
499 | 0 | (*cinfo->coef->start_pass) (cinfo, |
500 | 0 | (master->total_passes > 1 ? |
501 | 0 | JBUF_SAVE_AND_PASS : JBUF_PASS_THRU)); |
502 | 0 | (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU); |
503 | 0 | if (cinfo->optimize_coding) { |
504 | | /* No immediate data output; postpone writing frame/scan headers */ |
505 | 0 | master->pub.call_pass_startup = FALSE; |
506 | 0 | } else { |
507 | | /* Will write frame/scan headers at first jpeg_write_scanlines call */ |
508 | 0 | master->pub.call_pass_startup = TRUE; |
509 | 0 | } |
510 | 0 | break; |
511 | 0 | #ifdef ENTROPY_OPT_SUPPORTED |
512 | 0 | case huff_opt_pass: |
513 | | /* Do Huffman optimization for a scan after the first one. */ |
514 | 0 | select_scan_parameters(cinfo); |
515 | 0 | per_scan_setup(cinfo); |
516 | 0 | if (cinfo->Ss != 0 || cinfo->Ah == 0) { |
517 | 0 | (*cinfo->entropy->start_pass) (cinfo, TRUE); |
518 | 0 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
519 | 0 | master->pub.call_pass_startup = FALSE; |
520 | 0 | break; |
521 | 0 | } |
522 | | /* Special case: Huffman DC refinement scans need no Huffman table |
523 | | * and therefore we can skip the optimization pass for them. |
524 | | */ |
525 | 0 | master->pass_type = output_pass; |
526 | 0 | master->pass_number++; |
527 | | /*FALLTHROUGH*/ |
528 | 0 | #endif |
529 | 0 | case output_pass: |
530 | | /* Do a data-output pass. */ |
531 | | /* We need not repeat per-scan setup if prior optimization pass did it. */ |
532 | 0 | if (! cinfo->optimize_coding) { |
533 | 0 | select_scan_parameters(cinfo); |
534 | 0 | per_scan_setup(cinfo); |
535 | 0 | } |
536 | 0 | (*cinfo->entropy->start_pass) (cinfo, FALSE); |
537 | 0 | (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST); |
538 | | /* We emit frame/scan headers now */ |
539 | 0 | if (master->scan_number == 0) |
540 | 0 | (*cinfo->marker->write_frame_header) (cinfo); |
541 | 0 | (*cinfo->marker->write_scan_header) (cinfo); |
542 | 0 | master->pub.call_pass_startup = FALSE; |
543 | 0 | break; |
544 | 0 | default: |
545 | 0 | ERREXIT(cinfo, JERR_NOT_COMPILED); |
546 | 0 | } |
547 | | |
548 | 0 | master->pub.is_last_pass = (master->pass_number == master->total_passes-1); |
549 | | |
550 | | /* Set up progress monitor's pass info if present */ |
551 | 0 | if (cinfo->progress != NULL) { |
552 | 0 | cinfo->progress->completed_passes = master->pass_number; |
553 | 0 | cinfo->progress->total_passes = master->total_passes; |
554 | 0 | } |
555 | 0 | } |
556 | | |
557 | | |
558 | | /* |
559 | | * Special start-of-pass hook. |
560 | | * This is called by jpeg_write_scanlines if call_pass_startup is TRUE. |
561 | | * In single-pass processing, we need this hook because we don't want to |
562 | | * write frame/scan headers during jpeg_start_compress; we want to let the |
563 | | * application write COM markers etc. between jpeg_start_compress and the |
564 | | * jpeg_write_scanlines loop. |
565 | | * In multi-pass processing, this routine is not used. |
566 | | */ |
567 | | |
568 | | METHODDEF(void) |
569 | | pass_startup (j_compress_ptr cinfo) |
570 | 0 | { |
571 | 0 | cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */ |
572 | |
|
573 | 0 | (*cinfo->marker->write_frame_header) (cinfo); |
574 | 0 | (*cinfo->marker->write_scan_header) (cinfo); |
575 | 0 | } |
576 | | |
577 | | |
578 | | /* |
579 | | * Finish up at end of pass. |
580 | | */ |
581 | | |
582 | | METHODDEF(void) |
583 | | finish_pass_master (j_compress_ptr cinfo) |
584 | 0 | { |
585 | 0 | my_master_ptr master = (my_master_ptr) cinfo->master; |
586 | | |
587 | | /* The entropy coder always needs an end-of-pass call, |
588 | | * either to analyze statistics or to flush its output buffer. |
589 | | */ |
590 | 0 | (*cinfo->entropy->finish_pass) (cinfo); |
591 | | |
592 | | /* Update state for next pass */ |
593 | 0 | switch (master->pass_type) { |
594 | 0 | case main_pass: |
595 | | /* next pass is either output of scan 0 (after optimization) |
596 | | * or output of scan 1 (if no optimization). |
597 | | */ |
598 | 0 | master->pass_type = output_pass; |
599 | 0 | if (! cinfo->optimize_coding) |
600 | 0 | master->scan_number++; |
601 | 0 | break; |
602 | 0 | case huff_opt_pass: |
603 | | /* next pass is always output of current scan */ |
604 | 0 | master->pass_type = output_pass; |
605 | 0 | break; |
606 | 0 | case output_pass: |
607 | | /* next pass is either optimization or output of next scan */ |
608 | 0 | if (cinfo->optimize_coding) |
609 | 0 | master->pass_type = huff_opt_pass; |
610 | 0 | master->scan_number++; |
611 | 0 | break; |
612 | 0 | } |
613 | | |
614 | 0 | master->pass_number++; |
615 | 0 | } |
616 | | |
617 | | |
618 | | /* |
619 | | * Initialize master compression control. |
620 | | */ |
621 | | |
622 | | GLOBAL(void) |
623 | | jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only) |
624 | 0 | { |
625 | 0 | my_master_ptr master; |
626 | |
|
627 | 0 | master = (my_master_ptr) (*cinfo->mem->alloc_small) |
628 | 0 | ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(my_comp_master)); |
629 | 0 | cinfo->master = &master->pub; |
630 | 0 | master->pub.prepare_for_pass = prepare_for_pass; |
631 | 0 | master->pub.pass_startup = pass_startup; |
632 | 0 | master->pub.finish_pass = finish_pass_master; |
633 | 0 | master->pub.is_last_pass = FALSE; |
634 | | |
635 | | /* Validate parameters, determine derived values */ |
636 | 0 | initial_setup(cinfo); |
637 | |
|
638 | 0 | if (cinfo->scan_info != NULL) { |
639 | 0 | #ifdef C_MULTISCAN_FILES_SUPPORTED |
640 | 0 | validate_script(cinfo); |
641 | 0 | if (cinfo->block_size < DCTSIZE) |
642 | 0 | reduce_script(cinfo); |
643 | | #else |
644 | | ERREXIT(cinfo, JERR_NOT_COMPILED); |
645 | | #endif |
646 | 0 | } else { |
647 | 0 | cinfo->progressive_mode = FALSE; |
648 | 0 | cinfo->num_scans = 1; |
649 | 0 | } |
650 | |
|
651 | 0 | if (cinfo->optimize_coding) |
652 | 0 | cinfo->arith_code = FALSE; /* disable arithmetic coding */ |
653 | 0 | else if (! cinfo->arith_code && |
654 | 0 | (cinfo->progressive_mode || |
655 | 0 | (cinfo->block_size > 1 && cinfo->block_size < DCTSIZE))) |
656 | | /* TEMPORARY HACK ??? */ |
657 | | /* assume default tables no good for progressive or reduced AC mode */ |
658 | 0 | cinfo->optimize_coding = TRUE; /* force Huffman optimization */ |
659 | | |
660 | | /* Initialize my private state */ |
661 | 0 | if (transcode_only) { |
662 | | /* no main pass in transcoding */ |
663 | 0 | if (cinfo->optimize_coding) |
664 | 0 | master->pass_type = huff_opt_pass; |
665 | 0 | else |
666 | 0 | master->pass_type = output_pass; |
667 | 0 | } else { |
668 | | /* for normal compression, first pass is always this type: */ |
669 | 0 | master->pass_type = main_pass; |
670 | 0 | } |
671 | 0 | master->scan_number = 0; |
672 | 0 | master->pass_number = 0; |
673 | 0 | if (cinfo->optimize_coding) |
674 | 0 | master->total_passes = cinfo->num_scans * 2; |
675 | 0 | else |
676 | 0 | master->total_passes = cinfo->num_scans; |
677 | 0 | } |