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