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



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019