/[pcsx2_0.9.7]/trunk/3rdparty/libjpeg/jdinput.c
ViewVC logotype

Contents of /trunk/3rdparty/libjpeg/jdinput.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 10 - (show annotations) (download)
Mon Sep 6 11:40:06 2010 UTC (9 years, 10 months ago) by william
File MIME type: text/plain
File size: 13977 byte(s)
exported r3113 from ./upstream/trunk
1 /*
2 * jdinput.c
3 *
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2002-2009 by Guido Vollbeding.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains input control logic for the JPEG decompressor.
10 * These routines are concerned with controlling the decompressor's input
11 * processing (marker reading and coefficient decoding). The actual input
12 * reading is done in jdmarker.c, jdhuff.c, and jdarith.c.
13 */
14
15 #define JPEG_INTERNALS
16 #include "jinclude.h"
17 #include "jpeglib.h"
18
19
20 /* Private state */
21
22 typedef struct {
23 struct jpeg_input_controller pub; /* public fields */
24
25 boolean inheaders; /* TRUE until first SOS is reached */
26 } my_input_controller;
27
28 typedef my_input_controller * my_inputctl_ptr;
29
30
31 /* Forward declarations */
32 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
33
34
35 /*
36 * Routines to calculate various quantities related to the size of the image.
37 */
38
39 LOCAL(void)
40 initial_setup (j_decompress_ptr cinfo)
41 /* Called once, when first SOS marker is reached */
42 {
43 int ci;
44 jpeg_component_info *compptr;
45
46 /* Make sure image isn't bigger than I can handle */
47 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
48 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
49 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
50
51 /* For now, precision must match compiled-in value... */
52 if (cinfo->data_precision != BITS_IN_JSAMPLE)
53 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
54
55 /* Check that number of components won't exceed internal array sizes */
56 if (cinfo->num_components > MAX_COMPONENTS)
57 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
58 MAX_COMPONENTS);
59
60 /* Compute maximum sampling factors; check factor validity */
61 cinfo->max_h_samp_factor = 1;
62 cinfo->max_v_samp_factor = 1;
63 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
64 ci++, compptr++) {
65 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
66 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
67 ERREXIT(cinfo, JERR_BAD_SAMPLING);
68 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
69 compptr->h_samp_factor);
70 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
71 compptr->v_samp_factor);
72 }
73
74 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
75 * In the full decompressor, this will be overridden by jdmaster.c;
76 * but in the transcoder, jdmaster.c is not used, so we must do it here.
77 */
78 cinfo->min_DCT_h_scaled_size = DCTSIZE;
79 cinfo->min_DCT_v_scaled_size = DCTSIZE;
80
81 /* Compute dimensions of components */
82 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
83 ci++, compptr++) {
84 compptr->DCT_h_scaled_size = DCTSIZE;
85 compptr->DCT_v_scaled_size = DCTSIZE;
86 /* Size in DCT blocks */
87 compptr->width_in_blocks = (JDIMENSION)
88 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
89 (long) (cinfo->max_h_samp_factor * DCTSIZE));
90 compptr->height_in_blocks = (JDIMENSION)
91 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
92 (long) (cinfo->max_v_samp_factor * DCTSIZE));
93 /* downsampled_width and downsampled_height will also be overridden by
94 * jdmaster.c if we are doing full decompression. The transcoder library
95 * doesn't use these values, but the calling application might.
96 */
97 /* Size in samples */
98 compptr->downsampled_width = (JDIMENSION)
99 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
100 (long) cinfo->max_h_samp_factor);
101 compptr->downsampled_height = (JDIMENSION)
102 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
103 (long) cinfo->max_v_samp_factor);
104 /* Mark component needed, until color conversion says otherwise */
105 compptr->component_needed = TRUE;
106 /* Mark no quantization table yet saved for component */
107 compptr->quant_table = NULL;
108 }
109
110 /* Compute number of fully interleaved MCU rows. */
111 cinfo->total_iMCU_rows = (JDIMENSION)
112 jdiv_round_up((long) cinfo->image_height,
113 (long) (cinfo->max_v_samp_factor*DCTSIZE));
114
115 /* Decide whether file contains multiple scans */
116 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
117 cinfo->inputctl->has_multiple_scans = TRUE;
118 else
119 cinfo->inputctl->has_multiple_scans = FALSE;
120 }
121
122
123 LOCAL(void)
124 per_scan_setup (j_decompress_ptr cinfo)
125 /* Do computations that are needed before processing a JPEG scan */
126 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
127 {
128 int ci, mcublks, tmp;
129 jpeg_component_info *compptr;
130
131 if (cinfo->comps_in_scan == 1) {
132
133 /* Noninterleaved (single-component) scan */
134 compptr = cinfo->cur_comp_info[0];
135
136 /* Overall image size in MCUs */
137 cinfo->MCUs_per_row = compptr->width_in_blocks;
138 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
139
140 /* For noninterleaved scan, always one block per MCU */
141 compptr->MCU_width = 1;
142 compptr->MCU_height = 1;
143 compptr->MCU_blocks = 1;
144 compptr->MCU_sample_width = compptr->DCT_h_scaled_size;
145 compptr->last_col_width = 1;
146 /* For noninterleaved scans, it is convenient to define last_row_height
147 * as the number of block rows present in the last iMCU row.
148 */
149 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
150 if (tmp == 0) tmp = compptr->v_samp_factor;
151 compptr->last_row_height = tmp;
152
153 /* Prepare array describing MCU composition */
154 cinfo->blocks_in_MCU = 1;
155 cinfo->MCU_membership[0] = 0;
156
157 } else {
158
159 /* Interleaved (multi-component) scan */
160 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
161 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
162 MAX_COMPS_IN_SCAN);
163
164 /* Overall image size in MCUs */
165 cinfo->MCUs_per_row = (JDIMENSION)
166 jdiv_round_up((long) cinfo->image_width,
167 (long) (cinfo->max_h_samp_factor*DCTSIZE));
168 cinfo->MCU_rows_in_scan = (JDIMENSION)
169 jdiv_round_up((long) cinfo->image_height,
170 (long) (cinfo->max_v_samp_factor*DCTSIZE));
171
172 cinfo->blocks_in_MCU = 0;
173
174 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
175 compptr = cinfo->cur_comp_info[ci];
176 /* Sampling factors give # of blocks of component in each MCU */
177 compptr->MCU_width = compptr->h_samp_factor;
178 compptr->MCU_height = compptr->v_samp_factor;
179 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
180 compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_h_scaled_size;
181 /* Figure number of non-dummy blocks in last MCU column & row */
182 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
183 if (tmp == 0) tmp = compptr->MCU_width;
184 compptr->last_col_width = tmp;
185 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
186 if (tmp == 0) tmp = compptr->MCU_height;
187 compptr->last_row_height = tmp;
188 /* Prepare array describing MCU composition */
189 mcublks = compptr->MCU_blocks;
190 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
191 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
192 while (mcublks-- > 0) {
193 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
194 }
195 }
196
197 }
198 }
199
200
201 /*
202 * Save away a copy of the Q-table referenced by each component present
203 * in the current scan, unless already saved during a prior scan.
204 *
205 * In a multiple-scan JPEG file, the encoder could assign different components
206 * the same Q-table slot number, but change table definitions between scans
207 * so that each component uses a different Q-table. (The IJG encoder is not
208 * currently capable of doing this, but other encoders might.) Since we want
209 * to be able to dequantize all the components at the end of the file, this
210 * means that we have to save away the table actually used for each component.
211 * We do this by copying the table at the start of the first scan containing
212 * the component.
213 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
214 * slot between scans of a component using that slot. If the encoder does so
215 * anyway, this decoder will simply use the Q-table values that were current
216 * at the start of the first scan for the component.
217 *
218 * The decompressor output side looks only at the saved quant tables,
219 * not at the current Q-table slots.
220 */
221
222 LOCAL(void)
223 latch_quant_tables (j_decompress_ptr cinfo)
224 {
225 int ci, qtblno;
226 jpeg_component_info *compptr;
227 JQUANT_TBL * qtbl;
228
229 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
230 compptr = cinfo->cur_comp_info[ci];
231 /* No work if we already saved Q-table for this component */
232 if (compptr->quant_table != NULL)
233 continue;
234 /* Make sure specified quantization table is present */
235 qtblno = compptr->quant_tbl_no;
236 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
237 cinfo->quant_tbl_ptrs[qtblno] == NULL)
238 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
239 /* OK, save away the quantization table */
240 qtbl = (JQUANT_TBL *)
241 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
242 SIZEOF(JQUANT_TBL));
243 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
244 compptr->quant_table = qtbl;
245 }
246 }
247
248
249 /*
250 * Initialize the input modules to read a scan of compressed data.
251 * The first call to this is done by jdmaster.c after initializing
252 * the entire decompressor (during jpeg_start_decompress).
253 * Subsequent calls come from consume_markers, below.
254 */
255
256 METHODDEF(void)
257 start_input_pass (j_decompress_ptr cinfo)
258 {
259 per_scan_setup(cinfo);
260 latch_quant_tables(cinfo);
261 (*cinfo->entropy->start_pass) (cinfo);
262 (*cinfo->coef->start_input_pass) (cinfo);
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
273 METHODDEF(void)
274 finish_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
290 METHODDEF(int)
291 consume_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
341 METHODDEF(void)
342 reset_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
363 GLOBAL(void)
364 jinit_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 }

  ViewVC Help
Powered by ViewVC 1.1.22