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



Page generated by Doxygen 1.8.6 for MRPT 1.5.6 Git: 4c65e84 Tue Apr 24 08:18:17 2018 +0200 at mar abr 24 08:26:17 CEST 2018