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