istereo

annotate libs/libjpeg/jdapimin.c @ 33:33e9eedad390

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