/src/freeimage-svn/FreeImage/trunk/Source/LibJPEG/jdapistd.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |  * jdapistd.c  | 
3  |  |  *  | 
4  |  |  * Copyright (C) 1994-1996, Thomas G. Lane.  | 
5  |  |  * Modified 2002-2013 by Guido Vollbeding.  | 
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  |  |  | 
22  |  |  | 
23  |  | /* Forward declarations */  | 
24  |  | LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));  | 
25  |  |  | 
26  |  |  | 
27  |  | /*  | 
28  |  |  * Decompression initialization.  | 
29  |  |  * jpeg_read_header must be completed before calling this.  | 
30  |  |  *  | 
31  |  |  * If a multipass operating mode was selected, this will do all but the  | 
32  |  |  * last pass, and thus may take a great deal of time.  | 
33  |  |  *  | 
34  |  |  * Returns FALSE if suspended.  The return value need be inspected only if  | 
35  |  |  * a suspending data source is used.  | 
36  |  |  */  | 
37  |  |  | 
38  |  | GLOBAL(boolean)  | 
39  |  | jpeg_start_decompress (j_decompress_ptr cinfo)  | 
40  | 0  | { | 
41  | 0  |   if (cinfo->global_state == DSTATE_READY) { | 
42  |  |     /* First call: initialize master control, select active modules */  | 
43  | 0  |     jinit_master_decompress(cinfo);  | 
44  | 0  |     if (cinfo->buffered_image) { | 
45  |  |       /* No more work here; expecting jpeg_start_output next */  | 
46  | 0  |       cinfo->global_state = DSTATE_BUFIMAGE;  | 
47  | 0  |       return TRUE;  | 
48  | 0  |     }  | 
49  | 0  |     cinfo->global_state = DSTATE_PRELOAD;  | 
50  | 0  |   }  | 
51  | 0  |   if (cinfo->global_state == DSTATE_PRELOAD) { | 
52  |  |     /* If file has multiple scans, absorb them all into the coef buffer */  | 
53  | 0  |     if (cinfo->inputctl->has_multiple_scans) { | 
54  | 0  | #ifdef D_MULTISCAN_FILES_SUPPORTED  | 
55  | 0  |       for (;;) { | 
56  | 0  |   int retcode;  | 
57  |  |   /* Call progress monitor hook if present */  | 
58  | 0  |   if (cinfo->progress != NULL)  | 
59  | 0  |     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);  | 
60  |  |   /* Absorb some more input */  | 
61  | 0  |   retcode = (*cinfo->inputctl->consume_input) (cinfo);  | 
62  | 0  |   if (retcode == JPEG_SUSPENDED)  | 
63  | 0  |     return FALSE;  | 
64  | 0  |   if (retcode == JPEG_REACHED_EOI)  | 
65  | 0  |     break;  | 
66  |  |   /* Advance progress counter if appropriate */  | 
67  | 0  |   if (cinfo->progress != NULL &&  | 
68  | 0  |       (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) { | 
69  | 0  |     if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) { | 
70  |  |       /* jdmaster underestimated number of scans; ratchet up one scan */  | 
71  | 0  |       cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;  | 
72  | 0  |     }  | 
73  | 0  |   }  | 
74  | 0  |       }  | 
75  |  | #else  | 
76  |  |       ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
77  |  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  | 
78  | 0  |     }  | 
79  | 0  |     cinfo->output_scan_number = cinfo->input_scan_number;  | 
80  | 0  |   } else if (cinfo->global_state != DSTATE_PRESCAN)  | 
81  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
82  |  |   /* Perform any dummy output passes, and set up for the final pass */  | 
83  | 0  |   return output_pass_setup(cinfo);  | 
84  | 0  | }  | 
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  | 0  | { | 
98  | 0  |   if (cinfo->global_state != DSTATE_PRESCAN) { | 
99  |  |     /* First call: do pass setup */  | 
100  | 0  |     (*cinfo->master->prepare_for_output_pass) (cinfo);  | 
101  | 0  |     cinfo->output_scanline = 0;  | 
102  | 0  |     cinfo->global_state = DSTATE_PRESCAN;  | 
103  | 0  |   }  | 
104  |  |   /* Loop over any required dummy passes */  | 
105  | 0  |   while (cinfo->master->is_dummy_pass) { | 
106  | 0  | #ifdef QUANT_2PASS_SUPPORTED  | 
107  |  |     /* Crank through the dummy pass */  | 
108  | 0  |     while (cinfo->output_scanline < cinfo->output_height) { | 
109  | 0  |       JDIMENSION last_scanline;  | 
110  |  |       /* Call progress monitor hook if present */  | 
111  | 0  |       if (cinfo->progress != NULL) { | 
112  | 0  |   cinfo->progress->pass_counter = (long) cinfo->output_scanline;  | 
113  | 0  |   cinfo->progress->pass_limit = (long) cinfo->output_height;  | 
114  | 0  |   (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);  | 
115  | 0  |       }  | 
116  |  |       /* Process some data */  | 
117  | 0  |       last_scanline = cinfo->output_scanline;  | 
118  | 0  |       (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,  | 
119  | 0  |             &cinfo->output_scanline, (JDIMENSION) 0);  | 
120  | 0  |       if (cinfo->output_scanline == last_scanline)  | 
121  | 0  |   return FALSE;   /* No progress made, must suspend */  | 
122  | 0  |     }  | 
123  |  |     /* Finish up dummy pass, and set up for another one */  | 
124  | 0  |     (*cinfo->master->finish_output_pass) (cinfo);  | 
125  | 0  |     (*cinfo->master->prepare_for_output_pass) (cinfo);  | 
126  | 0  |     cinfo->output_scanline = 0;  | 
127  |  | #else  | 
128  |  |     ERREXIT(cinfo, JERR_NOT_COMPILED);  | 
129  |  | #endif /* QUANT_2PASS_SUPPORTED */  | 
130  | 0  |   }  | 
131  |  |   /* Ready for application to drive output pass through  | 
132  |  |    * jpeg_read_scanlines or jpeg_read_raw_data.  | 
133  |  |    */  | 
134  | 0  |   cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;  | 
135  | 0  |   return TRUE;  | 
136  | 0  | }  | 
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  | 0  | { | 
156  | 0  |   JDIMENSION row_ctr;  | 
157  |  | 
  | 
158  | 0  |   if (cinfo->global_state != DSTATE_SCANNING)  | 
159  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
160  | 0  |   if (cinfo->output_scanline >= cinfo->output_height) { | 
161  | 0  |     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);  | 
162  | 0  |     return 0;  | 
163  | 0  |   }  | 
164  |  |  | 
165  |  |   /* Call progress monitor hook if present */  | 
166  | 0  |   if (cinfo->progress != NULL) { | 
167  | 0  |     cinfo->progress->pass_counter = (long) cinfo->output_scanline;  | 
168  | 0  |     cinfo->progress->pass_limit = (long) cinfo->output_height;  | 
169  | 0  |     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);  | 
170  | 0  |   }  | 
171  |  |  | 
172  |  |   /* Process some data */  | 
173  | 0  |   row_ctr = 0;  | 
174  | 0  |   (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);  | 
175  | 0  |   cinfo->output_scanline += row_ctr;  | 
176  | 0  |   return row_ctr;  | 
177  | 0  | }  | 
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  | 0  | { | 
189  | 0  |   JDIMENSION lines_per_iMCU_row;  | 
190  |  | 
  | 
191  | 0  |   if (cinfo->global_state != DSTATE_RAW_OK)  | 
192  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
193  | 0  |   if (cinfo->output_scanline >= cinfo->output_height) { | 
194  | 0  |     WARNMS(cinfo, JWRN_TOO_MUCH_DATA);  | 
195  | 0  |     return 0;  | 
196  | 0  |   }  | 
197  |  |  | 
198  |  |   /* Call progress monitor hook if present */  | 
199  | 0  |   if (cinfo->progress != NULL) { | 
200  | 0  |     cinfo->progress->pass_counter = (long) cinfo->output_scanline;  | 
201  | 0  |     cinfo->progress->pass_limit = (long) cinfo->output_height;  | 
202  | 0  |     (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);  | 
203  | 0  |   }  | 
204  |  |  | 
205  |  |   /* Verify that at least one iMCU row can be returned. */  | 
206  | 0  |   lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_v_scaled_size;  | 
207  | 0  |   if (max_lines < lines_per_iMCU_row)  | 
208  | 0  |     ERREXIT(cinfo, JERR_BUFFER_SIZE);  | 
209  |  |  | 
210  |  |   /* Decompress directly into user's buffer. */  | 
211  | 0  |   if (! (*cinfo->coef->decompress_data) (cinfo, data))  | 
212  | 0  |     return 0;     /* suspension forced, can do nothing more */  | 
213  |  |  | 
214  |  |   /* OK, we processed one iMCU row. */  | 
215  | 0  |   cinfo->output_scanline += lines_per_iMCU_row;  | 
216  | 0  |   return lines_per_iMCU_row;  | 
217  | 0  | }  | 
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  | 0  | { | 
231  | 0  |   if (cinfo->global_state != DSTATE_BUFIMAGE &&  | 
232  | 0  |       cinfo->global_state != DSTATE_PRESCAN)  | 
233  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
234  |  |   /* Limit scan number to valid range */  | 
235  | 0  |   if (scan_number <= 0)  | 
236  | 0  |     scan_number = 1;  | 
237  | 0  |   if (cinfo->inputctl->eoi_reached &&  | 
238  | 0  |       scan_number > cinfo->input_scan_number)  | 
239  | 0  |     scan_number = cinfo->input_scan_number;  | 
240  | 0  |   cinfo->output_scan_number = scan_number;  | 
241  |  |   /* Perform any dummy output passes, and set up for the real pass */  | 
242  | 0  |   return output_pass_setup(cinfo);  | 
243  | 0  | }  | 
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  | 0  | { | 
256  | 0  |   if ((cinfo->global_state == DSTATE_SCANNING ||  | 
257  | 0  |        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  | 0  |     (*cinfo->master->finish_output_pass) (cinfo);  | 
261  | 0  |     cinfo->global_state = DSTATE_BUFPOST;  | 
262  | 0  |   } else if (cinfo->global_state != DSTATE_BUFPOST) { | 
263  |  |     /* BUFPOST = repeat call after a suspension, anything else is error */  | 
264  | 0  |     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);  | 
265  | 0  |   }  | 
266  |  |   /* Read markers looking for SOS or EOI */  | 
267  | 0  |   while (cinfo->input_scan_number <= cinfo->output_scan_number &&  | 
268  | 0  |    ! cinfo->inputctl->eoi_reached) { | 
269  | 0  |     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)  | 
270  | 0  |       return FALSE;   /* Suspend, come back later */  | 
271  | 0  |   }  | 
272  | 0  |   cinfo->global_state = DSTATE_BUFIMAGE;  | 
273  | 0  |   return TRUE;  | 
274  | 0  | }  | 
275  |  |  | 
276  |  | #endif /* D_MULTISCAN_FILES_SUPPORTED */  |