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