istereo

annotate libs/libjpeg/jdinput.c @ 43:73813c1176de

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