Remove 'oldcode'
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jdapimin.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: jdapimin.c /main/2 1996/05/09 03:46:12 drk $ */
24 /*
25  * jdapimin.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 "minimum" API routines that may be
33  * needed in either the normal full-decompression case or the
34  * transcoding-only case.
35  *
36  * Most of the routines intended to be called directly by an application
37  * are in this file or in jdapistd.c.  But also see jcomapi.c for routines
38  * shared by compression and decompression, and jdtrans.c for the transcoding
39  * case.
40  */
41
42 #define JPEG_INTERNALS
43 #include "jinclude.h"
44 #include "jpeglib.h"
45
46
47 /*
48  * Initialization of a JPEG decompression object.
49  * The error manager must already be set up (in case memory manager fails).
50  */
51
52 GLOBAL(void)
53 jpeg_CreateDecompress (j_decompress_ptr cinfo, int version, size_t structsize)
54 {
55   int i;
56
57   /* Guard against version mismatches between library and caller. */
58   cinfo->mem = NULL;            /* so jpeg_destroy knows mem mgr not called */
59   if (version != JPEG_LIB_VERSION)
60     ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
61   if (structsize != SIZEOF(struct jpeg_decompress_struct))
62     ERREXIT2(cinfo, JERR_BAD_STRUCT_SIZE, 
63              (int) SIZEOF(struct jpeg_decompress_struct), (int) structsize);
64
65   /* For debugging purposes, zero the whole master structure.
66    * But error manager pointer is already there, so save and restore it.
67    */
68   {
69     struct jpeg_error_mgr * err = cinfo->err;
70     MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
71     cinfo->err = err;
72   }
73   cinfo->is_decompressor = TRUE;
74
75   /* Initialize a memory manager instance for this object */
76   jinit_memory_mgr((j_common_ptr) cinfo);
77
78   /* Zero out pointers to permanent structures. */
79   cinfo->progress = NULL;
80   cinfo->src = NULL;
81
82   for (i = 0; i < NUM_QUANT_TBLS; i++)
83     cinfo->quant_tbl_ptrs[i] = NULL;
84
85   for (i = 0; i < NUM_HUFF_TBLS; i++) {
86     cinfo->dc_huff_tbl_ptrs[i] = NULL;
87     cinfo->ac_huff_tbl_ptrs[i] = NULL;
88   }
89
90   /* Initialize marker processor so application can override methods
91    * for COM, APPn markers before calling jpeg_read_header.
92    */
93   jinit_marker_reader(cinfo);
94
95   /* And initialize the overall input controller. */
96   jinit_input_controller(cinfo);
97
98   /* OK, I'm ready */
99   cinfo->global_state = DSTATE_START;
100 }
101
102
103 /*
104  * Destruction of a JPEG decompression object
105  */
106
107 GLOBAL(void)
108 jpeg_destroy_decompress (j_decompress_ptr cinfo)
109 {
110   jpeg_destroy((j_common_ptr) cinfo); /* use common routine */
111 }
112
113
114 /*
115  * Abort processing of a JPEG decompression operation,
116  * but don't destroy the object itself.
117  */
118
119 GLOBAL(void)
120 jpeg_abort_decompress (j_decompress_ptr cinfo)
121 {
122   jpeg_abort((j_common_ptr) cinfo); /* use common routine */
123 }
124
125
126 /*
127  * Install a special processing method for COM or APPn markers.
128  */
129
130 GLOBAL(void)
131 jpeg_set_marker_processor (j_decompress_ptr cinfo, int marker_code,
132                            jpeg_marker_parser_method routine)
133 {
134   if (marker_code == JPEG_COM)
135     cinfo->marker->process_COM = routine;
136   else if (marker_code >= JPEG_APP0 && marker_code <= JPEG_APP0+15)
137     cinfo->marker->process_APPn[marker_code-JPEG_APP0] = routine;
138   else
139     ERREXIT1(cinfo, JERR_UNKNOWN_MARKER, marker_code);
140 }
141
142
143 /*
144  * Set default decompression parameters.
145  */
146
147 LOCAL(void)
148 default_decompress_parms (j_decompress_ptr cinfo)
149 {
150   /* Guess the input colorspace, and set output colorspace accordingly. */
151   /* (Wish JPEG committee had provided a real way to specify this...) */
152   /* Note application may override our guesses. */
153   switch (cinfo->num_components) {
154   case 1:
155     cinfo->jpeg_color_space = JCS_GRAYSCALE;
156     cinfo->out_color_space = JCS_GRAYSCALE;
157     break;
158     
159   case 3:
160     if (cinfo->saw_JFIF_marker) {
161       cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
162     } else if (cinfo->saw_Adobe_marker) {
163       switch (cinfo->Adobe_transform) {
164       case 0:
165         cinfo->jpeg_color_space = JCS_RGB;
166         break;
167       case 1:
168         cinfo->jpeg_color_space = JCS_YCbCr;
169         break;
170       default:
171         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
172         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
173         break;
174       }
175     } else {
176       /* Saw no special markers, try to guess from the component IDs */
177       int cid0 = cinfo->comp_info[0].component_id;
178       int cid1 = cinfo->comp_info[1].component_id;
179       int cid2 = cinfo->comp_info[2].component_id;
180
181       if (cid0 == 1 && cid1 == 2 && cid2 == 3)
182         cinfo->jpeg_color_space = JCS_YCbCr; /* assume JFIF w/out marker */
183       else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
184         cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
185       else {
186         TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
187         cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
188       }
189     }
190     /* Always guess RGB is proper output colorspace. */
191     cinfo->out_color_space = JCS_RGB;
192     break;
193     
194   case 4:
195     if (cinfo->saw_Adobe_marker) {
196       switch (cinfo->Adobe_transform) {
197       case 0:
198         cinfo->jpeg_color_space = JCS_CMYK;
199         break;
200       case 2:
201         cinfo->jpeg_color_space = JCS_YCCK;
202         break;
203       default:
204         WARNMS1(cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
205         cinfo->jpeg_color_space = JCS_YCCK; /* assume it's YCCK */
206         break;
207       }
208     } else {
209       /* No special markers, assume straight CMYK. */
210       cinfo->jpeg_color_space = JCS_CMYK;
211     }
212     cinfo->out_color_space = JCS_CMYK;
213     break;
214     
215   default:
216     cinfo->jpeg_color_space = JCS_UNKNOWN;
217     cinfo->out_color_space = JCS_UNKNOWN;
218     break;
219   }
220
221   /* Set defaults for other decompression parameters. */
222   cinfo->scale_num = 1;         /* 1:1 scaling */
223   cinfo->scale_denom = 1;
224   cinfo->output_gamma = 1.0;
225   cinfo->buffered_image = FALSE;
226   cinfo->raw_data_out = FALSE;
227   cinfo->dct_method = JDCT_DEFAULT;
228   cinfo->do_fancy_upsampling = TRUE;
229   cinfo->do_block_smoothing = TRUE;
230   cinfo->quantize_colors = FALSE;
231   /* We set these in case application only sets quantize_colors. */
232   cinfo->dither_mode = JDITHER_FS;
233 #ifdef QUANT_2PASS_SUPPORTED
234   cinfo->two_pass_quantize = TRUE;
235 #else
236   cinfo->two_pass_quantize = FALSE;
237 #endif
238   cinfo->desired_number_of_colors = 256;
239   cinfo->colormap = NULL;
240   /* Initialize for no mode change in buffered-image mode. */
241   cinfo->enable_1pass_quant = FALSE;
242   cinfo->enable_external_quant = FALSE;
243   cinfo->enable_2pass_quant = FALSE;
244 }
245
246
247 /*
248  * Decompression startup: read start of JPEG datastream to see what's there.
249  * Need only initialize JPEG object and supply a data source before calling.
250  *
251  * This routine will read as far as the first SOS marker (ie, actual start of
252  * compressed data), and will save all tables and parameters in the JPEG
253  * object.  It will also initialize the decompression parameters to default
254  * values, and finally return JPEG_HEADER_OK.  On return, the application may
255  * adjust the decompression parameters and then call jpeg_start_decompress.
256  * (Or, if the application only wanted to determine the image parameters,
257  * the data need not be decompressed.  In that case, call jpeg_abort or
258  * jpeg_destroy to release any temporary space.)
259  * If an abbreviated (tables only) datastream is presented, the routine will
260  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI.  The application may then
261  * re-use the JPEG object to read the abbreviated image datastream(s).
262  * It is unnecessary (but OK) to call jpeg_abort in this case.
263  * The JPEG_SUSPENDED return code only occurs if the data source module
264  * requests suspension of the decompressor.  In this case the application
265  * should load more source data and then re-call jpeg_read_header to resume
266  * processing.
267  * If a non-suspending data source is used and require_image is TRUE, then the
268  * return code need not be inspected since only JPEG_HEADER_OK is possible.
269  *
270  * This routine is now just a front end to jpeg_consume_input, with some
271  * extra error checking.
272  */
273
274 GLOBAL(int)
275 jpeg_read_header (j_decompress_ptr cinfo, boolean require_image)
276 {
277   int retcode;
278
279   if (cinfo->global_state != DSTATE_START &&
280       cinfo->global_state != DSTATE_INHEADER)
281     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
282
283   retcode = jpeg_consume_input(cinfo);
284
285   switch (retcode) {
286   case JPEG_REACHED_SOS:
287     retcode = JPEG_HEADER_OK;
288     break;
289   case JPEG_REACHED_EOI:
290     if (require_image)          /* Complain if application wanted an image */
291       ERREXIT(cinfo, JERR_NO_IMAGE);
292     /* Reset to start state; it would be safer to require the application to
293      * call jpeg_abort, but we can't change it now for compatibility reasons.
294      * A side effect is to free any temporary memory (there shouldn't be any).
295      */
296     jpeg_abort((j_common_ptr) cinfo); /* sets state = DSTATE_START */
297     retcode = JPEG_HEADER_TABLES_ONLY;
298     break;
299   case JPEG_SUSPENDED:
300     /* no work */
301     break;
302   }
303
304   return retcode;
305 }
306
307
308 /*
309  * Consume data in advance of what the decompressor requires.
310  * This can be called at any time once the decompressor object has
311  * been created and a data source has been set up.
312  *
313  * This routine is essentially a state machine that handles a couple
314  * of critical state-transition actions, namely initial setup and
315  * transition from header scanning to ready-for-start_decompress.
316  * All the actual input is done via the input controller's consume_input
317  * method.
318  */
319
320 GLOBAL(int)
321 jpeg_consume_input (j_decompress_ptr cinfo)
322 {
323   int retcode = JPEG_SUSPENDED;
324
325   /* NB: every possible DSTATE value should be listed in this switch */
326   switch (cinfo->global_state) {
327   case DSTATE_START:
328     /* Start-of-datastream actions: reset appropriate modules */
329     (*cinfo->inputctl->reset_input_controller) (cinfo);
330     /* Initialize application's data source module */
331     (*cinfo->src->init_source) (cinfo);
332     cinfo->global_state = DSTATE_INHEADER;
333     /*FALLTHROUGH*/
334   case DSTATE_INHEADER:
335     retcode = (*cinfo->inputctl->consume_input) (cinfo);
336     if (retcode == JPEG_REACHED_SOS) { /* Found SOS, prepare to decompress */
337       /* Set up default parameters based on header data */
338       default_decompress_parms(cinfo);
339       /* Set global state: ready for start_decompress */
340       cinfo->global_state = DSTATE_READY;
341     }
342     break;
343   case DSTATE_READY:
344     /* Can't advance past first SOS until start_decompress is called */
345     retcode = JPEG_REACHED_SOS;
346     break;
347   case DSTATE_PRELOAD:
348   case DSTATE_PRESCAN:
349   case DSTATE_SCANNING:
350   case DSTATE_RAW_OK:
351   case DSTATE_BUFIMAGE:
352   case DSTATE_BUFPOST:
353   case DSTATE_STOPPING:
354     retcode = (*cinfo->inputctl->consume_input) (cinfo);
355     break;
356   default:
357     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
358   }
359   return retcode;
360 }
361
362
363 /*
364  * Have we finished reading the input file?
365  */
366
367 GLOBAL(boolean)
368 jpeg_input_complete (j_decompress_ptr cinfo)
369 {
370   /* Check for valid jpeg object */
371   if (cinfo->global_state < DSTATE_START ||
372       cinfo->global_state > DSTATE_STOPPING)
373     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
374   return cinfo->inputctl->eoi_reached;
375 }
376
377
378 /*
379  * Is there more than one scan?
380  */
381
382 GLOBAL(boolean)
383 jpeg_has_multiple_scans (j_decompress_ptr cinfo)
384 {
385   /* Only valid after jpeg_read_header completes */
386   if (cinfo->global_state < DSTATE_READY ||
387       cinfo->global_state > DSTATE_STOPPING)
388     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
389   return cinfo->inputctl->has_multiple_scans;
390 }
391
392
393 /*
394  * Finish JPEG decompression.
395  *
396  * This will normally just verify the file trailer and release temp storage.
397  *
398  * Returns FALSE if suspended.  The return value need be inspected only if
399  * a suspending data source is used.
400  */
401
402 GLOBAL(boolean)
403 jpeg_finish_decompress (j_decompress_ptr cinfo)
404 {
405   if ((cinfo->global_state == DSTATE_SCANNING ||
406        cinfo->global_state == DSTATE_RAW_OK) && ! cinfo->buffered_image) {
407     /* Terminate final pass of non-buffered mode */
408     if (cinfo->output_scanline < cinfo->output_height)
409       ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
410     (*cinfo->master->finish_output_pass) (cinfo);
411     cinfo->global_state = DSTATE_STOPPING;
412   } else if (cinfo->global_state == DSTATE_BUFIMAGE) {
413     /* Finishing after a buffered-image operation */
414     cinfo->global_state = DSTATE_STOPPING;
415   } else if (cinfo->global_state != DSTATE_STOPPING) {
416     /* STOPPING = repeat call after a suspension, anything else is error */
417     ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
418   }
419   /* Read until EOI */
420   while (! cinfo->inputctl->eoi_reached) {
421     if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
422       return FALSE;             /* Suspend, come back later */
423   }
424   /* Do final cleanup */
425   (*cinfo->src->term_source) (cinfo);
426   /* We can use jpeg_abort to release memory and reset global_state */
427   jpeg_abort((j_common_ptr) cinfo);
428   return TRUE;
429 }