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