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