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



Page generated by Doxygen 1.8.14 for MRPT 1.5.6 Git: 4c65e8431 Tue Apr 24 08:18:17 2018 +0200 at lun oct 28 01:35:26 CET 2019