7 * Copyright (C) 1994-1995, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
11 * For conditions of distribution and use, see the accompanying README file.
15 * This file contains application interface code for the decompression half
17 * of the JPEG library. These are the "minimum" API routines that may be
19 * needed in either the normal full-decompression case or the
21 * transcoding-only case.
25 * Most of the routines intended to be called directly by an application
27 * are in this file or in jdapistd.c. But also see jcomapi.c for routines
29 * shared by compression and decompression, and jdtrans.c for the transcoding
37 #define JPEG_INTERNALS
41 #include "radiant_jpeglib.h"
49 * Initialization of a JPEG decompression object.
51 * The error manager must already be set up (in case memory manager fails).
59 jpeg_create_decompress (j_decompress_ptr cinfo)
67 /* For debugging purposes, zero the whole master structure.
69 * But error manager pointer is already there, so save and restore it.
75 struct jpeg_error_mgr * err = cinfo->err;
77 i = sizeof(struct jpeg_decompress_struct);
79 i = SIZEOF(struct jpeg_decompress_struct);
81 MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
87 cinfo->is_decompressor = TRUE;
91 /* Initialize a memory manager instance for this object */
93 jinit_memory_mgr((j_common_ptr) cinfo);
97 /* Zero out pointers to permanent structures. */
99 cinfo->progress = NULL;
105 for (i = 0; i < NUM_QUANT_TBLS; i++)
107 cinfo->quant_tbl_ptrs[i] = NULL;
111 for (i = 0; i < NUM_HUFF_TBLS; i++) {
113 cinfo->dc_huff_tbl_ptrs[i] = NULL;
115 cinfo->ac_huff_tbl_ptrs[i] = NULL;
121 /* Initialize marker processor so application can override methods
123 * for COM, APPn markers before calling jpeg_read_header.
127 jinit_marker_reader(cinfo);
131 /* And initialize the overall input controller. */
133 jinit_input_controller(cinfo);
139 cinfo->global_state = DSTATE_START;
149 * Destruction of a JPEG decompression object
157 jpeg_destroy_decompress (j_decompress_ptr cinfo)
161 jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
171 * Abort processing of a JPEG decompression operation,
173 * but don't destroy the object itself.
181 jpeg_abort_decompress (j_decompress_ptr cinfo)
185 jpeg_abort((j_common_ptr) cinfo); /* use common routine */
195 * Install a special processing method for COM or APPn markers.
203 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
205 jpeg_marker_parser_method routine)
209 if (marker_code == JPEG_COM)
211 cinfo->marker->process_COM = routine;
213 else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
215 cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
219 ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
229 * Set default decompression parameters.
237 default_decompress_parms (j_decompress_ptr cinfo)
241 /* Guess the input colorspace, and set output colorspace accordingly. */
243 /* (Wish JPEG committee had provided a real way to specify this...) */
245 /* Note application may override our guesses. */
247 switch (cinfo->num_components) {
251 cinfo->jpeg_color_space = JCS_GRAYSCALE;
253 cinfo->out_color_space = JCS_GRAYSCALE;
261 if (cinfo->saw_JFIF_marker) {
263 cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
265 } else if (cinfo->saw_Adobe_marker) {
267 switch (cinfo->Adobe_transform) {
271 cinfo->jpeg_color_space = JCS_RGB;
277 cinfo->jpeg_color_space = JCS_YCbCr;
283 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
285 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
293 /* Saw no special markers, try to guess from the component IDs */
295 int cid0 = cinfo->comp_info[0].component_id;
297 int cid1 = cinfo->comp_info[1].component_id;
299 int cid2 = cinfo->comp_info[2].component_id;
303 if (cid0 == 1 && cid1 == 2 && cid2 == 3)
305 cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
307 else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
309 cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
313 TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
315 cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
321 /* Always guess RGB is proper output colorspace. */
323 cinfo->out_color_space = JCS_RGB;
331 if (cinfo->saw_Adobe_marker) {
333 switch (cinfo->Adobe_transform) {
337 cinfo->jpeg_color_space = JCS_CMYK;
343 cinfo->jpeg_color_space = JCS_YCCK;
349 WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
351 cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
359 /* No special markers, assume straight CMYK. */
361 cinfo->jpeg_color_space = JCS_CMYK;
365 cinfo->out_color_space = JCS_CMYK;
373 cinfo->jpeg_color_space = JCS_UNKNOWN;
375 cinfo->out_color_space = JCS_UNKNOWN;
383 /* Set defaults for other decompression parameters. */
385 cinfo->scale_num = 1; /* 1:1 scaling */
387 cinfo->scale_denom = 1;
389 cinfo->output_gamma = 1.0;
391 cinfo->buffered_image = FALSE;
393 cinfo->raw_data_out = FALSE;
395 cinfo->dct_method = JDCT_DEFAULT;
397 cinfo->do_fancy_upsampling = TRUE;
399 cinfo->do_block_smoothing = TRUE;
401 cinfo->quantize_colors = FALSE;
403 /* We set these in case application only sets quantize_colors. */
405 cinfo->dither_mode = JDITHER_FS;
407 #ifdef QUANT_2PASS_SUPPORTED
409 cinfo->two_pass_quantize = TRUE;
413 cinfo->two_pass_quantize = FALSE;
417 cinfo->desired_number_of_colors = 256;
419 cinfo->colormap = NULL;
421 /* Initialize for no mode change in buffered-image mode. */
423 cinfo->enable_1pass_quant = FALSE;
425 cinfo->enable_external_quant = FALSE;
427 cinfo->enable_2pass_quant = FALSE;
437 * Decompression startup: read start of JPEG datastream to see what's there.
439 * Need only initialize JPEG object and supply a data source before calling.
443 * This routine will read as far as the first SOS marker (ie, actual start of
445 * compressed data), and will save all tables and parameters in the JPEG
447 * object. It will also initialize the decompression parameters to default
449 * values, and finally return JPEG_HEADER_OK. On return, the application may
451 * adjust the decompression parameters and then call jpeg_start_decompress.
453 * (Or, if the application only wanted to determine the image parameters,
455 * the data need not be decompressed. In that case, call jpeg_abort or
457 * jpeg_destroy to release any temporary space.)
459 * If an abbreviated (tables only) datastream is presented, the routine will
461 * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
463 * re-use the JPEG object to read the abbreviated image datastream(s).
465 * It is unnecessary (but OK) to call jpeg_abort in this case.
467 * The JPEG_SUSPENDED return code only occurs if the data source module
469 * requests suspension of the decompressor. In this case the application
471 * should load more source data and then re-call jpeg_read_header to resume
475 * If a non-suspending data source is used and require_image is TRUE, then the
477 * return code need not be inspected since only JPEG_HEADER_OK is possible.
481 * This routine is now just a front end to jpeg_consume_input, with some
483 * extra error checking.
491 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
499 if (cinfo->global_state != DSTATE_START &&
501 cinfo->global_state != DSTATE_INHEADER)
503 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
507 retcode = jpeg_consume_input(cinfo);
513 case JPEG_REACHED_SOS:
515 retcode = JPEG_HEADER_OK;
519 case JPEG_REACHED_EOI:
521 if (require_image) /* Complain if application wanted an image */
523 ERREXIT(cinfo, JERR_NO_IMAGE);
525 /* Reset to start state; it would be safer to require the application to
527 * call jpeg_abort, but we can't change it now for compatibility reasons.
529 * A side effect is to free any temporary memory (there shouldn't be any).
533 jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
535 retcode = JPEG_HEADER_TABLES_ONLY;
559 * Consume data in advance of what the decompressor requires.
561 * This can be called at any time once the decompressor object has
563 * been created and a data source has been set up.
567 * This routine is essentially a state machine that handles a couple
569 * of critical state-transition actions, namely initial setup and
571 * transition from header scanning to ready-for-start_decompress.
573 * All the actual input is done via the input controller's consume_input
583 jpeg_consume_input (j_decompress_ptr cinfo)
587 int retcode = JPEG_SUSPENDED;
591 /* NB: every possible DSTATE value should be listed in this switch */
593 switch (cinfo->global_state) {
597 /* Start-of-datastream actions: reset appropriate modules */
599 (*cinfo->inputctl->reset_input_controller) (cinfo);
601 /* Initialize application's data source module */
603 (*cinfo->src->init_source) (cinfo);
605 cinfo->global_state = DSTATE_INHEADER;
609 case DSTATE_INHEADER:
611 retcode = (*cinfo->inputctl->consume_input) (cinfo);
613 if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
615 /* Set up default parameters based on header data */
617 default_decompress_parms(cinfo);
619 /* Set global state: ready for start_decompress */
621 cinfo->global_state = DSTATE_READY;
629 /* Can't advance past first SOS until start_decompress is called */
631 retcode = JPEG_REACHED_SOS;
639 case DSTATE_SCANNING:
643 case DSTATE_BUFIMAGE:
647 case DSTATE_STOPPING:
649 retcode = (*cinfo->inputctl->consume_input) (cinfo);
655 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
669 * Have we finished reading the input file?
677 jpeg_input_complete (j_decompress_ptr cinfo)
681 /* Check for valid jpeg object */
683 if (cinfo->global_state < DSTATE_START ||
685 cinfo->global_state > DSTATE_STOPPING)
687 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
689 return cinfo->inputctl->eoi_reached;
699 * Is there more than one scan?
707 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
711 /* Only valid after jpeg_read_header completes */
713 if (cinfo->global_state < DSTATE_READY ||
715 cinfo->global_state > DSTATE_STOPPING)
717 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
719 return cinfo->inputctl->has_multiple_scans;
729 * Finish JPEG decompression.
733 * This will normally just verify the file trailer and release temp storage.
737 * Returns FALSE if suspended. The return value need be inspected only if
739 * a suspending data source is used.
747 jpeg_finish_decompress (j_decompress_ptr cinfo)
751 if ((cinfo->global_state == DSTATE_SCANNING ||
753 cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
755 /* Terminate final pass of non-buffered mode */
757 if (cinfo->output_scanline < cinfo->output_height)
759 ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
761 (*cinfo->master->finish_output_pass) (cinfo);
763 cinfo->global_state = DSTATE_STOPPING;
765 } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
767 /* Finishing after a buffered-image operation */
769 cinfo->global_state = DSTATE_STOPPING;
771 } else if (cinfo->global_state != DSTATE_STOPPING) {
773 /* STOPPING = repeat call after a suspension, anything else is error */
775 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
781 while (! cinfo->inputctl->eoi_reached) {
783 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
785 return FALSE; /* Suspend, come back later */
789 /* Do final cleanup */
791 (*cinfo->src->term_source) (cinfo);
793 /* We can use jpeg_abort to release memory and reset global_state */
795 jpeg_abort((j_common_ptr) cinfo);