istereo2

annotate libs/libjpeg/jdinput.c @ 7:a3c4fcc9f8f3

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