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