media/libjpeg/jdapimin.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial