dbf-halloween2015

annotate libs/libjpeg/jdinput.c @ 1:c3f5c32cb210

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