]>
Commit | Line | Data |
---|---|---|
e1929140 RR |
1 | /* |
2 | * jdinput.c | |
3 | * | |
4 | * Copyright (C) 1991-1997, Thomas G. Lane. | |
5 | * This file is part of the Independent JPEG Group's software. | |
6 | * For conditions of distribution and use, see the accompanying README file. | |
7 | * | |
8 | * This file contains input control logic for the JPEG decompressor. | |
9 | * These routines are concerned with controlling the decompressor's input | |
10 | * processing (marker reading and coefficient decoding). The actual input | |
11 | * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. | |
12 | */ | |
13 | ||
14 | #define JPEG_INTERNALS | |
15 | #include "jinclude.h" | |
16 | #include "jpeglib.h" | |
17 | ||
7bfbbc0e DW |
18 | #if defined(__VISAGECPP__) |
19 | /* Visual Age fixups for multiple declarations */ | |
20 | # define start_input_pass start_input_pass2 /* already in jcmaint.c */ | |
21 | #endif | |
e1929140 RR |
22 | |
23 | /* Private state */ | |
24 | ||
25 | typedef struct { | |
26 | struct jpeg_input_controller pub; /* public fields */ | |
27 | ||
28 | boolean inheaders; /* TRUE until first SOS is reached */ | |
29 | } my_input_controller; | |
30 | ||
31 | typedef my_input_controller * my_inputctl_ptr; | |
32 | ||
33 | ||
34 | /* Forward declarations */ | |
35 | METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); | |
36 | ||
37 | ||
38 | /* | |
39 | * Routines to calculate various quantities related to the size of the image. | |
40 | */ | |
41 | ||
42 | LOCAL(void) | |
43 | initial_setup (j_decompress_ptr cinfo) | |
44 | /* Called once, when first SOS marker is reached */ | |
45 | { | |
46 | int ci; | |
47 | jpeg_component_info *compptr; | |
48 | ||
49 | /* Make sure image isn't bigger than I can handle */ | |
50 | if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || | |
51 | (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) | |
52 | ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); | |
53 | ||
54 | /* For now, precision must match compiled-in value... */ | |
55 | if (cinfo->data_precision != BITS_IN_JSAMPLE) | |
56 | ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); | |
57 | ||
58 | /* Check that number of components won't exceed internal array sizes */ | |
59 | if (cinfo->num_components > MAX_COMPONENTS) | |
60 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, | |
61 | MAX_COMPONENTS); | |
62 | ||
63 | /* Compute maximum sampling factors; check factor validity */ | |
64 | cinfo->max_h_samp_factor = 1; | |
65 | cinfo->max_v_samp_factor = 1; | |
66 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | |
67 | ci++, compptr++) { | |
68 | if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || | |
69 | compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) | |
70 | ERREXIT(cinfo, JERR_BAD_SAMPLING); | |
71 | cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, | |
72 | compptr->h_samp_factor); | |
73 | cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, | |
74 | compptr->v_samp_factor); | |
75 | } | |
76 | ||
77 | /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. | |
78 | * In the full decompressor, this will be overridden by jdmaster.c; | |
79 | * but in the transcoder, jdmaster.c is not used, so we must do it here. | |
80 | */ | |
81 | cinfo->min_DCT_scaled_size = DCTSIZE; | |
82 | ||
83 | /* Compute dimensions of components */ | |
84 | for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; | |
85 | ci++, compptr++) { | |
86 | compptr->DCT_scaled_size = DCTSIZE; | |
87 | /* Size in DCT blocks */ | |
88 | compptr->width_in_blocks = (JDIMENSION) | |
89 | jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, | |
90 | (long) (cinfo->max_h_samp_factor * DCTSIZE)); | |
91 | compptr->height_in_blocks = (JDIMENSION) | |
92 | jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, | |
93 | (long) (cinfo->max_v_samp_factor * DCTSIZE)); | |
94 | /* downsampled_width and downsampled_height will also be overridden by | |
95 | * jdmaster.c if we are doing full decompression. The transcoder library | |
96 | * doesn't use these values, but the calling application might. | |
97 | */ | |
98 | /* Size in samples */ | |
99 | compptr->downsampled_width = (JDIMENSION) | |
100 | jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, | |
101 | (long) cinfo->max_h_samp_factor); | |
102 | compptr->downsampled_height = (JDIMENSION) | |
103 | jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, | |
104 | (long) cinfo->max_v_samp_factor); | |
105 | /* Mark component needed, until color conversion says otherwise */ | |
106 | compptr->component_needed = TRUE; | |
107 | /* Mark no quantization table yet saved for component */ | |
108 | compptr->quant_table = NULL; | |
109 | } | |
110 | ||
111 | /* Compute number of fully interleaved MCU rows. */ | |
112 | cinfo->total_iMCU_rows = (JDIMENSION) | |
113 | jdiv_round_up((long) cinfo->image_height, | |
114 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); | |
115 | ||
116 | /* Decide whether file contains multiple scans */ | |
117 | if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) | |
118 | cinfo->inputctl->has_multiple_scans = TRUE; | |
119 | else | |
120 | cinfo->inputctl->has_multiple_scans = FALSE; | |
121 | } | |
122 | ||
123 | ||
124 | LOCAL(void) | |
125 | per_scan_setup (j_decompress_ptr cinfo) | |
126 | /* Do computations that are needed before processing a JPEG scan */ | |
127 | /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ | |
128 | { | |
129 | int ci, mcublks, tmp; | |
130 | jpeg_component_info *compptr; | |
7bfbbc0e | 131 | |
e1929140 | 132 | if (cinfo->comps_in_scan == 1) { |
7bfbbc0e | 133 | |
e1929140 RR |
134 | /* Noninterleaved (single-component) scan */ |
135 | compptr = cinfo->cur_comp_info[0]; | |
7bfbbc0e | 136 | |
e1929140 RR |
137 | /* Overall image size in MCUs */ |
138 | cinfo->MCUs_per_row = compptr->width_in_blocks; | |
139 | cinfo->MCU_rows_in_scan = compptr->height_in_blocks; | |
7bfbbc0e | 140 | |
e1929140 RR |
141 | /* For noninterleaved scan, always one block per MCU */ |
142 | compptr->MCU_width = 1; | |
143 | compptr->MCU_height = 1; | |
144 | compptr->MCU_blocks = 1; | |
145 | compptr->MCU_sample_width = compptr->DCT_scaled_size; | |
146 | compptr->last_col_width = 1; | |
147 | /* For noninterleaved scans, it is convenient to define last_row_height | |
148 | * as the number of block rows present in the last iMCU row. | |
149 | */ | |
150 | tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); | |
151 | if (tmp == 0) tmp = compptr->v_samp_factor; | |
152 | compptr->last_row_height = tmp; | |
7bfbbc0e | 153 | |
e1929140 RR |
154 | /* Prepare array describing MCU composition */ |
155 | cinfo->blocks_in_MCU = 1; | |
156 | cinfo->MCU_membership[0] = 0; | |
7bfbbc0e | 157 | |
e1929140 | 158 | } else { |
7bfbbc0e | 159 | |
e1929140 RR |
160 | /* Interleaved (multi-component) scan */ |
161 | if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) | |
162 | ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, | |
163 | MAX_COMPS_IN_SCAN); | |
7bfbbc0e | 164 | |
e1929140 RR |
165 | /* Overall image size in MCUs */ |
166 | cinfo->MCUs_per_row = (JDIMENSION) | |
167 | jdiv_round_up((long) cinfo->image_width, | |
168 | (long) (cinfo->max_h_samp_factor*DCTSIZE)); | |
169 | cinfo->MCU_rows_in_scan = (JDIMENSION) | |
170 | jdiv_round_up((long) cinfo->image_height, | |
171 | (long) (cinfo->max_v_samp_factor*DCTSIZE)); | |
7bfbbc0e | 172 | |
e1929140 | 173 | cinfo->blocks_in_MCU = 0; |
7bfbbc0e | 174 | |
e1929140 RR |
175 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { |
176 | compptr = cinfo->cur_comp_info[ci]; | |
177 | /* Sampling factors give # of blocks of component in each MCU */ | |
178 | compptr->MCU_width = compptr->h_samp_factor; | |
179 | compptr->MCU_height = compptr->v_samp_factor; | |
180 | compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; | |
181 | compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size; | |
182 | /* Figure number of non-dummy blocks in last MCU column & row */ | |
183 | tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); | |
184 | if (tmp == 0) tmp = compptr->MCU_width; | |
185 | compptr->last_col_width = tmp; | |
186 | tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); | |
187 | if (tmp == 0) tmp = compptr->MCU_height; | |
188 | compptr->last_row_height = tmp; | |
189 | /* Prepare array describing MCU composition */ | |
190 | mcublks = compptr->MCU_blocks; | |
191 | if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) | |
192 | ERREXIT(cinfo, JERR_BAD_MCU_SIZE); | |
193 | while (mcublks-- > 0) { | |
194 | cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; | |
195 | } | |
196 | } | |
7bfbbc0e | 197 | |
e1929140 RR |
198 | } |
199 | } | |
200 | ||
201 | ||
202 | /* | |
203 | * Save away a copy of the Q-table referenced by each component present | |
204 | * in the current scan, unless already saved during a prior scan. | |
205 | * | |
206 | * In a multiple-scan JPEG file, the encoder could assign different components | |
207 | * the same Q-table slot number, but change table definitions between scans | |
208 | * so that each component uses a different Q-table. (The IJG encoder is not | |
209 | * currently capable of doing this, but other encoders might.) Since we want | |
210 | * to be able to dequantize all the components at the end of the file, this | |
211 | * means that we have to save away the table actually used for each component. | |
212 | * We do this by copying the table at the start of the first scan containing | |
213 | * the component. | |
214 | * The JPEG spec prohibits the encoder from changing the contents of a Q-table | |
215 | * slot between scans of a component using that slot. If the encoder does so | |
216 | * anyway, this decoder will simply use the Q-table values that were current | |
217 | * at the start of the first scan for the component. | |
218 | * | |
219 | * The decompressor output side looks only at the saved quant tables, | |
220 | * not at the current Q-table slots. | |
221 | */ | |
222 | ||
223 | LOCAL(void) | |
224 | latch_quant_tables (j_decompress_ptr cinfo) | |
225 | { | |
226 | int ci, qtblno; | |
227 | jpeg_component_info *compptr; | |
228 | JQUANT_TBL * qtbl; | |
229 | ||
230 | for (ci = 0; ci < cinfo->comps_in_scan; ci++) { | |
231 | compptr = cinfo->cur_comp_info[ci]; | |
232 | /* No work if we already saved Q-table for this component */ | |
233 | if (compptr->quant_table != NULL) | |
234 | continue; | |
235 | /* Make sure specified quantization table is present */ | |
236 | qtblno = compptr->quant_tbl_no; | |
237 | if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || | |
238 | cinfo->quant_tbl_ptrs[qtblno] == NULL) | |
239 | ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); | |
240 | /* OK, save away the quantization table */ | |
241 | qtbl = (JQUANT_TBL *) | |
242 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, | |
243 | SIZEOF(JQUANT_TBL)); | |
244 | MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); | |
245 | compptr->quant_table = qtbl; | |
246 | } | |
247 | } | |
248 | ||
249 | ||
250 | /* | |
251 | * Initialize the input modules to read a scan of compressed data. | |
252 | * The first call to this is done by jdmaster.c after initializing | |
253 | * the entire decompressor (during jpeg_start_decompress). | |
254 | * Subsequent calls come from consume_markers, below. | |
255 | */ | |
256 | ||
257 | METHODDEF(void) | |
258 | start_input_pass (j_decompress_ptr cinfo) | |
259 | { | |
260 | per_scan_setup(cinfo); | |
261 | latch_quant_tables(cinfo); | |
262 | (*cinfo->entropy->start_pass) (cinfo); | |
2b5f62a0 VZ |
263 | #if defined(__VISAGECPP__) |
264 | (*cinfo->coef->start_input_pass2) (cinfo); | |
265 | #else | |
e1929140 | 266 | (*cinfo->coef->start_input_pass) (cinfo); |
2b5f62a0 | 267 | #endif |
e1929140 RR |
268 | cinfo->inputctl->consume_input = cinfo->coef->consume_data; |
269 | } | |
270 | ||
271 | ||
272 | /* | |
273 | * Finish up after inputting a compressed-data scan. | |
274 | * This is called by the coefficient controller after it's read all | |
275 | * the expected data of the scan. | |
276 | */ | |
277 | ||
278 | METHODDEF(void) | |
279 | finish_input_pass (j_decompress_ptr cinfo) | |
280 | { | |
281 | cinfo->inputctl->consume_input = consume_markers; | |
282 | } | |
283 | ||
284 | ||
285 | /* | |
286 | * Read JPEG markers before, between, or after compressed-data scans. | |
287 | * Change state as necessary when a new scan is reached. | |
288 | * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. | |
289 | * | |
290 | * The consume_input method pointer points either here or to the | |
291 | * coefficient controller's consume_data routine, depending on whether | |
292 | * we are reading a compressed data segment or inter-segment markers. | |
293 | */ | |
294 | ||
295 | METHODDEF(int) | |
296 | consume_markers (j_decompress_ptr cinfo) | |
297 | { | |
298 | my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; | |
299 | int val; | |
300 | ||
301 | if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ | |
302 | return JPEG_REACHED_EOI; | |
303 | ||
304 | val = (*cinfo->marker->read_markers) (cinfo); | |
305 | ||
306 | switch (val) { | |
307 | case JPEG_REACHED_SOS: /* Found SOS */ | |
308 | if (inputctl->inheaders) { /* 1st SOS */ | |
309 | initial_setup(cinfo); | |
310 | inputctl->inheaders = FALSE; | |
311 | /* Note: start_input_pass must be called by jdmaster.c | |
312 | * before any more input can be consumed. jdapimin.c is | |
313 | * responsible for enforcing this sequencing. | |
314 | */ | |
315 | } else { /* 2nd or later SOS marker */ | |
316 | if (! inputctl->pub.has_multiple_scans) | |
317 | ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ | |
318 | start_input_pass(cinfo); | |
319 | } | |
320 | break; | |
321 | case JPEG_REACHED_EOI: /* Found EOI */ | |
322 | inputctl->pub.eoi_reached = TRUE; | |
323 | if (inputctl->inheaders) { /* Tables-only datastream, apparently */ | |
324 | if (cinfo->marker->saw_SOF) | |
325 | ERREXIT(cinfo, JERR_SOF_NO_SOS); | |
326 | } else { | |
327 | /* Prevent infinite loop in coef ctlr's decompress_data routine | |
328 | * if user set output_scan_number larger than number of scans. | |
329 | */ | |
330 | if (cinfo->output_scan_number > cinfo->input_scan_number) | |
331 | cinfo->output_scan_number = cinfo->input_scan_number; | |
332 | } | |
333 | break; | |
334 | case JPEG_SUSPENDED: | |
335 | break; | |
336 | } | |
337 | ||
338 | return val; | |
339 | } | |
340 | ||
341 | ||
342 | /* | |
343 | * Reset state to begin a fresh datastream. | |
344 | */ | |
345 | ||
346 | METHODDEF(void) | |
347 | reset_input_controller (j_decompress_ptr cinfo) | |
348 | { | |
349 | my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; | |
350 | ||
351 | inputctl->pub.consume_input = consume_markers; | |
352 | inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ | |
353 | inputctl->pub.eoi_reached = FALSE; | |
354 | inputctl->inheaders = TRUE; | |
355 | /* Reset other modules */ | |
356 | (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); | |
357 | (*cinfo->marker->reset_marker_reader) (cinfo); | |
358 | /* Reset progression state -- would be cleaner if entropy decoder did this */ | |
359 | cinfo->coef_bits = NULL; | |
360 | } | |
361 | ||
362 | ||
363 | /* | |
364 | * Initialize the input controller module. | |
365 | * This is called only once, when the decompression object is created. | |
366 | */ | |
367 | ||
368 | GLOBAL(void) | |
369 | jinit_input_controller (j_decompress_ptr cinfo) | |
370 | { | |
371 | my_inputctl_ptr inputctl; | |
372 | ||
373 | /* Create subobject in permanent pool */ | |
374 | inputctl = (my_inputctl_ptr) | |
375 | (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, | |
376 | SIZEOF(my_input_controller)); | |
377 | cinfo->inputctl = (struct jpeg_input_controller *) inputctl; | |
378 | /* Initialize method pointers */ | |
379 | inputctl->pub.consume_input = consume_markers; | |
380 | inputctl->pub.reset_input_controller = reset_input_controller; | |
381 | inputctl->pub.start_input_pass = start_input_pass; | |
382 | inputctl->pub.finish_input_pass = finish_input_pass; | |
383 | /* Initialize state: can't use reset_input_controller since we don't | |
384 | * want to try to reset other modules yet. | |
385 | */ | |
386 | inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ | |
387 | inputctl->pub.eoi_reached = FALSE; | |
388 | inputctl->inheaders = TRUE; | |
389 | } | |
7bfbbc0e DW |
390 | |
391 | #if defined(__VISAGECPP__) | |
392 | # ifdef start_input_pass2 | |
393 | # undef start_input_pass2 | |
394 | # endif | |
395 | #endif | |
2b5f62a0 | 396 |