3dphotoshoot

annotate libs/libjpeg/jdapimin.c @ 17:aef7f51f6397

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