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