Main MRPT website > C++ reference for MRPT 1.5.9
jdmainct.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  * In the current system design, the main buffer need never be a full-image
17  * buffer; any full-height buffers will be found inside the coefficient or
18  * postprocessing controllers. Nonetheless, the main controller is not
19  * trivial. Its responsibility is to provide context rows for upsampling/
20  * rescaling, and doing this in an efficient fashion is a bit tricky.
21  *
22  * Postprocessor input data is counted in "row groups". A row group
23  * is defined to be (v_samp_factor * DCT_scaled_size / min_DCT_scaled_size)
24  * sample rows of each component. (We require DCT_scaled_size values to be
25  * chosen such that these numbers are integers. In practice DCT_scaled_size
26  * values will likely be powers of two, so we actually have the stronger
27  * condition that DCT_scaled_size / min_DCT_scaled_size is an integer.)
28  * Upsampling will typically produce max_v_samp_factor pixel rows from each
29  * row group (times any additional scale factor that the upsampler is
30  * applying).
31  *
32  * The coefficient controller will deliver data to us one iMCU row at a time;
33  * each iMCU row contains v_samp_factor * DCT_scaled_size sample rows, or
34  * exactly min_DCT_scaled_size row groups. (This amount of data corresponds
35  * to one row of MCUs when the image is fully interleaved.) Note that the
36  * number of sample rows varies across components, but the number of row
37  * groups does not. Some garbage sample rows may be included in the last iMCU
38  * row at the bottom of the image.
39  *
40  * Depending on the vertical scaling algorithm used, the upsampler may need
41  * access to the sample row(s) above and below its current input row group.
42  * The upsampler is required to set need_context_rows TRUE at global selection
43  * time if so. When need_context_rows is FALSE, this controller can simply
44  * obtain one iMCU row at a time from the coefficient controller and dole it
45  * out as row groups to the postprocessor.
46  *
47  * When need_context_rows is TRUE, this controller guarantees that the buffer
48  * passed to postprocessing contains at least one row group's worth of samples
49  * above and below the row group(s) being processed. Note that the context
50  * rows "above" the first passed row group appear at negative row offsets in
51  * the passed buffer. At the top and bottom of the image, the required
52  * context rows are manufactured by duplicating the first or last real sample
53  * row; this avoids having special cases in the upsampling inner loops.
54  *
55  * The amount of context is fixed at one row group just because that's a
56  * convenient number for this controller to work with. The existing
57  * upsamplers really only need one sample row of context. An upsampler
58  * supporting arbitrary output rescaling might wish for more than one row
59  * group of context when shrinking the image; tough, we don't handle that.
60  * (This is justified by the assumption that downsizing will be handled mostly
61  * by adjusting the DCT_scaled_size values, so that the actual scale factor at
62  * the upsample step needn't be much less than one.)
63  *
64  * To provide the desired context, we have to retain the last two row groups
65  * of one iMCU row while reading in the next iMCU row. (The last row group
66  * can't be processed until we have another row group for its below-context,
67  * and so we have to save the next-to-last group too for its above-context.)
68  * We could do this most simply by copying data around in our buffer, but
69  * that'd be very slow. We can avoid copying any data by creating a rather
70  * strange pointer structure. Here's how it works. We allocate a workspace
71  * consisting of M+2 row groups (where M = min_DCT_scaled_size is the number
72  * of row groups per iMCU row). We create two sets of redundant pointers to
73  * the workspace. Labeling the physical row groups 0 to M+1, the synthesized
74  * pointer lists look like this:
75  * M+1 M-1
76  * master pointer --> 0 master pointer --> 0
77  * 1 1
78  * ... ...
79  * M-3 M-3
80  * M-2 M
81  * M-1 M+1
82  * M M-2
83  * M+1 M-1
84  * 0 0
85  * We read alternate iMCU rows using each master pointer; thus the last two
86  * row groups of the previous iMCU row remain un-overwritten in the workspace.
87  * The pointer lists are set up so that the required context rows appear to
88  * be adjacent to the proper places when we pass the pointer lists to the
89  * upsampler.
90  *
91  * The above pictures describe the normal state of the pointer lists.
92  * At top and bottom of the image, we diddle the pointer lists to duplicate
93  * the first or last sample row as necessary (this is cheaper than copying
94  * sample rows around).
95  *
96  * This scheme breaks down if M < 2, ie, min_DCT_scaled_size is 1. In that
97  * situation each iMCU row provides only one row group so the buffering logic
98  * must be different (eg, we must read two iMCU rows before we can emit the
99  * first row group). For now, we simply do not support providing context
100  * rows when min_DCT_scaled_size is 1. That combination seems unlikely to
101  * be worth providing --- if someone wants a 1/8th-size preview, they probably
102  * want it quick and dirty, so a context-free upsampler is sufficient.
103  */
104 
105 
106 /* Private buffer controller object */
107 
108 typedef struct {
109  struct jpeg_d_main_controller pub; /* public fields */
110 
111  /* Pointer to allocated workspace (M or M+2 row groups). */
113 
114  boolean buffer_full; /* Have we gotten an iMCU row from decoder? */
115  JDIMENSION rowgroup_ctr; /* counts row groups output to postprocessor */
116 
117  /* Remaining fields are only used in the context case. */
118 
119  /* These are the master pointers to the funny-order pointer lists. */
120  JSAMPIMAGE xbuffer[2]; /* pointers to weird pointer lists */
121 
122  int whichptr; /* indicates which pointer set is now in use */
123  int context_state; /* process_data state machine status */
124  JDIMENSION rowgroups_avail; /* row groups available to postprocessor */
125  JDIMENSION iMCU_row_ctr; /* counts iMCU rows to detect image top/bot */
127 
129 
130 /* context_state values: */
131 #define CTX_PREPARE_FOR_IMCU 0 /* need to prepare for MCU row */
132 #define CTX_PROCESS_IMCU 1 /* feeding iMCU to postprocessor */
133 #define CTX_POSTPONED_ROW 2 /* feeding postponed row group */
134 
135 
136 /* Forward declarations */
143 #ifdef QUANT_2PASS_SUPPORTED
147 #endif
148 
149 
150 LOCAL(void)
152 /* Allocate space for the funny pointer lists.
153  * This is done only once, not once per pass.
154  */
155 {
156  my_main_ptr main = (my_main_ptr) cinfo->main;
157  int ci, rgroup;
158  int M = cinfo->min_DCT_scaled_size;
160  JSAMPARRAY xbuf;
161 
162  /* Get top-level space for component array pointers.
163  * We alloc both arrays with one call to save a few cycles.
164  */
165  main->xbuffer[0] = (JSAMPIMAGE)
166  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
167  cinfo->num_components * 2 * SIZEOF(JSAMPARRAY));
168  main->xbuffer[1] = main->xbuffer[0] + cinfo->num_components;
169 
170  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
171  ci++, compptr++) {
173  cinfo->min_DCT_scaled_size; /* height of a row group of component */
174  /* Get space for pointer lists --- M+4 row groups in each list.
175  * We alloc both pointer lists with one call to save a few cycles.
176  */
177  xbuf = (JSAMPARRAY)
178  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
179  2 * (rgroup * (M + 4)) * SIZEOF(JSAMPROW));
180  xbuf += rgroup; /* want one row group at negative offsets */
181  main->xbuffer[0][ci] = xbuf;
182  xbuf += rgroup * (M + 4);
183  main->xbuffer[1][ci] = xbuf;
184  }
185 }
186 
187 
188 LOCAL(void)
190 /* Create the funny pointer lists discussed in the comments above.
191  * The actual workspace is already allocated (in main->buffer),
192  * and the space for the pointer lists is allocated too.
193  * This routine just fills in the curiously ordered lists.
194  * This will be repeated at the beginning of each pass.
195  */
196 {
197  my_main_ptr main = (my_main_ptr) cinfo->main;
198  int ci, i, rgroup;
199  int M = cinfo->min_DCT_scaled_size;
201  JSAMPARRAY buf, xbuf0, xbuf1;
202 
203  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
204  ci++, compptr++) {
206  cinfo->min_DCT_scaled_size; /* height of a row group of component */
207  xbuf0 = main->xbuffer[0][ci];
208  xbuf1 = main->xbuffer[1][ci];
209  /* First copy the workspace pointers as-is */
210  buf = main->buffer[ci];
211  for (i = 0; i < rgroup * (M + 2); i++) {
212  xbuf0[i] = xbuf1[i] = buf[i];
213  }
214  /* In the second list, put the last four row groups in swapped order */
215  for (i = 0; i < rgroup * 2; i++) {
216  xbuf1[rgroup*(M-2) + i] = buf[rgroup*M + i];
217  xbuf1[rgroup*M + i] = buf[rgroup*(M-2) + i];
218  }
219  /* The wraparound pointers at top and bottom will be filled later
220  * (see set_wraparound_pointers, below). Initially we want the "above"
221  * pointers to duplicate the first actual data line. This only needs
222  * to happen in xbuffer[0].
223  */
224  for (i = 0; i < rgroup; i++) {
225  xbuf0[i - rgroup] = xbuf0[0];
226  }
227  }
228 }
229 
230 
231 LOCAL(void)
233 /* Set up the "wraparound" pointers at top and bottom of the pointer lists.
234  * This changes the pointer list state from top-of-image to the normal state.
235  */
236 {
237  my_main_ptr main = (my_main_ptr) cinfo->main;
238  int ci, i, rgroup;
239  int M = cinfo->min_DCT_scaled_size;
241  JSAMPARRAY xbuf0, xbuf1;
242 
243  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
244  ci++, compptr++) {
246  cinfo->min_DCT_scaled_size; /* height of a row group of component */
247  xbuf0 = main->xbuffer[0][ci];
248  xbuf1 = main->xbuffer[1][ci];
249  for (i = 0; i < rgroup; i++) {
250  xbuf0[i - rgroup] = xbuf0[rgroup*(M+1) + i];
251  xbuf1[i - rgroup] = xbuf1[rgroup*(M+1) + i];
252  xbuf0[rgroup*(M+2) + i] = xbuf0[i];
253  xbuf1[rgroup*(M+2) + i] = xbuf1[i];
254  }
255  }
256 }
257 
258 
259 LOCAL(void)
261 /* Change the pointer lists to duplicate the last sample row at the bottom
262  * of the image. whichptr indicates which xbuffer holds the final iMCU row.
263  * Also sets rowgroups_avail to indicate number of nondummy row groups in row.
264  */
265 {
266  my_main_ptr main = (my_main_ptr) cinfo->main;
267  int ci, i, rgroup, iMCUheight, rows_left;
269  JSAMPARRAY xbuf;
270 
271  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
272  ci++, compptr++) {
273  /* Count sample rows in one iMCU row and in one row group */
274  iMCUheight = compptr->v_samp_factor * compptr->DCT_scaled_size;
275  rgroup = iMCUheight / cinfo->min_DCT_scaled_size;
276  /* Count nondummy sample rows remaining for this component */
277  rows_left = (int) (compptr->downsampled_height % (JDIMENSION) iMCUheight);
278  if (rows_left == 0) rows_left = iMCUheight;
279  /* Count nondummy row groups. Should get same answer for each component,
280  * so we need only do it once.
281  */
282  if (ci == 0) {
283  main->rowgroups_avail = (JDIMENSION) ((rows_left-1) / rgroup + 1);
284  }
285  /* Duplicate the last real sample row rgroup*2 times; this pads out the
286  * last partial rowgroup and ensures at least one full rowgroup of context.
287  */
288  xbuf = main->xbuffer[main->whichptr][ci];
289  for (i = 0; i < rgroup * 2; i++) {
290  xbuf[rows_left + i] = xbuf[rows_left-1];
291  }
292  }
293 }
294 
295 
296 /*
297  * Initialize for a processing pass.
298  */
299 
300 METHODDEF(void)
302 {
303  my_main_ptr main = (my_main_ptr) cinfo->main;
304 
305  switch (pass_mode) {
306  case JBUF_PASS_THRU:
307  if (cinfo->upsample->need_context_rows) {
308  main->pub.process_data = process_data_context_main;
309  make_funny_pointers(cinfo); /* Create the xbuffer[] lists */
310  main->whichptr = 0; /* Read first iMCU row into xbuffer[0] */
312  main->iMCU_row_ctr = 0;
313  } else {
314  /* Simple case with no context needed */
315  main->pub.process_data = process_data_simple_main;
316  }
317  main->buffer_full = FALSE; /* Mark buffer empty */
318  main->rowgroup_ctr = 0;
319  break;
320 #ifdef QUANT_2PASS_SUPPORTED
321  case JBUF_CRANK_DEST:
322  /* For last pass of 2-pass quantization, just crank the postprocessor */
323  main->pub.process_data = process_data_crank_post;
324  break;
325 #endif
326  default:
327  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
328  break;
329  }
330 }
331 
332 
333 /*
334  * Process some data.
335  * This handles the simple case where no context is required.
336  */
337 
338 METHODDEF(void)
342 {
343  my_main_ptr main = (my_main_ptr) cinfo->main;
344  JDIMENSION rowgroups_avail;
345 
346  /* Read input data if we haven't filled the main buffer yet */
347  if (! main->buffer_full) {
348  if (! (*cinfo->coef->decompress_data) (cinfo, main->buffer))
349  return; /* suspension forced, can do nothing more */
350  main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
351  }
352 
353  /* There are always min_DCT_scaled_size row groups in an iMCU row. */
354  rowgroups_avail = (JDIMENSION) cinfo->min_DCT_scaled_size;
355  /* Note: at the bottom of the image, we may pass extra garbage row groups
356  * to the postprocessor. The postprocessor has to check for bottom
357  * of image anyway (at row resolution), so no point in us doing it too.
358  */
359 
360  /* Feed the postprocessor */
361  (*cinfo->post->post_process_data) (cinfo, main->buffer,
362  &main->rowgroup_ctr, rowgroups_avail,
364 
365  /* Has postprocessor consumed all the data yet? If so, mark buffer empty */
366  if (main->rowgroup_ctr >= rowgroups_avail) {
367  main->buffer_full = FALSE;
368  main->rowgroup_ctr = 0;
369  }
370 }
371 
372 
373 /*
374  * Process some data.
375  * This handles the case where context rows must be provided.
376  */
377 
378 METHODDEF(void)
382 {
383  my_main_ptr main = (my_main_ptr) cinfo->main;
384 
385  /* Read input data if we haven't filled the main buffer yet */
386  if (! main->buffer_full) {
387  if (! (*cinfo->coef->decompress_data) (cinfo,
388  main->xbuffer[main->whichptr]))
389  return; /* suspension forced, can do nothing more */
390  main->buffer_full = TRUE; /* OK, we have an iMCU row to work with */
391  main->iMCU_row_ctr++; /* count rows received */
392  }
393 
394  /* Postprocessor typically will not swallow all the input data it is handed
395  * in one call (due to filling the output buffer first). Must be prepared
396  * to exit and restart. This switch lets us keep track of how far we got.
397  * Note that each case falls through to the next on successful completion.
398  */
399  switch (main->context_state) {
400  case CTX_POSTPONED_ROW:
401  /* Call postprocessor using previously set pointers for postponed row */
402  (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
403  &main->rowgroup_ctr, main->rowgroups_avail,
405  if (main->rowgroup_ctr < main->rowgroups_avail)
406  return; /* Need to suspend */
408  if (*out_row_ctr >= out_rows_avail)
409  return; /* Postprocessor exactly filled output buf */
410  /*FALLTHROUGH*/
412  /* Prepare to process first M-1 row groups of this iMCU row */
413  main->rowgroup_ctr = 0;
414  main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size - 1);
415  /* Check for bottom of image: if so, tweak pointers to "duplicate"
416  * the last sample row, and adjust rowgroups_avail to ignore padding rows.
417  */
418  if (main->iMCU_row_ctr == cinfo->total_iMCU_rows)
419  set_bottom_pointers(cinfo);
421  /*FALLTHROUGH*/
422  case CTX_PROCESS_IMCU:
423  /* Call postprocessor using previously set pointers */
424  (*cinfo->post->post_process_data) (cinfo, main->xbuffer[main->whichptr],
425  &main->rowgroup_ctr, main->rowgroups_avail,
427  if (main->rowgroup_ctr < main->rowgroups_avail)
428  return; /* Need to suspend */
429  /* After the first iMCU, change wraparound pointers to normal state */
430  if (main->iMCU_row_ctr == 1)
432  /* Prepare to load new iMCU row using other xbuffer list */
433  main->whichptr ^= 1; /* 0=>1 or 1=>0 */
434  main->buffer_full = FALSE;
435  /* Still need to process last row group of this iMCU row, */
436  /* which is saved at index M+1 of the other xbuffer */
437  main->rowgroup_ctr = (JDIMENSION) (cinfo->min_DCT_scaled_size + 1);
438  main->rowgroups_avail = (JDIMENSION) (cinfo->min_DCT_scaled_size + 2);
440  }
441 }
442 
443 
444 /*
445  * Process some data.
446  * Final pass of two-pass quantization: just call the postprocessor.
447  * Source data will be the postprocessor controller's internal buffer.
448  */
449 
450 #ifdef QUANT_2PASS_SUPPORTED
451 
452 METHODDEF(void)
456 {
457  (*cinfo->post->post_process_data) (cinfo, (JSAMPIMAGE) NULL,
458  (JDIMENSION *) NULL, (JDIMENSION) 0,
460 }
461 
462 #endif /* QUANT_2PASS_SUPPORTED */
463 
464 
465 /*
466  * Initialize main buffer controller.
467  */
468 
469 GLOBAL(void)
471 {
472  my_main_ptr main;
473  int ci, rgroup, ngroups;
475 
476  main = (my_main_ptr)
477  (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
479  cinfo->main = (struct jpeg_d_main_controller *) main;
480  main->pub.start_pass = start_pass_main;
481 
482  if (need_full_buffer) /* shouldn't happen */
483  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
484 
485  /* Allocate the workspace.
486  * ngroups is the number of row groups we need.
487  */
488  if (cinfo->upsample->need_context_rows) {
489  if (cinfo->min_DCT_scaled_size < 2) /* unsupported, see comments above */
490  ERREXIT(cinfo, JERR_NOTIMPL);
491  alloc_funny_pointers(cinfo); /* Alloc space for xbuffer[] lists */
492  ngroups = cinfo->min_DCT_scaled_size + 2;
493  } else {
494  ngroups = cinfo->min_DCT_scaled_size;
495  }
496 
497  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
498  ci++, compptr++) {
500  cinfo->min_DCT_scaled_size; /* height of a row group of component */
501  main->buffer[ci] = (*cinfo->mem->alloc_sarray)
502  ((j_common_ptr) cinfo, JPOOL_IMAGE,
504  (JDIMENSION) (rgroup * ngroups));
505  }
506 }
process_data_simple_main(j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdmainct.cpp:339
struct jpeg_c_main_controller pub
Definition: jcmainct.cpp:26
process_data_crank_post(j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdmainct.cpp:453
METHODDEF(void) process_data_simple_main JPP((j_decompress_ptr cinfo
GLuint buffer
Definition: glext.h:3775
JDIMENSION rowgroups_avail
Definition: jdmainct.cpp:124
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define MAX_COMPONENTS
Definition: jmorecfg.h:32
start_pass_main(j_decompress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jdmainct.cpp:301
process_data_context_main(j_decompress_ptr cinfo, JSAMPARRAY output_buf, JDIMENSION *out_row_ctr, JDIMENSION out_rows_avail)
Definition: jdmainct.cpp:379
JSAMPARRAY JDIMENSION * out_row_ctr
Definition: jdmainct.cpp:138
#define ERREXIT(cinfo, code)
Definition: jerror.h:199
#define SIZEOF(object)
Definition: jinclude.h:73
boolean need_full_buffer
Definition: jpegint.h:335
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:63
JDIMENSION rowgroup_ctr
Definition: jcmainct.cpp:29
jpeg_component_info * compptr
Definition: jdct.h:97
JDIMENSION width_in_blocks
Definition: mrpt_jpeglib.h:136
my_main_controller * my_main_ptr
Definition: jdmainct.cpp:128
set_bottom_pointers(j_decompress_ptr cinfo)
Definition: jdmainct.cpp:260
JSAMPARRAY buffer[MAX_COMPONENTS]
Definition: jcmainct.cpp:37
#define CTX_PROCESS_IMCU
Definition: jdmainct.cpp:132
#define FALSE
Definition: jmorecfg.h:227
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:746
#define LOCAL(type)
Definition: jmorecfg.h:183
alloc_funny_pointers(j_decompress_ptr cinfo)
Definition: jdmainct.cpp:151
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:64
jinit_d_main_controller(j_decompress_ptr cinfo, boolean need_full_buffer)
Definition: jdmainct.cpp:470
#define JPP(arglist)
Definition: mrpt_jpeglib.h:815
#define TRUE
Definition: jmorecfg.h:230
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:65
make_funny_pointers(j_decompress_ptr cinfo)
Definition: jdmainct.cpp:189
#define GLOBAL(type)
Definition: jmorecfg.h:185
JSAMPIMAGE xbuffer[2]
Definition: jdmainct.cpp:120
JDIMENSION iMCU_row_ctr
Definition: jdmainct.cpp:125
JSAMPARRAY output_buf
Definition: jdmainct.cpp:138
J_BUF_MODE
Definition: jpegint.h:13
#define CTX_PREPARE_FOR_IMCU
Definition: jdmainct.cpp:131
unsigned int JDIMENSION
Definition: jmorecfg.h:168
#define CTX_POSTPONED_ROW
Definition: jdmainct.cpp:133
set_wraparound_pointers(j_decompress_ptr cinfo)
Definition: jdmainct.cpp:232
JDIMENSION downsampled_height
Definition: mrpt_jpeglib.h:152
JSAMPARRAY JDIMENSION JDIMENSION out_rows_avail
Definition: jdmainct.cpp:138



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020