blob: bee0cafc8c018bf4ab315d55c25b51d0339a96e0 [file] [log] [blame]
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00001/*
2 * jcmaster.c
3 *
DRCa73e8702012-12-31 02:52:30 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
Guido Vollbeding989630f2010-01-10 00:00:00 +00006 * Modified 2003-2010 by Guido Vollbeding.
DRCa73e8702012-12-31 02:52:30 +00007 * Modifications:
DRC36a6eec2010-10-08 08:05:44 +00008 * Copyright (C) 2010, D. R. Commander.
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +00009 * For conditions of distribution and use, see the accompanying README file.
10 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 * This file contains master control logic for the JPEG compressor.
Thomas G. Lanebc79e061995-08-02 00:00:00 +000012 * These routines are concerned with parameter validation, initial setup,
13 * and inter-pass control (determining the number of passes and the work
14 * to be done in each pass).
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000015 */
16
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017#define JPEG_INTERNALS
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000018#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000019#include "jpeglib.h"
DRCc04bd3c2010-10-10 02:15:56 +000020#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000021
22
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000023/* Private state */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000024
Thomas G. Lanebc79e061995-08-02 00:00:00 +000025typedef enum {
26 main_pass, /* input data, also do first output step */
27 huff_opt_pass, /* Huffman code optimization pass */
28 output_pass /* data output pass */
29} c_pass_type;
30
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000031typedef struct {
32 struct jpeg_comp_master pub; /* public fields */
33
Thomas G. Lanebc79e061995-08-02 00:00:00 +000034 c_pass_type pass_type; /* the type of the current pass */
35
36 int pass_number; /* # of passes completed */
37 int total_passes; /* total # of passes needed */
38
39 int scan_number; /* current index in scan_info[] */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000040} my_comp_master;
41
42typedef my_comp_master * my_master_ptr;
43
44
45/*
46 * Support routines that do various essential calculations.
47 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000048
DRC36a6eec2010-10-08 08:05:44 +000049#if JPEG_LIB_VERSION >= 70
Guido Vollbeding5996a252009-06-27 00:00:00 +000050/*
51 * Compute JPEG image dimensions and related values.
52 * NOTE: this is exported for possible use by application.
53 * Hence it mustn't do anything that can't be done twice.
54 */
55
56GLOBAL(void)
57jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
58/* Do computations that are needed before master selection phase */
59{
Guido Vollbeding5996a252009-06-27 00:00:00 +000060 /* Hardwire it to "no scaling" */
61 cinfo->jpeg_width = cinfo->image_width;
62 cinfo->jpeg_height = cinfo->image_height;
63 cinfo->min_DCT_h_scaled_size = DCTSIZE;
64 cinfo->min_DCT_v_scaled_size = DCTSIZE;
Guido Vollbeding5996a252009-06-27 00:00:00 +000065}
DRC36a6eec2010-10-08 08:05:44 +000066#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000067
68
Thomas G. Lane489583f1996-02-07 00:00:00 +000069LOCAL(void)
Guido Vollbeding989630f2010-01-10 00:00:00 +000070initial_setup (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000071/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000072{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000073 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000074 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000075 long samplesperrow;
76 JDIMENSION jd_samplesperrow;
77
DRC36a6eec2010-10-08 08:05:44 +000078#if JPEG_LIB_VERSION >= 70
DRCe7fde872011-04-03 07:09:49 +000079#if JPEG_LIB_VERSION >= 80
DRCc04bd3c2010-10-10 02:15:56 +000080 if (!transcode_only)
DRCe7fde872011-04-03 07:09:49 +000081#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +000082 jpeg_calc_jpeg_dimensions(cinfo);
DRC36a6eec2010-10-08 08:05:44 +000083#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000084
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 /* Sanity check on image dimensions */
DRCc04bd3c2010-10-10 02:15:56 +000086 if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 || cinfo->num_components <= 0 || cinfo->input_components <= 0)
88 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
89
90 /* Make sure image isn't bigger than I can handle */
DRCc04bd3c2010-10-10 02:15:56 +000091 if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
92 (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000093 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
94
95 /* Width of an input scanline must be representable as JDIMENSION. */
96 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
97 jd_samplesperrow = (JDIMENSION) samplesperrow;
98 if ((long) jd_samplesperrow != samplesperrow)
99 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
100
101 /* For now, precision must match compiled-in value... */
102 if (cinfo->data_precision != BITS_IN_JSAMPLE)
103 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
104
105 /* Check that number of components won't exceed internal array sizes */
106 if (cinfo->num_components > MAX_COMPONENTS)
107 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
108 MAX_COMPONENTS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000109
110 /* Compute maximum sampling factors; check factor validity */
111 cinfo->max_h_samp_factor = 1;
112 cinfo->max_v_samp_factor = 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
114 ci++, compptr++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000115 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
116 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000117 ERREXIT(cinfo, JERR_BAD_SAMPLING);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000118 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
119 compptr->h_samp_factor);
120 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
121 compptr->v_samp_factor);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000122 }
123
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000124 /* Compute dimensions of components */
125 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
126 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000127 /* Fill in the correct component_index value; don't rely on application */
128 compptr->component_index = ci;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000129 /* For compression, we never do DCT scaling. */
DRC36a6eec2010-10-08 08:05:44 +0000130#if JPEG_LIB_VERSION >= 70
131 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
132#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000133 compptr->DCT_scaled_size = DCTSIZE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000134#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000135 /* Size in DCT blocks */
136 compptr->width_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000137 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000138 (long) (cinfo->max_h_samp_factor * DCTSIZE));
139 compptr->height_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000140 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141 (long) (cinfo->max_v_samp_factor * DCTSIZE));
142 /* Size in samples */
143 compptr->downsampled_width = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000144 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000145 (long) cinfo->max_h_samp_factor);
146 compptr->downsampled_height = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000147 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148 (long) cinfo->max_v_samp_factor);
149 /* Mark component needed (this flag isn't actually used for compression) */
150 compptr->component_needed = TRUE;
151 }
152
153 /* Compute number of fully interleaved MCU rows (number of times that
154 * main controller will call coefficient controller).
155 */
156 cinfo->total_iMCU_rows = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000157 jdiv_round_up((long) cinfo->_jpeg_height,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000158 (long) (cinfo->max_v_samp_factor*DCTSIZE));
159}
160
161
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000162#ifdef C_MULTISCAN_FILES_SUPPORTED
163
Thomas G. Lane489583f1996-02-07 00:00:00 +0000164LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000165validate_script (j_compress_ptr cinfo)
166/* Verify that the scan script in cinfo->scan_info[] is valid; also
167 * determine whether it uses progressive JPEG, and set cinfo->progressive_mode.
168 */
169{
170 const jpeg_scan_info * scanptr;
171 int scanno, ncomps, ci, coefi, thisi;
172 int Ss, Se, Ah, Al;
173 boolean component_sent[MAX_COMPONENTS];
174#ifdef C_PROGRESSIVE_SUPPORTED
175 int * last_bitpos_ptr;
176 int last_bitpos[MAX_COMPONENTS][DCTSIZE2];
177 /* -1 until that coefficient has been seen; then last Al for it */
178#endif
179
180 if (cinfo->num_scans <= 0)
181 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, 0);
182
183 /* For sequential JPEG, all scans must have Ss=0, Se=DCTSIZE2-1;
184 * for progressive JPEG, no scan can have this.
185 */
186 scanptr = cinfo->scan_info;
187 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
188#ifdef C_PROGRESSIVE_SUPPORTED
189 cinfo->progressive_mode = TRUE;
190 last_bitpos_ptr = & last_bitpos[0][0];
191 for (ci = 0; ci < cinfo->num_components; ci++)
192 for (coefi = 0; coefi < DCTSIZE2; coefi++)
193 *last_bitpos_ptr++ = -1;
194#else
195 ERREXIT(cinfo, JERR_NOT_COMPILED);
196#endif
197 } else {
198 cinfo->progressive_mode = FALSE;
199 for (ci = 0; ci < cinfo->num_components; ci++)
200 component_sent[ci] = FALSE;
201 }
202
203 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
204 /* Validate component indexes */
205 ncomps = scanptr->comps_in_scan;
206 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
207 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
208 for (ci = 0; ci < ncomps; ci++) {
209 thisi = scanptr->component_index[ci];
210 if (thisi < 0 || thisi >= cinfo->num_components)
211 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
212 /* Components must appear in SOF order within each scan */
213 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
214 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
215 }
216 /* Validate progression parameters */
217 Ss = scanptr->Ss;
218 Se = scanptr->Se;
219 Ah = scanptr->Ah;
220 Al = scanptr->Al;
221 if (cinfo->progressive_mode) {
222#ifdef C_PROGRESSIVE_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000223 /* The JPEG spec simply gives the ranges 0..13 for Ah and Al, but that
224 * seems wrong: the upper bound ought to depend on data precision.
225 * Perhaps they really meant 0..N+1 for N-bit precision.
226 * Here we allow 0..10 for 8-bit data; Al larger than 10 results in
227 * out-of-range reconstructed DC values during the first DC scan,
228 * which might cause problems for some decoders.
229 */
230#if BITS_IN_JSAMPLE == 8
231#define MAX_AH_AL 10
232#else
233#define MAX_AH_AL 13
234#endif
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000235 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000236 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000237 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
238 if (Ss == 0) {
239 if (Se != 0) /* DC and AC together not OK */
240 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
241 } else {
242 if (ncomps != 1) /* AC scans must be for only one component */
243 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
244 }
245 for (ci = 0; ci < ncomps; ci++) {
246 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
247 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
248 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
249 for (coefi = Ss; coefi <= Se; coefi++) {
250 if (last_bitpos_ptr[coefi] < 0) {
251 /* first scan of this coefficient */
252 if (Ah != 0)
253 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
254 } else {
255 /* not first scan */
256 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
257 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
258 }
259 last_bitpos_ptr[coefi] = Al;
260 }
261 }
262#endif
263 } else {
264 /* For sequential JPEG, all progression parameters must be these: */
265 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
266 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
267 /* Make sure components are not sent twice */
268 for (ci = 0; ci < ncomps; ci++) {
269 thisi = scanptr->component_index[ci];
270 if (component_sent[thisi])
271 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
272 component_sent[thisi] = TRUE;
273 }
274 }
275 }
276
277 /* Now verify that everything got sent. */
278 if (cinfo->progressive_mode) {
279#ifdef C_PROGRESSIVE_SUPPORTED
280 /* For progressive mode, we only check that at least some DC data
281 * got sent for each component; the spec does not require that all bits
282 * of all coefficients be transmitted. Would it be wiser to enforce
283 * transmission of all coefficient bits??
284 */
285 for (ci = 0; ci < cinfo->num_components; ci++) {
286 if (last_bitpos[ci][0] < 0)
287 ERREXIT(cinfo, JERR_MISSING_DATA);
288 }
289#endif
290 } else {
291 for (ci = 0; ci < cinfo->num_components; ci++) {
292 if (! component_sent[ci])
293 ERREXIT(cinfo, JERR_MISSING_DATA);
294 }
295 }
296}
297
298#endif /* C_MULTISCAN_FILES_SUPPORTED */
299
300
Thomas G. Lane489583f1996-02-07 00:00:00 +0000301LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000302select_scan_parameters (j_compress_ptr cinfo)
303/* Set up the scan parameters for the current scan */
304{
305 int ci;
306
307#ifdef C_MULTISCAN_FILES_SUPPORTED
308 if (cinfo->scan_info != NULL) {
309 /* Prepare for current scan --- the script is already validated */
310 my_master_ptr master = (my_master_ptr) cinfo->master;
311 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
312
313 cinfo->comps_in_scan = scanptr->comps_in_scan;
314 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
315 cinfo->cur_comp_info[ci] =
316 &cinfo->comp_info[scanptr->component_index[ci]];
317 }
318 cinfo->Ss = scanptr->Ss;
319 cinfo->Se = scanptr->Se;
320 cinfo->Ah = scanptr->Ah;
321 cinfo->Al = scanptr->Al;
322 }
323 else
324#endif
325 {
326 /* Prepare for single sequential-JPEG scan containing all components */
327 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
328 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
329 MAX_COMPS_IN_SCAN);
330 cinfo->comps_in_scan = cinfo->num_components;
331 for (ci = 0; ci < cinfo->num_components; ci++) {
332 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
333 }
334 cinfo->Ss = 0;
335 cinfo->Se = DCTSIZE2-1;
336 cinfo->Ah = 0;
337 cinfo->Al = 0;
338 }
339}
340
341
Thomas G. Lane489583f1996-02-07 00:00:00 +0000342LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000343per_scan_setup (j_compress_ptr cinfo)
344/* Do computations that are needed before processing a JPEG scan */
345/* cinfo->comps_in_scan and cinfo->cur_comp_info[] are already set */
346{
347 int ci, mcublks, tmp;
348 jpeg_component_info *compptr;
349
350 if (cinfo->comps_in_scan == 1) {
351
352 /* Noninterleaved (single-component) scan */
353 compptr = cinfo->cur_comp_info[0];
354
355 /* Overall image size in MCUs */
356 cinfo->MCUs_per_row = compptr->width_in_blocks;
357 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
358
359 /* For noninterleaved scan, always one block per MCU */
360 compptr->MCU_width = 1;
361 compptr->MCU_height = 1;
362 compptr->MCU_blocks = 1;
363 compptr->MCU_sample_width = DCTSIZE;
364 compptr->last_col_width = 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000365 /* For noninterleaved scans, it is convenient to define last_row_height
366 * as the number of block rows present in the last iMCU row.
367 */
368 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
369 if (tmp == 0) tmp = compptr->v_samp_factor;
370 compptr->last_row_height = tmp;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000371
372 /* Prepare array describing MCU composition */
373 cinfo->blocks_in_MCU = 1;
374 cinfo->MCU_membership[0] = 0;
375
376 } else {
377
378 /* Interleaved (multi-component) scan */
379 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
380 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
381 MAX_COMPS_IN_SCAN);
382
383 /* Overall image size in MCUs */
384 cinfo->MCUs_per_row = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000385 jdiv_round_up((long) cinfo->_jpeg_width,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000386 (long) (cinfo->max_h_samp_factor*DCTSIZE));
387 cinfo->MCU_rows_in_scan = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000388 jdiv_round_up((long) cinfo->_jpeg_height,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 (long) (cinfo->max_v_samp_factor*DCTSIZE));
390
391 cinfo->blocks_in_MCU = 0;
392
393 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
394 compptr = cinfo->cur_comp_info[ci];
395 /* Sampling factors give # of blocks of component in each MCU */
396 compptr->MCU_width = compptr->h_samp_factor;
397 compptr->MCU_height = compptr->v_samp_factor;
398 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
399 compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
400 /* Figure number of non-dummy blocks in last MCU column & row */
401 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
402 if (tmp == 0) tmp = compptr->MCU_width;
403 compptr->last_col_width = tmp;
404 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
405 if (tmp == 0) tmp = compptr->MCU_height;
406 compptr->last_row_height = tmp;
407 /* Prepare array describing MCU composition */
408 mcublks = compptr->MCU_blocks;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000409 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000410 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
411 while (mcublks-- > 0) {
412 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
413 }
414 }
415
416 }
417
418 /* Convert restart specified in rows to actual MCU count. */
419 /* Note that count must fit in 16 bits, so we provide limiting. */
420 if (cinfo->restart_in_rows > 0) {
421 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
422 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000423 }
424}
425
426
427/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000428 * Per-pass setup.
429 * This is called at the beginning of each pass. We determine which modules
430 * will be active during this pass and give them appropriate start_pass calls.
431 * We also set is_last_pass to indicate whether any more passes will be
432 * required.
433 */
434
Thomas G. Lane489583f1996-02-07 00:00:00 +0000435METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436prepare_for_pass (j_compress_ptr cinfo)
437{
438 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000439
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000440 switch (master->pass_type) {
441 case main_pass:
442 /* Initial pass: will collect input data, and do either Huffman
443 * optimization or data output for the first scan.
444 */
445 select_scan_parameters(cinfo);
446 per_scan_setup(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000447 if (! cinfo->raw_data_in) {
448 (*cinfo->cconvert->start_pass) (cinfo);
449 (*cinfo->downsample->start_pass) (cinfo);
450 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
451 }
452 (*cinfo->fdct->start_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000453 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
454 (*cinfo->coef->start_pass) (cinfo,
455 (master->total_passes > 1 ?
456 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000457 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000458 if (cinfo->optimize_coding) {
459 /* No immediate data output; postpone writing frame/scan headers */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000460 master->pub.call_pass_startup = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000461 } else {
462 /* Will write frame/scan headers at first jpeg_write_scanlines call */
463 master->pub.call_pass_startup = TRUE;
464 }
465 break;
466#ifdef ENTROPY_OPT_SUPPORTED
467 case huff_opt_pass:
468 /* Do Huffman optimization for a scan after the first one. */
469 select_scan_parameters(cinfo);
470 per_scan_setup(cinfo);
471 if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000472 (*cinfo->entropy->start_pass) (cinfo, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000473 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000474 master->pub.call_pass_startup = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 break;
476 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000477 /* Special case: Huffman DC refinement scans need no Huffman table
478 * and therefore we can skip the optimization pass for them.
479 */
480 master->pass_type = output_pass;
481 master->pass_number++;
482 /*FALLTHROUGH*/
483#endif
484 case output_pass:
485 /* Do a data-output pass. */
486 /* We need not repeat per-scan setup if prior optimization pass did it. */
487 if (! cinfo->optimize_coding) {
488 select_scan_parameters(cinfo);
489 per_scan_setup(cinfo);
490 }
491 (*cinfo->entropy->start_pass) (cinfo, FALSE);
492 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
493 /* We emit frame/scan headers now */
494 if (master->scan_number == 0)
495 (*cinfo->marker->write_frame_header) (cinfo);
496 (*cinfo->marker->write_scan_header) (cinfo);
497 master->pub.call_pass_startup = FALSE;
498 break;
499 default:
500 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000501 }
502
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000503 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
504
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000505 /* Set up progress monitor's pass info if present */
506 if (cinfo->progress != NULL) {
507 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000508 cinfo->progress->total_passes = master->total_passes;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000509 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000510}
511
512
513/*
514 * Special start-of-pass hook.
515 * This is called by jpeg_write_scanlines if call_pass_startup is TRUE.
516 * In single-pass processing, we need this hook because we don't want to
517 * write frame/scan headers during jpeg_start_compress; we want to let the
518 * application write COM markers etc. between jpeg_start_compress and the
519 * jpeg_write_scanlines loop.
520 * In multi-pass processing, this routine is not used.
521 */
522
Thomas G. Lane489583f1996-02-07 00:00:00 +0000523METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000524pass_startup (j_compress_ptr cinfo)
525{
526 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
527
528 (*cinfo->marker->write_frame_header) (cinfo);
529 (*cinfo->marker->write_scan_header) (cinfo);
530}
531
532
533/*
534 * Finish up at end of pass.
535 */
536
Thomas G. Lane489583f1996-02-07 00:00:00 +0000537METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000538finish_pass_master (j_compress_ptr cinfo)
539{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000540 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000541
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000542 /* The entropy coder always needs an end-of-pass call,
543 * either to analyze statistics or to flush its output buffer.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000544 */
545 (*cinfo->entropy->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000546
547 /* Update state for next pass */
548 switch (master->pass_type) {
549 case main_pass:
550 /* next pass is either output of scan 0 (after optimization)
551 * or output of scan 1 (if no optimization).
552 */
553 master->pass_type = output_pass;
554 if (! cinfo->optimize_coding)
555 master->scan_number++;
556 break;
557 case huff_opt_pass:
558 /* next pass is always output of current scan */
559 master->pass_type = output_pass;
560 break;
561 case output_pass:
562 /* next pass is either optimization or output of next scan */
563 if (cinfo->optimize_coding)
564 master->pass_type = huff_opt_pass;
565 master->scan_number++;
566 break;
567 }
568
569 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000570}
571
572
573/*
574 * Initialize master compression control.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000575 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000576
Thomas G. Lane489583f1996-02-07 00:00:00 +0000577GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000578jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000579{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000580 my_master_ptr master;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000581
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000582 master = (my_master_ptr)
583 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
584 SIZEOF(my_comp_master));
585 cinfo->master = (struct jpeg_comp_master *) master;
586 master->pub.prepare_for_pass = prepare_for_pass;
587 master->pub.pass_startup = pass_startup;
588 master->pub.finish_pass = finish_pass_master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000589 master->pub.is_last_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000590
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000591 /* Validate parameters, determine derived values */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000592 initial_setup(cinfo, transcode_only);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000593
594 if (cinfo->scan_info != NULL) {
595#ifdef C_MULTISCAN_FILES_SUPPORTED
596 validate_script(cinfo);
597#else
598 ERREXIT(cinfo, JERR_NOT_COMPILED);
599#endif
600 } else {
601 cinfo->progressive_mode = FALSE;
602 cinfo->num_scans = 1;
603 }
604
605 if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
606 cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
607
608 /* Initialize my private state */
609 if (transcode_only) {
610 /* no main pass in transcoding */
611 if (cinfo->optimize_coding)
612 master->pass_type = huff_opt_pass;
613 else
614 master->pass_type = output_pass;
615 } else {
616 /* for normal compression, first pass is always this type: */
617 master->pass_type = main_pass;
618 }
619 master->scan_number = 0;
620 master->pass_number = 0;
621 if (cinfo->optimize_coding)
622 master->total_passes = cinfo->num_scans * 2;
623 else
624 master->total_passes = cinfo->num_scans;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000625}