4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
8 * This file contains application interface code for the decompression half
9 * of the JPEG library. These are the "standard" API routines that are
10 * used in the normal full-decompression case. They are not used by a
11 * transcoding-only application. Note that if an application links in
12 * jpeg_start_decompress, it will end up linking in the entire decompressor.
13 * We thus must separate this file from jdapimin.c to avoid linking the
14 * whole decompression library into a transcoder.
17 #define JPEG_INTERNALS
18 #include "jinclude12.h"
19 #include "jpeglib12.h"
21 /* Forward declarations */
22 LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
26 * Decompression initialization.
27 * jpeg_read_header must be completed before calling this.
29 * If a multipass operating mode was selected, this will do all but the
30 * last pass, and thus may take a great deal of time.
32 * Returns FALSE if suspended. The return value need be inspected only if
33 * a suspending data source is used.
37 jpeg_start_decompress (j_decompress_ptr cinfo)
39 if (cinfo->global_state == DSTATE_READY) {
40 /* First call: initialize master control, select active modules */
41 jinit_master_decompress(cinfo);
42 if (cinfo->buffered_image) {
43 /* No more work here; expecting jpeg_start_output next */
44 cinfo->global_state = DSTATE_BUFIMAGE;
47 cinfo->global_state = DSTATE_PRELOAD;
49 if (cinfo->global_state == DSTATE_PRELOAD) {
50 /* If file has multiple scans, absorb them all into the coef buffer */
51 if (cinfo->inputctl->has_multiple_scans) {
52 #ifdef D_MULTISCAN_FILES_SUPPORTED
55 /* Call progress monitor hook if present */
56 if (cinfo->progress != NULL)
57 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
58 /* Absorb some more input */
59 retcode = (*cinfo->inputctl->consume_input) (cinfo);
60 if (retcode == JPEG_SUSPENDED)
62 if (retcode == JPEG_REACHED_EOI)
64 /* Advance progress counter if appropriate */
65 if (cinfo->progress != NULL &&
66 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
67 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
68 /* jdmaster underestimated number of scans; ratchet up one scan */
69 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
74 ERREXIT(cinfo, JERR_NOT_COMPILED);
75 #endif /* D_MULTISCAN_FILES_SUPPORTED */
77 cinfo->output_scan_number = cinfo->input_scan_number;
78 } else if (cinfo->global_state != DSTATE_PRESCAN)
79 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
80 /* Perform any dummy output passes, and set up for the final pass */
81 return output_pass_setup(cinfo);
86 * Set up for an output pass, and perform any dummy pass(es) needed.
87 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
88 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
89 * Exit: If done, returns TRUE and sets global_state for proper output mode.
90 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
94 output_pass_setup (j_decompress_ptr cinfo)
96 if (cinfo->global_state != DSTATE_PRESCAN) {
97 /* First call: do pass setup */
98 (*cinfo->master->prepare_for_output_pass) (cinfo);
99 cinfo->output_scanline = 0;
100 cinfo->global_state = DSTATE_PRESCAN;
102 /* Loop over any required dummy passes */
103 while (cinfo->master->is_dummy_pass) {
104 #ifdef QUANT_2PASS_SUPPORTED
105 /* Crank through the dummy pass */
106 while (cinfo->output_scanline < cinfo->output_height) {
107 JDIMENSION last_scanline;
108 /* Call progress monitor hook if present */
109 if (cinfo->progress != NULL) {
110 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
111 cinfo->progress->pass_limit = (long) cinfo->output_height;
112 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
114 /* Process some data */
115 last_scanline = cinfo->output_scanline;
116 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
117 &cinfo->output_scanline, (JDIMENSION) 0);
118 if (cinfo->output_scanline == last_scanline)
119 return FALSE; /* No progress made, must suspend */
121 /* Finish up dummy pass, and set up for another one */
122 (*cinfo->master->finish_output_pass) (cinfo);
123 (*cinfo->master->prepare_for_output_pass) (cinfo);
124 cinfo->output_scanline = 0;
126 ERREXIT(cinfo, JERR_NOT_COMPILED);
127 #endif /* QUANT_2PASS_SUPPORTED */
129 /* Ready for application to drive output pass through
130 * jpeg_read_scanlines or jpeg_read_raw_data.
132 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
138 * Read some scanlines of data from the JPEG decompressor.
140 * The return value will be the number of lines actually read.
141 * This may be less than the number requested in several cases,
142 * including bottom of image, data source suspension, and operating
143 * modes that emit multiple scanlines at a time.
145 * Note: we warn about excess calls to jpeg_read_scanlines() since
146 * this likely signals an application programmer error. However,
147 * an oversize buffer (max_lines > scanlines remaining) is not an error.
151 //jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
152 // JDIMENSION max_lines)
155 jReadScanlines(j_decompress_ptr cinfo, JSAMPARRAY scanlines,
156 JDIMENSION max_lines)
160 if (cinfo->global_state != DSTATE_SCANNING)
161 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
162 if (cinfo->output_scanline >= cinfo->output_height) {
163 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
167 /* Call progress monitor hook if present */
168 if (cinfo->progress != NULL) {
169 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
170 cinfo->progress->pass_limit = (long) cinfo->output_height;
171 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
174 /* Process some data */
176 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
177 cinfo->output_scanline += row_ctr;
183 * Alternate entry point to read raw data.
184 * Processes exactly one iMCU row per call, unless suspended.
188 jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
189 JDIMENSION max_lines)
191 JDIMENSION lines_per_iMCU_row;
193 if (cinfo->global_state != DSTATE_RAW_OK)
194 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
195 if (cinfo->output_scanline >= cinfo->output_height) {
196 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
200 /* Call progress monitor hook if present */
201 if (cinfo->progress != NULL) {
202 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
203 cinfo->progress->pass_limit = (long) cinfo->output_height;
204 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
207 /* Verify that at least one iMCU row can be returned. */
208 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
209 if (max_lines < lines_per_iMCU_row)
210 ERREXIT(cinfo, JERR_BUFFER_SIZE);
212 /* Decompress directly into user's buffer. */
213 if (! (*cinfo->coef->decompress_data) (cinfo, data))
214 return 0; /* suspension forced, can do nothing more */
216 /* OK, we processed one iMCU row. */
217 cinfo->output_scanline += lines_per_iMCU_row;
218 return lines_per_iMCU_row;
222 /* Additional entry points for buffered-image mode. */
224 #ifdef D_MULTISCAN_FILES_SUPPORTED
227 * Initialize for an output pass in buffered-image mode.
231 jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
233 if (cinfo->global_state != DSTATE_BUFIMAGE &&
234 cinfo->global_state != DSTATE_PRESCAN)
235 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
236 /* Limit scan number to valid range */
237 if (scan_number <= 0)
239 if (cinfo->inputctl->eoi_reached &&
240 scan_number > cinfo->input_scan_number)
241 scan_number = cinfo->input_scan_number;
242 cinfo->output_scan_number = scan_number;
243 /* Perform any dummy output passes, and set up for the real pass */
244 return output_pass_setup(cinfo);
249 * Finish up after an output pass in buffered-image mode.
251 * Returns FALSE if suspended. The return value need be inspected only if
252 * a suspending data source is used.
256 jpeg_finish_output (j_decompress_ptr cinfo)
258 if ((cinfo->global_state == DSTATE_SCANNING ||
259 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
260 /* Terminate this pass. */
261 /* We do not require the whole pass to have been completed. */
262 (*cinfo->master->finish_output_pass) (cinfo);
263 cinfo->global_state = DSTATE_BUFPOST;
264 } else if (cinfo->global_state != DSTATE_BUFPOST) {
265 /* BUFPOST = repeat call after a suspension, anything else is error */
266 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
268 /* Read markers looking for SOS or EOI */
269 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
270 ! cinfo->inputctl->eoi_reached) {
271 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
272 return FALSE; /* Suspend, come back later */
274 cinfo->global_state = DSTATE_BUFIMAGE;
278 #endif /* D_MULTISCAN_FILES_SUPPORTED */