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
21 #include "jlossy.h" /* Private declarations for lossy codec */
27 main_pass
, /* input data, also do first output step */
28 huff_opt_pass
, /* Huffman code optimization pass */
29 output_pass
/* data output pass */
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[] */
43 typedef my_comp_master
* my_master_ptr
;
47 * Support routines that do various essential calculations.
51 initial_setup (j_compress_ptr cinfo
)
52 /* Do computations that are needed before master selection phase */
55 jpeg_component_info
*compptr
;
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
,
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
;
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
;
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
135 #ifdef C_LOSSLESS_SUPPORTED
136 #define NEED_SCAN_SCRIPT
140 #ifdef NEED_SCAN_SCRIPT
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
;
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 */
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
);
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
;
173 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
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;
187 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
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 */
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
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
;
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
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
);
250 if (Se
!= 0) /* DC and AC together not OK */
251 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
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 */
264 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
267 if (Ah
!= last_bitpos_ptr
[coefi
] || Al
!= Ah
-1)
268 ERREXIT1(cinfo
, JERR_BAD_PROG_SCRIPT
, scanno
);
270 last_bitpos_ptr
[coefi
] = Al
;
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
);
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 */
313 select_scan_parameters (j_compress_ptr cinfo
)
314 /* Set up the scan parameters for the current scan */
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
;
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
,
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
);
352 cinfo
->process
= JPROC_SEQUENTIAL
;
354 cinfo
->Se
= DCTSIZE2
-1;
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;
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
,
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);
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
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
) {
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
;
483 /* Will write frame/scan headers at first jpeg_write_scanlines call */
484 master
->pub
.call_pass_startup
= TRUE
;
487 #ifdef ENTROPY_OPT_SUPPORTED
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
;
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
++;
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
;
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.
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.
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
) {
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
++;
580 /* next pass is always output of current scan */
581 master
->pass_type
= 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
++;
591 master
->pass_number
++;
596 * Initialize master compression control.
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
);
622 ERREXIT(cinfo
, JERR_NOT_COMPILED
);
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
;
640 master
->pass_type
= output_pass
;
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;
650 master
->total_passes
= cinfo
->num_scans
;