source: golgotha/src/i4/loaders/jpg/jdinput.cc @ 608

Last change on this file since 608 was 80, checked in by Sam Hocevar, 15 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: 14.0 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 * jdinput.c
11 *
12 * Copyright (C) 1991-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 input control logic for the JPEG decompressor.
17 * These routines are concerned with controlling the decompressor's input
18 * processing (marker reading and coefficient decoding).  The actual input
19 * reading is done in jdmarker.c, jdhuff.c, and jdphuff.c.
20 */
21
22#define JPEG_INTERNALS
23#include "loaders/jpg/jinclude.h"
24#include "loaders/jpg/jpeglib.h"
25
26
27/* Private state */
28
29typedef struct {
30  struct jpeg_input_controller pub; /* public fields */
31
32  boolean inheaders;            /* TRUE until first SOS is reached */
33} my_input_controller;
34
35typedef my_input_controller * my_inputctl_ptr;
36
37
38/* Forward declarations */
39METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo));
40
41
42/*
43 * Routines to calculate various quantities related to the size of the image.
44 */
45
46LOCAL(void)
47initial_setup (j_decompress_ptr cinfo)
48/* Called once, when first SOS marker is reached */
49{
50  int ci;
51  jpeg_component_info *compptr;
52
53  /* Make sure image isn't bigger than I can handle */
54  if ((long) cinfo->image_height > (long) JPEG_MAX_DIMENSION ||
55      (long) cinfo->image_width > (long) JPEG_MAX_DIMENSION)
56    ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int) JPEG_MAX_DIMENSION);
57
58  /* For now, precision must match compiled-in value... */
59  if (cinfo->data_precision != BITS_IN_JSAMPLE)
60    ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
61
62  /* Check that number of components won't exceed internal array sizes */
63  if (cinfo->num_components > MAX_COMPONENTS)
64    ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->num_components,
65             MAX_COMPONENTS);
66
67  /* Compute maximum sampling factors; check factor validity */
68  cinfo->max_h_samp_factor = 1;
69  cinfo->max_v_samp_factor = 1;
70  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
71       ci++, compptr++) {
72    if (compptr->h_samp_factor<=0 || compptr->h_samp_factor>MAX_SAMP_FACTOR ||
73        compptr->v_samp_factor<=0 || compptr->v_samp_factor>MAX_SAMP_FACTOR)
74      ERREXIT(cinfo, JERR_BAD_SAMPLING);
75    cinfo->max_h_samp_factor = MAX(cinfo->max_h_samp_factor,
76                                   compptr->h_samp_factor);
77    cinfo->max_v_samp_factor = MAX(cinfo->max_v_samp_factor,
78                                   compptr->v_samp_factor);
79  }
80
81  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
82   * In the full decompressor, this will be overridden by jdmaster.c;
83   * but in the transcoder, jdmaster.c is not used, so we must do it here.
84   */
85  cinfo->min_DCT_scaled_size = DCTSIZE;
86
87  /* Compute dimensions of components */
88  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
89       ci++, compptr++) {
90    compptr->DCT_scaled_size = DCTSIZE;
91    /* Size in DCT blocks */
92    compptr->width_in_blocks = (JDIMENSION)
93      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
94                    (long) (cinfo->max_h_samp_factor * DCTSIZE));
95    compptr->height_in_blocks = (JDIMENSION)
96      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
97                    (long) (cinfo->max_v_samp_factor * DCTSIZE));
98    /* downsampled_width and downsampled_height will also be overridden by
99     * jdmaster.c if we are doing full decompression.  The transcoder library
100     * doesn't use these values, but the calling application might.
101     */
102    /* Size in samples */
103    compptr->downsampled_width = (JDIMENSION)
104      jdiv_round_up((long) cinfo->image_width * (long) compptr->h_samp_factor,
105                    (long) cinfo->max_h_samp_factor);
106    compptr->downsampled_height = (JDIMENSION)
107      jdiv_round_up((long) cinfo->image_height * (long) compptr->v_samp_factor,
108                    (long) cinfo->max_v_samp_factor);
109    /* Mark component needed, until color conversion says otherwise */
110    compptr->component_needed = TRUE;
111    /* Mark no quantization table yet saved for component */
112    compptr->quant_table = NULL;
113  }
114
115  /* Compute number of fully interleaved MCU rows. */
116  cinfo->total_iMCU_rows = (JDIMENSION)
117    jdiv_round_up((long) cinfo->image_height,
118                  (long) (cinfo->max_v_samp_factor*DCTSIZE));
119
120  /* Decide whether file contains multiple scans */
121  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
122    cinfo->inputctl->has_multiple_scans = TRUE;
123  else
124    cinfo->inputctl->has_multiple_scans = FALSE;
125}
126
127
128LOCAL(void)
129per_scan_setup (j_decompress_ptr cinfo)
130/* Do computations that are needed before processing a JPEG scan */
131/* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
132{
133  int ci, mcublks, tmp;
134  jpeg_component_info *compptr;
135 
136  if (cinfo->comps_in_scan == 1) {
137   
138    /* Noninterleaved (single-component) scan */
139    compptr = cinfo->cur_comp_info[0];
140   
141    /* Overall image size in MCUs */
142    cinfo->MCUs_per_row = compptr->width_in_blocks;
143    cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
144   
145    /* For noninterleaved scan, always one block per MCU */
146    compptr->MCU_width = 1;
147    compptr->MCU_height = 1;
148    compptr->MCU_blocks = 1;
149    compptr->MCU_sample_width = compptr->DCT_scaled_size;
150    compptr->last_col_width = 1;
151    /* For noninterleaved scans, it is convenient to define last_row_height
152     * as the number of block rows present in the last iMCU row.
153     */
154    tmp = (int) (compptr->height_in_blocks % compptr->v_samp_factor);
155    if (tmp == 0) tmp = compptr->v_samp_factor;
156    compptr->last_row_height = tmp;
157   
158    /* Prepare array describing MCU composition */
159    cinfo->blocks_in_MCU = 1;
160    cinfo->MCU_membership[0] = 0;
161   
162  } else {
163   
164    /* Interleaved (multi-component) scan */
165    if (cinfo->comps_in_scan <= 0 || cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
166      ERREXIT2(cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
167               MAX_COMPS_IN_SCAN);
168   
169    /* Overall image size in MCUs */
170    cinfo->MCUs_per_row = (JDIMENSION)
171      jdiv_round_up((long) cinfo->image_width,
172                    (long) (cinfo->max_h_samp_factor*DCTSIZE));
173    cinfo->MCU_rows_in_scan = (JDIMENSION)
174      jdiv_round_up((long) cinfo->image_height,
175                    (long) (cinfo->max_v_samp_factor*DCTSIZE));
176   
177    cinfo->blocks_in_MCU = 0;
178   
179    for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
180      compptr = cinfo->cur_comp_info[ci];
181      /* Sampling factors give # of blocks of component in each MCU */
182      compptr->MCU_width = compptr->h_samp_factor;
183      compptr->MCU_height = compptr->v_samp_factor;
184      compptr->MCU_blocks = compptr->MCU_width * compptr->MCU_height;
185      compptr->MCU_sample_width = compptr->MCU_width * compptr->DCT_scaled_size;
186      /* Figure number of non-dummy blocks in last MCU column & row */
187      tmp = (int) (compptr->width_in_blocks % compptr->MCU_width);
188      if (tmp == 0) tmp = compptr->MCU_width;
189      compptr->last_col_width = tmp;
190      tmp = (int) (compptr->height_in_blocks % compptr->MCU_height);
191      if (tmp == 0) tmp = compptr->MCU_height;
192      compptr->last_row_height = tmp;
193      /* Prepare array describing MCU composition */
194      mcublks = compptr->MCU_blocks;
195      if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
196        ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
197      while (mcublks-- > 0) {
198        cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
199      }
200    }
201   
202  }
203}
204
205
206/*
207 * Save away a copy of the Q-table referenced by each component present
208 * in the current scan, unless already saved during a prior scan.
209 *
210 * In a multiple-scan JPEG file, the encoder could assign different components
211 * the same Q-table slot number, but change table definitions between scans
212 * so that each component uses a different Q-table.  (The IJG encoder is not
213 * currently capable of doing this, but other encoders might.)  Since we want
214 * to be able to dequantize all the components at the end of the file, this
215 * means that we have to save away the table actually used for each component.
216 * We do this by copying the table at the start of the first scan containing
217 * the component.
218 * The JPEG spec prohibits the encoder from changing the contents of a Q-table
219 * slot between scans of a component using that slot.  If the encoder does so
220 * anyway, this decoder will simply use the Q-table values that were current
221 * at the start of the first scan for the component.
222 *
223 * The decompressor output side looks only at the saved quant tables,
224 * not at the current Q-table slots.
225 */
226
227LOCAL(void)
228latch_quant_tables (j_decompress_ptr cinfo)
229{
230  int ci, qtblno;
231  jpeg_component_info *compptr;
232  JQUANT_TBL * qtbl;
233
234  for (ci = 0; ci < cinfo->comps_in_scan; ci++) {
235    compptr = cinfo->cur_comp_info[ci];
236    /* No work if we already saved Q-table for this component */
237    if (compptr->quant_table != NULL)
238      continue;
239    /* Make sure specified quantization table is present */
240    qtblno = compptr->quant_tbl_no;
241    if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
242        cinfo->quant_tbl_ptrs[qtblno] == NULL)
243      ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
244    /* OK, save away the quantization table */
245    qtbl = (JQUANT_TBL *)
246      (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
247                                  SIZEOF(JQUANT_TBL));
248    MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
249    compptr->quant_table = qtbl;
250  }
251}
252
253
254/*
255 * Initialize the input modules to read a scan of compressed data.
256 * The first call to this is done by jdmaster.c after initializing
257 * the entire decompressor (during jpeg_start_decompress).
258 * Subsequent calls come from consume_markers, below.
259 */
260
261METHODDEF(void)
262start_input_pass (j_decompress_ptr cinfo)
263{
264  per_scan_setup(cinfo);
265  latch_quant_tables(cinfo);
266  (*cinfo->entropy->start_pass) (cinfo);
267  (*cinfo->coef->start_input_pass) (cinfo);
268  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
269}
270
271
272/*
273 * Finish up after inputting a compressed-data scan.
274 * This is called by the coefficient controller after it's read all
275 * the expected data of the scan.
276 */
277
278METHODDEF(void)
279finish_input_pass (j_decompress_ptr cinfo)
280{
281  cinfo->inputctl->consume_input = consume_markers;
282}
283
284
285/*
286 * Read JPEG markers before, between, or after compressed-data scans.
287 * Change state as necessary when a new scan is reached.
288 * Return value is JPEG_SUSPENDED, JPEG_REACHED_SOS, or JPEG_REACHED_EOI.
289 *
290 * The consume_input method pointer points either here or to the
291 * coefficient controller's consume_data routine, depending on whether
292 * we are reading a compressed data segment or inter-segment markers.
293 */
294
295METHODDEF(int)
296consume_markers (j_decompress_ptr cinfo)
297{
298  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
299  int val;
300
301  if (inputctl->pub.eoi_reached) /* After hitting EOI, read no further */
302    return JPEG_REACHED_EOI;
303
304  val = (*cinfo->marker->read_markers) (cinfo);
305
306  switch (val) {
307  case JPEG_REACHED_SOS:        /* Found SOS */
308    if (inputctl->inheaders) {  /* 1st SOS */
309      initial_setup(cinfo);
310      inputctl->inheaders = FALSE;
311      /* Note: start_input_pass must be called by jdmaster.c
312       * before any more input can be consumed.  jdapi.c is
313       * responsible for enforcing this sequencing.
314       */
315    } else {                    /* 2nd or later SOS marker */
316      if (! inputctl->pub.has_multiple_scans)
317        ERREXIT(cinfo, JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
318      start_input_pass(cinfo);
319    }
320    break;
321  case JPEG_REACHED_EOI:        /* Found EOI */
322    inputctl->pub.eoi_reached = TRUE;
323    if (inputctl->inheaders) {  /* Tables-only datastream, apparently */
324      if (cinfo->marker->saw_SOF)
325        ERREXIT(cinfo, JERR_SOF_NO_SOS);
326    } else {
327      /* Prevent infinite loop in coef ctlr's decompress_data routine
328       * if user set output_scan_number larger than number of scans.
329       */
330      if (cinfo->output_scan_number > cinfo->input_scan_number)
331        cinfo->output_scan_number = cinfo->input_scan_number;
332    }
333    break;
334  case JPEG_SUSPENDED:
335    break;
336  }
337
338  return val;
339}
340
341
342/*
343 * Reset state to begin a fresh datastream.
344 */
345
346METHODDEF(void)
347reset_input_controller (j_decompress_ptr cinfo)
348{
349  my_inputctl_ptr inputctl = (my_inputctl_ptr) cinfo->inputctl;
350
351  inputctl->pub.consume_input = consume_markers;
352  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
353  inputctl->pub.eoi_reached = FALSE;
354  inputctl->inheaders = TRUE;
355  /* Reset other modules */
356  (*cinfo->err->reset_error_mgr) ((j_common_ptr) cinfo);
357  (*cinfo->marker->reset_marker_reader) (cinfo);
358  /* Reset progression state -- would be cleaner if entropy decoder did this */
359  cinfo->coef_bits = NULL;
360}
361
362
363/*
364 * Initialize the input controller module.
365 * This is called only once, when the decompression object is created.
366 */
367
368GLOBAL(void)
369jinit_input_controller (j_decompress_ptr cinfo)
370{
371  my_inputctl_ptr inputctl;
372
373  /* Create subobject in permanent pool */
374  inputctl = (my_inputctl_ptr)
375    (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_PERMANENT,
376                                SIZEOF(my_input_controller));
377  cinfo->inputctl = (struct jpeg_input_controller *) inputctl;
378  /* Initialize method pointers */
379  inputctl->pub.consume_input = consume_markers;
380  inputctl->pub.reset_input_controller = reset_input_controller;
381  inputctl->pub.start_input_pass = start_input_pass;
382  inputctl->pub.finish_input_pass = finish_input_pass;
383  /* Initialize state: can't use reset_input_controller since we don't
384   * want to try to reset other modules yet.
385   */
386  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
387  inputctl->pub.eoi_reached = FALSE;
388  inputctl->inheaders = TRUE;
389}
Note: See TracBrowser for help on using the repository browser.