Main MRPT website > C++ reference for MRPT 1.9.9
jcapimin.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 compression object.
16  * The error manager must already be set up (in case memory manager fails).
17  */
18 
19 GLOBAL(void)
20 jpeg_CreateCompress(j_compress_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_compress_struct))
29  ERREXIT2(
30  cinfo, JERR_BAD_STRUCT_SIZE,
31  (int)SIZEOF(struct jpeg_compress_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_compress_struct));
44  cinfo->err = err;
45  cinfo->client_data = client_data;
46  }
47  cinfo->is_decompressor = FALSE;
48 
49  /* Initialize a memory manager instance for this object */
51 
52  /* Zero out pointers to permanent structures. */
53  cinfo->progress = nullptr;
54  cinfo->dest = nullptr;
55 
56  cinfo->comp_info = nullptr;
57 
58  for (i = 0; i < NUM_QUANT_TBLS; i++) cinfo->quant_tbl_ptrs[i] = nullptr;
59 
60  for (i = 0; i < NUM_HUFF_TBLS; i++)
61  {
62  cinfo->dc_huff_tbl_ptrs[i] = nullptr;
63  cinfo->ac_huff_tbl_ptrs[i] = nullptr;
64  }
65 
66  cinfo->script_space = nullptr;
67 
68  cinfo->input_gamma = 1.0; /* in case application forgets */
69 
70  /* OK, I'm ready */
71  cinfo->global_state = CSTATE_START;
72 }
73 
74 /*
75  * Destruction of a JPEG compression object
76  */
77 
78 GLOBAL(void)
80 {
81  jpeg_destroy((j_common_ptr)cinfo); /* use common routine */
82 }
83 
84 /*
85  * Abort processing of a JPEG compression operation,
86  * but don't destroy the object itself.
87  */
88 
89 GLOBAL(void)
91 {
92  jpeg_abort((j_common_ptr)cinfo); /* use common routine */
93 }
94 
95 /*
96  * Forcibly suppress or un-suppress all quantization and Huffman tables.
97  * Marks all currently defined tables as already written (if suppress)
98  * or not written (if !suppress). This will control whether they get emitted
99  * by a subsequent jpeg_start_compress call.
100  *
101  * This routine is exported for use by applications that want to produce
102  * abbreviated JPEG datastreams. It logically belongs in jcparam.c, but
103  * since it is called by jpeg_start_compress, we put it here --- otherwise
104  * jcparam.o would be linked whether the application used it or not.
105  */
106 
107 GLOBAL(void)
109 {
110  int i;
111  JQUANT_TBL* qtbl;
112  JHUFF_TBL* htbl;
113 
114  for (i = 0; i < NUM_QUANT_TBLS; i++)
115  {
116  if ((qtbl = cinfo->quant_tbl_ptrs[i]) != nullptr)
117  qtbl->sent_table = suppress;
118  }
119 
120  for (i = 0; i < NUM_HUFF_TBLS; i++)
121  {
122  if ((htbl = cinfo->dc_huff_tbl_ptrs[i]) != nullptr)
123  htbl->sent_table = suppress;
124  if ((htbl = cinfo->ac_huff_tbl_ptrs[i]) != nullptr)
125  htbl->sent_table = suppress;
126  }
127 }
128 
129 /*
130  * Finish JPEG compression.
131  *
132  * If a multipass operating mode was selected, this may do a great deal of
133  * work including most of the actual output.
134  */
135 
136 GLOBAL(void)
138 {
139  JDIMENSION iMCU_row;
140 
141  if (cinfo->global_state == CSTATE_SCANNING ||
142  cinfo->global_state == CSTATE_RAW_OK)
143  {
144  /* Terminate first pass */
145  if (cinfo->next_scanline < cinfo->image_height)
146  ERREXIT(cinfo, JERR_TOO_LITTLE_DATA);
147  (*cinfo->master->finish_pass)(cinfo);
148  }
149  else if (cinfo->global_state != CSTATE_WRCOEFS)
150  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
151  /* Perform any remaining passes */
152  while (!cinfo->master->is_last_pass)
153  {
154  (*cinfo->master->prepare_for_pass)(cinfo);
155  for (iMCU_row = 0; iMCU_row < cinfo->total_iMCU_rows; iMCU_row++)
156  {
157  if (cinfo->progress != nullptr)
158  {
159  cinfo->progress->pass_counter = (long)iMCU_row;
160  cinfo->progress->pass_limit = (long)cinfo->total_iMCU_rows;
161  (*cinfo->progress->progress_monitor)((j_common_ptr)cinfo);
162  }
163  /* We bypass the main controller and invoke coef controller
164  * directly;
165  * all work is being done from the coefficient buffer.
166  */
167  if (!(*cinfo->coef->compress_data)(cinfo, (JSAMPIMAGE) nullptr))
168  ERREXIT(cinfo, JERR_CANT_SUSPEND);
169  }
170  (*cinfo->master->finish_pass)(cinfo);
171  }
172  /* Write EOI, do final cleanup */
173  (*cinfo->marker->write_file_trailer)(cinfo);
174  (*cinfo->dest->term_destination)(cinfo);
175  /* We can use jpeg_abort to release memory and reset global_state */
176  jpeg_abort((j_common_ptr)cinfo);
177 }
178 
179 /*
180  * Write a special marker.
181  * This is only recommended for writing COM or APPn markers.
182  * Must be called after jpeg_start_compress() and before
183  * first call to jpeg_write_scanlines() or jpeg_write_raw_data().
184  */
185 
186 GLOBAL(void)
188  j_compress_ptr cinfo, int marker, const JOCTET* dataptr,
189  unsigned int datalen)
190 {
191  JMETHOD(void, write_marker_byte, (j_compress_ptr info, int val));
192 
193  if (cinfo->next_scanline != 0 || (cinfo->global_state != CSTATE_SCANNING &&
194  cinfo->global_state != CSTATE_RAW_OK &&
195  cinfo->global_state != CSTATE_WRCOEFS))
196  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
197 
198  (*cinfo->marker->write_marker_header)(cinfo, marker, datalen);
199  write_marker_byte = cinfo->marker->write_marker_byte; /* copy for speed */
200  while (datalen--)
201  {
202  (*write_marker_byte)(cinfo, *dataptr);
203  dataptr++;
204  }
205 }
206 
207 /* Same, but piecemeal. */
208 
209 GLOBAL(void)
210 jpeg_write_m_header(j_compress_ptr cinfo, int marker, unsigned int datalen)
211 {
212  if (cinfo->next_scanline != 0 || (cinfo->global_state != CSTATE_SCANNING &&
213  cinfo->global_state != CSTATE_RAW_OK &&
214  cinfo->global_state != CSTATE_WRCOEFS))
215  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
216 
217  (*cinfo->marker->write_marker_header)(cinfo, marker, datalen);
218 }
219 
220 GLOBAL(void)
222 {
223  (*cinfo->marker->write_marker_byte)(cinfo, val);
224 }
225 
226 /*
227  * Alternate compression function: just write an abbreviated table file.
228  * Before calling this, all parameters and a data destination must be set up.
229  *
230  * To produce a pair of files containing abbreviated tables and abbreviated
231  * image data, one would proceed as follows:
232  *
233  * initialize JPEG object
234  * set JPEG parameters
235  * set destination to table file
236  * jpeg_write_tables(cinfo);
237  * set destination to image file
238  * jpeg_start_compress(cinfo, FALSE);
239  * write data...
240  * jpeg_finish_compress(cinfo);
241  *
242  * jpeg_write_tables has the side effect of marking all tables written
243  * (same as jpeg_suppress_tables(..., TRUE)). Thus a subsequent start_compress
244  * will not re-emit the tables unless it is passed write_all_tables=TRUE.
245  */
246 
247 GLOBAL(void)
249 {
250  if (cinfo->global_state != CSTATE_START)
251  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
252 
253  /* (Re)initialize error mgr and destination modules */
254  (*cinfo->err->reset_error_mgr)((j_common_ptr)cinfo);
255  (*cinfo->dest->init_destination)(cinfo);
256  /* Initialize the marker writer ... bit of a crock to do it here. */
257  jinit_marker_writer(cinfo);
258  /* Write them tables! */
259  (*cinfo->marker->write_tables_only)(cinfo);
260  /* And clean up. */
261  (*cinfo->dest->term_destination)(cinfo);
262  /*
263  * In library releases up through v6a, we called jpeg_abort() here to free
264  * any working memory allocated by the destination manager and marker
265  * writer. Some applications had a problem with that: they allocated space
266  * of their own from the library memory manager, and didn't want it to go
267  * away during write_tables. So now we do nothing. This will cause a
268  * memory leak if an app calls write_tables repeatedly without doing a full
269  * compression cycle or otherwise resetting the JPEG object. However, that
270  * seems less bad than unexpectedly freeing memory in the normal case.
271  * An app that prefers the old behavior can call jpeg_abort for itself after
272  * each call to jpeg_write_tables().
273  */
274 }
#define CSTATE_START
Definition: jpegint.h:22
jpeg_write_tables(j_compress_ptr cinfo)
Definition: jcapimin.cpp:248
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
boolean sent_table
Definition: mrpt_jpeglib.h:87
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
jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)
Definition: jcapimin.cpp:108
#define CSTATE_RAW_OK
Definition: jpegint.h:24
#define CSTATE_SCANNING
Definition: jpegint.h:23
jpeg_CreateCompress(j_compress_ptr cinfo, int version, size_t structsize)
Definition: jcapimin.cpp:20
jpeg_write_m_byte(j_compress_ptr cinfo, int val)
Definition: jcapimin.cpp:221
jpeg_finish_compress(j_compress_ptr cinfo)
Definition: jcapimin.cpp:137
jpeg_abort(j_common_ptr cinfo)
Definition: jcomapi.cpp:26
jinit_memory_mgr(j_common_ptr cinfo)
Definition: jmemmgr.cpp:1050
#define FALSE
Definition: jmorecfg.h:216
int val
Definition: mrpt_jpeglib.h:955
#define JMETHOD(type, methodname, arglist)
Definition: jmorecfg.h:190
#define NUM_HUFF_TBLS
Definition: mrpt_jpeglib.h:39
jpeg_destroy_compress(j_compress_ptr cinfo)
Definition: jcapimin.cpp:79
boolean suppress
Definition: mrpt_jpeglib.h:929
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:454
jpeg_write_m_header(j_compress_ptr cinfo, int marker, unsigned int datalen)
Definition: jcapimin.cpp:210
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:62
jpeg_write_marker(j_compress_ptr cinfo, int marker, const JOCTET *dataptr, unsigned int datalen)
Definition: jcapimin.cpp:187
boolean sent_table
Definition: mrpt_jpeglib.h:103
#define JPEG_LIB_VERSION
Definition: mrpt_jpeglib.h:29
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:38
write_marker_byte(j_compress_ptr cinfo, int val)
Definition: jcmarker.cpp:450
unsigned int JDIMENSION
Definition: jmorecfg.h:161
char JOCTET
Definition: jmorecfg.h:106
#define CSTATE_WRCOEFS
Definition: jpegint.h:25
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:457
jinit_marker_writer(j_compress_ptr cinfo)
Definition: jcmarker.cpp:659
jpeg_abort_compress(j_compress_ptr cinfo)
Definition: jcapimin.cpp:90
#define MEMZERO(target, size)
Definition: jinclude.h:60



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