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 "standard" API routines that are
19 * used in the normal full-decompression case. They are not used by a
21 * transcoding-only application. Note that if an application links in
23 * jpeg_start_decompress, it will end up linking in the entire decompressor.
25 * We thus must separate this file from jdapimin.c to avoid linking the
27 * whole decompression library into a transcoder.
33 #define JPEG_INTERNALS
37 #include "radiant_jpeglib.h"
43 /* Forward declarations */
45 LOCAL boolean output_pass_setup JPP((j_decompress_ptr cinfo));
53 * Decompression initialization.
55 * jpeg_read_header must be completed before calling this.
59 * If a multipass operating mode was selected, this will do all but the
61 * last pass, and thus may take a great deal of time.
65 * Returns FALSE if suspended. The return value need be inspected only if
67 * a suspending data source is used.
75 jpeg_start_decompress (j_decompress_ptr cinfo)
79 if (cinfo->global_state == DSTATE_READY) {
81 /* First call: initialize master control, select active modules */
83 jinit_master_decompress(cinfo);
85 if (cinfo->buffered_image) {
87 /* No more work here; expecting jpeg_start_output next */
89 cinfo->global_state = DSTATE_BUFIMAGE;
95 cinfo->global_state = DSTATE_PRELOAD;
99 if (cinfo->global_state == DSTATE_PRELOAD) {
101 /* If file has multiple scans, absorb them all into the coef buffer */
103 if (cinfo->inputctl->has_multiple_scans) {
105 #ifdef D_MULTISCAN_FILES_SUPPORTED
111 /* Call progress monitor hook if present */
113 if (cinfo->progress != NULL)
115 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
117 /* Absorb some more input */
119 retcode = (*cinfo->inputctl->consume_input) (cinfo);
121 if (retcode == JPEG_SUSPENDED)
125 if (retcode == JPEG_REACHED_EOI)
129 /* Advance progress counter if appropriate */
131 if (cinfo->progress != NULL &&
133 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
135 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
137 /* jdmaster underestimated number of scans; ratchet up one scan */
139 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
149 ERREXIT(cinfo, JERR_NOT_COMPILED);
151 #endif /* D_MULTISCAN_FILES_SUPPORTED */
155 cinfo->output_scan_number = cinfo->input_scan_number;
157 } else if (cinfo->global_state != DSTATE_PRESCAN)
159 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
161 /* Perform any dummy output passes, and set up for the final pass */
163 return output_pass_setup(cinfo);
173 * Set up for an output pass, and perform any dummy pass(es) needed.
175 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
177 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
179 * Exit: If done, returns TRUE and sets global_state for proper output mode.
181 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
189 output_pass_setup (j_decompress_ptr cinfo)
193 if (cinfo->global_state != DSTATE_PRESCAN) {
195 /* First call: do pass setup */
197 (*cinfo->master->prepare_for_output_pass) (cinfo);
199 cinfo->output_scanline = 0;
201 cinfo->global_state = DSTATE_PRESCAN;
205 /* Loop over any required dummy passes */
207 while (cinfo->master->is_dummy_pass) {
209 #ifdef QUANT_2PASS_SUPPORTED
211 /* Crank through the dummy pass */
213 while (cinfo->output_scanline < cinfo->output_height) {
215 JDIMENSION last_scanline;
217 /* Call progress monitor hook if present */
219 if (cinfo->progress != NULL) {
221 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
223 cinfo->progress->pass_limit = (long) cinfo->output_height;
225 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
229 /* Process some data */
231 last_scanline = cinfo->output_scanline;
233 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
235 &cinfo->output_scanline, (JDIMENSION) 0);
237 if (cinfo->output_scanline == last_scanline)
239 return FALSE; /* No progress made, must suspend */
243 /* Finish up dummy pass, and set up for another one */
245 (*cinfo->master->finish_output_pass) (cinfo);
247 (*cinfo->master->prepare_for_output_pass) (cinfo);
249 cinfo->output_scanline = 0;
253 ERREXIT(cinfo, JERR_NOT_COMPILED);
255 #endif /* QUANT_2PASS_SUPPORTED */
259 /* Ready for application to drive output pass through
261 * jpeg_read_scanlines or jpeg_read_raw_data.
265 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
277 * Read some scanlines of data from the JPEG decompressor.
281 * The return value will be the number of lines actually read.
283 * This may be less than the number requested in several cases,
285 * including bottom of image, data source suspension, and operating
287 * modes that emit multiple scanlines at a time.
291 * Note: we warn about excess calls to jpeg_read_scanlines() since
293 * this likely signals an application programmer error. However,
295 * an oversize buffer (max_lines > scanlines remaining) is not an error.
303 jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
305 JDIMENSION max_lines)
313 if (cinfo->global_state != DSTATE_SCANNING)
315 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
317 if (cinfo->output_scanline >= cinfo->output_height) {
319 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
327 /* Call progress monitor hook if present */
329 if (cinfo->progress != NULL) {
331 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
333 cinfo->progress->pass_limit = (long) cinfo->output_height;
335 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
341 /* Process some data */
345 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
347 cinfo->output_scanline += row_ctr;
359 * Alternate entry point to read raw data.
361 * Processes exactly one iMCU row per call, unless suspended.
369 jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
371 JDIMENSION max_lines)
375 JDIMENSION lines_per_iMCU_row;
379 if (cinfo->global_state != DSTATE_RAW_OK)
381 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
383 if (cinfo->output_scanline >= cinfo->output_height) {
385 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
393 /* Call progress monitor hook if present */
395 if (cinfo->progress != NULL) {
397 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
399 cinfo->progress->pass_limit = (long) cinfo->output_height;
401 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
407 /* Verify that at least one iMCU row can be returned. */
409 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
411 if (max_lines < lines_per_iMCU_row)
413 ERREXIT(cinfo, JERR_BUFFER_SIZE);
417 /* Decompress directly into user's buffer. */
419 if (! (*cinfo->coef->decompress_data) (cinfo, data))
421 return 0; /* suspension forced, can do nothing more */
425 /* OK, we processed one iMCU row. */
427 cinfo->output_scanline += lines_per_iMCU_row;
429 return lines_per_iMCU_row;
437 /* Additional entry points for buffered-image mode. */
441 #ifdef D_MULTISCAN_FILES_SUPPORTED
447 * Initialize for an output pass in buffered-image mode.
455 jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
459 if (cinfo->global_state != DSTATE_BUFIMAGE &&
461 cinfo->global_state != DSTATE_PRESCAN)
463 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
465 /* Limit scan number to valid range */
467 if (scan_number <= 0)
471 if (cinfo->inputctl->eoi_reached &&
473 scan_number > cinfo->input_scan_number)
475 scan_number = cinfo->input_scan_number;
477 cinfo->output_scan_number = scan_number;
479 /* Perform any dummy output passes, and set up for the real pass */
481 return output_pass_setup(cinfo);
491 * Finish up after an output pass in buffered-image mode.
495 * Returns FALSE if suspended. The return value need be inspected only if
497 * a suspending data source is used.
505 jpeg_finish_output (j_decompress_ptr cinfo)
509 if ((cinfo->global_state == DSTATE_SCANNING ||
511 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
513 /* Terminate this pass. */
515 /* We do not require the whole pass to have been completed. */
517 (*cinfo->master->finish_output_pass) (cinfo);
519 cinfo->global_state = DSTATE_BUFPOST;
521 } else if (cinfo->global_state != DSTATE_BUFPOST) {
523 /* BUFPOST = repeat call after a suspension, anything else is error */
525 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
529 /* Read markers looking for SOS or EOI */
531 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
533 ! cinfo->inputctl->eoi_reached) {
535 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
537 return FALSE; /* Suspend, come back later */
541 cinfo->global_state = DSTATE_BUFIMAGE;
549 #endif /* D_MULTISCAN_FILES_SUPPORTED */