Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jcmaster.c
blobedd2d0481b78f2bc68019b6b18cdcd44b2a017d1
1 /*
2 $Id$
3 */
5 /*
6 * jcmaster.c
8 * Copyright (C) 1991-1998, 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.
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).
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jlossy.h" /* Private declarations for lossy codec */
24 /* Private state */
26 typedef enum {
27 main_pass, /* input data, also do first output step */
28 huff_opt_pass, /* Huffman code optimization pass */
29 output_pass /* data output pass */
30 } c_pass_type;
32 typedef struct {
33 struct jpeg_comp_master pub; /* public fields */
35 c_pass_type pass_type; /* the type of the current pass */
37 int pass_number; /* # of passes completed */
38 int total_passes; /* total # of passes needed */
40 int scan_number; /* current index in scan_info[] */
41 } my_comp_master;
43 typedef my_comp_master * my_master_ptr;
47 * Support routines that do various essential calculations.
50 LOCAL(void)
51 initial_setup (j_compress_ptr cinfo)
52 /* Do computations that are needed before master selection phase */
54 int ci;
55 jpeg_component_info *compptr;
56 long samplesperrow;
57 JDIMENSION jd_samplesperrow;
58 int data_unit = cinfo->data_unit;
60 /* Sanity check on image dimensions */
61 if (cinfo->image_height <= 0 || cinfo->image_width <= 0
62 || cinfo->num_components <= 0 || cinfo->input_components <= 0)
63 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
65 /* Make sure image isn't bigger than I can handle */
66 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
67 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
68 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
70 /* Width of an input scanline must be representable as JDIMENSION. */
71 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
72 jd_samplesperrow = (JDIMENSION) samplesperrow;
73 if ((long) jd_samplesperrow != samplesperrow)
74 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
76 /* For now, precision must match compiled-in value... */
77 if (cinfo->data_precision != BITS_IN_JSAMPLE)
78 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
80 /* Check that number of components won't exceed internal array sizes */
81 if (cinfo->num_components > MAX_COMPONENTS)
82 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
83 MAX_COMPONENTS);
85 /* Compute maximum sampling factors; check factor validity */
86 cinfo->max_h_samp_factor = 1;
87 cinfo->max_v_samp_factor = 1;
88 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
89 ci++, compptr++) {
90 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
91 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
92 ERREXIT(cinfo, JERR_BAD_SAMPLING);
93 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
94 compptr->h_samp_factor);
95 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
96 compptr->v_samp_factor);
99 /* Compute dimensions of components */
100 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
101 ci++, compptr++) {
102 /* Fill in the correct component_index value; don't rely on application */
103 compptr->component_index = ci;
104 /* For compression, we never do any codec-based processing. */
105 compptr->codec_data_unit = data_unit;
106 /* Size in data units */
107 compptr->width_in_data_units = (JDIMENSION)
108 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
109 (long) (cinfo->max_h_samp_factor * data_unit));
110 compptr->height_in_data_units = (JDIMENSION)
111 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
112 (long) (cinfo->max_v_samp_factor * data_unit));
113 /* Size in samples */
114 compptr->downsampled_width = (JDIMENSION)
115 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
116 (long) cinfo->max_h_samp_factor);
117 compptr->downsampled_height = (JDIMENSION)
118 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
119 (long) cinfo->max_v_samp_factor);
120 /* Mark component needed (this flag isn't actually used for compression) */
121 compptr->component_needed = TRUE;
124 /* Compute number of fully interleaved MCU rows (number of times that
125 * main controller will call coefficient controller).
127 cinfo->total_iMCU_rows = (JDIMENSION)
128 jdiv_round_up((long) cinfo->image_height,
129 (long) (cinfo->max_v_samp_factor*data_unit));
132 #ifdef C_MULTISCAN_FILES_SUPPORTED
133 #define NEED_SCAN_SCRIPT
134 #else
135 #ifdef C_LOSSLESS_SUPPORTED
136 #define NEED_SCAN_SCRIPT
137 #endif
138 #endif
140 #ifdef NEED_SCAN_SCRIPT
142 LOCAL(void)
143 validate_script (j_compress_ptr cinfo)
144 /* Verify that the scan script in cinfo->scan_info[] is valid; also
145 * determine whether it uses progressive JPEG, and set cinfo->process.
148 const jpeg_scan_info * scanptr;
149 int scanno, ncomps, ci, coefi, thisi;
150 int Ss, Se, Ah, Al;
151 boolean component_sent[MAX_COMPONENTS];
152 #ifdef C_PROGRESSIVE_SUPPORTED
153 int * last_bitpos_ptr;
154 int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
155 /* -1 until that coefficient has been seen; then last Al for it */
156 #endif
158 if (cinfo->num_scans <= 0)
159 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
161 #ifndef C_MULTISCAN_FILES_SUPPORTED
162 if (cinfo->num_scans > 1)
163 ERREXIT(cinfo, JERR_NOT_COMPILED);
164 #endif
166 scanptr = cinfo->scan_info;
167 if (cinfo->lossless) {
168 #ifdef C_LOSSLESS_SUPPORTED
169 cinfo->process = JPROC_LOSSLESS;
170 for (ci = 0; ci < cinfo->num_components; ci++)
171 component_sent[ci] = FALSE;
172 #else
173 ERREXIT(cinfo, JERR_NOT_COMPILED);
174 #endif
176 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
177 * for progressive JPEG, no scan can have this.
179 else if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
180 #ifdef C_PROGRESSIVE_SUPPORTED
181 cinfo->process = JPROC_PROGRESSIVE;
182 last_bitpos_ptr = & last_bitpos[0][0];
183 for (ci = 0; ci < cinfo->num_components; ci++)
184 for (coefi = 0; coefi < DCTSIZE2; coefi++)
185 *last_bitpos_ptr++ = -1;
186 #else
187 ERREXIT(cinfo, JERR_NOT_COMPILED);
188 #endif
189 } else {
190 cinfo->process = JPROC_SEQUENTIAL;
191 for (ci = 0; ci < cinfo->num_components; ci++)
192 component_sent[ci] = FALSE;
195 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
196 /* Validate component indexes */
197 ncomps = scanptr->comps_in_scan;
198 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
199 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
200 for (ci = 0; ci < ncomps; ci++) {
201 thisi = scanptr->component_index[ci];
202 if (thisi < 0 || thisi >= cinfo->num_components)
203 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
204 /* Components must appear in SOF order within each scan */
205 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
206 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
208 /* Validate progression parameters */
209 Ss = scanptr->Ss;
210 Se = scanptr->Se;
211 Ah = scanptr->Ah;
212 Al = scanptr->Al;
213 if (cinfo->process == JPROC_LOSSLESS) {
214 #ifdef C_LOSSLESS_SUPPORTED
215 /* The JPEG spec simply gives the range 0..15 for Al (Pt), but that
216 * seems wrong: the upper bound ought to depend on data precision.
217 * Perhaps they really meant 0..N-1 for N-bit precision, which is what
218 * we allow here.
220 if (Ss < 1 || Ss > 7 || /* predictor selector */
221 Se != 0 || Ah != 0 ||
222 Al < 0 || Al >= cinfo->data_precision) /* point transform */
223 ERREXIT1(cinfo, JERR_BAD_LOSSLESS_SCRIPT, scanno);
224 /* Make sure components are not sent twice */
225 for (ci = 0; ci < ncomps; ci++) {
226 thisi = scanptr->component_index[ci];
227 if (component_sent[thisi])
228 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
229 component_sent[thisi] = TRUE;
231 #endif
232 } else if (cinfo->process == JPROC_PROGRESSIVE) {
233 #ifdef C_PROGRESSIVE_SUPPORTED
234 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
235 * seems wrong: the upper bound ought to depend on data precision.
236 * 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.
241 #if BITS_IN_JSAMPLE == 8
242 #define MAX_AH_AL 10
243 #else
244 #define MAX_AH_AL 13
245 #endif
246 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
247 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
248 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
249 if (Ss == 0) {
250 if (Se != 0) /* DC and AC together not OK */
251 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
252 } else {
253 if (ncomps != 1) /* AC scans must be for only one component */
254 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
256 for (ci = 0; ci < ncomps; ci++) {
257 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
258 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
259 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
260 for (coefi = Ss; coefi <= Se; coefi++) {
261 if (last_bitpos_ptr[coefi] < 0) {
262 /* first scan of this coefficient */
263 if (Ah != 0)
264 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
265 } else {
266 /* not first scan */
267 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
268 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
270 last_bitpos_ptr[coefi] = Al;
273 #endif
274 } else {
275 /* For sequential JPEG, all progression parameters must be these: */
276 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
277 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
278 /* Make sure components are not sent twice */
279 for (ci = 0; ci < ncomps; ci++) {
280 thisi = scanptr->component_index[ci];
281 if (component_sent[thisi])
282 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
283 component_sent[thisi] = TRUE;
288 /* Now verify that everything got sent. */
289 if (cinfo->process == JPROC_PROGRESSIVE) {
290 #ifdef C_PROGRESSIVE_SUPPORTED
291 /* For progressive mode, we only check that at least some DC data
292 * got sent for each component; the spec does not require that all bits
293 * of all coefficients be transmitted. Would it be wiser to enforce
294 * transmission of all coefficient bits??
296 for (ci = 0; ci < cinfo->num_components; ci++) {
297 if (last_bitpos[ci][0] < 0)
298 ERREXIT(cinfo, JERR_MISSING_DATA);
300 #endif
301 } else {
302 for (ci = 0; ci < cinfo->num_components; ci++) {
303 if (! component_sent[ci])
304 ERREXIT(cinfo, JERR_MISSING_DATA);
309 #endif /* NEED_SCAN_SCRIPT */
312 LOCAL(void)
313 select_scan_parameters (j_compress_ptr cinfo)
314 /* Set up the scan parameters for the current scan */
316 int ci;
318 #ifdef NEED_SCAN_SCRIPT
319 if (cinfo->scan_info != NULL) {
320 /* Prepare for current scan --- the script is already validated */
321 my_master_ptr master = (my_master_ptr) cinfo->master;
322 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
324 cinfo->comps_in_scan = scanptr->comps_in_scan;
325 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
326 cinfo->cur_comp_info[ci] =
327 &cinfo->comp_info[scanptr->component_index[ci]];
329 cinfo->Ss = scanptr->Ss;
330 cinfo->Se = scanptr->Se;
331 cinfo->Ah = scanptr->Ah;
332 cinfo->Al = scanptr->Al;
333 } else
334 #endif
336 /* Prepare for single sequential-JPEG scan containing all components */
337 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
338 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
339 MAX_COMPS_IN_SCAN);
340 cinfo->comps_in_scan = cinfo->num_components;
341 for (ci = 0; ci < cinfo->num_components; ci++) {
342 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
344 if (cinfo->lossless) {
345 #ifdef C_LOSSLESS_SUPPORTED
346 /* If we fall through to here, the user specified lossless, but did not
347 * provide a scan script.
349 ERREXIT(cinfo, JERR_NO_LOSSLESS_SCRIPT);
350 #endif
351 } else {
352 cinfo->process = JPROC_SEQUENTIAL;
353 cinfo->Ss = 0;
354 cinfo->Se = DCTSIZE2-1;
355 cinfo->Ah = 0;
356 cinfo->Al = 0;
362 LOCAL(void)
363 per_scan_setup (j_compress_ptr cinfo)
364 /* Do computations that are needed before processing a JPEG scan */
365 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
367 int ci, mcublks, tmp;
368 jpeg_component_info *compptr;
369 int data_unit = cinfo->data_unit;
371 if (cinfo->comps_in_scan == 1) {
373 /* Noninterleaved (single-component) scan */
374 compptr = cinfo->cur_comp_info[0];
376 /* Overall image size in MCUs */
377 cinfo->MCUs_per_row = compptr->width_in_data_units;
378 cinfo->MCU_rows_in_scan = compptr->height_in_data_units;
380 /* For noninterleaved scan, always one block per MCU */
381 compptr->MCU_width = 1;
382 compptr->MCU_height = 1;
383 compptr->MCU_data_units = 1;
384 compptr->MCU_sample_width = data_unit;
385 compptr->last_col_width = 1;
386 /* For noninterleaved scans, it is convenient to define last_row_height
387 * as the number of block rows present in the last iMCU row.
389 tmp = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
390 if (tmp == 0) tmp = compptr->v_samp_factor;
391 compptr->last_row_height = tmp;
393 /* Prepare array describing MCU composition */
394 cinfo->data_units_in_MCU = 1;
395 cinfo->MCU_membership[0] = 0;
397 } else {
399 /* Interleaved (multi-component) scan */
400 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
401 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
402 MAX_COMPS_IN_SCAN);
404 /* Overall image size in MCUs */
405 cinfo->MCUs_per_row = (JDIMENSION)
406 jdiv_round_up((long) cinfo->image_width,
407 (long) (cinfo->max_h_samp_factor*data_unit));
408 cinfo->MCU_rows_in_scan = (JDIMENSION)
409 jdiv_round_up((long) cinfo->image_height,
410 (long) (cinfo->max_v_samp_factor*data_unit));
412 cinfo->data_units_in_MCU = 0;
414 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
415 compptr = cinfo->cur_comp_info[ci];
416 /* Sampling factors give # of blocks of component in each MCU */
417 compptr->MCU_width = compptr->h_samp_factor;
418 compptr->MCU_height = compptr->v_samp_factor;
419 compptr->MCU_data_units = compptr->MCU_width * compptr->MCU_height;
420 compptr->MCU_sample_width = compptr->MCU_width * data_unit;
421 /* Figure number of non-dummy blocks in last MCU column & row */
422 tmp = (int) (compptr->width_in_data_units % compptr->MCU_width);
423 if (tmp == 0) tmp = compptr->MCU_width;
424 compptr->last_col_width = tmp;
425 tmp = (int) (compptr->height_in_data_units % compptr->MCU_height);
426 if (tmp == 0) tmp = compptr->MCU_height;
427 compptr->last_row_height = tmp;
428 /* Prepare array describing MCU composition */
429 mcublks = compptr->MCU_data_units;
430 if (cinfo->data_units_in_MCU + mcublks > C_MAX_DATA_UNITS_IN_MCU)
431 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
432 while (mcublks-- > 0) {
433 cinfo->MCU_membership[cinfo->data_units_in_MCU++] = ci;
439 /* Convert restart specified in rows to actual MCU count. */
440 /* Note that count must fit in 16 bits, so we provide limiting. */
441 if (cinfo->restart_in_rows > 0) {
442 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
443 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
449 * Per-pass setup.
450 * This is called at the beginning of each pass. We determine which modules
451 * will be active during this pass and give them appropriate start_pass calls.
452 * We also set is_last_pass to indicate whether any more passes will be
453 * required.
456 METHODDEF(void)
457 prepare_for_pass (j_compress_ptr cinfo)
459 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
460 my_master_ptr master = (my_master_ptr) cinfo->master;
462 switch (master->pass_type) {
463 case main_pass:
464 /* Initial pass: will collect input data, and do either Huffman
465 * optimization or data output for the first scan.
467 select_scan_parameters(cinfo);
468 per_scan_setup(cinfo);
469 if (! cinfo->raw_data_in) {
470 (*cinfo->cconvert->start_pass) (cinfo);
471 (*cinfo->downsample->start_pass) (cinfo);
472 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
474 (*cinfo->codec->entropy_start_pass) (cinfo, cinfo->optimize_coding);
475 (*cinfo->codec->start_pass) (cinfo,
476 (master->total_passes > 1 ?
477 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
478 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
479 if (cinfo->optimize_coding) {
480 /* No immediate data output; postpone writing frame/scan headers */
481 master->pub.call_pass_startup = FALSE;
482 } else {
483 /* Will write frame/scan headers at first jpeg_write_scanlines call */
484 master->pub.call_pass_startup = TRUE;
486 break;
487 #ifdef ENTROPY_OPT_SUPPORTED
488 case huff_opt_pass:
489 /* Do Huffman optimization for a scan after the first one. */
490 select_scan_parameters(cinfo);
491 per_scan_setup(cinfo);
492 if ((*cinfo->codec->need_optimization_pass) (cinfo) || cinfo->arith_code) {
493 (*cinfo->codec->entropy_start_pass) (cinfo, TRUE);
494 (*cinfo->codec->start_pass) (cinfo, JBUF_CRANK_DEST);
495 master->pub.call_pass_startup = FALSE;
496 break;
498 /* Special case: Huffman DC refinement scans need no Huffman table
499 * and therefore we can skip the optimization pass for them.
501 master->pass_type = output_pass;
502 master->pass_number++;
503 /*FALLTHROUGH*/
504 #endif
505 case output_pass:
506 /* Do a data-output pass. */
507 /* We need not repeat per-scan setup if prior optimization pass did it. */
508 if (! cinfo->optimize_coding) {
509 select_scan_parameters(cinfo);
510 per_scan_setup(cinfo);
512 (*cinfo->codec->entropy_start_pass) (cinfo, FALSE);
513 (*cinfo->codec->start_pass) (cinfo, JBUF_CRANK_DEST);
514 /* We emit frame/scan headers now */
515 if (master->scan_number == 0)
516 (*cinfo->marker->write_frame_header) (cinfo);
517 (*cinfo->marker->write_scan_header) (cinfo);
518 master->pub.call_pass_startup = FALSE;
519 break;
520 default:
521 ERREXIT(cinfo, JERR_NOT_COMPILED);
524 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
526 /* Set up progress monitor's pass info if present */
527 if (cinfo->progress != NULL) {
528 cinfo->progress->completed_passes = master->pass_number;
529 cinfo->progress->total_passes = master->total_passes;
535 * Special start-of-pass hook.
536 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
537 * In single-pass processing, we need this hook because we don't want to
538 * write frame/scan headers during jpeg_start_compress; we want to let the
539 * application write COM markers etc. between jpeg_start_compress and the
540 * jpeg_write_scanlines loop.
541 * In multi-pass processing, this routine is not used.
544 METHODDEF(void)
545 pass_startup (j_compress_ptr cinfo)
547 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
549 (*cinfo->marker->write_frame_header) (cinfo);
550 (*cinfo->marker->write_scan_header) (cinfo);
555 * Finish up at end of pass.
558 METHODDEF(void)
559 finish_pass_master (j_compress_ptr cinfo)
561 j_lossy_c_ptr lossyc = (j_lossy_c_ptr) cinfo->codec;
562 my_master_ptr master = (my_master_ptr) cinfo->master;
564 /* The entropy coder always needs an end-of-pass call,
565 * either to analyze statistics or to flush its output buffer.
567 (*lossyc->pub.entropy_finish_pass) (cinfo);
569 /* Update state for next pass */
570 switch (master->pass_type) {
571 case main_pass:
572 /* next pass is either output of scan 0 (after optimization)
573 * or output of scan 1 (if no optimization).
575 master->pass_type = output_pass;
576 if (! cinfo->optimize_coding)
577 master->scan_number++;
578 break;
579 case huff_opt_pass:
580 /* next pass is always output of current scan */
581 master->pass_type = output_pass;
582 break;
583 case output_pass:
584 /* next pass is either optimization or output of next scan */
585 if (cinfo->optimize_coding)
586 master->pass_type = huff_opt_pass;
587 master->scan_number++;
588 break;
591 master->pass_number++;
596 * Initialize master compression control.
599 JGLOBAL(void)
600 jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
602 my_master_ptr master;
604 master = (my_master_ptr)
605 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
606 SIZEOF(my_comp_master));
607 cinfo->master = (struct jpeg_comp_master *) master;
608 master->pub.prepare_for_pass = prepare_for_pass;
609 master->pub.pass_startup = pass_startup;
610 master->pub.finish_pass = finish_pass_master;
611 master->pub.is_last_pass = FALSE;
613 cinfo->data_unit = cinfo->lossless ? 1 : DCTSIZE;
615 /* Validate parameters, determine derived values */
616 initial_setup(cinfo);
618 if (cinfo->scan_info != NULL) {
619 #ifdef NEED_SCAN_SCRIPT
620 validate_script(cinfo);
621 #else
622 ERREXIT(cinfo, JERR_NOT_COMPILED);
623 #endif
624 } else {
625 cinfo->process = JPROC_SEQUENTIAL;
626 cinfo->num_scans = 1;
629 if (cinfo->process == JPROC_PROGRESSIVE || /* TEMPORARY HACK ??? */
630 cinfo->process == JPROC_LOSSLESS)
631 cinfo->optimize_coding = TRUE; /* assume default tables no good for
632 * progressive mode or lossless mode */
634 /* Initialize my private state */
635 if (transcode_only) {
636 /* no main pass in transcoding */
637 if (cinfo->optimize_coding)
638 master->pass_type = huff_opt_pass;
639 else
640 master->pass_type = output_pass;
641 } else {
642 /* for normal compression, first pass is always this type: */
643 master->pass_type = main_pass;
645 master->scan_number = 0;
646 master->pass_number = 0;
647 if (cinfo->optimize_coding)
648 master->total_passes = cinfo->num_scans * 2;
649 else
650 master->total_passes = cinfo->num_scans;