Main MRPT website > C++ reference for MRPT 1.9.9
jdapimin.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  * Initialization of a JPEG decompression object.
16  * The error manager must already be set up (in case memory manager fails).
17  */
18 
19 GLOBAL(void)
20 jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
21 {
22  int i;
23 
24  /* Guard against version mismatches between library and caller. */
25  cinfo->mem = nullptr; /* so jpeg_destroy knows mem mgr not called */
26  if (version != JPEG_LIB_VERSION)
27  ERREXIT2(cinfo, JERR_BAD_LIB_VERSION, JPEG_LIB_VERSION, version);
28  if (structsize != SIZEOF(struct jpeg_decompress_struct))
29  ERREXIT2(
30  cinfo, JERR_BAD_STRUCT_SIZE,
31  (int)SIZEOF(struct jpeg_decompress_struct), (int)structsize);
32 
33  /* For debugging purposes, we zero the whole master structure.
34  * But the application has already set the err pointer, and may have set
35  * client_data, so we have to save and restore those fields.
36  * Note: if application hasn't set client_data, tools like Purify may
37  * complain here.
38  */
39  {
40  struct jpeg_error_mgr* err = cinfo->err;
41  void* client_data =
42  cinfo->client_data; /* ignore Purify complaint here */
43  MEMZERO(cinfo, SIZEOF(struct jpeg_decompress_struct));
44  cinfo->err = err;
45  cinfo->client_data = client_data;
46  }
47  cinfo->is_decompressor = TRUE;
48 
49  /* Initialize a memory manager instance for this object */
51 
52  /* Zero out pointers to permanent structures. */
53  cinfo->progress = nullptr;
54  cinfo->src = nullptr;
55 
56  for (i = 0; i < NUM_QUANT_TBLS; i++) cinfo->quant_tbl_ptrs[i] = nullptr;
57 
58  for (i = 0; i < NUM_HUFF_TBLS; i++)
59  {
60  cinfo->dc_huff_tbl_ptrs[i] = nullptr;
61  cinfo->ac_huff_tbl_ptrs[i] = nullptr;
62  }
63 
64  /* Initialize marker processor so application can override methods
65  * for COM, APPn markers before calling jpeg_read_header.
66  */
67  cinfo->marker_list = nullptr;
68  jinit_marker_reader(cinfo);
69 
70  /* And initialize the overall input controller. */
72 
73  /* OK, I'm ready */
74  cinfo->global_state = DSTATE_START;
75 }
76 
77 /*
78  * Destruction of a JPEG decompression object
79  */
80 
81 GLOBAL(void)
83 {
84  jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
85 }
86 
87 /*
88  * Abort processing of a JPEG decompression operation,
89  * but don't destroy the object itself.
90  */
91 
92 GLOBAL(void)
94 {
95  jpeg_abort((j_common_ptr)cinfo); /* use common routine */
96 }
97 
98 /*
99  * Set default decompression parameters.
100  */
101 
102 LOCAL(void)
104 {
105  /* Guess the input colorspace, and set output colorspace accordingly. */
106  /* (Wish JPEG committee had provided a real way to specify this...) */
107  /* Note application may override our guesses. */
108  switch (cinfo->num_components)
109  {
110  case 1:
111  cinfo->jpeg_color_space = JCS_GRAYSCALE;
112  cinfo->out_color_space = JCS_GRAYSCALE;
113  break;
114 
115  case 3:
116  if (cinfo->saw_JFIF_marker)
117  {
118  cinfo->jpeg_color_space = JCS_YCbCr; /* JFIF implies YCbCr */
119  }
120  else if (cinfo->saw_Adobe_marker)
121  {
122  switch (cinfo->Adobe_transform)
123  {
124  case 0:
125  cinfo->jpeg_color_space = JCS_RGB;
126  break;
127  case 1:
128  cinfo->jpeg_color_space = JCS_YCbCr;
129  break;
130  default:
131  WARNMS1(
132  cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
133  cinfo->jpeg_color_space =
134  JCS_YCbCr; /* assume it's YCbCr */
135  break;
136  }
137  }
138  else
139  {
140  /* Saw no special markers, try to guess from the component IDs
141  */
142  int cid0 = cinfo->comp_info[0].component_id;
143  int cid1 = cinfo->comp_info[1].component_id;
144  int cid2 = cinfo->comp_info[2].component_id;
145 
146  if (cid0 == 1 && cid1 == 2 && cid2 == 3)
147  cinfo->jpeg_color_space =
148  JCS_YCbCr; /* assume JFIF w/out marker */
149  else if (cid0 == 82 && cid1 == 71 && cid2 == 66)
150  cinfo->jpeg_color_space = JCS_RGB; /* ASCII 'R', 'G', 'B' */
151  else
152  {
153  TRACEMS3(cinfo, 1, JTRC_UNKNOWN_IDS, cid0, cid1, cid2);
154  cinfo->jpeg_color_space = JCS_YCbCr; /* assume it's YCbCr */
155  }
156  }
157  /* Always guess RGB is proper output colorspace. */
158  cinfo->out_color_space = JCS_RGB;
159  break;
160 
161  case 4:
162  if (cinfo->saw_Adobe_marker)
163  {
164  switch (cinfo->Adobe_transform)
165  {
166  case 0:
167  cinfo->jpeg_color_space = JCS_CMYK;
168  break;
169  case 2:
170  cinfo->jpeg_color_space = JCS_YCCK;
171  break;
172  default:
173  WARNMS1(
174  cinfo, JWRN_ADOBE_XFORM, cinfo->Adobe_transform);
175  cinfo->jpeg_color_space =
176  JCS_YCCK; /* assume it's YCCK */
177  break;
178  }
179  }
180  else
181  {
182  /* No special markers, assume straight CMYK. */
183  cinfo->jpeg_color_space = JCS_CMYK;
184  }
185  cinfo->out_color_space = JCS_CMYK;
186  break;
187 
188  default:
189  cinfo->jpeg_color_space = JCS_UNKNOWN;
190  cinfo->out_color_space = JCS_UNKNOWN;
191  break;
192  }
193 
194  /* Set defaults for other decompression parameters. */
195  cinfo->scale_num = 1; /* 1:1 scaling */
196  cinfo->scale_denom = 1;
197  cinfo->output_gamma = 1.0;
198  cinfo->buffered_image = FALSE;
199  cinfo->raw_data_out = FALSE;
200  cinfo->dct_method = JDCT_DEFAULT;
201  cinfo->do_fancy_upsampling = TRUE;
202  cinfo->do_block_smoothing = TRUE;
203  cinfo->quantize_colors = FALSE;
204  /* We set these in case application only sets quantize_colors. */
205  cinfo->dither_mode = JDITHER_FS;
206 #ifdef QUANT_2PASS_SUPPORTED
207  cinfo->two_pass_quantize = TRUE;
208 #else
209  cinfo->two_pass_quantize = FALSE;
210 #endif
211  cinfo->desired_number_of_colors = 256;
212  cinfo->colormap = nullptr;
213  /* Initialize for no mode change in buffered-image mode. */
214  cinfo->enable_1pass_quant = FALSE;
215  cinfo->enable_external_quant = FALSE;
216  cinfo->enable_2pass_quant = FALSE;
217 }
218 
219 /*
220  * Decompression startup: read start of JPEG datastream to see what's there.
221  * Need only initialize JPEG object and supply a data source before calling.
222  *
223  * This routine will read as far as the first SOS marker (ie, actual start of
224  * compressed data), and will save all tables and parameters in the JPEG
225  * object. It will also initialize the decompression parameters to default
226  * values, and finally return JPEG_HEADER_OK. On return, the application may
227  * adjust the decompression parameters and then call jpeg_start_decompress.
228  * (Or, if the application only wanted to determine the image parameters,
229  * the data need not be decompressed. In that case, call jpeg_abort or
230  * jpeg_destroy to release any temporary space.)
231  * If an abbreviated (tables only) datastream is presented, the routine will
232  * return JPEG_HEADER_TABLES_ONLY upon reaching EOI. The application may then
233  * re-use the JPEG object to read the abbreviated image datastream(s).
234  * It is unnecessary (but OK) to call jpeg_abort in this case.
235  * The JPEG_SUSPENDED return code only occurs if the data source module
236  * requests suspension of the decompressor. In this case the application
237  * should load more source data and then re-call jpeg_read_header to resume
238  * processing.
239  * If a non-suspending data source is used and require_image is TRUE, then the
240  * return code need not be inspected since only JPEG_HEADER_OK is possible.
241  *
242  * This routine is now just a front end to jpeg_consume_input, with some
243  * extra error checking.
244  */
245 
246 GLOBAL(int)
247 jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
248 {
249  int retcode;
250 
251  if (cinfo->global_state != DSTATE_START &&
252  cinfo->global_state != DSTATE_INHEADER)
253  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
254 
255  retcode = jpeg_consume_input(cinfo);
256 
257  switch (retcode)
258  {
259  case JPEG_REACHED_SOS:
260  retcode = JPEG_HEADER_OK;
261  break;
262  case JPEG_REACHED_EOI:
263  if (require_image) /* Complain if application wanted an image */
264  ERREXIT(cinfo, JERR_NO_IMAGE);
265  /* Reset to start state; it would be safer to require the
266  * application to
267  * call jpeg_abort, but we can't change it now for compatibility
268  * reasons.
269  * A side effect is to free any temporary memory (there shouldn't be
270  * any).
271  */
272  jpeg_abort((j_common_ptr)cinfo); /* sets state = DSTATE_START */
273  retcode = JPEG_HEADER_TABLES_ONLY;
274  break;
275  case JPEG_SUSPENDED:
276  /* no work */
277  break;
278  }
279 
280  return retcode;
281 }
282 
283 /*
284  * Consume data in advance of what the decompressor requires.
285  * This can be called at any time once the decompressor object has
286  * been created and a data source has been set up.
287  *
288  * This routine is essentially a state machine that handles a couple
289  * of critical state-transition actions, namely initial setup and
290  * transition from header scanning to ready-for-start_decompress.
291  * All the actual input is done via the input controller's consume_input
292  * method.
293  */
294 
295 GLOBAL(int)
297 {
298  int retcode = JPEG_SUSPENDED;
299 
300  /* NB: every possible DSTATE value should be listed in this switch */
301  switch (cinfo->global_state)
302  {
303  case DSTATE_START:
304  /* Start-of-datastream actions: reset appropriate modules */
305  (*cinfo->inputctl->reset_input_controller)(cinfo);
306  /* Initialize application's data source module */
307  (*cinfo->src->init_source)(cinfo);
308  cinfo->global_state = DSTATE_INHEADER;
309  /*FALLTHROUGH*/
310  case DSTATE_INHEADER:
311  retcode = (*cinfo->inputctl->consume_input)(cinfo);
312  if (retcode == JPEG_REACHED_SOS)
313  { /* Found SOS, prepare to decompress */
314  /* Set up default parameters based on header data */
316  /* Set global state: ready for start_decompress */
317  cinfo->global_state = DSTATE_READY;
318  }
319  break;
320  case DSTATE_READY:
321  /* Can't advance past first SOS until start_decompress is called */
322  retcode = JPEG_REACHED_SOS;
323  break;
324  case DSTATE_PRELOAD:
325  case DSTATE_PRESCAN:
326  case DSTATE_SCANNING:
327  case DSTATE_RAW_OK:
328  case DSTATE_BUFIMAGE:
329  case DSTATE_BUFPOST:
330  case DSTATE_STOPPING:
331  retcode = (*cinfo->inputctl->consume_input)(cinfo);
332  break;
333  default:
334  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
335  }
336  return retcode;
337 }
338 
339 /*
340  * Have we finished reading the input file?
341  */
342 
343 GLOBAL(boolean)
345 {
346  /* Check for valid jpeg object */
347  if (cinfo->global_state < DSTATE_START ||
348  cinfo->global_state > DSTATE_STOPPING)
349  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
350  return cinfo->inputctl->eoi_reached;
351 }
352 
353 /*
354  * Is there more than one scan?
355  */
356 
357 GLOBAL(boolean)
359 {
360  /* Only valid after jpeg_read_header completes */
361  if (cinfo->global_state < DSTATE_READY ||
362  cinfo->global_state > DSTATE_STOPPING)
363  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
364  return cinfo->inputctl->has_multiple_scans;
365 }
366 
367 /*
368  * Finish JPEG decompression.
369  *
370  * This will normally just verify the file trailer and release temp storage.
371  *
372  * Returns FALSE if suspended. The return value need be inspected only if
373  * a suspending data source is used.
374  */
375 
376 GLOBAL(boolean)
378 {
379  if ((cinfo->global_state == DSTATE_SCANNING ||
380  cinfo->global_state == DSTATE_RAW_OK) &&
381  !cinfo->buffered_image)
382  {
383  /* Terminate final pass of non-buffered mode */
384  if (cinfo->output_scanline < cinfo->output_height)
385  ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
386  (*cinfo->master->finish_output_pass)(cinfo);
387  cinfo->global_state = DSTATE_STOPPING;
388  }
389  else if (cinfo->global_state == DSTATE_BUFIMAGE)
390  {
391  /* Finishing after a buffered-image operation */
392  cinfo->global_state = DSTATE_STOPPING;
393  }
394  else if (cinfo->global_state != DSTATE_STOPPING)
395  {
396  /* STOPPING = repeat call after a suspension, anything else is error */
397  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
398  }
399  /* Read until EOI */
400  while (!cinfo->inputctl->eoi_reached)
401  {
402  if ((*cinfo->inputctl->consume_input)(cinfo) == JPEG_SUSPENDED)
403  return FALSE; /* Suspend, come back later */
404  }
405  /* Do final cleanup */
406  (*cinfo->src->term_source)(cinfo);
407  /* We can use jpeg_abort to release memory and reset global_state */
408  jpeg_abort((j_common_ptr)cinfo);
409  return TRUE;
410 }
jinit_marker_reader(j_decompress_ptr cinfo)
Definition: jdmarker.cpp:1275
#define DSTATE_RAW_OK
Definition: jpegint.h:32
#define DSTATE_INHEADER
Definition: jpegint.h:27
jinit_input_controller(j_decompress_ptr cinfo)
Definition: jdinput.cpp:364
jpeg_consume_input(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:296
#define DSTATE_BUFIMAGE
Definition: jpegint.h:33
jpeg_finish_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:377
jpeg_destroy(j_common_ptr cinfo)
Definition: jcomapi.cpp:68
#define ERREXIT(cinfo, code)
Definition: jerror.h:451
for(ctr=DCTSIZE;ctr > 0;ctr--)
Definition: jidctflt.cpp:56
#define SIZEOF(object)
Definition: jinclude.h:74
#define DSTATE_STOPPING
Definition: jpegint.h:36
#define DSTATE_PRELOAD
Definition: jpegint.h:29
jpeg_destroy_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:82
jpeg_abort(j_common_ptr cinfo)
Definition: jcomapi.cpp:26
#define JPEG_HEADER_TABLES_ONLY
Definition: mrpt_jpeglib.h:966
jinit_memory_mgr(j_common_ptr cinfo)
Definition: jmemmgr.cpp:1050
#define FALSE
Definition: jmorecfg.h:216
#define JPEG_REACHED_EOI
Definition: mrpt_jpeglib.h:998
#define LOCAL(type)
Definition: jmorecfg.h:175
#define JPEG_HEADER_OK
Definition: mrpt_jpeglib.h:965
#define JDCT_DEFAULT
Definition: mrpt_jpeglib.h:220
#define DSTATE_READY
Definition: jpegint.h:28
#define DSTATE_START
Definition: jpegint.h:26
jpeg_CreateDecompress(j_decompress_ptr cinfo, int version, size_t structsize)
Definition: jdapimin.cpp:20
#define NUM_HUFF_TBLS
Definition: mrpt_jpeglib.h:39
#define JPEG_SUSPENDED
Definition: mrpt_jpeglib.h:964
jpeg_read_header(j_decompress_ptr cinfo, boolean require_image)
Definition: jdapimin.cpp:247
#define DSTATE_SCANNING
Definition: jpegint.h:31
#define WARNMS1(cinfo, code, p1)
Definition: jerror.h:485
#define TRUE
Definition: jmorecfg.h:219
jpeg_abort_decompress(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:93
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:454
#define DSTATE_BUFPOST
Definition: jpegint.h:34
#define JPEG_LIB_VERSION
Definition: mrpt_jpeglib.h:29
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define DSTATE_PRESCAN
Definition: jpegint.h:30
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:38
#define JPEG_REACHED_SOS
Definition: mrpt_jpeglib.h:997
#define TRACEMS3(cinfo, lvl, code, p1, p2, p3)
Definition: jerror.h:504
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:457
jpeg_has_multiple_scans(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:358
jpeg_input_complete(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:344
#define MEMZERO(target, size)
Definition: jinclude.h:60
default_decompress_parms(j_decompress_ptr cinfo)
Definition: jdapimin.cpp:103



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