3dphotoshoot
diff libs/libjpeg/jdinput.c @ 14:06dc8b9b4f89
added libimago, libjpeg and libpng
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Sun, 07 Jun 2015 17:25:49 +0300 |
parents | |
children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/libs/libjpeg/jdinput.c Sun Jun 07 17:25:49 2015 +0300 1.3 @@ -0,0 +1,381 @@ 1.4 +/* 1.5 + * jdinput.c 1.6 + * 1.7 + * Copyright (C) 1991-1997, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains input control logic for the JPEG decompressor. 1.12 + * These routines are concerned with controlling the decompressor's input 1.13 + * processing (marker reading and coefficient decoding). The actual input 1.14 + * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c. 1.15 + */ 1.16 + 1.17 +#define JPEG_INTERNALS 1.18 +#include "jinclude.h" 1.19 +#include "jpeglib.h" 1.20 + 1.21 + 1.22 +/* Private state */ 1.23 + 1.24 +typedef struct { 1.25 + struct jpeg_input_controller pub; /* public fields */ 1.26 + 1.27 + boolean inheaders; /* TRUE until first SOS is reached */ 1.28 +} my_input_controller; 1.29 + 1.30 +typedef my_input_controller * my_inputctl_ptr; 1.31 + 1.32 + 1.33 +/* Forward declarations */ 1.34 +METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo)); 1.35 + 1.36 + 1.37 +/* 1.38 + * Routines to calculate various quantities related to the size of the image. 1.39 + */ 1.40 + 1.41 +LOCAL(void) 1.42 +initial_setup (j_decompress_ptr cinfo) 1.43 +/* Called once, when first SOS marker is reached */ 1.44 +{ 1.45 + int ci; 1.46 + jpeg_component_info *compptr; 1.47 + 1.48 + /* Make sure image isn't bigger than I can handle */ 1.49 + if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION || 1.50 + (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION) 1.51 + ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION); 1.52 + 1.53 + /* For now, precision must match compiled-in value... */ 1.54 + if (cinfo->data_precision != BITS_IN_JSAMPLE) 1.55 + ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision); 1.56 + 1.57 + /* Check that number of components won't exceed internal array sizes */ 1.58 + if (cinfo->num_components > MAX_COMPONENTS) 1.59 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, 1.60 + MAX_COMPONENTS); 1.61 + 1.62 + /* Compute maximum sampling factors; check factor validity */ 1.63 + cinfo->max_h_samp_factor = 1; 1.64 + cinfo->max_v_samp_factor = 1; 1.65 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.66 + ci++, compptr++) { 1.67 + if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR || 1.68 + compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR) 1.69 + ERREXIT(cinfo, JERR_BAD_SAMPLING); 1.70 + cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor, 1.71 + compptr->h_samp_factor); 1.72 + cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor, 1.73 + compptr->v_samp_factor); 1.74 + } 1.75 + 1.76 + /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE. 1.77 + * In the full decompressor, this will be overridden by jdmaster.c; 1.78 + * but in the transcoder, jdmaster.c is not used, so we must do it here. 1.79 + */ 1.80 + cinfo->min_DCT_scaled_size = DCTSIZE; 1.81 + 1.82 + /* Compute dimensions of components */ 1.83 + for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components; 1.84 + ci++, compptr++) { 1.85 + compptr->DCT_scaled_size = DCTSIZE; 1.86 + /* Size in DCT blocks */ 1.87 + compptr->width_in_blocks = (JDIMENSION) 1.88 + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 1.89 + (long) (cinfo->max_h_samp_factor * DCTSIZE)); 1.90 + compptr->height_in_blocks = (JDIMENSION) 1.91 + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 1.92 + (long) (cinfo->max_v_samp_factor * DCTSIZE)); 1.93 + /* downsampled_width and downsampled_height will also be overridden by 1.94 + * jdmaster.c if we are doing full decompression. The transcoder library 1.95 + * doesn't use these values, but the calling application might. 1.96 + */ 1.97 + /* Size in samples */ 1.98 + compptr->downsampled_width = (JDIMENSION) 1.99 + jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor, 1.100 + (long) cinfo->max_h_samp_factor); 1.101 + compptr->downsampled_height = (JDIMENSION) 1.102 + jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor, 1.103 + (long) cinfo->max_v_samp_factor); 1.104 + /* Mark component needed, until color conversion says otherwise */ 1.105 + compptr->component_needed = TRUE; 1.106 + /* Mark no quantization table yet saved for component */ 1.107 + compptr->quant_table = NULL; 1.108 + } 1.109 + 1.110 + /* Compute number of fully interleaved MCU rows. */ 1.111 + cinfo->total_iMCU_rows = (JDIMENSION) 1.112 + jdiv_round_up((long) cinfo->image_height, 1.113 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.114 + 1.115 + /* Decide whether file contains multiple scans */ 1.116 + if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode) 1.117 + cinfo->inputctl->has_multiple_scans = TRUE; 1.118 + else 1.119 + cinfo->inputctl->has_multiple_scans = FALSE; 1.120 +} 1.121 + 1.122 + 1.123 +LOCAL(void) 1.124 +per_scan_setup (j_decompress_ptr cinfo) 1.125 +/* Do computations that are needed before processing a JPEG scan */ 1.126 +/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */ 1.127 +{ 1.128 + int ci, mcublks, tmp; 1.129 + jpeg_component_info *compptr; 1.130 + 1.131 + if (cinfo->comps_in_scan == 1) { 1.132 + 1.133 + /* Noninterleaved (single-component) scan */ 1.134 + compptr = cinfo->cur_comp_info[0]; 1.135 + 1.136 + /* Overall image size in MCUs */ 1.137 + cinfo->MCUs_per_row = compptr->width_in_blocks; 1.138 + cinfo->MCU_rows_in_scan = compptr->height_in_blocks; 1.139 + 1.140 + /* For noninterleaved scan, always one block per MCU */ 1.141 + compptr->MCU_width = 1; 1.142 + compptr->MCU_height = 1; 1.143 + compptr->MCU_blocks = 1; 1.144 + compptr->MCU_sample_width = compptr->DCT_scaled_size; 1.145 + compptr->last_col_width = 1; 1.146 + /* For noninterleaved scans, it is convenient to define last_row_height 1.147 + * as the number of block rows present in the last iMCU row. 1.148 + */ 1.149 + tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor); 1.150 + if (tmp == 0) tmp = compptr->v_samp_factor; 1.151 + compptr->last_row_height = tmp; 1.152 + 1.153 + /* Prepare array describing MCU composition */ 1.154 + cinfo->blocks_in_MCU = 1; 1.155 + cinfo->MCU_membership[0] = 0; 1.156 + 1.157 + } else { 1.158 + 1.159 + /* Interleaved (multi-component) scan */ 1.160 + if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN) 1.161 + ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan, 1.162 + MAX_COMPS_IN_SCAN); 1.163 + 1.164 + /* Overall image size in MCUs */ 1.165 + cinfo->MCUs_per_row = (JDIMENSION) 1.166 + jdiv_round_up((long) cinfo->image_width, 1.167 + (long) (cinfo->max_h_samp_factor*DCTSIZE)); 1.168 + cinfo->MCU_rows_in_scan = (JDIMENSION) 1.169 + jdiv_round_up((long) cinfo->image_height, 1.170 + (long) (cinfo->max_v_samp_factor*DCTSIZE)); 1.171 + 1.172 + cinfo->blocks_in_MCU = 0; 1.173 + 1.174 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.175 + compptr = cinfo->cur_comp_info[ci]; 1.176 + /* Sampling factors give # of blocks of component in each MCU */ 1.177 + compptr->MCU_width = compptr->h_samp_factor; 1.178 + compptr->MCU_height = compptr->v_samp_factor; 1.179 + compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height; 1.180 + compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size; 1.181 + /* Figure number of non-dummy blocks in last MCU column & row */ 1.182 + tmp = (int) (compptr->width_in_blocks % compptr->MCU_width); 1.183 + if (tmp == 0) tmp = compptr->MCU_width; 1.184 + compptr->last_col_width = tmp; 1.185 + tmp = (int) (compptr->height_in_blocks % compptr->MCU_height); 1.186 + if (tmp == 0) tmp = compptr->MCU_height; 1.187 + compptr->last_row_height = tmp; 1.188 + /* Prepare array describing MCU composition */ 1.189 + mcublks = compptr->MCU_blocks; 1.190 + if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU) 1.191 + ERREXIT(cinfo, JERR_BAD_MCU_SIZE); 1.192 + while (mcublks-- > 0) { 1.193 + cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci; 1.194 + } 1.195 + } 1.196 + 1.197 + } 1.198 +} 1.199 + 1.200 + 1.201 +/* 1.202 + * Save away a copy of the Q-table referenced by each component present 1.203 + * in the current scan, unless already saved during a prior scan. 1.204 + * 1.205 + * In a multiple-scan JPEG file, the encoder could assign different components 1.206 + * the same Q-table slot number, but change table definitions between scans 1.207 + * so that each component uses a different Q-table. (The IJG encoder is not 1.208 + * currently capable of doing this, but other encoders might.) Since we want 1.209 + * to be able to dequantize all the components at the end of the file, this 1.210 + * means that we have to save away the table actually used for each component. 1.211 + * We do this by copying the table at the start of the first scan containing 1.212 + * the component. 1.213 + * The JPEG spec prohibits the encoder from changing the contents of a Q-table 1.214 + * slot between scans of a component using that slot. If the encoder does so 1.215 + * anyway, this decoder will simply use the Q-table values that were current 1.216 + * at the start of the first scan for the component. 1.217 + * 1.218 + * The decompressor output side looks only at the saved quant tables, 1.219 + * not at the current Q-table slots. 1.220 + */ 1.221 + 1.222 +LOCAL(void) 1.223 +latch_quant_tables (j_decompress_ptr cinfo) 1.224 +{ 1.225 + int ci, qtblno; 1.226 + jpeg_component_info *compptr; 1.227 + JQUANT_TBL * qtbl; 1.228 + 1.229 + for (ci = 0; ci < cinfo->comps_in_scan; ci++) { 1.230 + compptr = cinfo->cur_comp_info[ci]; 1.231 + /* No work if we already saved Q-table for this component */ 1.232 + if (compptr->quant_table != NULL) 1.233 + continue; 1.234 + /* Make sure specified quantization table is present */ 1.235 + qtblno = compptr->quant_tbl_no; 1.236 + if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS || 1.237 + cinfo->quant_tbl_ptrs[qtblno] == NULL) 1.238 + ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno); 1.239 + /* OK, save away the quantization table */ 1.240 + qtbl = (JQUANT_TBL *) 1.241 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 1.242 + SIZEOF(JQUANT_TBL)); 1.243 + MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL)); 1.244 + compptr->quant_table = qtbl; 1.245 + } 1.246 +} 1.247 + 1.248 + 1.249 +/* 1.250 + * Initialize the input modules to read a scan of compressed data. 1.251 + * The first call to this is done by jdmaster.c after initializing 1.252 + * the entire decompressor (during jpeg_start_decompress). 1.253 + * Subsequent calls come from consume_markers, below. 1.254 + */ 1.255 + 1.256 +METHODDEF(void) 1.257 +start_input_pass (j_decompress_ptr cinfo) 1.258 +{ 1.259 + per_scan_setup(cinfo); 1.260 + latch_quant_tables(cinfo); 1.261 + (*cinfo->entropy->start_pass) (cinfo); 1.262 + (*cinfo->coef->start_input_pass) (cinfo); 1.263 + cinfo->inputctl->consume_input = cinfo->coef->consume_data; 1.264 +} 1.265 + 1.266 + 1.267 +/* 1.268 + * Finish up after inputting a compressed-data scan. 1.269 + * This is called by the coefficient controller after it's read all 1.270 + * the expected data of the scan. 1.271 + */ 1.272 + 1.273 +METHODDEF(void) 1.274 +finish_input_pass (j_decompress_ptr cinfo) 1.275 +{ 1.276 + cinfo->inputctl->consume_input = consume_markers; 1.277 +} 1.278 + 1.279 + 1.280 +/* 1.281 + * Read JPEG markers before, between, or after compressed-data scans. 1.282 + * Change state as necessary when a new scan is reached. 1.283 + * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI. 1.284 + * 1.285 + * The consume_input method pointer points either here or to the 1.286 + * coefficient controller's consume_data routine, depending on whether 1.287 + * we are reading a compressed data segment or inter-segment markers. 1.288 + */ 1.289 + 1.290 +METHODDEF(int) 1.291 +consume_markers (j_decompress_ptr cinfo) 1.292 +{ 1.293 + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 1.294 + int val; 1.295 + 1.296 + if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */ 1.297 + return JPEG_REACHED_EOI; 1.298 + 1.299 + val = (*cinfo->marker->read_markers) (cinfo); 1.300 + 1.301 + switch (val) { 1.302 + case JPEG_REACHED_SOS: /* Found SOS */ 1.303 + if (inputctl->inheaders) { /* 1st SOS */ 1.304 + initial_setup(cinfo); 1.305 + inputctl->inheaders = FALSE; 1.306 + /* Note: start_input_pass must be called by jdmaster.c 1.307 + * before any more input can be consumed. jdapimin.c is 1.308 + * responsible for enforcing this sequencing. 1.309 + */ 1.310 + } else { /* 2nd or later SOS marker */ 1.311 + if (! inputctl->pub.has_multiple_scans) 1.312 + ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */ 1.313 + start_input_pass(cinfo); 1.314 + } 1.315 + break; 1.316 + case JPEG_REACHED_EOI: /* Found EOI */ 1.317 + inputctl->pub.eoi_reached = TRUE; 1.318 + if (inputctl->inheaders) { /* Tables-only datastream, apparently */ 1.319 + if (cinfo->marker->saw_SOF) 1.320 + ERREXIT(cinfo, JERR_SOF_NO_SOS); 1.321 + } else { 1.322 + /* Prevent infinite loop in coef ctlr's decompress_data routine 1.323 + * if user set output_scan_number larger than number of scans. 1.324 + */ 1.325 + if (cinfo->output_scan_number > cinfo->input_scan_number) 1.326 + cinfo->output_scan_number = cinfo->input_scan_number; 1.327 + } 1.328 + break; 1.329 + case JPEG_SUSPENDED: 1.330 + break; 1.331 + } 1.332 + 1.333 + return val; 1.334 +} 1.335 + 1.336 + 1.337 +/* 1.338 + * Reset state to begin a fresh datastream. 1.339 + */ 1.340 + 1.341 +METHODDEF(void) 1.342 +reset_input_controller (j_decompress_ptr cinfo) 1.343 +{ 1.344 + my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl; 1.345 + 1.346 + inputctl->pub.consume_input = consume_markers; 1.347 + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 1.348 + inputctl->pub.eoi_reached = FALSE; 1.349 + inputctl->inheaders = TRUE; 1.350 + /* Reset other modules */ 1.351 + (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo); 1.352 + (*cinfo->marker->reset_marker_reader) (cinfo); 1.353 + /* Reset progression state -- would be cleaner if entropy decoder did this */ 1.354 + cinfo->coef_bits = NULL; 1.355 +} 1.356 + 1.357 + 1.358 +/* 1.359 + * Initialize the input controller module. 1.360 + * This is called only once, when the decompression object is created. 1.361 + */ 1.362 + 1.363 +GLOBAL(void) 1.364 +jinit_input_controller (j_decompress_ptr cinfo) 1.365 +{ 1.366 + my_inputctl_ptr inputctl; 1.367 + 1.368 + /* Create subobject in permanent pool */ 1.369 + inputctl = (my_inputctl_ptr) 1.370 + (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT, 1.371 + SIZEOF(my_input_controller)); 1.372 + cinfo->inputctl = (struct jpeg_input_controller *) inputctl; 1.373 + /* Initialize method pointers */ 1.374 + inputctl->pub.consume_input = consume_markers; 1.375 + inputctl->pub.reset_input_controller = reset_input_controller; 1.376 + inputctl->pub.start_input_pass = start_input_pass; 1.377 + inputctl->pub.finish_input_pass = finish_input_pass; 1.378 + /* Initialize state: can't use reset_input_controller since we don't 1.379 + * want to try to reset other modules yet. 1.380 + */ 1.381 + inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */ 1.382 + inputctl->pub.eoi_reached = FALSE; 1.383 + inputctl->inheaders = TRUE; 1.384 +}