vrshoot

annotate libs/libjpeg/jdinput.c @ 2:334d17aed7de

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