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