Bug 573948 - Part 1: Use libjpeg-turbo instead of libjpeg. r=jmuizelaar
[gecko.git] / jpeg / jdinput.c
blob9fcd089d3cf6ea8345fe016a1fc1159183184453
1 /*
2 * jdinput.c
4 * Copyright (C) 1991-1997, Thomas G. Lane.
5 * Modified 2002-2009 by Guido Vollbeding.
6 * Copyright (C) 2010, D. R. Commander.
7 * This file is part of the Independent JPEG Group's software.
8 * For conditions of distribution and use, see the accompanying README file.
10 * This file contains input control logic for the JPEG decompressor.
11 * These routines are concerned with controlling the decompressor's input
12 * processing (marker reading and coefficient decoding). The actual input
13 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
16 #define JPEG_INTERNALS
17 #include "jinclude.h"
18 #include "jpeglib.h"
19 #include "jpegcomp.h"
22 /* Private state */
24 typedef struct {
25 struct jpeg_input_controller pub; /* public fields */
27 boolean inheaders; /* TRUE until first SOS is reached */
28 } my_input_controller;
30 typedef my_input_controller * my_inputctl_ptr;
33 /* Forward declarations */
34 METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
38 * Routines to calculate various quantities related to the size of the image.
42 #if JPEG_LIB_VERSION >= 80
44 * Compute output image dimensions and related values.
45 * NOTE: this is exported for possible use by application.
46 * Hence it mustn't do anything that can't be done twice.
49 GLOBAL(void)
50 jpeg_core_output_dimensions (j_decompress_ptr cinfo)
51 /* Do computations that are needed before master selection phase.
52 * This function is used for transcoding and full decompression.
55 #ifdef IDCT_SCALING_SUPPORTED
56 int ci;
57 jpeg_component_info *compptr;
59 /* Compute actual output image dimensions and DCT scaling choices. */
60 if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom) {
61 /* Provide 1/block_size scaling */
62 cinfo->output_width = (JDIMENSION)
63 jdiv_round_up((long) cinfo->image_width, (long) cinfo->block_size);
64 cinfo->output_height = (JDIMENSION)
65 jdiv_round_up((long) cinfo->image_height, (long) cinfo->block_size);
66 cinfo->min_DCT_h_scaled_size = 1;
67 cinfo->min_DCT_v_scaled_size = 1;
68 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 2) {
69 /* Provide 2/block_size scaling */
70 cinfo->output_width = (JDIMENSION)
71 jdiv_round_up((long) cinfo->image_width * 2L, (long) cinfo->block_size);
72 cinfo->output_height = (JDIMENSION)
73 jdiv_round_up((long) cinfo->image_height * 2L, (long) cinfo->block_size);
74 cinfo->min_DCT_h_scaled_size = 2;
75 cinfo->min_DCT_v_scaled_size = 2;
76 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 4) {
77 /* Provide 4/block_size scaling */
78 cinfo->output_width = (JDIMENSION)
79 jdiv_round_up((long) cinfo->image_width * 4L, (long) cinfo->block_size);
80 cinfo->output_height = (JDIMENSION)
81 jdiv_round_up((long) cinfo->image_height * 4L, (long) cinfo->block_size);
82 cinfo->min_DCT_h_scaled_size = 4;
83 cinfo->min_DCT_v_scaled_size = 4;
84 } else if (cinfo->scale_num * cinfo->block_size <= cinfo->scale_denom * 8) {
85 /* Provide 8/block_size scaling */
86 cinfo->output_width = (JDIMENSION)
87 jdiv_round_up((long) cinfo->image_width * 8L, (long) cinfo->block_size);
88 cinfo->output_height = (JDIMENSION)
89 jdiv_round_up((long) cinfo->image_height * 8L, (long) cinfo->block_size);
90 cinfo->min_DCT_h_scaled_size = 8;
91 cinfo->min_DCT_v_scaled_size = 8;
93 /* Recompute dimensions of components */
94 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
95 ci++, compptr++) {
96 compptr->DCT_h_scaled_size = cinfo->min_DCT_h_scaled_size;
97 compptr->DCT_v_scaled_size = cinfo->min_DCT_v_scaled_size;
100 #else /* !IDCT_SCALING_SUPPORTED */
102 /* Hardwire it to "no scaling" */
103 cinfo->output_width = cinfo->image_width;
104 cinfo->output_height = cinfo->image_height;
105 /* jdinput.c has already initialized DCT_scaled_size,
106 * and has computed unscaled downsampled_width and downsampled_height.
109 #endif /* IDCT_SCALING_SUPPORTED */
111 #endif
114 LOCAL(void)
115 initial_setup (j_decompress_ptr cinfo)
116 /* Called once, when first SOS marker is reached */
118 int ci;
119 jpeg_component_info *compptr;
121 /* Make sure image isn't bigger than I can handle */
122 if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
123 (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
124 ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
126 /* For now, precision must match compiled-in value... */
127 if (cinfo->data_precision != BITS_IN_JSAMPLE)
128 ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
130 /* Check that number of components won't exceed internal array sizes */
131 if (cinfo->num_components > MAX_COMPONENTS)
132 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
133 MAX_COMPONENTS);
135 /* Compute maximum sampling factors; check factor validity */
136 cinfo->max_h_samp_factor = 1;
137 cinfo->max_v_samp_factor = 1;
138 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
139 ci++, compptr++) {
140 if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
141 compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
142 ERREXIT(cinfo, JERR_BAD_SAMPLING);
143 cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
144 compptr->h_samp_factor);
145 cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
146 compptr->v_samp_factor);
149 #if JPEG_LIB_VERSION >=80
150 cinfo->block_size = DCTSIZE;
151 cinfo->natural_order = jpeg_natural_order;
152 cinfo->lim_Se = DCTSIZE2-1;
153 #endif
155 /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
156 * In the full decompressor, this will be overridden by jdmaster.c;
157 * but in the transcoder, jdmaster.c is not used, so we must do it here.
159 #if JPEG_LIB_VERSION >= 70
160 cinfo->min_DCT_h_scaled_size = cinfo->min_DCT_v_scaled_size = DCTSIZE;
161 #else
162 cinfo->min_DCT_scaled_size = DCTSIZE;
163 #endif
165 /* Compute dimensions of components */
166 for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
167 ci++, compptr++) {
168 #if JPEG_LIB_VERSION >= 70
169 compptr->DCT_h_scaled_size = compptr->DCT_v_scaled_size = DCTSIZE;
170 #else
171 compptr->DCT_scaled_size = DCTSIZE;
172 #endif
173 /* Size in DCT blocks */
174 compptr->width_in_blocks = (JDIMENSION)
175 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
176 (long) (cinfo->max_h_samp_factor * DCTSIZE));
177 compptr->height_in_blocks = (JDIMENSION)
178 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
179 (long) (cinfo->max_v_samp_factor * DCTSIZE));
180 /* downsampled_width and downsampled_height will also be overridden by
181 * jdmaster.c if we are doing full decompression. The transcoder library
182 * doesn't use these values, but the calling application might.
184 /* Size in samples */
185 compptr->downsampled_width = (JDIMENSION)
186 jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
187 (long) cinfo->max_h_samp_factor);
188 compptr->downsampled_height = (JDIMENSION)
189 jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
190 (long) cinfo->max_v_samp_factor);
191 /* Mark component needed, until color conversion says otherwise */
192 compptr->component_needed = TRUE;
193 /* Mark no quantization table yet saved for component */
194 compptr->quant_table = NULL;
197 /* Compute number of fully interleaved MCU rows. */
198 cinfo->total_iMCU_rows = (JDIMENSION)
199 jdiv_round_up((long) cinfo->image_height,
200 (long) (cinfo->max_v_samp_factor*DCTSIZE));
202 /* Decide whether file contains multiple scans */
203 if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
204 cinfo->inputctl->has_multiple_scans = TRUE;
205 else
206 cinfo->inputctl->has_multiple_scans = FALSE;
210 LOCAL(void)
211 per_scan_setup (j_decompress_ptr cinfo)
212 /* Do computations that are needed before processing a JPEG scan */
213 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
215 int ci, mcublks, tmp;
216 jpeg_component_info *compptr;
218 if (cinfo->comps_in_scan == 1) {
220 /* Noninterleaved (single-component) scan */
221 compptr = cinfo->cur_comp_info[0];
223 /* Overall image size in MCUs */
224 cinfo->MCUs_per_row = compptr->width_in_blocks;
225 cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
227 /* For noninterleaved scan, always one block per MCU */
228 compptr->MCU_width = 1;
229 compptr->MCU_height = 1;
230 compptr->MCU_blocks = 1;
231 compptr->MCU_sample_width = compptr->_DCT_scaled_size;
232 compptr->last_col_width = 1;
233 /* For noninterleaved scans, it is convenient to define last_row_height
234 * as the number of block rows present in the last iMCU row.
236 tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
237 if (tmp == 0) tmp = compptr->v_samp_factor;
238 compptr->last_row_height = tmp;
240 /* Prepare array describing MCU composition */
241 cinfo->blocks_in_MCU = 1;
242 cinfo->MCU_membership[0] = 0;
244 } else {
246 /* Interleaved (multi-component) scan */
247 if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
248 ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
249 MAX_COMPS_IN_SCAN);
251 /* Overall image size in MCUs */
252 cinfo->MCUs_per_row = (JDIMENSION)
253 jdiv_round_up((long) cinfo->image_width,
254 (long) (cinfo->max_h_samp_factor*DCTSIZE));
255 cinfo->MCU_rows_in_scan = (JDIMENSION)
256 jdiv_round_up((long) cinfo->image_height,
257 (long) (cinfo->max_v_samp_factor*DCTSIZE));
259 cinfo->blocks_in_MCU = 0;
261 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
262 compptr = cinfo->cur_comp_info[ci];
263 /* Sampling factors give # of blocks of component in each MCU */
264 compptr->MCU_width = compptr->h_samp_factor;
265 compptr->MCU_height = compptr->v_samp_factor;
266 compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
267 compptr->MCU_sample_width = compptr->MCU_width * compptr->_DCT_scaled_size;
268 /* Figure number of non-dummy blocks in last MCU column & row */
269 tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
270 if (tmp == 0) tmp = compptr->MCU_width;
271 compptr->last_col_width = tmp;
272 tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
273 if (tmp == 0) tmp = compptr->MCU_height;
274 compptr->last_row_height = tmp;
275 /* Prepare array describing MCU composition */
276 mcublks = compptr->MCU_blocks;
277 if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
278 ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
279 while (mcublks-- > 0) {
280 cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
289 * Save away a copy of the Q-table referenced by each component present
290 * in the current scan, unless already saved during a prior scan.
292 * In a multiple-scan JPEG file, the encoder could assign different components
293 * the same Q-table slot number, but change table definitions between scans
294 * so that each component uses a different Q-table. (The IJG encoder is not
295 * currently capable of doing this, but other encoders might.) Since we want
296 * to be able to dequantize all the components at the end of the file, this
297 * means that we have to save away the table actually used for each component.
298 * We do this by copying the table at the start of the first scan containing
299 * the component.
300 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
301 * slot between scans of a component using that slot. If the encoder does so
302 * anyway, this decoder will simply use the Q-table values that were current
303 * at the start of the first scan for the component.
305 * The decompressor output side looks only at the saved quant tables,
306 * not at the current Q-table slots.
309 LOCAL(void)
310 latch_quant_tables (j_decompress_ptr cinfo)
312 int ci, qtblno;
313 jpeg_component_info *compptr;
314 JQUANT_TBL * qtbl;
316 for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
317 compptr = cinfo->cur_comp_info[ci];
318 /* No work if we already saved Q-table for this component */
319 if (compptr->quant_table != NULL)
320 continue;
321 /* Make sure specified quantization table is present */
322 qtblno = compptr->quant_tbl_no;
323 if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
324 cinfo->quant_tbl_ptrs[qtblno] == NULL)
325 ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
326 /* OK, save away the quantization table */
327 qtbl = (JQUANT_TBL *)
328 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
329 SIZEOF(JQUANT_TBL));
330 MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
331 compptr->quant_table = qtbl;
337 * Initialize the input modules to read a scan of compressed data.
338 * The first call to this is done by jdmaster.c after initializing
339 * the entire decompressor (during jpeg_start_decompress).
340 * Subsequent calls come from consume_markers, below.
343 METHODDEF(void)
344 start_input_pass (j_decompress_ptr cinfo)
346 per_scan_setup(cinfo);
347 latch_quant_tables(cinfo);
348 (*cinfo->entropy->start_pass) (cinfo);
349 (*cinfo->coef->start_input_pass) (cinfo);
350 cinfo->inputctl->consume_input = cinfo->coef->consume_data;
355 * Finish up after inputting a compressed-data scan.
356 * This is called by the coefficient controller after it's read all
357 * the expected data of the scan.
360 METHODDEF(void)
361 finish_input_pass (j_decompress_ptr cinfo)
363 cinfo->inputctl->consume_input = consume_markers;
368 * Read JPEG markers before, between, or after compressed-data scans.
369 * Change state as necessary when a new scan is reached.
370 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
372 * The consume_input method pointer points either here or to the
373 * coefficient controller's consume_data routine, depending on whether
374 * we are reading a compressed data segment or inter-segment markers.
377 METHODDEF(int)
378 consume_markers (j_decompress_ptr cinfo)
380 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
381 int val;
383 if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
384 return JPEG_REACHED_EOI;
386 val = (*cinfo->marker->read_markers) (cinfo);
388 switch (val) {
389 case JPEG_REACHED_SOS: /* Found SOS */
390 if (inputctl->inheaders) { /* 1st SOS */
391 initial_setup(cinfo);
392 inputctl->inheaders = FALSE;
393 /* Note: start_input_pass must be called by jdmaster.c
394 * before any more input can be consumed. jdapimin.c is
395 * responsible for enforcing this sequencing.
397 } else { /* 2nd or later SOS marker */
398 if (! inputctl->pub.has_multiple_scans)
399 ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
400 start_input_pass(cinfo);
402 break;
403 case JPEG_REACHED_EOI: /* Found EOI */
404 inputctl->pub.eoi_reached = TRUE;
405 if (inputctl->inheaders) { /* Tables-only datastream, apparently */
406 if (cinfo->marker->saw_SOF)
407 ERREXIT(cinfo, JERR_SOF_NO_SOS);
408 } else {
409 /* Prevent infinite loop in coef ctlr's decompress_data routine
410 * if user set output_scan_number larger than number of scans.
412 if (cinfo->output_scan_number > cinfo->input_scan_number)
413 cinfo->output_scan_number = cinfo->input_scan_number;
415 break;
416 case JPEG_SUSPENDED:
417 break;
420 return val;
425 * Reset state to begin a fresh datastream.
428 METHODDEF(void)
429 reset_input_controller (j_decompress_ptr cinfo)
431 my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
433 inputctl->pub.consume_input = consume_markers;
434 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
435 inputctl->pub.eoi_reached = FALSE;
436 inputctl->inheaders = TRUE;
437 /* Reset other modules */
438 (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
439 (*cinfo->marker->reset_marker_reader) (cinfo);
440 /* Reset progression state -- would be cleaner if entropy decoder did this */
441 cinfo->coef_bits = NULL;
446 * Initialize the input controller module.
447 * This is called only once, when the decompression object is created.
450 GLOBAL(void)
451 jinit_input_controller (j_decompress_ptr cinfo)
453 my_inputctl_ptr inputctl;
455 /* Create subobject in permanent pool */
456 inputctl = (my_inputctl_ptr)
457 (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
458 SIZEOF(my_input_controller));
459 cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
460 /* Initialize method pointers */
461 inputctl->pub.consume_input = consume_markers;
462 inputctl->pub.reset_input_controller = reset_input_controller;
463 inputctl->pub.start_input_pass = start_input_pass;
464 inputctl->pub.finish_input_pass = finish_input_pass;
465 /* Initialize state: can't use reset_input_controller since we don't
466 * want to try to reset other modules yet.
468 inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
469 inputctl->pub.eoi_reached = FALSE;
470 inputctl->inheaders = TRUE;