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

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