Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 405 lines (358 sloc) 14.121 kb
ca48cf7 Initial commit based on 1.1.0 release from
Tom Gall authored
1 /*
2 * jdapistd.c
3 *
4 * Copyright (C) 1994-1996, Thomas G. Lane.
5 * Copyright (C) 2010, D. R. Commander.
6 * This file is part of the Independent JPEG Group's software.
7 * For conditions of distribution and use, see the accompanying README file.
8 *
9 * This file contains application interface code for the decompression half
10 * of the JPEG library. These are the "standard" API routines that are
11 * used in the normal full-decompression case. They are not used by a
12 * transcoding-only application. Note that if an application links in
13 * jpeg_start_decompress, it will end up linking in the entire decompressor.
14 * We thus must separate this file from jdapimin.c to avoid linking the
15 * whole decompression library into a transcoder.
16 */
17
18 #define JPEG_INTERNALS
19 #include "jinclude.h"
20 #include "jpeglib.h"
21 #include "jpegcomp.h"
22
23
24 /* Forward declarations */
25 LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
26
27
28 /*
29 * Decompression initialization.
30 * jpeg_read_header must be completed before calling this.
31 *
32 * If a multipass operating mode was selected, this will do all but the
33 * last pass, and thus may take a great deal of time.
34 *
35 * Returns FALSE if suspended. The return value need be inspected only if
36 * a suspending data source is used.
37 */
38
39 GLOBAL(boolean)
40 jpeg_start_decompress (j_decompress_ptr cinfo)
41 {
42 if (cinfo->global_state == DSTATE_READY) {
43 /* First call: initialize master control, select active modules */
44 jinit_master_decompress(cinfo);
45 if (cinfo->buffered_image) {
46 /* No more work here; expecting jpeg_start_output next */
47 cinfo->global_state = DSTATE_BUFIMAGE;
48 return TRUE;
49 }
50 cinfo->global_state = DSTATE_PRELOAD;
51 }
52 if (cinfo->global_state == DSTATE_PRELOAD) {
53 /* If file has multiple scans, absorb them all into the coef buffer */
54 if (cinfo->inputctl->has_multiple_scans) {
55 #ifdef D_MULTISCAN_FILES_SUPPORTED
56 for (;;) {
57 int retcode;
58 /* Call progress monitor hook if present */
59 if (cinfo->progress != NULL)
60 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
61 /* Absorb some more input */
62 retcode = (*cinfo->inputctl->consume_input) (cinfo);
63 if (retcode == JPEG_SUSPENDED)
64 return FALSE;
65 if (retcode == JPEG_REACHED_EOI)
66 break;
67 /* Advance progress counter if appropriate */
68 if (cinfo->progress != NULL &&
69 (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
70 if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
71 /* jdmaster underestimated number of scans; ratchet up one scan */
72 cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
73 }
74 }
75 }
76 #else
77 ERREXIT(cinfo, JERR_NOT_COMPILED);
78 #endif /* D_MULTISCAN_FILES_SUPPORTED */
79 }
80 cinfo->output_scan_number = cinfo->input_scan_number;
81 } else if (cinfo->global_state != DSTATE_PRESCAN)
82 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
83 /* Perform any dummy output passes, and set up for the final pass */
84 return output_pass_setup(cinfo);
85 }
86
87 /*
88 * Set up for an output pass, and perform any dummy pass(es) needed.
89 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
90 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
91 * Exit: If done, returns TRUE and sets global_state for proper output mode.
92 * If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
93 */
94
95 LOCAL(boolean)
96 output_pass_setup (j_decompress_ptr cinfo)
97 {
98 if (cinfo->global_state != DSTATE_PRESCAN) {
99 /* First call: do pass setup */
100 (*cinfo->master->prepare_for_output_pass) (cinfo);
101 cinfo->output_scanline = 0;
102 cinfo->global_state = DSTATE_PRESCAN;
103 }
104 /* Loop over any required dummy passes */
105 while (cinfo->master->is_dummy_pass) {
106 #ifdef QUANT_2PASS_SUPPORTED
107 /* Crank through the dummy pass */
108 while (cinfo->output_scanline < cinfo->output_height) {
109 JDIMENSION last_scanline;
110 /* Call progress monitor hook if present */
111 if (cinfo->progress != NULL) {
112 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
113 cinfo->progress->pass_limit = (long) cinfo->output_height;
114 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
115 }
116 /* Process some data */
117 last_scanline = cinfo->output_scanline;
118 (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
119 &cinfo->output_scanline, (JDIMENSION) 0);
120 if (cinfo->output_scanline == last_scanline)
121 return FALSE; /* No progress made, must suspend */
122 }
123 /* Finish up dummy pass, and set up for another one */
124 (*cinfo->master->finish_output_pass) (cinfo);
125 (*cinfo->master->prepare_for_output_pass) (cinfo);
126 cinfo->output_scanline = 0;
127 #else
128 ERREXIT(cinfo, JERR_NOT_COMPILED);
129 #endif /* QUANT_2PASS_SUPPORTED */
130 }
131 /* Ready for application to drive output pass through
132 * jpeg_read_scanlines or jpeg_read_raw_data.
133 */
134 cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
135 return TRUE;
136 }
137
138
139 /*
140 * Read some scanlines of data from the JPEG decompressor.
141 *
142 * The return value will be the number of lines actually read.
143 * This may be less than the number requested in several cases,
144 * including bottom of image, data source suspension, and operating
145 * modes that emit multiple scanlines at a time.
146 *
147 * Note: we warn about excess calls to jpeg_read_scanlines() since
148 * this likely signals an application programmer error. However,
149 * an oversize buffer (max_lines > scanlines remaining) is not an error.
150 */
151
152 GLOBAL(JDIMENSION)
153 jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
154 JDIMENSION max_lines)
155 {
156 JDIMENSION row_ctr;
157
158 if (cinfo->global_state != DSTATE_SCANNING)
159 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
160 if (cinfo->output_scanline >= cinfo->output_height) {
161 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
162 return 0;
163 }
164
165 /* Call progress monitor hook if present */
166 if (cinfo->progress != NULL) {
167 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
168 cinfo->progress->pass_limit = (long) cinfo->output_height;
169 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
170 }
171
172 /* Process some data */
173 row_ctr = 0;
174 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
175 cinfo->output_scanline += row_ctr;
176 return row_ctr;
177 }
178
179
180 /*
181 * Alternate entry point to read raw data.
182 * Processes exactly one iMCU row per call, unless suspended.
183 */
184
185 GLOBAL(JDIMENSION)
186 jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
187 JDIMENSION max_lines)
188 {
189 JDIMENSION lines_per_iMCU_row;
190
191 if (cinfo->global_state != DSTATE_RAW_OK)
192 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
193 if (cinfo->output_scanline >= cinfo->output_height) {
194 WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
195 return 0;
196 }
197
198 /* Call progress monitor hook if present */
199 if (cinfo->progress != NULL) {
200 cinfo->progress->pass_counter = (long) cinfo->output_scanline;
201 cinfo->progress->pass_limit = (long) cinfo->output_height;
202 (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
203 }
204
205 /* Verify that at least one iMCU row can be returned. */
206 lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->_min_DCT_scaled_size;
207 if (max_lines < lines_per_iMCU_row)
208 ERREXIT(cinfo, JERR_BUFFER_SIZE);
209
210 /* Decompress directly into user's buffer. */
211 if (! (*cinfo->coef->decompress_data) (cinfo, data))
212 return 0; /* suspension forced, can do nothing more */
213
214 /* OK, we processed one iMCU row. */
215 cinfo->output_scanline += lines_per_iMCU_row;
216 return lines_per_iMCU_row;
217 }
218
219
220 /* Additional entry points for buffered-image mode. */
221
222 #ifdef D_MULTISCAN_FILES_SUPPORTED
223
224 /*
225 * Initialize for an output pass in buffered-image mode.
226 */
227
228 GLOBAL(boolean)
229 jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
230 {
231 if (cinfo->global_state != DSTATE_BUFIMAGE &&
232 cinfo->global_state != DSTATE_PRESCAN)
233 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
234 /* Limit scan number to valid range */
235 if (scan_number <= 0)
236 scan_number = 1;
237 if (cinfo->inputctl->eoi_reached &&
238 scan_number > cinfo->input_scan_number)
239 scan_number = cinfo->input_scan_number;
240 cinfo->output_scan_number = scan_number;
241 /* Perform any dummy output passes, and set up for the real pass */
242 return output_pass_setup(cinfo);
243 }
244
245
246 /*
247 * Finish up after an output pass in buffered-image mode.
248 *
249 * Returns FALSE if suspended. The return value need be inspected only if
250 * a suspending data source is used.
251 */
252
253 GLOBAL(boolean)
254 jpeg_finish_output (j_decompress_ptr cinfo)
255 {
256 if ((cinfo->global_state == DSTATE_SCANNING ||
257 cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
258 /* Terminate this pass. */
259 /* We do not require the whole pass to have been completed. */
260 (*cinfo->master->finish_output_pass) (cinfo);
261 cinfo->global_state = DSTATE_BUFPOST;
262 } else if (cinfo->global_state != DSTATE_BUFPOST) {
263 /* BUFPOST = repeat call after a suspension, anything else is error */
264 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
265 }
266 /* Read markers looking for SOS or EOI */
267 while (cinfo->input_scan_number <= cinfo->output_scan_number &&
268 ! cinfo->inputctl->eoi_reached) {
269 if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
270 return FALSE; /* Suspend, come back later */
271 }
272 cinfo->global_state = DSTATE_BUFIMAGE;
273 return TRUE;
274 }
275
276 #endif /* D_MULTISCAN_FILES_SUPPORTED */
ff1f5e7 first cut at port of Android extentions
Tom Gall authored
277
278 #ifdef ANDROID
279
280 /*
281 * Tile decompression initialization.
282 * jpeg_read_header must be completed before calling this.
283 */
284
285 GLOBAL(boolean)
286 jpeg_start_tile_decompress (j_decompress_ptr cinfo)
287 {
288 if (cinfo->global_state == DSTATE_READY) {
289 /* First call: initialize master control, select active modules */
290 cinfo->tile_decode = TRUE;
291 jinit_master_decompress(cinfo);
292 if (cinfo->buffered_image) {
293 cinfo->global_state = DSTATE_BUFIMAGE;
294 return TRUE;
295 }
296 cinfo->global_state = DSTATE_PRELOAD;
297 }
298 if (cinfo->global_state == DSTATE_PRELOAD) {
299 cinfo->output_scan_number = cinfo->input_scan_number;
300 } else if (cinfo->global_state != DSTATE_PRESCAN)
301 ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
302 /* Perform any dummy output passes, and set up for the final pass */
303 return output_pass_setup(cinfo);
304 }
305
306 /*
307 * Initialize the jpeg decoder to decompressing a rectangle with size of (width, height)
308 * and its upper-left corner located at (start_x, start_y).
309 * Align start_x and start_y to multiplies of iMCU width and height, respectively.
310 * Also, the new reader position and sampled image size will be returned in
311 * (start_x, start_y) and (width, height), respectively.
312 + */
313
314 GLOBAL(void)
315 jpeg_init_read_tile_scanline(j_decompress_ptr cinfo, huffman_index *index,
316 int *start_x, int *start_y, int *width, int *height)
317 {
318 // Calculates the boundary of iMCU
319 int lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
320 int lines_per_iMCU_col = cinfo->max_h_samp_factor * DCTSIZE;
321 int row_offset = *start_y / lines_per_iMCU_row;
322 int col_left_boundary = ((*start_x / lines_per_iMCU_col)
323 / index->MCU_sample_size) * index->MCU_sample_size;
324 int col_right_boundary =
325 jdiv_round_up(*start_x + *width, lines_per_iMCU_col);
326
327 cinfo->coef->MCU_columns_to_skip =
328 *start_x / lines_per_iMCU_col - col_left_boundary;
329
330 *height = (*start_y - row_offset * lines_per_iMCU_row) + *height;
331 *start_x = col_left_boundary * lines_per_iMCU_col;
332 *start_y = row_offset * lines_per_iMCU_row;
333 cinfo->image_width = jmin(cinfo->original_image_width,
334 col_right_boundary * lines_per_iMCU_col) -
335 col_left_boundary * lines_per_iMCU_col;
336 cinfo->input_iMCU_row = row_offset;
337 cinfo->output_iMCU_row = row_offset;
338
339 // Updates JPEG decoder parameter
340 jinit_color_deconverter(cinfo);
341 jpeg_calc_output_dimensions(cinfo);
342 jinit_upsampler(cinfo);
343 (*cinfo->master->prepare_for_output_pass) (cinfo);
344 if (cinfo->progressive_mode)
345 (*cinfo->entropy->start_pass) (cinfo);
346 else
347 jpeg_decompress_per_scan_setup(cinfo);
348
349 int sample_size = DCTSIZE / cinfo->min_DCT_scaled_size;
350
351 *height = jdiv_round_up(*height, sample_size);
352 *width = cinfo->output_width;
353 cinfo->output_scanline = lines_per_iMCU_row * row_offset / sample_size;
354 cinfo->inputctl->consume_input = cinfo->coef->consume_data;
355 cinfo->inputctl->consume_input_build_huffman_index =
356 cinfo->coef->consume_data_build_huffman_index;
357 cinfo->entropy->index = index;
358 cinfo->input_iMCU_row = row_offset;
359 cinfo->output_iMCU_row = row_offset;
360 cinfo->coef->MCU_column_left_boundary = col_left_boundary;
361 cinfo->coef->MCU_column_right_boundary = col_right_boundary;
362 cinfo->coef->column_left_boundary =
363 col_left_boundary / index->MCU_sample_size;
364 cinfo->coef->column_right_boundary =
365 jdiv_round_up(col_right_boundary, index->MCU_sample_size);
366 }
367
368 /*
369 * Read a scanline from the current position.
370 *
371 * Return the number of lines actually read.
372 */
373
374 GLOBAL(JDIMENSION)
375 jpeg_read_tile_scanline (j_decompress_ptr cinfo, huffman_index *index,
376 JSAMPARRAY scanlines)
377 {
378 // Calculates the boundary of iMCU
379 int lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
380 int lines_per_iMCU_col = cinfo->max_h_samp_factor * DCTSIZE;
381 int sample_size = DCTSIZE / cinfo->min_DCT_scaled_size;
382 JDIMENSION row_ctr = 0;
383
384 if (cinfo->progressive_mode) {
385 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, 1);
386 } else {
387 if (cinfo->output_scanline % (lines_per_iMCU_row / sample_size) == 0) {
388 // Set the read head to the next iMCU row
389 int iMCU_row_offset = cinfo->output_scanline /
390 (lines_per_iMCU_row / sample_size);
391 int offset_data_col_position = cinfo->coef->MCU_column_left_boundary /
392 index->MCU_sample_size;
393 huffman_offset_data offset_data =
394 index->scan[0].offset[iMCU_row_offset][offset_data_col_position];
395 (*cinfo->entropy->configure_huffman_decoder) (cinfo, offset_data);
396 }
397 (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, 1);
398 }
399
400 cinfo->output_scanline += row_ctr;
401 return row_ctr;
402 }
403
404 #endif /* ANDROID */
Something went wrong with that request. Please try again.