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