w32tex
About: TeX Live provides a comprehensive TeX system including all the major TeX-related programs, macro packages, and fonts that are free software. Windows sources.
  Fossies Dox: w32tex-src.tar.xz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

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