istereo2

annotate libs/libjpeg/jdapimin.c @ 9:64e15874f3bd

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 26 Sep 2015 02:56:07 +0300
parents
children
rev   line source
nuclear@2 1 /*
nuclear@2 2 * jdapimin.c
nuclear@2 3 *
nuclear@2 4 * Copyright (C) 1994-1998, 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 application interface code for the decompression half
nuclear@2 9 * of the JPEG library. These are the "minimum" API routines that may be
nuclear@2 10 * needed in either the normal full-decompression case or the
nuclear@2 11 * transcoding-only case.
nuclear@2 12 *
nuclear@2 13 * Most of the routines intended to be called directly by an application
nuclear@2 14 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
nuclear@2 15 * shared by compression and decompression, and jdtrans.c for the transcoding
nuclear@2 16 * case.
nuclear@2 17 */
nuclear@2 18
nuclear@2 19 #define JPEG_INTERNALS
nuclear@2 20 #include "jinclude.h"
nuclear@2 21 #include "jpeglib.h"
nuclear@2 22
nuclear@2 23
nuclear@2 24 /*
nuclear@2 25 * Initialization of a JPEG decompression object.
nuclear@2 26 * The error manager must already be set up (in case memory manager fails).
nuclear@2 27 */
nuclear@2 28
nuclear@2 29 GLOBAL(void)
nuclear@2 30 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
nuclear@2 31 {
nuclear@2 32 int i;
nuclear@2 33
nuclear@2 34 /* Guard against version mismatches between library and caller. */
nuclear@2 35 cinfo->mem = NULL; /* so jpeg_destroy knows mem mgr not called */
nuclear@2 36 if (version != JPEG_LIB_VERSION)
nuclear@2 37 ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
nuclear@2 38 if (structsize != SIZEOF(struct jpeg_decompress_struct))
nuclear@2 39 ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE,
nuclear@2 40 (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
nuclear@2 41
nuclear@2 42 /* For debugging purposes, we zero the whole master structure.
nuclear@2 43 * But the application has already set the err pointer, and may have set
nuclear@2 44 * client_data, so we have to save and restore those fields.
nuclear@2 45 * Note: if application hasn't set client_data, tools like Purify may
nuclear@2 46 * complain here.
nuclear@2 47 */
nuclear@2 48 {
nuclear@2 49 struct jpeg_error_mgr * err = cinfo->err;
nuclear@2 50 void * client_data = cinfo->client_data; /* ignore Purify complaint here */
nuclear@2 51 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
nuclear@2 52 cinfo->err = err;
nuclear@2 53 cinfo->client_data = client_data;
nuclear@2 54 }
nuclear@2 55 cinfo->is_decompressor = TRUE;
nuclear@2 56
nuclear@2 57 /* Initialize a memory manager instance for this object */
nuclear@2 58 jinit_memory_mgr((j_common_ptr) cinfo);
nuclear@2 59
nuclear@2 60 /* Zero out pointers to permanent structures. */
nuclear@2 61 cinfo->progress = NULL;
nuclear@2 62 cinfo->src = NULL;
nuclear@2 63
nuclear@2 64 for (i = 0; i < NUM_QUANT_TBLS; i++)
nuclear@2 65 cinfo->quant_tbl_ptrs[i] = NULL;
nuclear@2 66
nuclear@2 67 for (i = 0; i < NUM_HUFF_TBLS; i++) {
nuclear@2 68 cinfo->dc_huff_tbl_ptrs[i] = NULL;
nuclear@2 69 cinfo->ac_huff_tbl_ptrs[i] = NULL;
nuclear@2 70 }
nuclear@2 71
nuclear@2 72 /* Initialize marker processor so application can override methods
nuclear@2 73 * for COM, APPn markers before calling jpeg_read_header.
nuclear@2 74 */
nuclear@2 75 cinfo->marker_list = NULL;
nuclear@2 76 jinit_marker_reader(cinfo);
nuclear@2 77
nuclear@2 78 /* And initialize the overall input controller. */
nuclear@2 79 jinit_input_controller(cinfo);
nuclear@2 80
nuclear@2 81 /* OK, I'm ready */
nuclear@2 82 cinfo->global_state = DSTATE_START;
nuclear@2 83 }
nuclear@2 84
nuclear@2 85
nuclear@2 86 /*
nuclear@2 87 * Destruction of a JPEG decompression object
nuclear@2 88 */
nuclear@2 89
nuclear@2 90 GLOBAL(void)
nuclear@2 91 jpeg_destroy_decompress (j_decompress_ptr cinfo)
nuclear@2 92 {
nuclear@2 93 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
nuclear@2 94 }
nuclear@2 95
nuclear@2 96
nuclear@2 97 /*
nuclear@2 98 * Abort processing of a JPEG decompression operation,
nuclear@2 99 * but don't destroy the object itself.
nuclear@2 100 */
nuclear@2 101
nuclear@2 102 GLOBAL(void)
nuclear@2 103 jpeg_abort_decompress (j_decompress_ptr cinfo)
nuclear@2 104 {
nuclear@2 105 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
nuclear@2 106 }
nuclear@2 107
nuclear@2 108
nuclear@2 109 /*
nuclear@2 110 * Set default decompression parameters.
nuclear@2 111 */
nuclear@2 112
nuclear@2 113 LOCAL(void)
nuclear@2 114 default_decompress_parms (j_decompress_ptr cinfo)
nuclear@2 115 {
nuclear@2 116 /* Guess the input colorspace, and set output colorspace accordingly. */
nuclear@2 117 /* (Wish JPEG committee had provided a real way to specify this...) */
nuclear@2 118 /* Note application may override our guesses. */
nuclear@2 119 switch (cinfo->num_components) {
nuclear@2 120 case 1:
nuclear@2 121 cinfo->jpeg_color_space = JCS_GRAYSCALE;
nuclear@2 122 cinfo->out_color_space = JCS_GRAYSCALE;
nuclear@2 123 break;
nuclear@2 124
nuclear@2 125 case 3:
nuclear@2 126 if (cinfo->saw_JFIF_marker) {
nuclear@2 127 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
nuclear@2 128 } else if (cinfo->saw_Adobe_marker) {
nuclear@2 129 switch (cinfo->Adobe_transform) {
nuclear@2 130 case 0:
nuclear@2 131 cinfo->jpeg_color_space = JCS_RGB;
nuclear@2 132 break;
nuclear@2 133 case 1:
nuclear@2 134 cinfo->jpeg_color_space = JCS_YCbCr;
nuclear@2 135 break;
nuclear@2 136 default:
nuclear@2 137 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
nuclear@2 138 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
nuclear@2 139 break;
nuclear@2 140 }
nuclear@2 141 } else {
nuclear@2 142 /* Saw no special markers, try to guess from the component IDs */
nuclear@2 143 int cid0 = cinfo->comp_info[0].component_id;
nuclear@2 144 int cid1 = cinfo->comp_info[1].component_id;
nuclear@2 145 int cid2 = cinfo->comp_info[2].component_id;
nuclear@2 146
nuclear@2 147 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
nuclear@2 148 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
nuclear@2 149 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
nuclear@2 150 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
nuclear@2 151 else {
nuclear@2 152 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
nuclear@2 153 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
nuclear@2 154 }
nuclear@2 155 }
nuclear@2 156 /* Always guess RGB is proper output colorspace. */
nuclear@2 157 cinfo->out_color_space = JCS_RGB;
nuclear@2 158 break;
nuclear@2 159
nuclear@2 160 case 4:
nuclear@2 161 if (cinfo->saw_Adobe_marker) {
nuclear@2 162 switch (cinfo->Adobe_transform) {
nuclear@2 163 case 0:
nuclear@2 164 cinfo->jpeg_color_space = JCS_CMYK;
nuclear@2 165 break;
nuclear@2 166 case 2:
nuclear@2 167 cinfo->jpeg_color_space = JCS_YCCK;
nuclear@2 168 break;
nuclear@2 169 default:
nuclear@2 170 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
nuclear@2 171 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
nuclear@2 172 break;
nuclear@2 173 }
nuclear@2 174 } else {
nuclear@2 175 /* No special markers, assume straight CMYK. */
nuclear@2 176 cinfo->jpeg_color_space = JCS_CMYK;
nuclear@2 177 }
nuclear@2 178 cinfo->out_color_space = JCS_CMYK;
nuclear@2 179 break;
nuclear@2 180
nuclear@2 181 default:
nuclear@2 182 cinfo->jpeg_color_space = JCS_UNKNOWN;
nuclear@2 183 cinfo->out_color_space = JCS_UNKNOWN;
nuclear@2 184 break;
nuclear@2 185 }
nuclear@2 186
nuclear@2 187 /* Set defaults for other decompression parameters. */
nuclear@2 188 cinfo->scale_num = 1; /* 1:1 scaling */
nuclear@2 189 cinfo->scale_denom = 1;
nuclear@2 190 cinfo->output_gamma = 1.0;
nuclear@2 191 cinfo->buffered_image = FALSE;
nuclear@2 192 cinfo->raw_data_out = FALSE;
nuclear@2 193 cinfo->dct_method = JDCT_DEFAULT;
nuclear@2 194 cinfo->do_fancy_upsampling = TRUE;
nuclear@2 195 cinfo->do_block_smoothing = TRUE;
nuclear@2 196 cinfo->quantize_colors = FALSE;
nuclear@2 197 /* We set these in case application only sets quantize_colors. */
nuclear@2 198 cinfo->dither_mode = JDITHER_FS;
nuclear@2 199 #ifdef QUANT_2PASS_SUPPORTED
nuclear@2 200 cinfo->two_pass_quantize = TRUE;
nuclear@2 201 #else
nuclear@2 202 cinfo->two_pass_quantize = FALSE;
nuclear@2 203 #endif
nuclear@2 204 cinfo->desired_number_of_colors = 256;
nuclear@2 205 cinfo->colormap = NULL;
nuclear@2 206 /* Initialize for no mode change in buffered-image mode. */
nuclear@2 207 cinfo->enable_1pass_quant = FALSE;
nuclear@2 208 cinfo->enable_external_quant = FALSE;
nuclear@2 209 cinfo->enable_2pass_quant = FALSE;
nuclear@2 210 }
nuclear@2 211
nuclear@2 212
nuclear@2 213 /*
nuclear@2 214 * Decompression startup: read start of JPEG datastream to see what's there.
nuclear@2 215 * Need only initialize JPEG object and supply a data source before calling.
nuclear@2 216 *
nuclear@2 217 * This routine will read as far as the first SOS marker (ie, actual start of
nuclear@2 218 * compressed data), and will save all tables and parameters in the JPEG
nuclear@2 219 * object. It will also initialize the decompression parameters to default
nuclear@2 220 * values, and finally return JPEG_HEADER_OK. On return, the application may
nuclear@2 221 * adjust the decompression parameters and then call jpeg_start_decompress.
nuclear@2 222 * (Or, if the application only wanted to determine the image parameters,
nuclear@2 223 * the data need not be decompressed. In that case, call jpeg_abort or
nuclear@2 224 * jpeg_destroy to release any temporary space.)
nuclear@2 225 * If an abbreviated (tables only) datastream is presented, the routine will
nuclear@2 226 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
nuclear@2 227 * re-use the JPEG object to read the abbreviated image datastream(s).
nuclear@2 228 * It is unnecessary (but OK) to call jpeg_abort in this case.
nuclear@2 229 * The JPEG_SUSPENDED return code only occurs if the data source module
nuclear@2 230 * requests suspension of the decompressor. In this case the application
nuclear@2 231 * should load more source data and then re-call jpeg_read_header to resume
nuclear@2 232 * processing.
nuclear@2 233 * If a non-suspending data source is used and require_image is TRUE, then the
nuclear@2 234 * return code need not be inspected since only JPEG_HEADER_OK is possible.
nuclear@2 235 *
nuclear@2 236 * This routine is now just a front end to jpeg_consume_input, with some
nuclear@2 237 * extra error checking.
nuclear@2 238 */
nuclear@2 239
nuclear@2 240 GLOBAL(int)
nuclear@2 241 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
nuclear@2 242 {
nuclear@2 243 int retcode;
nuclear@2 244
nuclear@2 245 if (cinfo->global_state != DSTATE_START &&
nuclear@2 246 cinfo->global_state != DSTATE_INHEADER)
nuclear@2 247 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@2 248
nuclear@2 249 retcode = jpeg_consume_input(cinfo);
nuclear@2 250
nuclear@2 251 switch (retcode) {
nuclear@2 252 case JPEG_REACHED_SOS:
nuclear@2 253 retcode = JPEG_HEADER_OK;
nuclear@2 254 break;
nuclear@2 255 case JPEG_REACHED_EOI:
nuclear@2 256 if (require_image) /* Complain if application wanted an image */
nuclear@2 257 ERREXIT(cinfo, JERR_NO_IMAGE);
nuclear@2 258 /* Reset to start state; it would be safer to require the application to
nuclear@2 259 * call jpeg_abort, but we can't change it now for compatibility reasons.
nuclear@2 260 * A side effect is to free any temporary memory (there shouldn't be any).
nuclear@2 261 */
nuclear@2 262 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
nuclear@2 263 retcode = JPEG_HEADER_TABLES_ONLY;
nuclear@2 264 break;
nuclear@2 265 case JPEG_SUSPENDED:
nuclear@2 266 /* no work */
nuclear@2 267 break;
nuclear@2 268 }
nuclear@2 269
nuclear@2 270 return retcode;
nuclear@2 271 }
nuclear@2 272
nuclear@2 273
nuclear@2 274 /*
nuclear@2 275 * Consume data in advance of what the decompressor requires.
nuclear@2 276 * This can be called at any time once the decompressor object has
nuclear@2 277 * been created and a data source has been set up.
nuclear@2 278 *
nuclear@2 279 * This routine is essentially a state machine that handles a couple
nuclear@2 280 * of critical state-transition actions, namely initial setup and
nuclear@2 281 * transition from header scanning to ready-for-start_decompress.
nuclear@2 282 * All the actual input is done via the input controller's consume_input
nuclear@2 283 * method.
nuclear@2 284 */
nuclear@2 285
nuclear@2 286 GLOBAL(int)
nuclear@2 287 jpeg_consume_input (j_decompress_ptr cinfo)
nuclear@2 288 {
nuclear@2 289 int retcode = JPEG_SUSPENDED;
nuclear@2 290
nuclear@2 291 /* NB: every possible DSTATE value should be listed in this switch */
nuclear@2 292 switch (cinfo->global_state) {
nuclear@2 293 case DSTATE_START:
nuclear@2 294 /* Start-of-datastream actions: reset appropriate modules */
nuclear@2 295 (*cinfo->inputctl->reset_input_controller) (cinfo);
nuclear@2 296 /* Initialize application's data source module */
nuclear@2 297 (*cinfo->src->init_source) (cinfo);
nuclear@2 298 cinfo->global_state = DSTATE_INHEADER;
nuclear@2 299 /*FALLTHROUGH*/
nuclear@2 300 case DSTATE_INHEADER:
nuclear@2 301 retcode = (*cinfo->inputctl->consume_input) (cinfo);
nuclear@2 302 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
nuclear@2 303 /* Set up default parameters based on header data */
nuclear@2 304 default_decompress_parms(cinfo);
nuclear@2 305 /* Set global state: ready for start_decompress */
nuclear@2 306 cinfo->global_state = DSTATE_READY;
nuclear@2 307 }
nuclear@2 308 break;
nuclear@2 309 case DSTATE_READY:
nuclear@2 310 /* Can't advance past first SOS until start_decompress is called */
nuclear@2 311 retcode = JPEG_REACHED_SOS;
nuclear@2 312 break;
nuclear@2 313 case DSTATE_PRELOAD:
nuclear@2 314 case DSTATE_PRESCAN:
nuclear@2 315 case DSTATE_SCANNING:
nuclear@2 316 case DSTATE_RAW_OK:
nuclear@2 317 case DSTATE_BUFIMAGE:
nuclear@2 318 case DSTATE_BUFPOST:
nuclear@2 319 case DSTATE_STOPPING:
nuclear@2 320 retcode = (*cinfo->inputctl->consume_input) (cinfo);
nuclear@2 321 break;
nuclear@2 322 default:
nuclear@2 323 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@2 324 }
nuclear@2 325 return retcode;
nuclear@2 326 }
nuclear@2 327
nuclear@2 328
nuclear@2 329 /*
nuclear@2 330 * Have we finished reading the input file?
nuclear@2 331 */
nuclear@2 332
nuclear@2 333 GLOBAL(boolean)
nuclear@2 334 jpeg_input_complete (j_decompress_ptr cinfo)
nuclear@2 335 {
nuclear@2 336 /* Check for valid jpeg object */
nuclear@2 337 if (cinfo->global_state < DSTATE_START ||
nuclear@2 338 cinfo->global_state > DSTATE_STOPPING)
nuclear@2 339 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@2 340 return cinfo->inputctl->eoi_reached;
nuclear@2 341 }
nuclear@2 342
nuclear@2 343
nuclear@2 344 /*
nuclear@2 345 * Is there more than one scan?
nuclear@2 346 */
nuclear@2 347
nuclear@2 348 GLOBAL(boolean)
nuclear@2 349 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
nuclear@2 350 {
nuclear@2 351 /* Only valid after jpeg_read_header completes */
nuclear@2 352 if (cinfo->global_state < DSTATE_READY ||
nuclear@2 353 cinfo->global_state > DSTATE_STOPPING)
nuclear@2 354 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@2 355 return cinfo->inputctl->has_multiple_scans;
nuclear@2 356 }
nuclear@2 357
nuclear@2 358
nuclear@2 359 /*
nuclear@2 360 * Finish JPEG decompression.
nuclear@2 361 *
nuclear@2 362 * This will normally just verify the file trailer and release temp storage.
nuclear@2 363 *
nuclear@2 364 * Returns FALSE if suspended. The return value need be inspected only if
nuclear@2 365 * a suspending data source is used.
nuclear@2 366 */
nuclear@2 367
nuclear@2 368 GLOBAL(boolean)
nuclear@2 369 jpeg_finish_decompress (j_decompress_ptr cinfo)
nuclear@2 370 {
nuclear@2 371 if ((cinfo->global_state == DSTATE_SCANNING ||
nuclear@2 372 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
nuclear@2 373 /* Terminate final pass of non-buffered mode */
nuclear@2 374 if (cinfo->output_scanline < cinfo->output_height)
nuclear@2 375 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
nuclear@2 376 (*cinfo->master->finish_output_pass) (cinfo);
nuclear@2 377 cinfo->global_state = DSTATE_STOPPING;
nuclear@2 378 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
nuclear@2 379 /* Finishing after a buffered-image operation */
nuclear@2 380 cinfo->global_state = DSTATE_STOPPING;
nuclear@2 381 } else if (cinfo->global_state != DSTATE_STOPPING) {
nuclear@2 382 /* STOPPING = repeat call after a suspension, anything else is error */
nuclear@2 383 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
nuclear@2 384 }
nuclear@2 385 /* Read until EOI */
nuclear@2 386 while (! cinfo->inputctl->eoi_reached) {
nuclear@2 387 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
nuclear@2 388 return FALSE; /* Suspend, come back later */
nuclear@2 389 }
nuclear@2 390 /* Do final cleanup */
nuclear@2 391 (*cinfo->src->term_source) (cinfo);
nuclear@2 392 /* We can use jpeg_abort to release memory and reset global_state */
nuclear@2 393 jpeg_abort((j_common_ptr) cinfo);
nuclear@2 394 return TRUE;
nuclear@2 395 }