7 * Copyright (C) 1991-1995, Thomas G. Lane.
\r
9 * This file is part of the Independent JPEG Group's software.
\r
11 * For conditions of distribution and use, see the accompanying README file.
\r
15 * This file contains input control logic for the JPEG decompressor.
\r
17 * These routines are concerned with controlling the decompressor's input
\r
19 * processing (marker reading and coefficient decoding). The actual input
\r
21 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
\r
27 #define JPEG_INTERNALS
\r
29 #include "jinclude.h"
\r
31 #include "radiant_jpeglib.h"
\r
43 struct jpeg_input_controller pub; /* public fields */
\r
47 boolean inheaders; /* TRUE until first SOS is reached */
\r
49 } my_input_controller;
\r
53 typedef my_input_controller * my_inputctl_ptr;
\r
59 /* Forward declarations */
\r
61 METHODDEF int consume_markers JPP((j_decompress_ptr cinfo));
\r
69 * Routines to calculate various quantities related to the size of the image.
\r
77 initial_setup (j_decompress_ptr cinfo)
\r
79 /* Called once, when first SOS marker is reached */
\r
85 jpeg_component_info *compptr;
\r
89 /* Make sure image isn't bigger than I can handle */
\r
91 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
\r
93 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
\r
95 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
\r
99 /* For now, precision must match compiled-in value... */
\r
101 if (cinfo->data_precision != BITS_IN_JSAMPLE)
\r
103 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
\r
107 /* Check that number of components won't exceed internal array sizes */
\r
109 if (cinfo->num_components > MAX_COMPONENTS)
\r
111 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
\r
117 /* Compute maximum sampling factors; check factor validity */
\r
119 cinfo->max_h_samp_factor = 1;
\r
121 cinfo->max_v_samp_factor = 1;
\r
123 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
\r
127 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
\r
129 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
\r
131 ERREXIT(cinfo, JERR_BAD_SAMPLING);
\r
133 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
\r
135 compptr->h_samp_factor);
\r
137 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
\r
139 compptr->v_samp_factor);
\r
145 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
\r
147 * In the full decompressor, this will be overridden by jdmaster.c;
\r
149 * but in the transcoder, jdmaster.c is not used, so we must do it here.
\r
153 cinfo->min_DCT_scaled_size = DCTSIZE;
\r
157 /* Compute dimensions of components */
\r
159 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
\r
163 compptr->DCT_scaled_size = DCTSIZE;
\r
165 /* Size in DCT blocks */
\r
167 compptr->width_in_blocks = (JDIMENSION)
\r
169 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
\r
171 (long) (cinfo->max_h_samp_factor * DCTSIZE));
\r
173 compptr->height_in_blocks = (JDIMENSION)
\r
175 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
\r
177 (long) (cinfo->max_v_samp_factor * DCTSIZE));
\r
179 /* downsampled_width and downsampled_height will also be overridden by
\r
181 * jdmaster.c if we are doing full decompression. The transcoder library
\r
183 * doesn't use these values, but the calling application might.
\r
187 /* Size in samples */
\r
189 compptr->downsampled_width = (JDIMENSION)
\r
191 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
\r
193 (long) cinfo->max_h_samp_factor);
\r
195 compptr->downsampled_height = (JDIMENSION)
\r
197 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
\r
199 (long) cinfo->max_v_samp_factor);
\r
201 /* Mark component needed, until color conversion says otherwise */
\r
203 compptr->component_needed = TRUE;
\r
205 /* Mark no quantization table yet saved for component */
\r
207 compptr->quant_table = NULL;
\r
213 /* Compute number of fully interleaved MCU rows. */
\r
215 cinfo->total_iMCU_rows = (JDIMENSION)
\r
217 jdiv_round_up((long) cinfo->image_height,
\r
219 (long) (cinfo->max_v_samp_factor*DCTSIZE));
\r
223 /* Decide whether file contains multiple scans */
\r
225 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
\r
227 cinfo->inputctl->has_multiple_scans = TRUE;
\r
231 cinfo->inputctl->has_multiple_scans = FALSE;
\r
241 per_scan_setup (j_decompress_ptr cinfo)
\r
243 /* Do computations that are needed before processing a JPEG scan */
\r
245 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
\r
249 int ci, mcublks, tmp;
\r
251 jpeg_component_info *compptr;
\r
255 if (cinfo->comps_in_scan == 1) {
\r
259 /* Noninterleaved (single-component) scan */
\r
261 compptr = cinfo->cur_comp_info[0];
\r
265 /* Overall image size in MCUs */
\r
267 cinfo->MCUs_per_row = compptr->width_in_blocks;
\r
269 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
\r
273 /* For noninterleaved scan, always one block per MCU */
\r
275 compptr->MCU_width = 1;
\r
277 compptr->MCU_height = 1;
\r
279 compptr->MCU_blocks = 1;
\r
281 compptr->MCU_sample_width = compptr->DCT_scaled_size;
\r
283 compptr->last_col_width = 1;
\r
285 /* For noninterleaved scans, it is convenient to define last_row_height
\r
287 * as the number of block rows present in the last iMCU row.
\r
291 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
\r
293 if (tmp == 0) tmp = compptr->v_samp_factor;
\r
295 compptr->last_row_height = tmp;
\r
299 /* Prepare array describing MCU composition */
\r
301 cinfo->blocks_in_MCU = 1;
\r
303 cinfo->MCU_membership[0] = 0;
\r
311 /* Interleaved (multi-component) scan */
\r
313 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
\r
315 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
\r
317 MAX_COMPS_IN_SCAN);
\r
321 /* Overall image size in MCUs */
\r
323 cinfo->MCUs_per_row = (JDIMENSION)
\r
325 jdiv_round_up((long) cinfo->image_width,
\r
327 (long) (cinfo->max_h_samp_factor*DCTSIZE));
\r
329 cinfo->MCU_rows_in_scan = (JDIMENSION)
\r
331 jdiv_round_up((long) cinfo->image_height,
\r
333 (long) (cinfo->max_v_samp_factor*DCTSIZE));
\r
337 cinfo->blocks_in_MCU = 0;
\r
341 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
\r
343 compptr = cinfo->cur_comp_info[ci];
\r
345 /* Sampling factors give # of blocks of component in each MCU */
\r
347 compptr->MCU_width = compptr->h_samp_factor;
\r
349 compptr->MCU_height = compptr->v_samp_factor;
\r
351 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
\r
353 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
\r
355 /* Figure number of non-dummy blocks in last MCU column & row */
\r
357 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
\r
359 if (tmp == 0) tmp = compptr->MCU_width;
\r
361 compptr->last_col_width = tmp;
\r
363 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
\r
365 if (tmp == 0) tmp = compptr->MCU_height;
\r
367 compptr->last_row_height = tmp;
\r
369 /* Prepare array describing MCU composition */
\r
371 mcublks = compptr->MCU_blocks;
\r
373 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
\r
375 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
\r
377 while (mcublks-- > 0) {
\r
379 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
\r
397 * Save away a copy of the Q-table referenced by each component present
\r
399 * in the current scan, unless already saved during a prior scan.
\r
403 * In a multiple-scan JPEG file, the encoder could assign different components
\r
405 * the same Q-table slot number, but change table definitions between scans
\r
407 * so that each component uses a different Q-table. (The IJG encoder is not
\r
409 * currently capable of doing this, but other encoders might.) Since we want
\r
411 * to be able to dequantize all the components at the end of the file, this
\r
413 * means that we have to save away the table actually used for each component.
\r
415 * We do this by copying the table at the start of the first scan containing
\r
419 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
\r
421 * slot between scans of a component using that slot. If the encoder does so
\r
423 * anyway, this decoder will simply use the Q-table values that were current
\r
425 * at the start of the first scan for the component.
\r
429 * The decompressor output side looks only at the saved quant tables,
\r
431 * not at the current Q-table slots.
\r
439 latch_quant_tables (j_decompress_ptr cinfo)
\r
445 jpeg_component_info *compptr;
\r
451 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
\r
453 compptr = cinfo->cur_comp_info[ci];
\r
455 /* No work if we already saved Q-table for this component */
\r
457 if (compptr->quant_table != NULL)
\r
461 /* Make sure specified quantization table is present */
\r
463 qtblno = compptr->quant_tbl_no;
\r
465 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
\r
467 cinfo->quant_tbl_ptrs[qtblno] == NULL)
\r
469 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
\r
471 /* OK, save away the quantization table */
\r
473 qtbl = (JQUANT_TBL *)
\r
475 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
\r
477 SIZEOF(JQUANT_TBL));
\r
479 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
\r
481 compptr->quant_table = qtbl;
\r
493 * Initialize the input modules to read a scan of compressed data.
\r
495 * The first call to this is done by jdmaster.c after initializing
\r
497 * the entire decompressor (during jpeg_start_decompress).
\r
499 * Subsequent calls come from consume_markers, below.
\r
507 start_input_pass (j_decompress_ptr cinfo)
\r
511 per_scan_setup(cinfo);
\r
513 latch_quant_tables(cinfo);
\r
515 (*cinfo->entropy->start_pass) (cinfo);
\r
517 (*cinfo->coef->start_input_pass) (cinfo);
\r
519 cinfo->inputctl->consume_input = cinfo->coef->consume_data;
\r
529 * Finish up after inputting a compressed-data scan.
\r
531 * This is called by the coefficient controller after it's read all
\r
533 * the expected data of the scan.
\r
541 finish_input_pass (j_decompress_ptr cinfo)
\r
545 cinfo->inputctl->consume_input = consume_markers;
\r
555 * Read JPEG markers before, between, or after compressed-data scans.
\r
557 * Change state as necessary when a new scan is reached.
\r
559 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
\r
563 * The consume_input method pointer points either here or to the
\r
565 * coefficient controller's consume_data routine, depending on whether
\r
567 * we are reading a compressed data segment or inter-segment markers.
\r
575 consume_markers (j_decompress_ptr cinfo)
\r
579 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
\r
585 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
\r
587 return JPEG_REACHED_EOI;
\r
591 val = (*cinfo->marker->read_markers) (cinfo);
\r
597 case JPEG_REACHED_SOS: /* Found SOS */
\r
599 if (inputctl->inheaders) { /* 1st SOS */
\r
601 initial_setup(cinfo);
\r
603 inputctl->inheaders = FALSE;
\r
605 /* Note: start_input_pass must be called by jdmaster.c
\r
607 * before any more input can be consumed. jdapi.c is
\r
609 * responsible for enforcing this sequencing.
\r
613 } else { /* 2nd or later SOS marker */
\r
615 if (! inputctl->pub.has_multiple_scans)
\r
617 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
\r
619 start_input_pass(cinfo);
\r
625 case JPEG_REACHED_EOI: /* Found EOI */
\r
627 inputctl->pub.eoi_reached = TRUE;
\r
629 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
\r
631 if (cinfo->marker->saw_SOF)
\r
633 ERREXIT(cinfo, JERR_SOF_NO_SOS);
\r
637 /* Prevent infinite loop in coef ctlr's decompress_data routine
\r
639 * if user set output_scan_number larger than number of scans.
\r
643 if (cinfo->output_scan_number > cinfo->input_scan_number)
\r
645 cinfo->output_scan_number = cinfo->input_scan_number;
\r
651 case JPEG_SUSPENDED:
\r
669 * Reset state to begin a fresh datastream.
\r
677 reset_input_controller (j_decompress_ptr cinfo)
\r
681 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
\r
685 inputctl->pub.consume_input = consume_markers;
\r
687 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
\r
689 inputctl->pub.eoi_reached = FALSE;
\r
691 inputctl->inheaders = TRUE;
\r
693 /* Reset other modules */
\r
695 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
\r
697 (*cinfo->marker->reset_marker_reader) (cinfo);
\r
699 /* Reset progression state -- would be cleaner if entropy decoder did this */
\r
701 cinfo->coef_bits = NULL;
\r
711 * Initialize the input controller module.
\r
713 * This is called only once, when the decompression object is created.
\r
721 jinit_input_controller (j_decompress_ptr cinfo)
\r
725 my_inputctl_ptr inputctl;
\r
729 /* Create subobject in permanent pool */
\r
731 inputctl = (my_inputctl_ptr)
\r
733 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
\r
735 SIZEOF(my_input_controller));
\r
737 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
\r
739 /* Initialize method pointers */
\r
741 inputctl->pub.consume_input = consume_markers;
\r
743 inputctl->pub.reset_input_controller = reset_input_controller;
\r
745 inputctl->pub.start_input_pass = start_input_pass;
\r
747 inputctl->pub.finish_input_pass = finish_input_pass;
\r
749 /* Initialize state: can't use reset_input_controller since we don't
\r
751 * want to try to reset other modules yet.
\r
755 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
\r
757 inputctl->pub.eoi_reached = FALSE;
\r
759 inputctl->inheaders = TRUE;
\r