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