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