Main MRPT website > C++ reference for MRPT 1.9.9
jctrans.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 /* Forward declarations */
15 LOCAL(void)
17  (j_compress_ptr cinfo, jvirt_barray_ptr* coef_arrays));
18 LOCAL(void)
20  (j_compress_ptr cinfo, jvirt_barray_ptr* coef_arrays));
21 
22 /*
23  * Compression initialization for writing raw-coefficient data.
24  * Before calling this, all parameters and a data destination must be set up.
25  * Call jpeg_finish_compress() to actually write the data.
26  *
27  * The number of passed virtual arrays must match cinfo->num_components.
28  * Note that the virtual arrays need not be filled or even realized at
29  * the time write_coefficients is called; indeed, if the virtual arrays
30  * were requested from this compression object's memory manager, they
31  * typically will be realized during this routine and filled afterwards.
32  */
33 
34 GLOBAL(void)
36 {
37  if (cinfo->global_state != CSTATE_START)
38  ERREXIT1(cinfo, JERR_BAD_STATE, cinfo->global_state);
39  /* Mark all tables to be written */
41  /* (Re)initialize error mgr and destination modules */
42  (*cinfo->err->reset_error_mgr)((j_common_ptr)cinfo);
43  (*cinfo->dest->init_destination)(cinfo);
44  /* Perform master selection of active modules */
45  transencode_master_selection(cinfo, coef_arrays);
46  /* Wait for jpeg_finish_compress() call */
47  cinfo->next_scanline = 0; /* so jpeg_write_marker works */
48  cinfo->global_state = CSTATE_WRCOEFS;
49 }
50 
51 /*
52  * Initialize the compression object with default parameters,
53  * then copy from the source object all parameters needed for lossless
54  * transcoding. Parameters that can be varied without loss (such as
55  * scan script and Huffman optimization) are left in their default states.
56  */
57 
58 GLOBAL(void)
60 {
61  JQUANT_TBL** qtblptr;
62  jpeg_component_info *incomp, *outcomp;
63  JQUANT_TBL *c_quant, *slot_quant;
64  int tblno, ci, coefi;
65 
66  /* Safety check to ensure start_compress not called yet. */
67  if (dstinfo->global_state != CSTATE_START)
68  ERREXIT1(dstinfo, JERR_BAD_STATE, dstinfo->global_state);
69  /* Copy fundamental image dimensions */
70  dstinfo->image_width = srcinfo->image_width;
71  dstinfo->image_height = srcinfo->image_height;
72  dstinfo->input_components = srcinfo->num_components;
73  dstinfo->in_color_space = srcinfo->jpeg_color_space;
74  /* Initialize all parameters to default values */
75  jpeg_set_defaults(dstinfo);
76  /* jpeg_set_defaults may choose wrong colorspace, eg YCbCr if input is RGB.
77  * Fix it to get the right header markers for the image colorspace.
78  */
79  jpeg_set_colorspace(dstinfo, srcinfo->jpeg_color_space);
80  dstinfo->data_precision = srcinfo->data_precision;
81  dstinfo->CCIR601_sampling = srcinfo->CCIR601_sampling;
82  /* Copy the source's quantization tables. */
83  for (tblno = 0; tblno < NUM_QUANT_TBLS; tblno++)
84  {
85  if (srcinfo->quant_tbl_ptrs[tblno] != nullptr)
86  {
87  qtblptr = &dstinfo->quant_tbl_ptrs[tblno];
88  if (*qtblptr == nullptr)
89  *qtblptr = jpeg_alloc_quant_table((j_common_ptr)dstinfo);
90  MEMCOPY(
91  (*qtblptr)->quantval, srcinfo->quant_tbl_ptrs[tblno]->quantval,
92  SIZEOF((*qtblptr)->quantval));
93  (*qtblptr)->sent_table = FALSE;
94  }
95  }
96  /* Copy the source's per-component info.
97  * Note we assume jpeg_set_defaults has allocated the dest comp_info array.
98  */
99  dstinfo->num_components = srcinfo->num_components;
100  if (dstinfo->num_components < 1 || dstinfo->num_components > MAX_COMPONENTS)
101  ERREXIT2(
102  dstinfo, JERR_COMPONENT_COUNT, dstinfo->num_components,
104  for (ci = 0, incomp = srcinfo->comp_info, outcomp = dstinfo->comp_info;
105  ci < dstinfo->num_components; ci++, incomp++, outcomp++)
106  {
107  outcomp->component_id = incomp->component_id;
108  outcomp->h_samp_factor = incomp->h_samp_factor;
109  outcomp->v_samp_factor = incomp->v_samp_factor;
110  outcomp->quant_tbl_no = incomp->quant_tbl_no;
111  /* Make sure saved quantization table for component matches the qtable
112  * slot. If not, the input file re-used this qtable slot.
113  * IJG encoder currently cannot duplicate this.
114  */
115  tblno = outcomp->quant_tbl_no;
116  if (tblno < 0 || tblno >= NUM_QUANT_TBLS ||
117  srcinfo->quant_tbl_ptrs[tblno] == nullptr)
118  ERREXIT1(dstinfo, JERR_NO_QUANT_TABLE, tblno);
119  slot_quant = srcinfo->quant_tbl_ptrs[tblno];
120  c_quant = incomp->quant_table;
121  if (c_quant != nullptr)
122  {
123  for (coefi = 0; coefi < DCTSIZE2; coefi++)
124  {
125  if (c_quant->quantval[coefi] != slot_quant->quantval[coefi])
126  ERREXIT1(dstinfo, JERR_MISMATCHED_QUANT_TABLE, tblno);
127  }
128  }
129  /* Note: we do not copy the source's Huffman table assignments;
130  * instead we rely on jpeg_set_colorspace to have made a suitable
131  * choice.
132  */
133  }
134  /* Also copy JFIF version and resolution information, if available.
135  * Strictly speaking this isn't "critical" info, but it's nearly
136  * always appropriate to copy it if available. In particular,
137  * if the application chooses to copy JFIF 1.02 extension markers from
138  * the source file, we need to copy the version to make sure we don't
139  * emit a file that has 1.02 extensions but a claimed version of 1.01.
140  * We will *not*, however, copy version info from mislabeled "2.01" files.
141  */
142  if (srcinfo->saw_JFIF_marker)
143  {
144  if (srcinfo->JFIF_major_version == 1)
145  {
146  dstinfo->JFIF_major_version = srcinfo->JFIF_major_version;
147  dstinfo->JFIF_minor_version = srcinfo->JFIF_minor_version;
148  }
149  dstinfo->density_unit = srcinfo->density_unit;
150  dstinfo->X_density = srcinfo->X_density;
151  dstinfo->Y_density = srcinfo->Y_density;
152  }
153 }
154 
155 /*
156  * Master selection of compression modules for transcoding.
157  * This substitutes for jcinit.c's initialization of the full compressor.
158  */
159 
160 LOCAL(void)
162  j_compress_ptr cinfo, jvirt_barray_ptr* coef_arrays)
163 {
164  /* Although we don't actually use input_components for transcoding,
165  * jcmaster.c's initial_setup will complain if input_components is 0.
166  */
167  cinfo->input_components = 1;
168  /* Initialize master control (includes parameter checking/processing) */
169  jinit_c_master_control(cinfo, TRUE /* transcode only */);
170 
171  /* Entropy encoding: either Huffman or arithmetic coding. */
172  if (cinfo->arith_code)
173  {
174  ERREXIT(cinfo, JERR_ARITH_NOTIMPL);
175  }
176  else
177  {
178  if (cinfo->progressive_mode)
179  {
180 #ifdef C_PROGRESSIVE_SUPPORTED
181  jinit_phuff_encoder(cinfo);
182 #else
183  ERREXIT(cinfo, JERR_NOT_COMPILED);
184 #endif
185  }
186  else
187  jinit_huff_encoder(cinfo);
188  }
189 
190  /* We need a special coefficient buffer controller. */
191  transencode_coef_controller(cinfo, coef_arrays);
192 
193  jinit_marker_writer(cinfo);
194 
195  /* We can now tell the memory manager to allocate virtual arrays. */
196  (*cinfo->mem->realize_virt_arrays)((j_common_ptr)cinfo);
197 
198  /* Write the datastream header (SOI, JFIF) immediately.
199  * Frame and scan headers are postponed till later.
200  * This lets application insert special markers after the SOI.
201  */
202  (*cinfo->marker->write_file_header)(cinfo);
203 }
204 
205 /*
206  * The rest of this file is a special implementation of the coefficient
207  * buffer controller. This is similar to jccoefct.c, but it handles only
208  * output from presupplied virtual arrays. Furthermore, we generate any
209  * dummy padding blocks on-the-fly rather than expecting them to be present
210  * in the arrays.
211  */
212 
213 /* Private buffer controller object */
214 
215 typedef struct
216 {
217  struct jpeg_c_coef_controller pub; /* public fields */
218 
219  JDIMENSION iMCU_row_num; /* iMCU row # within image */
220  JDIMENSION mcu_ctr; /* counts MCUs processed in current row */
221  int MCU_vert_offset; /* counts MCU rows within iMCU row */
222  int MCU_rows_per_iMCU_row; /* number of such rows needed */
223 
224  /* Virtual block array for each component. */
226 
227  /* Workspace for constructing dummy blocks at right/bottom edges. */
230 
232 
233 LOCAL(void)
235 /* Reset within-iMCU-row counters for a new row */
236 {
237  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
238 
239  /* In an interleaved scan, an MCU row is the same as an iMCU row.
240  * In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
241  * But at the bottom of the image, process only what's left.
242  */
243  if (cinfo->comps_in_scan > 1)
244  {
245  coef->MCU_rows_per_iMCU_row = 1;
246  }
247  else
248  {
249  if (coef->iMCU_row_num < (cinfo->total_iMCU_rows - 1))
250  coef->MCU_rows_per_iMCU_row =
251  cinfo->cur_comp_info[0]->v_samp_factor;
252  else
253  coef->MCU_rows_per_iMCU_row =
254  cinfo->cur_comp_info[0]->last_row_height;
255  }
256 
257  coef->mcu_ctr = 0;
258  coef->MCU_vert_offset = 0;
259 }
260 
261 /*
262  * Initialize for a processing pass.
263  */
264 
265 METHODDEF(void)
267 {
268  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
269 
270  if (pass_mode != JBUF_CRANK_DEST) ERREXIT(cinfo, JERR_BAD_BUFFER_MODE);
271 
272  coef->iMCU_row_num = 0;
273  start_iMCU_row(cinfo);
274 }
275 
276 /*
277  * Process some data.
278  * We process the equivalent of one fully interleaved MCU row ("iMCU" row)
279  * per call, ie, v_samp_factor block rows for each component in the scan.
280  * The data is obtained from the virtual arrays and fed to the entropy coder.
281  * Returns TRUE if the iMCU row is completed, FALSE if suspended.
282  *
283  * NB: input_buf is ignored; it is likely to be a nullptr pointer.
284  */
285 
286 METHODDEF(boolean)
288 {
289  my_coef_ptr coef = (my_coef_ptr)cinfo->coef;
290  JDIMENSION MCU_col_num; /* index of current MCU within row */
291  JDIMENSION last_MCU_col = cinfo->MCUs_per_row - 1;
292  JDIMENSION last_iMCU_row = cinfo->total_iMCU_rows - 1;
293  int blkn, ci, xindex, yindex, yoffset, blockcnt;
294  JDIMENSION start_col;
296  JBLOCKROW MCU_buffer[C_MAX_BLOCKS_IN_MCU];
297  JBLOCKROW buffer_ptr;
299 
300  /* Align the virtual buffers for the components used in this scan. */
301  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
302  {
303  compptr = cinfo->cur_comp_info[ci];
304  buffer[ci] = (*cinfo->mem->access_virt_barray)(
308  }
309 
310  /* Loop to process one whole iMCU row */
311  for (yoffset = coef->MCU_vert_offset; yoffset < coef->MCU_rows_per_iMCU_row;
312  yoffset++)
313  {
314  for (MCU_col_num = coef->mcu_ctr; MCU_col_num < cinfo->MCUs_per_row;
315  MCU_col_num++)
316  {
317  /* Construct list of pointers to DCT blocks belonging to this MCU */
318  blkn = 0; /* index of current DCT block within MCU */
319  for (ci = 0; ci < cinfo->comps_in_scan; ci++)
320  {
321  compptr = cinfo->cur_comp_info[ci];
322  start_col = MCU_col_num * compptr->MCU_width;
323  blockcnt = (MCU_col_num < last_MCU_col)
324  ? compptr->MCU_width
326  for (yindex = 0; yindex < compptr->MCU_height; yindex++)
327  {
328  if (coef->iMCU_row_num < last_iMCU_row ||
329  yindex + yoffset < compptr->last_row_height)
330  {
331  /* Fill in pointers to real blocks in this row */
332  buffer_ptr = buffer[ci][yindex + yoffset] + start_col;
333  for (xindex = 0; xindex < blockcnt; xindex++)
334  MCU_buffer[blkn++] = buffer_ptr++;
335  }
336  else
337  {
338  /* At bottom of image, need a whole row of dummy blocks
339  */
340  xindex = 0;
341  }
342  /* Fill in any dummy blocks needed in this row.
343  * Dummy blocks are filled in the same way as in jccoefct.c:
344  * all zeroes in the AC entries, DC entries equal to
345  * previous
346  * block's DC value. The init routine has already zeroed
347  * the
348  * AC entries, so we need only set the DC entries correctly.
349  */
350  for (; xindex < compptr->MCU_width; xindex++)
351  {
352  MCU_buffer[blkn] = coef->dummy_buffer[blkn];
353  MCU_buffer[blkn][0][0] = MCU_buffer[blkn - 1][0][0];
354  blkn++;
355  }
356  }
357  }
358  /* Try to write the MCU. */
359  if (!(*cinfo->entropy->encode_mcu)(cinfo, MCU_buffer))
360  {
361  /* Suspension forced; update state counters and exit */
362  coef->MCU_vert_offset = yoffset;
363  coef->mcu_ctr = MCU_col_num;
364  return FALSE;
365  }
366  }
367  /* Completed an MCU row, but perhaps not an iMCU row */
368  coef->mcu_ctr = 0;
369  }
370  /* Completed the iMCU row, advance counters for next one */
371  coef->iMCU_row_num++;
372  start_iMCU_row(cinfo);
373  return TRUE;
374 }
375 
376 /*
377  * Initialize coefficient buffer controller.
378  *
379  * Each passed coefficient array must be the right size for that
380  * coefficient: width_in_blocks wide and height_in_blocks high,
381  * with unitheight at least v_samp_factor.
382  */
383 
384 LOCAL(void)
386 {
387  my_coef_ptr coef;
389  int i;
390 
391  coef = (my_coef_ptr)(*cinfo->mem->alloc_small)(
393  cinfo->coef = (struct jpeg_c_coef_controller*)coef;
394  coef->pub.start_pass = start_pass_coef;
395  coef->pub.compress_data = compress_output;
396 
397  /* Save pointer to virtual arrays */
398  coef->whole_image = coef_arrays;
399 
400  /* Allocate and pre-zero space for dummy DCT blocks. */
401  buffer = (JBLOCKROW)(*cinfo->mem->alloc_large)(
404  for (i = 0; i < C_MAX_BLOCKS_IN_MCU; i++)
405  {
406  coef->dummy_buffer[i] = buffer + i;
407  }
408 }
jzero_far(void FAR *target, size_t bytestozero)
Definition: jutils.cpp:150
UINT16 quantval[DCTSIZE2]
Definition: mrpt_jpeglib.h:81
#define CSTATE_START
Definition: jpegint.h:22
jpeg_write_coefficients(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.cpp:35
GLuint buffer
Definition: glext.h:3917
#define MAX_COMPS_IN_SCAN
Definition: mrpt_jpeglib.h:41
jpeg_set_defaults(j_compress_ptr cinfo)
Definition: jcparam.cpp:299
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
jpeg_suppress_tables(j_compress_ptr cinfo, boolean suppress)
Definition: jcapimin.cpp:108
start_iMCU_row(j_compress_ptr cinfo)
Definition: jctrans.cpp:234
jinit_phuff_encoder(j_compress_ptr cinfo)
Definition: jcphuff.cpp:844
jpeg_copy_critical_parameters(j_decompress_ptr srcinfo, j_compress_ptr dstinfo)
Definition: jctrans.cpp:59
struct jpeg_c_coef_controller pub
Definition: jccoefct.cpp:31
jinit_huff_encoder(j_compress_ptr cinfo)
Definition: jchuff.cpp:914
#define MEMCOPY(dest, src, size)
Definition: jinclude.h:61
jpeg_set_colorspace(j_compress_ptr cinfo, J_COLOR_SPACE colorspace)
Definition: jcparam.cpp:422
GLint GLint GLint yoffset
Definition: glext.h:3604
#define FALSE
Definition: jmorecfg.h:216
jvirt_barray_ptr * whole_image
Definition: jctrans.cpp:225
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
#define LOCAL(type)
Definition: jmorecfg.h:175
my_coef_controller * my_coef_ptr
Definition: jctrans.cpp:231
transencode_master_selection JPP((j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays))
JDIMENSION iMCU_row_num
Definition: jccoefct.cpp:33
#define DCTSIZE2
Definition: mrpt_jpeglib.h:37
#define TRUE
Definition: jmorecfg.h:219
JBLOCKROW dummy_buffer[C_MAX_BLOCKS_IN_MCU]
Definition: jctrans.cpp:228
jpeg_alloc_quant_table(j_common_ptr cinfo)
Definition: jcomapi.cpp:83
#define C_MAX_BLOCKS_IN_MCU
Definition: mrpt_jpeglib.h:50
JQUANT_TBL * quant_table
Definition: mrpt_jpeglib.h:169
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:454
JSAMPARRAY * JSAMPIMAGE
Definition: mrpt_jpeglib.h:62
JBLOCKROW * JBLOCKARRAY
Definition: mrpt_jpeglib.h:66
start_pass_coef(j_compress_ptr cinfo, J_BUF_MODE pass_mode)
Definition: jctrans.cpp:266
#define GLOBAL(type)
Definition: jmorecfg.h:177
#define METHODDEF(type)
Definition: jmorecfg.h:173
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:38
transencode_coef_controller(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.cpp:385
J_BUF_MODE
Definition: jpegint.h:12
transencode_master_selection(j_compress_ptr cinfo, jvirt_barray_ptr *coef_arrays)
Definition: jctrans.cpp:161
JBLOCK FAR * JBLOCKROW
Definition: mrpt_jpeglib.h:65
jinit_c_master_control(j_compress_ptr cinfo, boolean transcode_only)
Definition: jcmaster.cpp:574
unsigned int JDIMENSION
Definition: jmorecfg.h:161
#define FAR
Definition: zconf.h:262
#define CSTATE_WRCOEFS
Definition: jpegint.h:25
#define ERREXIT2(cinfo, code, p1, p2)
Definition: jerror.h:457
jinit_marker_writer(j_compress_ptr cinfo)
Definition: jcmarker.cpp:659
JCOEF JBLOCK[DCTSIZE2]
Definition: mrpt_jpeglib.h:64
compress_output(j_compress_ptr cinfo, JSAMPIMAGE)
Definition: jctrans.cpp:287
jpeg_component_info * compptr
Definition: jidctflt.cpp:36
jvirt_barray_ptr whole_image[MAX_COMPONENTS]
Definition: jccoefct.cpp:51



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