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