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