Main MRPT website > C++ reference for MRPT 1.9.9
jccoefct.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 /* We use a full-image coefficient buffer when doing Huffman optimization,
15  * and also for writing multiple-scan JPEG files. In all cases, the DCT
16  * step is run during the first pass, and subsequent passes need only read
17  * the buffered coefficients.
18  */
19 #ifdef ENTROPY_OPT_SUPPORTED
20 #define FULL_COEF_BUFFER_SUPPORTED
21 #else
22 #ifdef C_MULTISCAN_FILES_SUPPORTED
23 #define FULL_COEF_BUFFER_SUPPORTED
24 #endif
25 #endif
26 
27 /* Private buffer controller object */
28 
29 typedef struct
30 {
31  struct jpeg_c_coef_controller pub; /* public fields */
32 
33  JDIMENSION iMCU_row_num; /* iMCU row # within image */
34  JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
35  int MCU_vert_offset; /* counts MCU rows within iMCU row */
36  int MCU_rows_per_iMCU_row; /* number of such rows needed */
37 
38  /* For single-pass compression, it's sufficient to buffer just one MCU
39  * (although this may prove a bit slow in practice). We allocate a
40  * workspace of C_MAX_BLOCKS_IN_MCU coefficient blocks, and reuse it for
41  * each
42  * MCU constructed and sent. (On 80x86, the workspace is FAR even though
43  * it's not really very big; this is to keep the module interfaces unchanged
44  * when a large coefficient buffer is necessary.)
45  * In multi-pass modes, this array points to the current MCU's blocks
46  * within the virtual arrays.
47  */
49 
50  /* In multi-pass modes, we need a virtual block array for each component. */
53 
55 
56 /* Forward declarations */
57 METHODDEF(boolean)
58 compress_data JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
59 #ifdef FULL_COEF_BUFFER_SUPPORTED
60 METHODDEF(boolean)
62 METHODDEF(boolean)
63 compress_output JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf));
64 #endif
65 
66 LOCAL(void)
68 /* Reset within-iMCU-row counters for a new row */
69 {
70  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
71 
72  /* In an interleaved scan, an MCU row is the same as an iMCU row.
73  * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
74  * But at the bottom of the image, process only what's left.
75  */
76  if (cinfo->comps_in_scan > 1)
77  {
78  coef->MCU_rows_per_iMCU_row = 1;
79  }
80  else
81  {
82  if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
83  coef->MCU_rows_per_iMCU_row =
84  cinfo->cur_comp_info[0]->v_samp_factor;
85  else
86  coef->MCU_rows_per_iMCU_row =
87  cinfo->cur_comp_info[0]->last_row_height;
88  }
89 
90  coef->mcu_ctr = 0;
91  coef->MCU_vert_offset = 0;
92 }
93 
94 /*
95  * Initialize for a processing pass.
96  */
97 
98 METHODDEF(void)
100 {
101  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
102 
103  coef->iMCU_row_num = 0;
104  start_iMCU_row(cinfo);
105 
106  switch (pass_mode)
107  {
108  case JBUF_PASS_THRU:
109  if (coef->whole_image[0] != nullptr)
110  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
111  coef->pub.compress_data = compress_data;
112  break;
113 #ifdef FULL_COEF_BUFFER_SUPPORTED
114  case JBUF_SAVE_AND_PASS:
115  if (coef->whole_image[0] == nullptr)
116  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
117  coef->pub.compress_data = compress_first_pass;
118  break;
119  case JBUF_CRANK_DEST:
120  if (coef->whole_image[0] == nullptr)
121  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
122  coef->pub.compress_data = compress_output;
123  break;
124 #endif
125  default:
126  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
127  break;
128  }
129 }
130 
131 /*
132  * Process some data in the single-pass case.
133  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
134  * per call, ie, v_samp_factor block rows for each component in the image.
135  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
136  *
137  * NB: input_buf contains a plane for each component in image,
138  * which we index according to the component's SOF position.
139  */
140 
141 METHODDEF(boolean)
143 {
144  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
145  JDIMENSION MCU_col_num; /* index of current MCU within row */
146  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
147  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
148  int blkn, bi, ci, yindex, yoffset, blockcnt;
149  JDIMENSION ypos, xpos;
151 
152  /* Loop to write as much as one whole iMCU row */
153  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
154  yoffset++)
155  {
156  for (MCU_col_num = coef->mcu_ctr; MCU_col_num <= last_MCU_col;
157  MCU_col_num++)
158  {
159  /* Determine where data comes from in input_buf and do the DCT
160  * thing.
161  * Each call on forward_DCT processes a horizontal row of DCT blocks
162  * as wide as an MCU; we rely on having allocated the MCU_buffer[]
163  * blocks
164  * sequentially. Dummy blocks at the right or bottom edge are
165  * filled in
166  * specially. The data in them does not matter for image
167  * reconstruction,
168  * so we fill them with values that will encode to the smallest
169  * amount of
170  * data, viz: all zeroes in the AC entries, DC entries equal to
171  * previous
172  * block's DC value. (Thanks to Thomas Kinsman for this idea.)
173  */
174  blkn = 0;
175  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
176  {
177  compptr = cinfo->cur_comp_info[ci];
178  blockcnt = (MCU_col_num < last_MCU_col)
179  ? compptr->MCU_width
181  xpos = MCU_col_num * compptr->MCU_sample_width;
182  ypos =
183  yoffset * DCTSIZE; /* ypos == (yoffset+yindex) * DCTSIZE */
184  for (yindex = 0; yindex < compptr->MCU_height; yindex++)
185  {
186  if (coef->iMCU_row_num < last_iMCU_row ||
187  yoffset + yindex < compptr->last_row_height)
188  {
189  (*cinfo->fdct->forward_DCT)(
190  cinfo, compptr, input_buf[compptr->component_index],
191  coef->MCU_buffer[blkn], ypos, xpos,
192  (JDIMENSION)blockcnt);
193  if (blockcnt < compptr->MCU_width)
194  {
195  /* Create some dummy blocks at the right edge of the
196  * image. */
197  jzero_far(
198  (void FAR*)coef->MCU_buffer[blkn + blockcnt],
199  (compptr->MCU_width - blockcnt) *
200  SIZEOF(JBLOCK));
201  for (bi = blockcnt; bi < compptr->MCU_width; bi++)
202  {
203  coef->MCU_buffer[blkn + bi][0][0] =
204  coef->MCU_buffer[blkn + bi - 1][0][0];
205  }
206  }
207  }
208  else
209  {
210  /* Create a row of dummy blocks at the bottom of the
211  * image. */
212  jzero_far(
213  (void FAR*)coef->MCU_buffer[blkn],
215  for (bi = 0; bi < compptr->MCU_width; bi++)
216  {
217  coef->MCU_buffer[blkn + bi][0][0] =
218  coef->MCU_buffer[blkn - 1][0][0];
219  }
220  }
221  blkn += compptr->MCU_width;
222  ypos += DCTSIZE;
223  }
224  }
225  /* Try to write the MCU. In event of a suspension failure, we will
226  * re-DCT the MCU on restart (a bit inefficient, could be fixed...)
227  */
228  if (!(*cinfo->entropy->encode_mcu)(cinfo, coef->MCU_buffer))
229  {
230  /* Suspension forced; update state counters and exit */
231  coef->MCU_vert_offset = yoffset;
232  coef->mcu_ctr = MCU_col_num;
233  return FALSE;
234  }
235  }
236  /* Completed an MCU row, but perhaps not an iMCU row */
237  coef->mcu_ctr = 0;
238  }
239  /* Completed the iMCU row, advance counters for next one */
240  coef->iMCU_row_num++;
241  start_iMCU_row(cinfo);
242  return TRUE;
243 }
244 
245 #ifdef FULL_COEF_BUFFER_SUPPORTED
246 
247 /*
248  * Process some data in the first pass of a multi-pass case.
249  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
250  * per call, ie, v_samp_factor block rows for each component in the image.
251  * This amount of data is read from the source buffer, DCT'd and quantized,
252  * and saved into the virtual arrays. We also generate suitable dummy blocks
253  * as needed at the right and lower edges. (The dummy blocks are constructed
254  * in the virtual arrays, which have been padded appropriately.) This makes
255  * it possible for subsequent passes not to worry about real vs. dummy blocks.
256  *
257  * We must also emit the data to the entropy encoder. This is conveniently
258  * done by calling compress_output() after we've loaded the current strip
259  * of the virtual arrays.
260  *
261  * NB: input_buf contains a plane for each component in image. All
262  * components are DCT'd and loaded into the virtual arrays in this pass.
263  * However, it may be that only a subset of the components are emitted to
264  * the entropy encoder during this first pass; be careful about looking
265  * at the scan-dependent variables (MCU dimensions, etc).
266  */
267 
268 METHODDEF(boolean)
270 {
271  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
272  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
273  JDIMENSION blocks_across, MCUs_across, MCUindex;
274  int bi, ci, h_samp_factor, block_row, block_rows, ndummy;
275  JCOEF lastDC;
278  JBLOCKROW thisblockrow, lastblockrow;
279 
280  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
281  ci++, compptr++)
282  {
283  /* Align the virtual buffer for this component. */
284  buffer = (*cinfo->mem->access_virt_barray)(
285  (j_common_ptr)cinfo, coef->whole_image[ci],
288  /* Count non-dummy DCT block rows in this iMCU row. */
289  if (coef->iMCU_row_num < last_iMCU_row)
290  block_rows = compptr->v_samp_factor;
291  else
292  {
293  /* NB: can't use last_row_height here, since may not be set! */
294  block_rows =
296  if (block_rows == 0) block_rows = compptr->v_samp_factor;
297  }
298  blocks_across = compptr->width_in_blocks;
299  h_samp_factor = compptr->h_samp_factor;
300  /* Count number of dummy blocks to be added at the right margin. */
301  ndummy = (int)(blocks_across % h_samp_factor);
302  if (ndummy > 0) ndummy = h_samp_factor - ndummy;
303  /* Perform DCT for all non-dummy blocks in this iMCU row. Each call
304  * on forward_DCT processes a complete horizontal row of DCT blocks.
305  */
306  for (block_row = 0; block_row < block_rows; block_row++)
307  {
308  thisblockrow = buffer[block_row];
309  (*cinfo->fdct->forward_DCT)(
310  cinfo, compptr, input_buf[ci], thisblockrow,
311  (JDIMENSION)(block_row * DCTSIZE), (JDIMENSION)0,
312  blocks_across);
313  if (ndummy > 0)
314  {
315  /* Create dummy blocks at the right edge of the image. */
316  thisblockrow += blocks_across; /* => first dummy block */
317  jzero_far((void FAR*)thisblockrow, ndummy * SIZEOF(JBLOCK));
318  lastDC = thisblockrow[-1][0];
319  for (bi = 0; bi < ndummy; bi++)
320  {
321  thisblockrow[bi][0] = lastDC;
322  }
323  }
324  }
325  /* If at end of image, create dummy block rows as needed.
326  * The tricky part here is that within each MCU, we want the DC values
327  * of the dummy blocks to match the last real block's DC value.
328  * This squeezes a few more bytes out of the resulting file...
329  */
330  if (coef->iMCU_row_num == last_iMCU_row)
331  {
332  blocks_across += ndummy; /* include lower right corner */
333  MCUs_across = blocks_across / h_samp_factor;
334  for (block_row = block_rows; block_row < compptr->v_samp_factor;
335  block_row++)
336  {
337  thisblockrow = buffer[block_row];
338  lastblockrow = buffer[block_row - 1];
339  jzero_far(
340  (void FAR*)thisblockrow,
341  (size_t)(blocks_across * SIZEOF(JBLOCK)));
342  for (MCUindex = 0; MCUindex < MCUs_across; MCUindex++)
343  {
344  lastDC = lastblockrow[h_samp_factor - 1][0];
345  for (bi = 0; bi < h_samp_factor; bi++)
346  {
347  thisblockrow[bi][0] = lastDC;
348  }
349  thisblockrow +=
350  h_samp_factor; /* advance to next MCU in row */
351  lastblockrow += h_samp_factor;
352  }
353  }
354  }
355  }
356  /* NB: compress_output will increment iMCU_row_num if successful.
357  * A suspension return will result in redoing all the work above next time.
358  */
359 
360  /* Emit data to the entropy encoder, sharing code with subsequent passes */
361  return compress_output(cinfo, input_buf);
362 }
363 
364 /*
365  * Process some data in subsequent passes of a multi-pass case.
366  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
367  * per call, ie, v_samp_factor block rows for each component in the scan.
368  * The data is obtained from the virtual arrays and fed to the entropy coder.
369  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
370  *
371  * NB: input_buf is ignored; it is likely to be a nullptr pointer.
372  */
373 
374 METHODDEF(boolean)
376 {
377  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
378  JDIMENSION MCU_col_num; /* index of current MCU within row */
379  int blkn, ci, xindex, yindex, yoffset;
380  JDIMENSION start_col;
382  JBLOCKROW buffer_ptr;
384 
385  /* Align the virtual buffers for the components used in this scan.
386  * NB: during first pass, this is safe only because the buffers will
387  * already be aligned properly, so jmemmgr.c won't need to do any I/O.
388  */
389  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
390  {
391  compptr = cinfo->cur_comp_info[ci];
392  buffer[ci] = (*cinfo->mem->access_virt_barray)(
396  }
397 
398  /* Loop to process one whole iMCU row */
399  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
400  yoffset++)
401  {
402  for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
403  MCU_col_num++)
404  {
405  /* Construct list of pointers to DCT blocks belonging to this MCU */
406  blkn = 0; /* index of current DCT block within MCU */
407  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
408  {
409  compptr = cinfo->cur_comp_info[ci];
410  start_col = MCU_col_num * compptr->MCU_width;
411  for (yindex = 0; yindex < compptr->MCU_height; yindex++)
412  {
413  buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
414  for (xindex = 0; xindex < compptr->MCU_width; xindex++)
415  {
416  coef->MCU_buffer[blkn++] = buffer_ptr++;
417  }
418  }
419  }
420  /* Try to write the MCU. */
421  if (!(*cinfo->entropy->encode_mcu)(cinfo, coef->MCU_buffer))
422  {
423  /* Suspension forced; update state counters and exit */
424  coef->MCU_vert_offset = yoffset;
425  coef->mcu_ctr = MCU_col_num;
426  return FALSE;
427  }
428  }
429  /* Completed an MCU row, but perhaps not an iMCU row */
430  coef->mcu_ctr = 0;
431  }
432  /* Completed the iMCU row, advance counters for next one */
433  coef->iMCU_row_num++;
434  start_iMCU_row(cinfo);
435  return TRUE;
436 }
437 
438 #endif /* FULL_COEF_BUFFER_SUPPORTED */
439 
440 /*
441  * Initialize coefficient buffer controller.
442  */
443 
444 GLOBAL(void)
445 jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
446 {
447  my_coef_ptr coef;
448 
449  coef = (my_coef_ptr)(*cinfo->mem->alloc_small)(
451  cinfo->coef = (struct jpeg_c_coef_controller*)coef;
452  coef->pub.start_pass = start_pass_coef;
453 
454  /* Create the coefficient buffer. */
455  if (need_full_buffer)
456  {
457 #ifdef FULL_COEF_BUFFER_SUPPORTED
458  /* Allocate a full-image virtual array for each component, */
459  /* padded to a multiple of samp_factor DCT blocks in each direction. */
460  int ci;
462 
463  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
464  ci++, compptr++)
465  {
466  coef->whole_image[ci] = (*cinfo->mem->request_virt_barray)(
467  (j_common_ptr)cinfo, JPOOL_IMAGE, FALSE,
469  (long)compptr->width_in_blocks,
470  (long)compptr->h_samp_factor),
472  (long)compptr->height_in_blocks,
473  (long)compptr->v_samp_factor),
475  }
476 #else
477  ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
478 #endif
479  }
480  else
481  {
482  /* We only need a single-MCU buffer. */
484  int i;
485 
486  buffer = (JBLOCKROW)(*cinfo->mem->alloc_large)(
487  (j_common_ptr)cinfo, JPOOL_IMAGE,
489  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++)
490  {
491  coef->MCU_buffer[i] = buffer + i;
492  }
493  coef->whole_image[0] = nullptr; /* flag for no virtual arrays */
494  }
495 }
jzero_far(void FAR *target, size_t bytestozero)
Definition: jutils.cpp:150
compress_data(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
Definition: jccoefct.cpp:142
GLuint buffer
Definition: glext.h:3917
#define MAX_COMPS_IN_SCAN
Definition: mrpt_jpeglib.h:41
#define DCTSIZE
Definition: mrpt_jpeglib.h:36
jround_up(long a, long b)
Definition: jutils.cpp:67
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
#define MAX_COMPONENTS
Definition: jmorecfg.h:30
JDIMENSION mcu_ctr
Definition: jccoefct.cpp:34
#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
short JCOEF
Definition: jmorecfg.h:91
compress_first_pass(j_compress_ptr cinfo, JSAMPIMAGE input_buf)
Definition: jccoefct.cpp:269
JDIMENSION width_in_blocks
Definition: mrpt_jpeglib.h:132
struct jpeg_c_coef_controller pub
Definition: jccoefct.cpp:31
JDIMENSION height_in_blocks
Definition: mrpt_jpeglib.h:133
GLint GLint GLint yoffset
Definition: glext.h:3604
#define FALSE
Definition: jmorecfg.h:216
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
#define LOCAL(type)
Definition: jmorecfg.h:175
JDIMENSION iMCU_row_num
Definition: jccoefct.cpp:33
#define TRUE
Definition: jmorecfg.h:219
#define C_MAX_BLOCKS_IN_MCU
Definition: mrpt_jpeglib.h:50
compress_output(j_compress_ptr cinfo, JSAMPIMAGE)
Definition: jccoefct.cpp:375
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:62
JBLOCKROW * JBLOCKARRAY
Definition: mrpt_jpeglib.h:66
start_iMCU_row(j_compress_ptr cinfo)
Definition: jccoefct.cpp:67
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define METHODDEF(type)
Definition: jmorecfg.h:173
J_BUF_MODE
Definition: jpegint.h:12
JBLOCK FAR * JBLOCKROW
Definition: mrpt_jpeglib.h:65
jinit_c_coef_controller(j_compress_ptr cinfo, boolean need_full_buffer)
Definition: jccoefct.cpp:445
unsigned int JDIMENSION
Definition: jmorecfg.h:161
#define FAR
Definition: zconf.h:262
compress_data JPP((j_compress_ptr cinfo, JSAMPIMAGE input_buf))
JCOEF JBLOCK[DCTSIZE2]
Definition: mrpt_jpeglib.h:64
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jccoefct.cpp:99
JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU]
Definition: jccoefct.cpp:48
jpeg_component_info * compptr
Definition: jidctflt.cpp:36
jvirt_barray_ptr whole_image[MAX_COMPONENTS]
Definition: jccoefct.cpp:51
my_coef_controller * my_coef_ptr
Definition: jccoefct.cpp:54



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