dbf-halloween2015

annotate libs/libjpeg/jdapimin.c @ 1:c3f5c32cb210

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