Fixed binary search: no more infinite loops when vendor is unknown.
[tangerine.git] / compiler / libjpeg / main / jdinput.c
blob10e5f2e8f0ab35c8a54036c416794bf9a4044942
1 /*
2 $Id$
3 */
5 /*
6 * jdinput.c
8 * Copyright (C) 1991-1998, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
12 * This file contains input control logic for the JPEG decompressor.
13 * These routines are concerned with controlling the decompressor's input
14 * processing (marker reading and coefficient/difference decoding).
15 * The actual input reading is done in jdmarker.c, jdhuff.c, jdphuff.c,
16 * and jdlhuff.c.
19 #define JPEG_INTERNALS
20 #include "jinclude.h"
21 #include "jpeglib.h"
24 /* Private state */
26 typedef struct {
27 struct jpeg_input_controller pub; /* public fields */
29 boolean inheaders; /* TRUE until first SOS is reached */
30 } my_input_controller;
32 typedef my_input_controller * my_inputctl_ptr;
35 /* Forward declarations */
36 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
40 * Routines to calculate various quantities related to the size of the image.
43 LOCAL(void)
44 initial_setup (j_decompress_ptr cinfo)
45 /* Called once, when first SOS marker is reached */
47 int ci;
48 jpeg_component_info *compptr;
50 /* Make sure image isn't bigger than I can handle */
51 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
52 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
53 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
55 if (cinfo->process == JPROC_LOSSLESS) {
56 /* If precision > compiled-in value, we must downscale */
57 if (cinfo->data_precision > BITS_IN_JSAMPLE)
58 WARNMS2(cinfo, JWRN_MUST_DOWNSCALE,
59 cinfo->data_precision, BITS_IN_JSAMPLE);
61 else { /* Lossy processes */
62 /* For now, precision must match compiled-in value... */
63 if (cinfo->data_precision != BITS_IN_JSAMPLE)
64 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
67 /* Check that number of components won't exceed internal array sizes */
68 if (cinfo->num_components > MAX_COMPONENTS)
69 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
70 MAX_COMPONENTS);
72 /* Compute maximum sampling factors; check factor validity */
73 cinfo->max_h_samp_factor = 1;
74 cinfo->max_v_samp_factor = 1;
75 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
76 ci++, compptr++) {
77 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
78 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
79 ERREXIT(cinfo, JERR_BAD_SAMPLING);
80 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
81 compptr->h_samp_factor);
82 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
83 compptr->v_samp_factor);
86 /* We initialize codec_data_unit and min_codec_data_unit to data_unit.
87 * In the full decompressor, this will be overridden by jdmaster.c;
88 * but in the transcoder, jdmaster.c is not used, so we must do it here.
90 cinfo->min_codec_data_unit = cinfo->data_unit;
92 /* Compute dimensions of components */
93 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
94 ci++, compptr++) {
95 compptr->codec_data_unit = cinfo->data_unit;
96 /* Size in data units */
97 compptr->width_in_data_units = (JDIMENSION)
98 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
99 (long) (cinfo->max_h_samp_factor * cinfo->data_unit));
100 compptr->height_in_data_units = (JDIMENSION)
101 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
102 (long) (cinfo->max_v_samp_factor * cinfo->data_unit));
103 /* downsampled_width and downsampled_height will also be overridden by
104 * jdmaster.c if we are doing full decompression. The transcoder library
105 * doesn't use these values, but the calling application might.
107 /* Size in samples */
108 compptr->downsampled_width = (JDIMENSION)
109 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
110 (long) cinfo->max_h_samp_factor);
111 compptr->downsampled_height = (JDIMENSION)
112 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
113 (long) cinfo->max_v_samp_factor);
114 /* Mark component needed, until color conversion says otherwise */
115 compptr->component_needed = TRUE;
116 /* Mark no quantization table yet saved for component */
117 compptr->quant_table = NULL;
120 /* Compute number of fully interleaved MCU rows. */
121 cinfo->total_iMCU_rows = (JDIMENSION)
122 jdiv_round_up((long) cinfo->image_height,
123 (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
125 /* Decide whether file contains multiple scans */
126 if (cinfo->comps_in_scan < cinfo->num_components ||
127 cinfo->process == JPROC_PROGRESSIVE)
128 cinfo->inputctl->has_multiple_scans = TRUE;
129 else
130 cinfo->inputctl->has_multiple_scans = FALSE;
134 LOCAL(void)
135 per_scan_setup (j_decompress_ptr cinfo)
136 /* Do computations that are needed before processing a JPEG scan */
137 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
139 int ci, mcublks, tmp;
140 jpeg_component_info *compptr;
142 if (cinfo->comps_in_scan == 1) {
144 /* Noninterleaved (single-component) scan */
145 compptr = cinfo->cur_comp_info[0];
147 /* Overall image size in MCUs */
148 cinfo->MCUs_per_row = compptr->width_in_data_units;
149 cinfo->MCU_rows_in_scan = compptr->height_in_data_units;
151 /* For noninterleaved scan, always one data unit per MCU */
152 compptr->MCU_width = 1;
153 compptr->MCU_height = 1;
154 compptr->MCU_data_units = 1;
155 compptr->MCU_sample_width = compptr->codec_data_unit;
156 compptr->last_col_width = 1;
157 /* For noninterleaved scans, it is convenient to define last_row_height
158 * as the number of data unit rows present in the last iMCU row.
160 tmp = (int) (compptr->height_in_data_units % compptr->v_samp_factor);
161 if (tmp == 0) tmp = compptr->v_samp_factor;
162 compptr->last_row_height = tmp;
164 /* Prepare array describing MCU composition */
165 cinfo->data_units_in_MCU = 1;
166 cinfo->MCU_membership[0] = 0;
168 } else {
170 /* Interleaved (multi-component) scan */
171 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
172 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
173 MAX_COMPS_IN_SCAN);
175 /* Overall image size in MCUs */
176 cinfo->MCUs_per_row = (JDIMENSION)
177 jdiv_round_up((long) cinfo->image_width,
178 (long) (cinfo->max_h_samp_factor*cinfo->data_unit));
179 cinfo->MCU_rows_in_scan = (JDIMENSION)
180 jdiv_round_up((long) cinfo->image_height,
181 (long) (cinfo->max_v_samp_factor*cinfo->data_unit));
183 cinfo->data_units_in_MCU = 0;
185 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
186 compptr = cinfo->cur_comp_info[ci];
187 /* Sampling factors give # of data units of component in each MCU */
188 compptr->MCU_width = compptr->h_samp_factor;
189 compptr->MCU_height = compptr->v_samp_factor;
190 compptr->MCU_data_units = compptr->MCU_width * compptr->MCU_height;
191 compptr->MCU_sample_width = compptr->MCU_width * compptr->codec_data_unit;
192 /* Figure number of non-dummy data units in last MCU column & row */
193 tmp = (int) (compptr->width_in_data_units % compptr->MCU_width);
194 if (tmp == 0) tmp = compptr->MCU_width;
195 compptr->last_col_width = tmp;
196 tmp = (int) (compptr->height_in_data_units % compptr->MCU_height);
197 if (tmp == 0) tmp = compptr->MCU_height;
198 compptr->last_row_height = tmp;
199 /* Prepare array describing MCU composition */
200 mcublks = compptr->MCU_data_units;
201 if (cinfo->data_units_in_MCU + mcublks > D_MAX_DATA_UNITS_IN_MCU)
202 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
203 while (mcublks-- > 0) {
204 cinfo->MCU_membership[cinfo->data_units_in_MCU++] = ci;
213 * Initialize the input modules to read a scan of compressed data.
214 * The first call to this is done by jdmaster.c after initializing
215 * the entire decompressor (during jpeg_start_decompress).
216 * Subsequent calls come from consume_markers, below.
219 METHODDEF(void)
220 start_input_pass (j_decompress_ptr cinfo)
222 per_scan_setup(cinfo);
223 (*cinfo->codec->start_input_pass) (cinfo);
224 cinfo->inputctl->consume_input = cinfo->codec->consume_data;
229 * Finish up after inputting a compressed-data scan.
230 * This is called by the coefficient controller after it's read all
231 * the expected data of the scan.
234 METHODDEF(void)
235 finish_input_pass (j_decompress_ptr cinfo)
237 cinfo->inputctl->consume_input = consume_markers;
242 * Read JPEG markers before, between, or after compressed-data scans.
243 * Change state as necessary when a new scan is reached.
244 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
246 * The consume_input method pointer points either here or to the
247 * coefficient controller's consume_data routine, depending on whether
248 * we are reading a compressed data segment or inter-segment markers.
251 METHODDEF(int)
252 consume_markers (j_decompress_ptr cinfo)
254 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
255 int val;
257 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
258 return JPEG_REACHED_EOI;
260 val = (*cinfo->marker->read_markers) (cinfo);
262 switch (val) {
263 case JPEG_REACHED_SOS: /* Found SOS */
264 if (inputctl->inheaders) { /* 1st SOS */
265 initial_setup(cinfo);
267 * Initialize the decompression codec. We need to do this here so that
268 * any codec-specific fields and function pointers are available to
269 * the rest of the library.
271 jinit_d_codec(cinfo);
272 inputctl->inheaders = FALSE;
273 /* Note: start_input_pass must be called by jdmaster.c
274 * before any more input can be consumed. jdapimin.c is
275 * responsible for enforcing this sequencing.
277 } else { /* 2nd or later SOS marker */
278 if (! inputctl->pub.has_multiple_scans)
279 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
280 start_input_pass(cinfo);
282 break;
283 case JPEG_REACHED_EOI: /* Found EOI */
284 inputctl->pub.eoi_reached = TRUE;
285 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
286 if (cinfo->marker->saw_SOF)
287 ERREXIT(cinfo, JERR_SOF_NO_SOS);
288 } else {
289 /* Prevent infinite loop in coef ctlr's decompress_data routine
290 * if user set output_scan_number larger than number of scans.
292 if (cinfo->output_scan_number > cinfo->input_scan_number)
293 cinfo->output_scan_number = cinfo->input_scan_number;
295 break;
296 case JPEG_SUSPENDED:
297 break;
300 return val;
305 * Reset state to begin a fresh datastream.
308 METHODDEF(void)
309 reset_input_controller (j_decompress_ptr cinfo)
311 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
313 inputctl->pub.consume_input = consume_markers;
314 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
315 inputctl->pub.eoi_reached = FALSE;
316 inputctl->inheaders = TRUE;
317 /* Reset other modules */
318 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
319 (*cinfo->marker->reset_marker_reader) (cinfo);
320 /* Reset progression state -- would be cleaner if entropy decoder did this */
321 cinfo->coef_bits = NULL;
326 * Initialize the input controller module.
327 * This is called only once, when the decompression object is created.
330 JGLOBAL(void)
331 jinit_input_controller (j_decompress_ptr cinfo)
333 my_inputctl_ptr inputctl;
335 /* Create subobject in permanent pool */
336 inputctl = (my_inputctl_ptr)
337 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
338 SIZEOF(my_input_controller));
339 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
340 /* Initialize method pointers */
341 inputctl->pub.consume_input = consume_markers;
342 inputctl->pub.reset_input_controller = reset_input_controller;
343 inputctl->pub.start_input_pass = start_input_pass;
344 inputctl->pub.finish_input_pass = finish_input_pass;
345 /* Initialize state: can't use reset_input_controller since we don't
346 * want to try to reset other modules yet.
348 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
349 inputctl->pub.eoi_reached = FALSE;
350 inputctl->inheaders = TRUE;