source: golgotha/src/i4/loaders/jpg/jdapimin.cc @ 80

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