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