Main MRPT website > C++ reference for MRPT 1.9.9
jcdctmgr.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 "jdct.h" /* Private declarations for DCT subsystem */
14 
15 /* Private subobject for this module */
16 
17 typedef struct
18 {
19  struct jpeg_forward_dct pub; /* public fields */
20 
21  /* Pointer to the DCT routine actually in use */
22  forward_DCT_method_ptr do_dct;
23 
24  /* The actual post-DCT divisors --- not identical to the quant table
25  * entries, because of scaling (especially for an unnormalized DCT).
26  * Each table is given in normal array order.
27  */
28  DCTELEM* divisors[NUM_QUANT_TBLS];
29 
30 #ifdef DCT_FLOAT_SUPPORTED
31  /* Same as above for the floating-point case. */
32  float_DCT_method_ptr do_float_dct;
33  FAST_FLOAT* float_divisors[NUM_QUANT_TBLS];
34 #endif
36 
38 
39 /*
40  * Initialize for a processing pass.
41  * Verify that all referenced Q-tables are present, and set up
42  * the divisor table for each one.
43  * In the current implementation, DCT of all components is done during
44  * the first pass, even if only some components will be output in the
45  * first scan. Hence all components should be examined here.
46  */
47 
48 METHODDEF(void)
50 {
51  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
52  int ci, qtblno, i;
54  JQUANT_TBL* qtbl;
55  DCTELEM* dtbl;
56 
57  for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
58  ci++, compptr++)
59  {
60  qtblno = compptr->quant_tbl_no;
61  /* Make sure specified quantization table is present */
62  if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
63  cinfo->quant_tbl_ptrs[qtblno] == nullptr)
64  ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
65  qtbl = cinfo->quant_tbl_ptrs[qtblno];
66  /* Compute divisors for this quant table */
67  /* We may do this more than once for same table, but it's not a big deal
68  */
69  switch (cinfo->dct_method)
70  {
71 #ifdef DCT_ISLOW_SUPPORTED
72  case JDCT_ISLOW:
73  /* For LL&M IDCT method, divisors are equal to raw quantization
74  * coefficients multiplied by 8 (to counteract scaling).
75  */
76  if (fdct->divisors[qtblno] == nullptr)
77  {
78  fdct->divisors[qtblno] =
79  (DCTELEM*)(*cinfo->mem->alloc_small)(
80  (j_common_ptr)cinfo, JPOOL_IMAGE,
82  }
83  dtbl = fdct->divisors[qtblno];
84  for (i = 0; i < DCTSIZE2; i++)
85  {
86  dtbl[i] = ((DCTELEM)qtbl->quantval[i]) << 3;
87  }
88  break;
89 #endif
90 #ifdef DCT_IFAST_SUPPORTED
91  case JDCT_IFAST:
92  {
93 /* For AA&N IDCT method, divisors are equal to quantization
94  * coefficients scaled by scalefactor[row]*scalefactor[col], where
95  * scalefactor[0] = 1
96  * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
97  * We apply a further scale factor of 8.
98  */
99 #define CONST_BITS 14
100  static const INT16 aanscales[DCTSIZE2] = {
101  /* precomputed values scaled up by 14 bits */
102  16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
103  22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
104  21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
105  19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
106  16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
107  12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
108  8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
109  4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247};
111 
112  if (fdct->divisors[qtblno] == nullptr)
113  {
114  fdct->divisors[qtblno] =
115  (DCTELEM*)(*cinfo->mem->alloc_small)(
116  (j_common_ptr)cinfo, JPOOL_IMAGE,
117  DCTSIZE2 * SIZEOF(DCTELEM));
118  }
119  dtbl = fdct->divisors[qtblno];
120  for (i = 0; i < DCTSIZE2; i++)
121  {
122  dtbl[i] = (DCTELEM)DESCALE(
124  (INT32)qtbl->quantval[i], (INT32)aanscales[i]),
125  CONST_BITS - 3);
126  }
127  }
128  break;
129 #endif
130 #ifdef DCT_FLOAT_SUPPORTED
131  case JDCT_FLOAT:
132  {
133  /* For float AA&N IDCT method, divisors are equal to
134  * quantization
135  * coefficients scaled by scalefactor[row]*scalefactor[col],
136  * where
137  * scalefactor[0] = 1
138  * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
139  * We apply a further scale factor of 8.
140  * What's actually stored is 1/divisor so that the inner loop
141  * can
142  * use a multiplication rather than a division.
143  */
144  FAST_FLOAT* fdtbl;
145  int row, col;
146  static const double aanscalefactor[DCTSIZE] = {
147  1.0, 1.387039845, 1.306562965, 1.175875602,
148  1.0, 0.785694958, 0.541196100, 0.275899379};
149 
150  if (fdct->float_divisors[qtblno] == nullptr)
151  {
152  fdct->float_divisors[qtblno] =
153  (FAST_FLOAT*)(*cinfo->mem->alloc_small)(
154  (j_common_ptr)cinfo, JPOOL_IMAGE,
155  DCTSIZE2 * SIZEOF(FAST_FLOAT));
156  }
157  fdtbl = fdct->float_divisors[qtblno];
158  i = 0;
159  for (row = 0; row < DCTSIZE; row++)
160  {
161  for (col = 0; col < DCTSIZE; col++)
162  {
163  fdtbl[i] = (FAST_FLOAT)(
164  1.0 /
165  (((double)qtbl->quantval[i] * aanscalefactor[row] *
166  aanscalefactor[col] * 8.0)));
167  i++;
168  }
169  }
170  }
171  break;
172 #endif
173  default:
174  ERREXIT(cinfo, JERR_NOT_COMPILED);
175  break;
176  }
177  }
178 }
179 
180 /*
181  * Perform forward DCT on one or more blocks of a component.
182  *
183  * The input samples are taken from the sample_data[] array starting at
184  * position start_row/start_col, and moving to the right for any additional
185  * blocks. The quantized coefficients are returned in coef_blocks[].
186  */
187 
188 METHODDEF(void)
191  JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col,
192  JDIMENSION num_blocks)
193 /* This version is used for integer DCT implementations. */
194 {
195  /* This routine is heavily used, so it's worth coding it tightly. */
196  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
197  forward_DCT_method_ptr do_dct = fdct->do_dct;
198  DCTELEM* divisors = fdct->divisors[compptr->quant_tbl_no];
199  DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
200  JDIMENSION bi;
201 
202  sample_data += start_row; /* fold in the vertical offset once */
203 
204  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE)
205  {
206  /* Load data into workspace, applying unsigned->signed conversion */
207  {
208  DCTELEM* workspaceptr;
209  JSAMPROW elemptr;
210  int elemr;
211 
212  workspaceptr = workspace;
213  for (elemr = 0; elemr < DCTSIZE; elemr++)
214  {
215  elemptr = sample_data[elemr] + start_col;
216 #if DCTSIZE == 8 /* unroll the inner loop */
217  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
218  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
219  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
220  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
221  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
222  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
223  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
224  *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
225 #else
226  {
227  int elemc;
228  for (elemc = DCTSIZE; elemc > 0; elemc--)
229  {
230  *workspaceptr++ =
231  GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
232  }
233  }
234 #endif
235  }
236  }
237 
238  /* Perform the DCT */
239  (*do_dct)(workspace);
240 
241  /* Quantize/descale the coefficients, and store into coef_blocks[] */
242  {
243  DCTELEM temp, qval;
244  int i;
245  JCOEFPTR output_ptr = coef_blocks[bi];
246 
247  for (i = 0; i < DCTSIZE2; i++)
248  {
249  qval = divisors[i];
250  temp = workspace[i];
251 /* Divide the coefficient value by qval, ensuring proper rounding.
252  * Since C does not specify the direction of rounding for negative
253  * quotients, we have to force the dividend positive for portability.
254  *
255  * In most files, at least half of the output values will be zero
256  * (at default quantization settings, more like three-quarters...)
257  * so we should ensure that this case is fast. On many machines,
258  * a comparison is enough cheaper than a divide to make a special test
259  * a win. Since both inputs will be nonnegative, we need only test
260  * for a < b to discover whether a/b is 0.
261  * If your machine's division is fast enough, define FAST_DIVIDE.
262  */
263 #ifdef FAST_DIVIDE
264 #define DIVIDE_BY(a, b) a /= b
265 #else
266 #define DIVIDE_BY(a, b) \
267  if (a >= b) \
268  a /= b; \
269  else \
270  a = 0
271 #endif
272  if (temp < 0)
273  {
274  temp = -temp;
275  temp += qval >> 1; /* for rounding */
276  DIVIDE_BY(temp, qval);
277  temp = -temp;
278  }
279  else
280  {
281  temp += qval >> 1; /* for rounding */
282  DIVIDE_BY(temp, qval);
283  }
284  output_ptr[i] = (JCOEF)temp;
285  }
286  }
287  }
288 }
289 
290 #ifdef DCT_FLOAT_SUPPORTED
291 
292 METHODDEF(void)
295  JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col,
296  JDIMENSION num_blocks)
297 /* This version is used for floating-point DCT implementations. */
298 {
299  /* This routine is heavily used, so it's worth coding it tightly. */
300  my_fdct_ptr fdct = (my_fdct_ptr)cinfo->fdct;
301  float_DCT_method_ptr do_dct = fdct->do_float_dct;
302  FAST_FLOAT* divisors = fdct->float_divisors[compptr->quant_tbl_no];
303  FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
304  JDIMENSION bi;
305 
306  sample_data += start_row; /* fold in the vertical offset once */
307 
308  for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE)
309  {
310  /* Load data into workspace, applying unsigned->signed conversion */
311  {
312  FAST_FLOAT* workspaceptr;
313  JSAMPROW elemptr;
314  int elemr;
315 
316  workspaceptr = workspace;
317  for (elemr = 0; elemr < DCTSIZE; elemr++)
318  {
319  elemptr = sample_data[elemr] + start_col;
320 #if DCTSIZE == 8 /* unroll the inner loop */
321  *workspaceptr++ =
322  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
323  *workspaceptr++ =
324  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
325  *workspaceptr++ =
326  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
327  *workspaceptr++ =
328  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
329  *workspaceptr++ =
330  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
331  *workspaceptr++ =
332  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
333  *workspaceptr++ =
334  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
335  *workspaceptr++ =
336  (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
337 #else
338  {
339  int elemc;
340  for (elemc = DCTSIZE; elemc > 0; elemc--)
341  {
342  *workspaceptr++ = (FAST_FLOAT)(
343  GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
344  }
345  }
346 #endif
347  }
348  }
349 
350  /* Perform the DCT */
351  (*do_dct)(workspace);
352 
353  /* Quantize/descale the coefficients, and store into coef_blocks[] */
354  {
355  FAST_FLOAT temp;
356  int i;
357  JCOEFPTR output_ptr = coef_blocks[bi];
358 
359  for (i = 0; i < DCTSIZE2; i++)
360  {
361  /* Apply the quantization and scaling factor */
362  temp = workspace[i] * divisors[i];
363  /* Round to nearest integer.
364  * Since C does not specify the direction of rounding for
365  * negative
366  * quotients, we have to force the dividend positive for
367  * portability.
368  * The maximum coefficient size is +-16K (for 12-bit data), so
369  * this
370  * code should work for either 16-bit or 32-bit ints.
371  */
372  output_ptr[i] =
373  (JCOEF)((int)(temp + (FAST_FLOAT)16384.5) - 16384);
374  }
375  }
376  }
377 }
378 
379 #endif /* DCT_FLOAT_SUPPORTED */
380 
381 /*
382  * Initialize FDCT manager.
383  */
384 
385 GLOBAL(void)
387 {
388  my_fdct_ptr fdct;
389  int i;
390 
391  fdct = (my_fdct_ptr)(*cinfo->mem->alloc_small)(
393  cinfo->fdct = (struct jpeg_forward_dct*)fdct;
394  fdct->pub.start_pass = start_pass_fdctmgr;
395 
396  switch (cinfo->dct_method)
397  {
398 #ifdef DCT_ISLOW_SUPPORTED
399  case JDCT_ISLOW:
400  fdct->pub.forward_DCT = forward_DCT;
401  fdct->do_dct = jpeg_fdct_islow;
402  break;
403 #endif
404 #ifdef DCT_IFAST_SUPPORTED
405  case JDCT_IFAST:
406  fdct->pub.forward_DCT = forward_DCT;
407  fdct->do_dct = jpeg_fdct_ifast;
408  break;
409 #endif
410 #ifdef DCT_FLOAT_SUPPORTED
411  case JDCT_FLOAT:
412  fdct->pub.forward_DCT = forward_DCT_float;
413  fdct->do_float_dct = jpeg_fdct_float;
414  break;
415 #endif
416  default:
417  ERREXIT(cinfo, JERR_NOT_COMPILED);
418  break;
419  }
420 
421  /* Mark divisor tables unallocated */
422  for (i = 0; i < NUM_QUANT_TBLS; i++)
423  {
424  fdct->divisors[i] = nullptr;
425 #ifdef DCT_FLOAT_SUPPORTED
426  fdct->float_divisors[i] = nullptr;
427 #endif
428  }
429 }
#define DESCALE(x, n)
Definition: jdct.h:142
#define CENTERJSAMPLE
Definition: jmorecfg.h:68
forward_DCT_method_ptr do_dct
Definition: jcdctmgr.cpp:22
short INT16
Definition: jmorecfg.h:145
FAST_FLOAT workspace[DCTSIZE2]
Definition: jidctflt.cpp:48
#define DIVIDE_BY(a, b)
my_fdct_controller * my_fdct_ptr
Definition: jcdctmgr.cpp:37
FAST_FLOAT * float_divisors[NUM_QUANT_TBLS]
Definition: jcdctmgr.cpp:33
#define DCTSIZE
Definition: mrpt_jpeglib.h:36
forward_DCT_float(j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks)
Definition: jcdctmgr.cpp:293
struct jpeg_common_struct * j_common_ptr
Definition: mrpt_jpeglib.h:258
INT32 DCTELEM
Definition: jdct.h:26
#define GETJSAMPLE(value)
Definition: jmorecfg.h:62
#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
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:60
short JCOEF
Definition: jmorecfg.h:91
long INT32
Definition: jmorecfg.h:151
#define SHIFT_TEMPS
Definition: jpegint.h:301
DCTELEM * divisors[NUM_QUANT_TBLS]
Definition: jcdctmgr.cpp:28
float_DCT_method_ptr do_float_dct
Definition: jcdctmgr.cpp:32
#define JPOOL_IMAGE
Definition: mrpt_jpeglib.h:750
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:61
JCOEF FAR * JCOEFPTR
Definition: mrpt_jpeglib.h:69
#define DCTSIZE2
Definition: mrpt_jpeglib.h:37
struct jpeg_forward_dct pub
Definition: jcdctmgr.cpp:19
forward_DCT(j_compress_ptr cinfo, jpeg_component_info *compptr, JSAMPARRAY sample_data, JBLOCKROW coef_blocks, JDIMENSION start_row, JDIMENSION start_col, JDIMENSION num_blocks)
Definition: jcdctmgr.cpp:189
#define ERREXIT1(cinfo, code, p1)
Definition: jerror.h:454
#define GLOBAL(type)
Definition: jmorecfg.h:177
jinit_forward_dct(j_compress_ptr cinfo)
Definition: jcdctmgr.cpp:386
GLenum GLenum GLvoid * row
Definition: glext.h:3576
#define METHODDEF(type)
Definition: jmorecfg.h:173
#define NUM_QUANT_TBLS
Definition: mrpt_jpeglib.h:38
JBLOCK FAR * JBLOCKROW
Definition: mrpt_jpeglib.h:65
unsigned int JDIMENSION
Definition: jmorecfg.h:161
start_pass_fdctmgr(j_compress_ptr cinfo)
Definition: jcdctmgr.cpp:49
#define CONST_BITS
jpeg_component_info * compptr
Definition: jidctflt.cpp:36
#define MULTIPLY16V16(var1, var2)
Definition: jdct.h:171



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