Main MRPT website > C++ reference for MRPT 1.9.9
jdpostct.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 #include <mrpt/utils/mrpt_macros.h>
14 
15 /* Private buffer controller object */
16 
17 typedef struct
18 {
19  struct jpeg_d_post_controller pub; /* public fields */
20 
21  /* Color quantization source buffer: this holds output data from
22  * the upsample/color conversion step to be passed to the quantizer.
23  * For two-pass color quantization, we need a full-image buffer;
24  * for one-pass operation, a strip buffer is sufficient.
25  */
26  jvirt_sarray_ptr whole_image; /* virtual array, or nullptr if one-pass */
27  JSAMPARRAY buffer; /* strip buffer, or current strip of virtual */
28  JDIMENSION strip_height; /* buffer size in rows */
29  /* for two-pass mode only: */
30  JDIMENSION starting_row; /* row # of first row in current strip */
31  JDIMENSION next_row; /* index of next row to fill/empty in strip */
33 
35 
36 /* Forward declarations */
37 METHODDEF(void)
39  (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
40  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
41  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail));
42 #ifdef QUANT_2PASS_SUPPORTED
43 METHODDEF(void)
45  (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
46  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
47  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail));
48 METHODDEF(void)
50  (j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
51  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
52  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail));
53 #endif
54 
55 /*
56  * Initialize for a processing pass.
57  */
58 
59 METHODDEF(void)
61 {
62  my_post_ptr post = (my_post_ptr)cinfo->post;
63 
64  switch (pass_mode)
65  {
66  case JBUF_PASS_THRU:
67  if (cinfo->quantize_colors)
68  {
69  /* Single-pass processing with color quantization. */
70  post->pub.post_process_data = post_process_1pass;
71  /* We could be doing buffered-image output before starting a
72  * 2-pass
73  * color quantization; in that case, jinit_d_post_controller did
74  * not
75  * allocate a strip buffer. Use the virtual-array buffer as
76  * workspace.
77  */
78  if (post->buffer == nullptr)
79  {
80  post->buffer = (*cinfo->mem->access_virt_sarray)(
81  (j_common_ptr)cinfo, post->whole_image, (JDIMENSION)0,
82  post->strip_height, TRUE);
83  }
84  }
85  else
86  {
87  /* For single-pass processing without color quantization,
88  * I have no work to do; just call the upsampler directly.
89  */
90  post->pub.post_process_data = cinfo->upsample->upsample;
91  }
92  break;
93 #ifdef QUANT_2PASS_SUPPORTED
94  case JBUF_SAVE_AND_PASS:
95  /* First pass of 2-pass quantization */
96  if (post->whole_image == nullptr)
97  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
98  post->pub.post_process_data = post_process_prepass;
99  break;
100  case JBUF_CRANK_DEST:
101  /* Second pass of 2-pass quantization */
102  if (post->whole_image == nullptr)
103  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
104  post->pub.post_process_data = post_process_2pass;
105  break;
106 #endif /* QUANT_2PASS_SUPPORTED */
107  default:
108  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
109  break;
110  }
111  post->starting_row = post->next_row = 0;
112 }
113 
114 /*
115  * Process some data in the one-pass (strip buffer) case.
116  * This is used for color precision reduction as well as one-pass quantization.
117  */
118 
119 METHODDEF(void)
121  j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
122  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
123  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail)
124 {
125  my_post_ptr post = (my_post_ptr)cinfo->post;
126  JDIMENSION num_rows, max_rows;
127 
128  /* Fill the buffer, but not more than what we can dump out in one go. */
129  /* Note we rely on the upsampler to detect bottom of image. */
130  max_rows = out_rows_avail - *out_row_ctr;
131  if (max_rows > post->strip_height) max_rows = post->strip_height;
132  num_rows = 0;
133  (*cinfo->upsample->upsample)(
134  cinfo, input_buf, in_row_group_ctr, in_row_groups_avail, post->buffer,
135  &num_rows, max_rows);
136  /* Quantize and emit data. */
137  (*cinfo->cquantize->color_quantize)(
138  cinfo, post->buffer, output_buf + *out_row_ctr, (int)num_rows);
139  *out_row_ctr += num_rows;
140 }
141 
142 #ifdef QUANT_2PASS_SUPPORTED
143 
144 /*
145  * Process some data in the first pass of 2-pass quantization.
146  */
147 
148 METHODDEF(void)
150  j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
151  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
152  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail)
153 {
155  MRPT_UNUSED_PARAM(out_rows_avail);
156  my_post_ptr post = (my_post_ptr)cinfo->post;
157  JDIMENSION old_next_row, num_rows;
158 
159  /* Reposition virtual buffer if at start of strip. */
160  if (post->next_row == 0)
161  {
162  post->buffer = (*cinfo->mem->access_virt_sarray)(
163  (j_common_ptr)cinfo, post->whole_image, post->starting_row,
164  post->strip_height, TRUE);
165  }
166 
167  /* Upsample some data (up to a strip height's worth). */
168  old_next_row = post->next_row;
169  (*cinfo->upsample->upsample)(
170  cinfo, input_buf, in_row_group_ctr, in_row_groups_avail, post->buffer,
171  &post->next_row, post->strip_height);
172 
173  /* Allow quantizer to scan new data. No data is emitted, */
174  /* but we advance out_row_ctr so outer loop can tell when we're done. */
175  if (post->next_row > old_next_row)
176  {
177  num_rows = post->next_row - old_next_row;
178  (*cinfo->cquantize->color_quantize)(
179  cinfo, post->buffer + old_next_row, (JSAMPARRAY) nullptr,
180  (int)num_rows);
181  *out_row_ctr += num_rows;
182  }
183 
184  /* Advance if we filled the strip. */
185  if (post->next_row >= post->strip_height)
186  {
187  post->starting_row += post->strip_height;
188  post->next_row = 0;
189  }
190 }
191 
192 /*
193  * Process some data in the second pass of 2-pass quantization.
194  */
195 
196 METHODDEF(void)
198  j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION* in_row_group_ctr,
199  JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf,
200  JDIMENSION* out_row_ctr, JDIMENSION out_rows_avail)
201 {
202  MRPT_UNUSED_PARAM(input_buf);
203  MRPT_UNUSED_PARAM(in_row_group_ctr);
204  MRPT_UNUSED_PARAM(in_row_groups_avail);
205  my_post_ptr post = (my_post_ptr)cinfo->post;
206  JDIMENSION num_rows, max_rows;
207 
208  /* Reposition virtual buffer if at start of strip. */
209  if (post->next_row == 0)
210  {
211  post->buffer = (*cinfo->mem->access_virt_sarray)(
212  (j_common_ptr)cinfo, post->whole_image, post->starting_row,
213  post->strip_height, FALSE);
214  }
215 
216  /* Determine number of rows to emit. */
217  num_rows = post->strip_height - post->next_row; /* available in strip */
218  max_rows = out_rows_avail - *out_row_ctr; /* available in output area */
219  if (num_rows > max_rows) num_rows = max_rows;
220  /* We have to check bottom of image here, can't depend on upsampler. */
221  max_rows = cinfo->output_height - post->starting_row;
222  if (num_rows > max_rows) num_rows = max_rows;
223 
224  /* Quantize and emit data. */
225  (*cinfo->cquantize->color_quantize)(
226  cinfo, post->buffer + post->next_row, output_buf + *out_row_ctr,
227  (int)num_rows);
228  *out_row_ctr += num_rows;
229 
230  /* Advance if we filled the strip. */
231  post->next_row += num_rows;
232  if (post->next_row >= post->strip_height)
233  {
234  post->starting_row += post->strip_height;
235  post->next_row = 0;
236  }
237 }
238 
239 #endif /* QUANT_2PASS_SUPPORTED */
240 
241 /*
242  * Initialize postprocessing controller.
243  */
244 
245 GLOBAL(void)
246 jinit_d_post_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
247 {
248  my_post_ptr post;
249 
250  post = (my_post_ptr)(*cinfo->mem->alloc_small)(
252  cinfo->post = (struct jpeg_d_post_controller*)post;
253  post->pub.start_pass = start_pass_dpost;
254  post->whole_image = nullptr; /* flag for no virtual arrays */
255  post->buffer = nullptr; /* flag for no strip buffer */
256 
257  /* Create the quantization buffer, if needed */
258  if (cinfo->quantize_colors)
259  {
260  /* The buffer strip height is max_v_samp_factor, which is typically
261  * an efficient number of rows for upsampling to return.
262  * (In the presence of output rescaling, we might want to be smarter?)
263  */
264  post->strip_height = (JDIMENSION)cinfo->max_v_samp_factor;
265  if (need_full_buffer)
266  {
267 /* Two-pass color quantization: need full-image storage. */
268 /* We round up the number of rows to a multiple of the strip height. */
269 #ifdef QUANT_2PASS_SUPPORTED
270  post->whole_image = (*cinfo->mem->request_virt_sarray)(
271  (j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
272  cinfo->output_width * cinfo->out_color_components,
274  (long)cinfo->output_height, (long)post->strip_height),
275  post->strip_height);
276 #else
277  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
278 #endif /* QUANT_2PASS_SUPPORTED */
279  }
280  else
281  {
282  /* One-pass color quantization: just make a strip buffer. */
283  post->buffer = (*cinfo->mem->alloc_sarray)(
284  (j_common_ptr)cinfo, JPOOL_IMAGE,
285  cinfo->output_width * cinfo->out_color_components,
286  post->strip_height);
287  }
288  }
289 }
jinit_d_post_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
Definition: jdpostct.cpp:246
post_process_1pass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdpostct.cpp:120
jround_up(long a, long b)
Definition: jutils.cpp:67
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define ERREXIT(cinfo, code)
Definition: jerror.h:451
#define SIZEOF(object)
Definition: jinclude.h:74
Definition: jpegint.h:187
post_process_prepass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdpostct.cpp:149
JDIMENSION strip_height
Definition: jdpostct.cpp:28
struct jpeg_d_post_controller pub
Definition: jdpostct.cpp:19
my_post_controller * my_post_ptr
Definition: jdpostct.cpp:34
start_pass_dpost(j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jdpostct.cpp:60
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define FALSE
Definition: jmorecfg.h:216
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
Definition: jidctflt.cpp:36
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:61
#define TRUE
Definition: jmorecfg.h:219
JDIMENSION next_row
Definition: jdpostct.cpp:31
post_process_2pass(j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdpostct.cpp:197
JDIMENSION starting_row
Definition: jdpostct.cpp:30
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:62
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define METHODDEF(type)
Definition: jmorecfg.h:173
JSAMPARRAY buffer
Definition: jdpostct.cpp:27
post_process_1pass JPP((j_decompress_ptr cinfo, JSAMPIMAGE input_buf, JDIMENSION *in_row_group_ctr, JDIMENSION in_row_groups_avail, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail))
J_BUF_MODE
Definition: jpegint.h:12
Definition: jdpostct.cpp:17
unsigned int JDIMENSION
Definition: jmorecfg.h:161
jvirt_sarray_ptr whole_image
Definition: jdpostct.cpp:26



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