blob: 3ca346ca80d99c17555d87a10a3321c098120cce [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"
DRCc04bd3c2010-10-10 02:15:56 +000019#include "jpegcomp.h"
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000020
21
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022/* Private state */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000023
Thomas G. Lanebc79e061995-08-02 00:00:00 +000024typedef 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
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030typedef struct {
31 struct jpeg_comp_master pub; /* public fields */
32
Thomas G. Lanebc79e061995-08-02 00:00:00 +000033 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[] */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000039} my_comp_master;
40
41typedef my_comp_master * my_master_ptr;
42
43
44/*
45 * Support routines that do various essential calculations.
46 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000047
DRC36a6eec2010-10-08 08:05:44 +000048#if JPEG_LIB_VERSION >= 70
Guido Vollbeding5996a252009-06-27 00:00:00 +000049/*
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
55GLOBAL(void)
56jpeg_calc_jpeg_dimensions (j_compress_ptr cinfo)
57/* Do computations that are needed before master selection phase */
58{
Guido Vollbeding5996a252009-06-27 00:00:00 +000059 /* 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;
Guido Vollbeding5996a252009-06-27 00:00:00 +000064}
DRC36a6eec2010-10-08 08:05:44 +000065#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000066
67
Thomas G. Lane489583f1996-02-07 00:00:00 +000068LOCAL(void)
Guido Vollbeding989630f2010-01-10 00:00:00 +000069initial_setup (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000070/* Do computations that are needed before master selection phase */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000071{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000072 int ci;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +000073 jpeg_component_info *compptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000074 long samplesperrow;
75 JDIMENSION jd_samplesperrow;
76
DRC36a6eec2010-10-08 08:05:44 +000077#if JPEG_LIB_VERSION >= 70
DRCe7fde872011-04-03 07:09:49 +000078#if JPEG_LIB_VERSION >= 80
DRCc04bd3c2010-10-10 02:15:56 +000079 if (!transcode_only)
DRCe7fde872011-04-03 07:09:49 +000080#endif
Guido Vollbeding989630f2010-01-10 00:00:00 +000081 jpeg_calc_jpeg_dimensions(cinfo);
DRC36a6eec2010-10-08 08:05:44 +000082#endif
Guido Vollbeding5996a252009-06-27 00:00:00 +000083
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000084 /* Sanity check on image dimensions */
DRCc04bd3c2010-10-10 02:15:56 +000085 if (cinfo->_jpeg_height <= 0 || cinfo->_jpeg_width <= 0
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000086 || cinfo->num_components <= 0 || cinfo->input_components <= 0)
87 ERREXIT(cinfo, JERR_EMPTY_IMAGE);
88
89 /* Make sure image isn't bigger than I can handle */
DRCc04bd3c2010-10-10 02:15:56 +000090 if ((long) cinfo->_jpeg_height > (long) JPEG_MAX_DIMENSION ||
91 (long) cinfo->_jpeg_width > (long) JPEG_MAX_DIMENSION)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
93
94 /* Width of an input scanline must be representable as JDIMENSION. */
95 samplesperrow = (long) cinfo->image_width * (long) cinfo->input_components;
96 jd_samplesperrow = (JDIMENSION) samplesperrow;
97 if ((long) jd_samplesperrow != samplesperrow)
98 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
99
100 /* For now, precision must match compiled-in value... */
101 if (cinfo->data_precision != BITS_IN_JSAMPLE)
102 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
103
104 /* Check that number of components won't exceed internal array sizes */
105 if (cinfo->num_components > MAX_COMPONENTS)
106 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
107 MAX_COMPONENTS);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000108
109 /* Compute maximum sampling factors; check factor validity */
110 cinfo->max_h_samp_factor = 1;
111 cinfo->max_v_samp_factor = 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000112 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
113 ci++, compptr++) {
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000114 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
115 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116 ERREXIT(cinfo, JERR_BAD_SAMPLING);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000117 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
118 compptr->h_samp_factor);
119 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
120 compptr->v_samp_factor);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000121 }
122
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000123 /* Compute dimensions of components */
124 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
125 ci++, compptr++) {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000126 /* Fill in the correct component_index value; don't rely on application */
127 compptr->component_index = ci;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000128 /* For compression, we never do DCT scaling. */
DRC36a6eec2010-10-08 08:05:44 +0000129#if JPEG_LIB_VERSION >= 70
130 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
131#else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000132 compptr->DCT_scaled_size = DCTSIZE;
Guido Vollbeding5996a252009-06-27 00:00:00 +0000133#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000134 /* Size in DCT blocks */
135 compptr->width_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000136 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000137 (long) (cinfo->max_h_samp_factor * DCTSIZE));
138 compptr->height_in_blocks = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000139 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000140 (long) (cinfo->max_v_samp_factor * DCTSIZE));
141 /* Size in samples */
142 compptr->downsampled_width = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000143 jdiv_round_up((long) cinfo->_jpeg_width * (long) compptr->h_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144 (long) cinfo->max_h_samp_factor);
145 compptr->downsampled_height = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000146 jdiv_round_up((long) cinfo->_jpeg_height * (long) compptr->v_samp_factor,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147 (long) cinfo->max_v_samp_factor);
148 /* Mark component needed (this flag isn't actually used for compression) */
149 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 cinfo->total_iMCU_rows = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000156 jdiv_round_up((long) cinfo->_jpeg_height,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000157 (long) (cinfo->max_v_samp_factor*DCTSIZE));
158}
159
160
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000161#ifdef C_MULTISCAN_FILES_SUPPORTED
162
Thomas G. Lane489583f1996-02-07 00:00:00 +0000163LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000164validate_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 if (cinfo->num_scans <= 0)
180 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 scanptr = cinfo->scan_info;
186 if (scanptr->Ss != 0 || scanptr->Se != DCTSIZE2-1) {
187#ifdef C_PROGRESSIVE_SUPPORTED
188 cinfo->progressive_mode = TRUE;
189 last_bitpos_ptr = & last_bitpos[0][0];
190 for (ci = 0; ci < cinfo->num_components; ci++)
191 for (coefi = 0; coefi < DCTSIZE2; coefi++)
192 *last_bitpos_ptr++ = -1;
193#else
194 ERREXIT(cinfo, JERR_NOT_COMPILED);
195#endif
196 } else {
197 cinfo->progressive_mode = FALSE;
198 for (ci = 0; ci < cinfo->num_components; ci++)
199 component_sent[ci] = FALSE;
200 }
201
202 for (scanno = 1; scanno <= cinfo->num_scans; scanptr++, scanno++) {
203 /* Validate component indexes */
204 ncomps = scanptr->comps_in_scan;
205 if (ncomps <= 0 || ncomps > MAX_COMPS_IN_SCAN)
206 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, ncomps, MAX_COMPS_IN_SCAN);
207 for (ci = 0; ci < ncomps; ci++) {
208 thisi = scanptr->component_index[ci];
209 if (thisi < 0 || thisi >= cinfo->num_components)
210 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
211 /* Components must appear in SOF order within each scan */
212 if (ci > 0 && thisi <= scanptr->component_index[ci-1])
213 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
214 }
215 /* Validate progression parameters */
216 Ss = scanptr->Ss;
217 Se = scanptr->Se;
218 Ah = scanptr->Ah;
219 Al = scanptr->Al;
220 if (cinfo->progressive_mode) {
221#ifdef C_PROGRESSIVE_SUPPORTED
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000222 /* 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
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000234 if (Ss < 0 || Ss >= DCTSIZE2 || Se < Ss || Se >= DCTSIZE2 ||
Thomas G. Lane5ead57a1998-03-27 00:00:00 +0000235 Ah < 0 || Ah > MAX_AH_AL || Al < 0 || Al > MAX_AH_AL)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000236 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
237 if (Ss == 0) {
238 if (Se != 0) /* DC and AC together not OK */
239 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
240 } else {
241 if (ncomps != 1) /* AC scans must be for only one component */
242 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
243 }
244 for (ci = 0; ci < ncomps; ci++) {
245 last_bitpos_ptr = & last_bitpos[scanptr->component_index[ci]][0];
246 if (Ss != 0 && last_bitpos_ptr[0] < 0) /* AC without prior DC scan */
247 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
248 for (coefi = Ss; coefi <= Se; coefi++) {
249 if (last_bitpos_ptr[coefi] < 0) {
250 /* first scan of this coefficient */
251 if (Ah != 0)
252 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
253 } else {
254 /* not first scan */
255 if (Ah != last_bitpos_ptr[coefi] || Al != Ah-1)
256 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
257 }
258 last_bitpos_ptr[coefi] = Al;
259 }
260 }
261#endif
262 } else {
263 /* For sequential JPEG, all progression parameters must be these: */
264 if (Ss != 0 || Se != DCTSIZE2-1 || Ah != 0 || Al != 0)
265 ERREXIT1(cinfo, JERR_BAD_PROG_SCRIPT, scanno);
266 /* Make sure components are not sent twice */
267 for (ci = 0; ci < ncomps; ci++) {
268 thisi = scanptr->component_index[ci];
269 if (component_sent[thisi])
270 ERREXIT1(cinfo, JERR_BAD_SCAN_SCRIPT, scanno);
271 component_sent[thisi] = TRUE;
272 }
273 }
274 }
275
276 /* Now verify that everything got sent. */
277 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 for (ci = 0; ci < cinfo->num_components; ci++) {
285 if (last_bitpos[ci][0] < 0)
286 ERREXIT(cinfo, JERR_MISSING_DATA);
287 }
288#endif
289 } else {
290 for (ci = 0; ci < cinfo->num_components; ci++) {
291 if (! component_sent[ci])
292 ERREXIT(cinfo, JERR_MISSING_DATA);
293 }
294 }
295}
296
297#endif /* C_MULTISCAN_FILES_SUPPORTED */
298
299
Thomas G. Lane489583f1996-02-07 00:00:00 +0000300LOCAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000301select_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 if (cinfo->scan_info != NULL) {
308 /* Prepare for current scan --- the script is already validated */
309 my_master_ptr master = (my_master_ptr) cinfo->master;
310 const jpeg_scan_info * scanptr = cinfo->scan_info + master->scan_number;
311
312 cinfo->comps_in_scan = scanptr->comps_in_scan;
313 for (ci = 0; ci < scanptr->comps_in_scan; ci++) {
314 cinfo->cur_comp_info[ci] =
315 &cinfo->comp_info[scanptr->component_index[ci]];
316 }
317 cinfo->Ss = scanptr->Ss;
318 cinfo->Se = scanptr->Se;
319 cinfo->Ah = scanptr->Ah;
320 cinfo->Al = scanptr->Al;
321 }
322 else
323#endif
324 {
325 /* Prepare for single sequential-JPEG scan containing all components */
326 if (cinfo->num_components > MAX_COMPS_IN_SCAN)
327 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
328 MAX_COMPS_IN_SCAN);
329 cinfo->comps_in_scan = cinfo->num_components;
330 for (ci = 0; ci < cinfo->num_components; ci++) {
331 cinfo->cur_comp_info[ci] = &cinfo->comp_info[ci];
332 }
333 cinfo->Ss = 0;
334 cinfo->Se = DCTSIZE2-1;
335 cinfo->Ah = 0;
336 cinfo->Al = 0;
337 }
338}
339
340
Thomas G. Lane489583f1996-02-07 00:00:00 +0000341LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000342per_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 if (cinfo->comps_in_scan == 1) {
350
351 /* Noninterleaved (single-component) scan */
352 compptr = cinfo->cur_comp_info[0];
353
354 /* Overall image size in MCUs */
355 cinfo->MCUs_per_row = compptr->width_in_blocks;
356 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
357
358 /* For noninterleaved scan, always one block per MCU */
359 compptr->MCU_width = 1;
360 compptr->MCU_height = 1;
361 compptr->MCU_blocks = 1;
362 compptr->MCU_sample_width = DCTSIZE;
363 compptr->last_col_width = 1;
Thomas G. Lanea8b67c41995-03-15 00:00:00 +0000364 /* 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 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
368 if (tmp == 0) tmp = compptr->v_samp_factor;
369 compptr->last_row_height = tmp;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370
371 /* Prepare array describing MCU composition */
372 cinfo->blocks_in_MCU = 1;
373 cinfo->MCU_membership[0] = 0;
374
375 } else {
376
377 /* Interleaved (multi-component) scan */
378 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
379 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
380 MAX_COMPS_IN_SCAN);
381
382 /* Overall image size in MCUs */
383 cinfo->MCUs_per_row = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000384 jdiv_round_up((long) cinfo->_jpeg_width,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000385 (long) (cinfo->max_h_samp_factor*DCTSIZE));
386 cinfo->MCU_rows_in_scan = (JDIMENSION)
DRCc04bd3c2010-10-10 02:15:56 +0000387 jdiv_round_up((long) cinfo->_jpeg_height,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000388 (long) (cinfo->max_v_samp_factor*DCTSIZE));
389
390 cinfo->blocks_in_MCU = 0;
391
392 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
393 compptr = cinfo->cur_comp_info[ci];
394 /* Sampling factors give # of blocks of component in each MCU */
395 compptr->MCU_width = compptr->h_samp_factor;
396 compptr->MCU_height = compptr->v_samp_factor;
397 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
398 compptr->MCU_sample_width = compptr->MCU_width * DCTSIZE;
399 /* Figure number of non-dummy blocks in last MCU column & row */
400 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
401 if (tmp == 0) tmp = compptr->MCU_width;
402 compptr->last_col_width = tmp;
403 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
404 if (tmp == 0) tmp = compptr->MCU_height;
405 compptr->last_row_height = tmp;
406 /* Prepare array describing MCU composition */
407 mcublks = compptr->MCU_blocks;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000408 if (cinfo->blocks_in_MCU + mcublks > C_MAX_BLOCKS_IN_MCU)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000409 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
410 while (mcublks-- > 0) {
411 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 if (cinfo->restart_in_rows > 0) {
420 long nominal = (long) cinfo->restart_in_rows * (long) cinfo->MCUs_per_row;
421 cinfo->restart_interval = (unsigned int) MIN(nominal, 65535L);
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000422 }
423}
424
425
426/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000427 * 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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000434METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000435prepare_for_pass (j_compress_ptr cinfo)
436{
437 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000439 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 select_scan_parameters(cinfo);
445 per_scan_setup(cinfo);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000446 if (! cinfo->raw_data_in) {
447 (*cinfo->cconvert->start_pass) (cinfo);
448 (*cinfo->downsample->start_pass) (cinfo);
449 (*cinfo->prep->start_pass) (cinfo, JBUF_PASS_THRU);
450 }
451 (*cinfo->fdct->start_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000452 (*cinfo->entropy->start_pass) (cinfo, cinfo->optimize_coding);
453 (*cinfo->coef->start_pass) (cinfo,
454 (master->total_passes > 1 ?
455 JBUF_SAVE_AND_PASS : JBUF_PASS_THRU));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000456 (*cinfo->main->start_pass) (cinfo, JBUF_PASS_THRU);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000457 if (cinfo->optimize_coding) {
458 /* No immediate data output; postpone writing frame/scan headers */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000459 master->pub.call_pass_startup = FALSE;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000460 } else {
461 /* Will write frame/scan headers at first jpeg_write_scanlines call */
462 master->pub.call_pass_startup = TRUE;
463 }
464 break;
465#ifdef ENTROPY_OPT_SUPPORTED
466 case huff_opt_pass:
467 /* Do Huffman optimization for a scan after the first one. */
468 select_scan_parameters(cinfo);
469 per_scan_setup(cinfo);
470 if (cinfo->Ss != 0 || cinfo->Ah == 0 || cinfo->arith_code) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000471 (*cinfo->entropy->start_pass) (cinfo, TRUE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000472 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000473 master->pub.call_pass_startup = FALSE;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000474 break;
475 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000476 /* Special case: Huffman DC refinement scans need no Huffman table
477 * and therefore we can skip the optimization pass for them.
478 */
479 master->pass_type = output_pass;
480 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 if (! cinfo->optimize_coding) {
487 select_scan_parameters(cinfo);
488 per_scan_setup(cinfo);
489 }
490 (*cinfo->entropy->start_pass) (cinfo, FALSE);
491 (*cinfo->coef->start_pass) (cinfo, JBUF_CRANK_DEST);
492 /* We emit frame/scan headers now */
493 if (master->scan_number == 0)
494 (*cinfo->marker->write_frame_header) (cinfo);
495 (*cinfo->marker->write_scan_header) (cinfo);
496 master->pub.call_pass_startup = FALSE;
497 break;
498 default:
499 ERREXIT(cinfo, JERR_NOT_COMPILED);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000500 }
501
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000502 master->pub.is_last_pass = (master->pass_number == master->total_passes-1);
503
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000504 /* Set up progress monitor's pass info if present */
505 if (cinfo->progress != NULL) {
506 cinfo->progress->completed_passes = master->pass_number;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000507 cinfo->progress->total_passes = master->total_passes;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000508 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000509}
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
Thomas G. Lane489583f1996-02-07 00:00:00 +0000522METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000523pass_startup (j_compress_ptr cinfo)
524{
525 cinfo->master->call_pass_startup = FALSE; /* reset flag so call only once */
526
527 (*cinfo->marker->write_frame_header) (cinfo);
528 (*cinfo->marker->write_scan_header) (cinfo);
529}
530
531
532/*
533 * Finish up at end of pass.
534 */
535
Thomas G. Lane489583f1996-02-07 00:00:00 +0000536METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000537finish_pass_master (j_compress_ptr cinfo)
538{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000539 my_master_ptr master = (my_master_ptr) cinfo->master;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000540
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000541 /* The entropy coder always needs an end-of-pass call,
542 * either to analyze statistics or to flush its output buffer.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000543 */
544 (*cinfo->entropy->finish_pass) (cinfo);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000545
546 /* Update state for next pass */
547 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 master->pass_type = output_pass;
553 if (! cinfo->optimize_coding)
554 master->scan_number++;
555 break;
556 case huff_opt_pass:
557 /* next pass is always output of current scan */
558 master->pass_type = output_pass;
559 break;
560 case output_pass:
561 /* next pass is either optimization or output of next scan */
562 if (cinfo->optimize_coding)
563 master->pass_type = huff_opt_pass;
564 master->scan_number++;
565 break;
566 }
567
568 master->pass_number++;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000569}
570
571
572/*
573 * Initialize master compression control.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000574 */
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000575
Thomas G. Lane489583f1996-02-07 00:00:00 +0000576GLOBAL(void)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000577jinit_c_master_control (j_compress_ptr cinfo, boolean transcode_only)
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000578{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000579 my_master_ptr master;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000580
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000581 master = (my_master_ptr)
582 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
583 SIZEOF(my_comp_master));
584 cinfo->master = (struct jpeg_comp_master *) master;
585 master->pub.prepare_for_pass = prepare_for_pass;
586 master->pub.pass_startup = pass_startup;
587 master->pub.finish_pass = finish_pass_master;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000588 master->pub.is_last_pass = FALSE;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000589
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000590 /* Validate parameters, determine derived values */
Guido Vollbeding989630f2010-01-10 00:00:00 +0000591 initial_setup(cinfo, transcode_only);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000592
593 if (cinfo->scan_info != NULL) {
594#ifdef C_MULTISCAN_FILES_SUPPORTED
595 validate_script(cinfo);
596#else
597 ERREXIT(cinfo, JERR_NOT_COMPILED);
598#endif
599 } else {
600 cinfo->progressive_mode = FALSE;
601 cinfo->num_scans = 1;
602 }
603
604 if (cinfo->progressive_mode) /* TEMPORARY HACK ??? */
605 cinfo->optimize_coding = TRUE; /* assume default tables no good for progressive mode */
606
607 /* Initialize my private state */
608 if (transcode_only) {
609 /* no main pass in transcoding */
610 if (cinfo->optimize_coding)
611 master->pass_type = huff_opt_pass;
612 else
613 master->pass_type = output_pass;
614 } else {
615 /* for normal compression, first pass is always this type: */
616 master->pass_type = main_pass;
617 }
618 master->scan_number = 0;
619 master->pass_number = 0;
620 if (cinfo->optimize_coding)
621 master->total_passes = cinfo->num_scans * 2;
622 else
623 master->total_passes = cinfo->num_scans;
Thomas G. Lane2cbeb8a1991-10-07 00:00:00 +0000624}