jdapistd.c revision 3147fbe7688fc353e6ae03825a37cf101a4ee01d
1/*
2 * jdapistd.c
3 *
4 * Copyright (C) 1994-1996, 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 "standard" API routines that are
10 * used in the normal full-decompression case.  They are not used by a
11 * transcoding-only application.  Note that if an application links in
12 * jpeg_start_decompress, it will end up linking in the entire decompressor.
13 * We thus must separate this file from jdapimin.c to avoid linking the
14 * whole decompression library into a transcoder.
15 */
16
17#define JPEG_INTERNALS
18#include "jinclude.h"
19#include "jpeglib.h"
20
21
22/* Forward declarations */
23LOCAL(boolean) output_pass_setup JPP((j_decompress_ptr cinfo));
24
25
26/*
27 * Decompression initialization.
28 * jpeg_read_header must be completed before calling this.
29 *
30 * If a multipass operating mode was selected, this will do all but the
31 * last pass, and thus may take a great deal of time.
32 *
33 * Returns FALSE if suspended.  The return value need be inspected only if
34 * a suspending data source is used.
35 */
36
37GLOBAL(boolean)
38jpeg_start_decompress (j_decompress_ptr cinfo)
39{
40  if (cinfo->global_state == DSTATE_READY) {
41    /* First call: initialize master control, select active modules */
42    jinit_master_decompress(cinfo);
43    if (cinfo->buffered_image) {
44      /* No more work here; expecting jpeg_start_output next */
45      cinfo->global_state = DSTATE_BUFIMAGE;
46      return TRUE;
47    }
48    cinfo->global_state = DSTATE_PRELOAD;
49  }
50  if (cinfo->global_state == DSTATE_PRELOAD) {
51    /* If file has multiple scans, absorb them all into the coef buffer */
52    if (cinfo->inputctl->has_multiple_scans) {
53#ifdef D_MULTISCAN_FILES_SUPPORTED
54      for (;;) {
55	int retcode;
56	/* Call progress monitor hook if present */
57	if (cinfo->progress != NULL)
58	  (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
59	/* Absorb some more input */
60	retcode = (*cinfo->inputctl->consume_input) (cinfo);
61	if (retcode == JPEG_SUSPENDED)
62	  return FALSE;
63	if (retcode == JPEG_REACHED_EOI)
64	  break;
65	/* Advance progress counter if appropriate */
66	if (cinfo->progress != NULL &&
67	    (retcode == JPEG_ROW_COMPLETED || retcode == JPEG_REACHED_SOS)) {
68	  if (++cinfo->progress->pass_counter >= cinfo->progress->pass_limit) {
69	    /* jdmaster underestimated number of scans; ratchet up one scan */
70	    cinfo->progress->pass_limit += (long) cinfo->total_iMCU_rows;
71	  }
72	}
73      }
74#else
75      ERREXIT(cinfo, JERR_NOT_COMPILED);
76#endif /* D_MULTISCAN_FILES_SUPPORTED */
77    }
78    cinfo->output_scan_number = cinfo->input_scan_number;
79  } else if (cinfo->global_state != DSTATE_PRESCAN)
80    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
81  /* Perform any dummy output passes, and set up for the final pass */
82  return output_pass_setup(cinfo);
83}
84
85
86/*
87 * Set up for an output pass, and perform any dummy pass(es) needed.
88 * Common subroutine for jpeg_start_decompress and jpeg_start_output.
89 * Entry: global_state = DSTATE_PRESCAN only if previously suspended.
90 * Exit: If done, returns TRUE and sets global_state for proper output mode.
91 *       If suspended, returns FALSE and sets global_state = DSTATE_PRESCAN.
92 */
93
94LOCAL(boolean)
95output_pass_setup (j_decompress_ptr cinfo)
96{
97  if (cinfo->global_state != DSTATE_PRESCAN) {
98    /* First call: do pass setup */
99    (*cinfo->master->prepare_for_output_pass) (cinfo);
100    cinfo->output_scanline = 0;
101    cinfo->global_state = DSTATE_PRESCAN;
102  }
103  /* Loop over any required dummy passes */
104  while (cinfo->master->is_dummy_pass) {
105#ifdef QUANT_2PASS_SUPPORTED
106    /* Crank through the dummy pass */
107    while (cinfo->output_scanline < cinfo->output_height) {
108      JDIMENSION last_scanline;
109      /* Call progress monitor hook if present */
110      if (cinfo->progress != NULL) {
111	cinfo->progress->pass_counter = (long) cinfo->output_scanline;
112	cinfo->progress->pass_limit = (long) cinfo->output_height;
113	(*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
114      }
115      /* Process some data */
116      last_scanline = cinfo->output_scanline;
117      (*cinfo->main->process_data) (cinfo, (JSAMPARRAY) NULL,
118				    &cinfo->output_scanline, (JDIMENSION) 0);
119      if (cinfo->output_scanline == last_scanline)
120	return FALSE;		/* No progress made, must suspend */
121    }
122    /* Finish up dummy pass, and set up for another one */
123    (*cinfo->master->finish_output_pass) (cinfo);
124    (*cinfo->master->prepare_for_output_pass) (cinfo);
125    cinfo->output_scanline = 0;
126#else
127    ERREXIT(cinfo, JERR_NOT_COMPILED);
128#endif /* QUANT_2PASS_SUPPORTED */
129  }
130  /* Ready for application to drive output pass through
131   * jpeg_read_scanlines or jpeg_read_raw_data.
132   */
133  cinfo->global_state = cinfo->raw_data_out ? DSTATE_RAW_OK : DSTATE_SCANNING;
134  return TRUE;
135}
136
137
138/*
139 * Read some scanlines of data from the JPEG decompressor.
140 *
141 * The return value will be the number of lines actually read.
142 * This may be less than the number requested in several cases,
143 * including bottom of image, data source suspension, and operating
144 * modes that emit multiple scanlines at a time.
145 *
146 * Note: we warn about excess calls to jpeg_read_scanlines() since
147 * this likely signals an application programmer error.  However,
148 * an oversize buffer (max_lines > scanlines remaining) is not an error.
149 */
150
151GLOBAL(JDIMENSION)
152jpeg_read_scanlines (j_decompress_ptr cinfo, JSAMPARRAY scanlines,
153		     JDIMENSION max_lines)
154{
155  JDIMENSION row_ctr;
156
157  if (cinfo->global_state != DSTATE_SCANNING)
158    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
159  if (cinfo->output_scanline >= cinfo->output_height) {
160    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
161    return 0;
162  }
163
164  /* Call progress monitor hook if present */
165  if (cinfo->progress != NULL) {
166    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
167    cinfo->progress->pass_limit = (long) cinfo->output_height;
168    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
169  }
170
171  /* Process some data */
172  row_ctr = 0;
173  (*cinfo->main->process_data) (cinfo, scanlines, &row_ctr, max_lines);
174  cinfo->output_scanline += row_ctr;
175  return row_ctr;
176}
177/*
178 * Initialize the jpeg decoder to decompressing a rectangle with size of (width, height)
179 * and its upper-left corner located at (start_x, start_y).
180 * Align start_x and start_y to multiplies of iMCU width and height, respectively.
181 * Also, the new reader position will be returned in (start_x, start_y).
182 */
183
184GLOBAL(void)
185jpeg_init_read_tile_scanline(j_decompress_ptr cinfo, huffman_index *index,
186		     int *start_x, int *start_y, int *width, int *height)
187{
188  // Calculates the boundary of iMCU
189  int lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
190  int lines_per_iMCU_col = cinfo->max_h_samp_factor * DCTSIZE;
191  int row_offset = *start_y / lines_per_iMCU_row;
192  int col_left_boundary = ((*start_x / lines_per_iMCU_col) / index->MCU_sample_size)
193      * index->MCU_sample_size;
194  int col_right_boundary = (*start_x + *width + lines_per_iMCU_col - 1) / lines_per_iMCU_col;
195
196  *height = (*start_y - row_offset * lines_per_iMCU_row) + *height;
197  *start_x = col_left_boundary * lines_per_iMCU_col;
198  *start_y = row_offset * lines_per_iMCU_row;
199  cinfo->image_width = (col_right_boundary - col_left_boundary) * lines_per_iMCU_col;
200  cinfo->input_iMCU_row = row_offset;
201  cinfo->output_iMCU_row = row_offset;
202
203  // Updates JPEG decoder parameter
204  jinit_color_deconverter(cinfo);
205  jpeg_calc_output_dimensions(cinfo);
206  jinit_upsampler(cinfo);
207  jpeg_decompress_per_scan_setup(cinfo);
208  cinfo->MCUs_per_row = col_right_boundary - col_left_boundary;
209
210  int sampleSize = cinfo->image_width / cinfo->output_width;
211  *height /= sampleSize;
212  *width = cinfo->output_width;
213  cinfo->output_scanline = lines_per_iMCU_row * row_offset / sampleSize;
214  (*cinfo->master->prepare_for_output_pass) (cinfo);
215}
216
217/*
218 * Read a scanline from the current position.
219 *
220 * Return the number of lines actually read.
221 */
222
223GLOBAL(JDIMENSION)
224jpeg_read_tile_scanline (j_decompress_ptr cinfo, huffman_index *index,
225        JSAMPARRAY scanlines, int start_x, int start_y, int width, int height)
226{
227  // Calculates the boundary of iMCU
228  int lines_per_iMCU_row = cinfo->max_v_samp_factor * DCTSIZE;
229  int lines_per_iMCU_col = cinfo->max_h_samp_factor * DCTSIZE;
230  int col_left_boundary = ((start_x / lines_per_iMCU_col) / index->MCU_sample_size)
231      * index->MCU_sample_size;
232  int sampleSize = cinfo->image_width / cinfo->output_width;
233
234  if (cinfo->output_scanline % (lines_per_iMCU_row / sampleSize) == 0) {
235    // Set the read head to the next iMCU row
236    cinfo->unread_marker = 0;
237    int iMCU_row_offset = cinfo->output_scanline / (lines_per_iMCU_row / sampleSize);
238    int offset_data_col_position = col_left_boundary / index->MCU_sample_size;
239    huffman_offset_data *offset_data =
240        &index->scan[0].offset[iMCU_row_offset][offset_data_col_position];
241
242    jpeg_configure_huffman_decoder(cinfo,
243            offset_data->bitstream_offset, offset_data->prev_dc);
244  }
245
246  int row_ctr = jpeg_read_scanlines(cinfo, scanlines, 1); // Read one line
247  return row_ctr;
248}
249
250
251/*
252 * Alternate entry point to read raw data.
253 * Processes exactly one iMCU row per call, unless suspended.
254 */
255
256GLOBAL(JDIMENSION)
257jpeg_read_raw_data (j_decompress_ptr cinfo, JSAMPIMAGE data,
258		    JDIMENSION max_lines)
259{
260  JDIMENSION lines_per_iMCU_row;
261
262  if (cinfo->global_state != DSTATE_RAW_OK)
263    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
264  if (cinfo->output_scanline >= cinfo->output_height) {
265    WARNMS(cinfo, JWRN_TOO_MUCH_DATA);
266    return 0;
267  }
268
269  /* Call progress monitor hook if present */
270  if (cinfo->progress != NULL) {
271    cinfo->progress->pass_counter = (long) cinfo->output_scanline;
272    cinfo->progress->pass_limit = (long) cinfo->output_height;
273    (*cinfo->progress->progress_monitor) ((j_common_ptr) cinfo);
274  }
275
276  /* Verify that at least one iMCU row can be returned. */
277  lines_per_iMCU_row = cinfo->max_v_samp_factor * cinfo->min_DCT_scaled_size;
278  if (max_lines < lines_per_iMCU_row)
279    ERREXIT(cinfo, JERR_BUFFER_SIZE);
280
281  /* Decompress directly into user's buffer. */
282  if (! (*cinfo->coef->decompress_data) (cinfo, data))
283    return 0;			/* suspension forced, can do nothing more */
284
285  /* OK, we processed one iMCU row. */
286  cinfo->output_scanline += lines_per_iMCU_row;
287  return lines_per_iMCU_row;
288}
289
290
291/* Additional entry points for buffered-image mode. */
292
293#ifdef D_MULTISCAN_FILES_SUPPORTED
294
295/*
296 * Initialize for an output pass in buffered-image mode.
297 */
298
299GLOBAL(boolean)
300jpeg_start_output (j_decompress_ptr cinfo, int scan_number)
301{
302  if (cinfo->global_state != DSTATE_BUFIMAGE &&
303      cinfo->global_state != DSTATE_PRESCAN)
304    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
305  /* Limit scan number to valid range */
306  if (scan_number <= 0)
307    scan_number = 1;
308  if (cinfo->inputctl->eoi_reached &&
309      scan_number > cinfo->input_scan_number)
310    scan_number = cinfo->input_scan_number;
311  cinfo->output_scan_number = scan_number;
312  /* Perform any dummy output passes, and set up for the real pass */
313  return output_pass_setup(cinfo);
314}
315
316
317/*
318 * Finish up after an output pass in buffered-image mode.
319 *
320 * Returns FALSE if suspended.  The return value need be inspected only if
321 * a suspending data source is used.
322 */
323
324GLOBAL(boolean)
325jpeg_finish_output (j_decompress_ptr cinfo)
326{
327  if ((cinfo->global_state == DSTATE_SCANNING ||
328       cinfo->global_state == DSTATE_RAW_OK) && cinfo->buffered_image) {
329    /* Terminate this pass. */
330    /* We do not require the whole pass to have been completed. */
331    (*cinfo->master->finish_output_pass) (cinfo);
332    cinfo->global_state = DSTATE_BUFPOST;
333  } else if (cinfo->global_state != DSTATE_BUFPOST) {
334    /* BUFPOST = repeat call after a suspension, anything else is error */
335    ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
336  }
337  /* Read markers looking for SOS or EOI */
338  while (cinfo->input_scan_number <= cinfo->output_scan_number &&
339	 ! cinfo->inputctl->eoi_reached) {
340    if ((*cinfo->inputctl->consume_input) (cinfo) == JPEG_SUSPENDED)
341      return FALSE;		/* Suspend, come back later */
342  }
343  cinfo->global_state = DSTATE_BUFIMAGE;
344  return TRUE;
345}
346
347#endif /* D_MULTISCAN_FILES_SUPPORTED */
348