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