Main MRPT website > C++ reference for MRPT 1.9.9
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 /* Private state */
15 
16 typedef struct
17 {
18  struct jpeg_input_controller pub; /* public fields */
19 
20  boolean inheaders; /* TRUE until first SOS is reached */
22 
24 
25 /* Forward declarations */
27 
28 /*
29  * Routines to calculate various quantities related to the size of the image.
30  */
31 
32 LOCAL(void)
34 /* Called once, when first SOS marker is reached */
35 {
36  int ci;
38 
39  /* Make sure image isn't bigger than I can handle */
40  if ((long)cinfo->image_height > (long)JPEG_MAX_DIMENSION ||
41  (long)cinfo->image_width > (long)JPEG_MAX_DIMENSION)
42  ERREXIT1(cinfo, JERR_IMAGE_TOO_BIG, (unsigned int)JPEG_MAX_DIMENSION);
43 
44  /* For now, precision must match compiled-in value... */
45  if (cinfo->data_precision != BITS_IN_JSAMPLE)
46  ERREXIT1(cinfo, JERR_BAD_PRECISION, cinfo->data_precision);
47 
48  /* Check that number of components won't exceed internal array sizes */
49  if (cinfo->num_components > MAX_COMPONENTS)
50  ERREXIT2(
51  cinfo, JERR_COMPONENT_COUNT, cinfo->num_components, MAX_COMPONENTS);
52 
53  /* Compute maximum sampling factors; check factor validity */
54  cinfo->max_h_samp_factor = 1;
55  cinfo->max_v_samp_factor = 1;
56  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
57  ci++, compptr++)
58  {
59  if (compptr->h_samp_factor <= 0 ||
61  compptr->v_samp_factor <= 0 ||
63  ERREXIT(cinfo, JERR_BAD_SAMPLING);
64  cinfo->max_h_samp_factor =
65  MAX(cinfo->max_h_samp_factor, compptr->h_samp_factor);
66  cinfo->max_v_samp_factor =
67  MAX(cinfo->max_v_samp_factor, compptr->v_samp_factor);
68  }
69 
70  /* We initialize DCT_scaled_size and min_DCT_scaled_size to DCTSIZE.
71  * In the full decompressor, this will be overridden by jdmaster.c;
72  * but in the transcoder, jdmaster.c is not used, so we must do it here.
73  */
74  cinfo->min_DCT_scaled_size = DCTSIZE;
75 
76  /* Compute dimensions of components */
77  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
78  ci++, compptr++)
79  {
81  /* Size in DCT blocks */
83  (long)cinfo->image_width * (long)compptr->h_samp_factor,
84  (long)(cinfo->max_h_samp_factor * DCTSIZE));
86  (long)cinfo->image_height * (long)compptr->v_samp_factor,
87  (long)(cinfo->max_v_samp_factor * DCTSIZE));
88  /* downsampled_width and downsampled_height will also be overridden by
89  * jdmaster.c if we are doing full decompression. The transcoder
90  * library
91  * doesn't use these values, but the calling application might.
92  */
93  /* Size in samples */
95  (long)cinfo->image_width * (long)compptr->h_samp_factor,
96  (long)cinfo->max_h_samp_factor);
98  (long)cinfo->image_height * (long)compptr->v_samp_factor,
99  (long)cinfo->max_v_samp_factor);
100  /* Mark component needed, until color conversion says otherwise */
102  /* Mark no quantization table yet saved for component */
103  compptr->quant_table = nullptr;
104  }
105 
106  /* Compute number of fully interleaved MCU rows. */
107  cinfo->total_iMCU_rows = (JDIMENSION)jdiv_round_up(
108  (long)cinfo->image_height, (long)(cinfo->max_v_samp_factor * DCTSIZE));
109 
110  /* Decide whether file contains multiple scans */
111  if (cinfo->comps_in_scan < cinfo->num_components || cinfo->progressive_mode)
112  cinfo->inputctl->has_multiple_scans = TRUE;
113  else
114  cinfo->inputctl->has_multiple_scans = FALSE;
115 }
116 
117 LOCAL(void)
119 /* Do computations that are needed before processing a JPEG scan */
120 /* cinfo->comps_in_scan and cinfo->cur_comp_info[] were set from SOS marker */
121 {
122  int ci, mcublks, tmp;
124 
125  if (cinfo->comps_in_scan == 1)
126  {
127  /* Noninterleaved (single-component) scan */
128  compptr = cinfo->cur_comp_info[0];
129 
130  /* Overall image size in MCUs */
131  cinfo->MCUs_per_row = compptr->width_in_blocks;
132  cinfo->MCU_rows_in_scan = compptr->height_in_blocks;
133 
134  /* For noninterleaved scan, always one block per MCU */
135  compptr->MCU_width = 1;
136  compptr->MCU_height = 1;
137  compptr->MCU_blocks = 1;
139  compptr->last_col_width = 1;
140  /* For noninterleaved scans, it is convenient to define last_row_height
141  * as the number of block rows present in the last iMCU row.
142  */
144  if (tmp == 0) tmp = compptr->v_samp_factor;
145  compptr->last_row_height = tmp;
146 
147  /* Prepare array describing MCU composition */
148  cinfo->blocks_in_MCU = 1;
149  cinfo->MCU_membership[0] = 0;
150  }
151  else
152  {
153  /* Interleaved (multi-component) scan */
154  if (cinfo->comps_in_scan <= 0 ||
155  cinfo->comps_in_scan > MAX_COMPS_IN_SCAN)
156  ERREXIT2(
157  cinfo, JERR_COMPONENT_COUNT, cinfo->comps_in_scan,
159 
160  /* Overall image size in MCUs */
161  cinfo->MCUs_per_row = (JDIMENSION)jdiv_round_up(
162  (long)cinfo->image_width,
163  (long)(cinfo->max_h_samp_factor * DCTSIZE));
164  cinfo->MCU_rows_in_scan = (JDIMENSION)jdiv_round_up(
165  (long)cinfo->image_height,
166  (long)(cinfo->max_v_samp_factor * DCTSIZE));
167 
168  cinfo->blocks_in_MCU = 0;
169 
170  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
171  {
172  compptr = cinfo->cur_comp_info[ci];
173  /* Sampling factors give # of blocks of component in each MCU */
179  /* Figure number of non-dummy blocks in last MCU column & row */
180  tmp = (int)(compptr->width_in_blocks % compptr->MCU_width);
181  if (tmp == 0) tmp = compptr->MCU_width;
182  compptr->last_col_width = tmp;
183  tmp = (int)(compptr->height_in_blocks % compptr->MCU_height);
184  if (tmp == 0) tmp = compptr->MCU_height;
185  compptr->last_row_height = tmp;
186  /* Prepare array describing MCU composition */
187  mcublks = compptr->MCU_blocks;
188  if (cinfo->blocks_in_MCU + mcublks > D_MAX_BLOCKS_IN_MCU)
189  ERREXIT(cinfo, JERR_BAD_MCU_SIZE);
190  while (mcublks-- > 0)
191  {
192  cinfo->MCU_membership[cinfo->blocks_in_MCU++] = ci;
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  {
228  compptr = cinfo->cur_comp_info[ci];
229  /* No work if we already saved Q-table for this component */
230  if (compptr->quant_table != nullptr) 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] == nullptr)
235  ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
236  /* OK, save away the quantization table */
237  qtbl = (JQUANT_TBL*)(*cinfo->mem->alloc_small)(
239  MEMCOPY(qtbl, cinfo->quant_tbl_ptrs[qtblno], SIZEOF(JQUANT_TBL));
240  compptr->quant_table = qtbl;
241  }
242 }
243 
244 /*
245  * Initialize the input modules to read a scan of compressed data.
246  * The first call to this is done by jdmaster.c after initializing
247  * the entire decompressor (during jpeg_start_decompress).
248  * Subsequent calls come from consume_markers, below.
249  */
250 
251 METHODDEF(void)
253 {
254  per_scan_setup(cinfo);
255  latch_quant_tables(cinfo);
256  (*cinfo->entropy->start_pass)(cinfo);
257  (*cinfo->coef->start_input_pass)(cinfo);
258  cinfo->inputctl->consume_input = cinfo->coef->consume_data;
259 }
260 
261 /*
262  * Finish up after inputting a compressed-data scan.
263  * This is called by the coefficient controller after it's read all
264  * the expected data of the scan.
265  */
266 
267 METHODDEF(void)
269 {
270  cinfo->inputctl->consume_input = consume_markers;
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  {
296  case JPEG_REACHED_SOS: /* Found SOS */
297  if (inputctl->inheaders)
298  { /* 1st SOS */
299  initial_setup(cinfo);
300  inputctl->inheaders = FALSE;
301  /* Note: start_input_pass must be called by jdmaster.c
302  * before any more input can be consumed. jdapimin.c is
303  * responsible for enforcing this sequencing.
304  */
305  }
306  else
307  { /* 2nd or later SOS marker */
308  if (!inputctl->pub.has_multiple_scans)
309  ERREXIT(
310  cinfo,
311  JERR_EOI_EXPECTED); /* Oops, I wasn't expecting this! */
312  start_input_pass(cinfo);
313  }
314  break;
315  case JPEG_REACHED_EOI: /* Found EOI */
316  inputctl->pub.eoi_reached = TRUE;
317  if (inputctl->inheaders)
318  { /* Tables-only datastream, apparently */
319  if (cinfo->marker->saw_SOF) ERREXIT(cinfo, JERR_SOF_NO_SOS);
320  }
321  else
322  {
323  /* Prevent infinite loop in coef ctlr's decompress_data routine
324  * if user set output_scan_number larger than number of scans.
325  */
326  if (cinfo->output_scan_number > cinfo->input_scan_number)
327  cinfo->output_scan_number = cinfo->input_scan_number;
328  }
329  break;
330  case JPEG_SUSPENDED:
331  break;
332  }
333 
334  return val;
335 }
336 
337 /*
338  * Reset state to begin a fresh datastream.
339  */
340 
341 METHODDEF(void)
343 {
344  my_inputctl_ptr inputctl = (my_inputctl_ptr)cinfo->inputctl;
345 
346  inputctl->pub.consume_input = consume_markers;
347  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
348  inputctl->pub.eoi_reached = FALSE;
349  inputctl->inheaders = TRUE;
350  /* Reset other modules */
351  (*cinfo->err->reset_error_mgr)((j_common_ptr)cinfo);
352  (*cinfo->marker->reset_marker_reader)(cinfo);
353  /* Reset progression state -- would be cleaner if entropy decoder did this
354  */
355  cinfo->coef_bits = nullptr;
356 }
357 
358 /*
359  * Initialize the input controller module.
360  * This is called only once, when the decompression object is created.
361  */
362 
363 GLOBAL(void)
365 {
366  my_inputctl_ptr inputctl;
367 
368  /* Create subobject in permanent pool */
369  inputctl = (my_inputctl_ptr)(*cinfo->mem->alloc_small)(
371  cinfo->inputctl = (struct jpeg_input_controller*)inputctl;
372  /* Initialize method pointers */
373  inputctl->pub.consume_input = consume_markers;
374  inputctl->pub.reset_input_controller = reset_input_controller;
375  inputctl->pub.start_input_pass = start_input_pass;
376  inputctl->pub.finish_input_pass = finish_input_pass;
377  /* Initialize state: can't use reset_input_controller since we don't
378  * want to try to reset other modules yet.
379  */
380  inputctl->pub.has_multiple_scans = FALSE; /* "unknown" would be better */
381  inputctl->pub.eoi_reached = FALSE;
382  inputctl->inheaders = TRUE;
383 }
initial_setup(j_decompress_ptr cinfo)
Definition: jdinput.cpp:33
jdiv_round_up(long a, long b)
Definition: jutils.cpp:63
boolean has_multiple_scans
Definition: jpegint.h:160
#define BITS_IN_JSAMPLE
Definition: jmorecfg.h:19
JDIMENSION downsampled_width
Definition: mrpt_jpeglib.h:148
jinit_input_controller(j_decompress_ptr cinfo)
Definition: jdinput.cpp:364
#define MAX_COMPS_IN_SCAN
Definition: mrpt_jpeglib.h:41
#define DCTSIZE
Definition: mrpt_jpeglib.h:36
#define JPOOL_PERMANENT
Definition: mrpt_jpeglib.h:749
#define D_MAX_BLOCKS_IN_MCU
Definition: mrpt_jpeglib.h:52
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define MAX_COMPONENTS
Definition: jmorecfg.h:30
#define ERREXIT(cinfo, code)
Definition: jerror.h:451
#define SIZEOF(object)
Definition: jinclude.h:74
JDIMENSION width_in_blocks
Definition: mrpt_jpeglib.h:132
latch_quant_tables(j_decompress_ptr cinfo)
Definition: jdinput.cpp:220
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:61
JDIMENSION height_in_blocks
Definition: mrpt_jpeglib.h:133
#define JPEG_MAX_DIMENSION
Definition: jmorecfg.h:163
#define FALSE
Definition: jmorecfg.h:216
#define JPEG_REACHED_EOI
Definition: mrpt_jpeglib.h:998
#define MAX(a, b)
Definition: jpegint.h:280
start_input_pass(j_decompress_ptr cinfo)
Definition: jdinput.cpp:252
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
#define LOCAL(type)
Definition: jmorecfg.h:175
int val
Definition: mrpt_jpeglib.h:955
METHODDEF(int) consume_markers JPP((j_decompress_ptr cinfo))
struct jpeg_input_controller pub
Definition: jdinput.cpp:18
#define JPEG_SUSPENDED
Definition: mrpt_jpeglib.h:964
#define JPP(arglist)
Definition: mrpt_jpeglib.h:815
#define TRUE
Definition: jmorecfg.h:219
finish_input_pass(j_decompress_ptr cinfo)
Definition: jdinput.cpp:268
JQUANT_TBL * quant_table
Definition: mrpt_jpeglib.h:169
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:454
#define GLOBAL(type)
Definition: jmorecfg.h:177
per_scan_setup(j_decompress_ptr cinfo)
Definition: jdinput.cpp:118
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:38
#define JPEG_REACHED_SOS
Definition: mrpt_jpeglib.h:997
#define MAX_SAMP_FACTOR
Definition: mrpt_jpeglib.h:42
unsigned int JDIMENSION
Definition: jmorecfg.h:161
consume_markers(j_decompress_ptr cinfo)
Definition: jdinput.cpp:284
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:457
reset_input_controller(j_decompress_ptr cinfo)
Definition: jdinput.cpp:342
my_input_controller * my_inputctl_ptr
Definition: jdinput.cpp:23
jpeg_component_info * compptr
Definition: jidctflt.cpp:36
JDIMENSION downsampled_height
Definition: mrpt_jpeglib.h:149



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019