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