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