vrshoot

annotate libs/libjpeg/jdapimin.c @ 0:b2f14e535253

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