Main MRPT website > C++ reference for MRPT 1.5.7
jidctred.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 #ifdef IDCT_SCALING_SUPPORTED
16 
17 
18 /*
19  * This module is specialized to the case DCTSIZE = 8.
20  */
21 
22 #if DCTSIZE != 8
23  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
24 #endif
25 
26 
27 /* Scaling is the same as in jidctint.c. */
28 
29 #if BITS_IN_JSAMPLE == 8
30 #define CONST_BITS 13
31 #define PASS1_BITS 2
32 #else
33 #define CONST_BITS 13
34 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
35 #endif
36 
37 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
38  * causing a lot of useless floating-point operations at run time.
39  * To get around this we use the following pre-calculated constants.
40  * If you change CONST_BITS you may want to add appropriate values.
41  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
42  */
43 
44 #if CONST_BITS == 13
45 #define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */
46 #define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */
47 #define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */
48 #define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */
49 #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
50 #define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */
51 #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
52 #define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */
53 #define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */
54 #define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */
55 #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
56 #define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */
57 #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
58 #define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */
59 #else
60 #define FIX_0_211164243 FIX(0.211164243)
61 #define FIX_0_509795579 FIX(0.509795579)
62 #define FIX_0_601344887 FIX(0.601344887)
63 #define FIX_0_720959822 FIX(0.720959822)
64 #define FIX_0_765366865 FIX(0.765366865)
65 #define FIX_0_850430095 FIX(0.850430095)
66 #define FIX_0_899976223 FIX(0.899976223)
67 #define FIX_1_061594337 FIX(1.061594337)
68 #define FIX_1_272758580 FIX(1.272758580)
69 #define FIX_1_451774981 FIX(1.451774981)
70 #define FIX_1_847759065 FIX(1.847759065)
71 #define FIX_2_172734803 FIX(2.172734803)
72 #define FIX_2_562915447 FIX(2.562915447)
73 #define FIX_3_624509785 FIX(3.624509785)
74 #endif
75 
76 
77 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
78  * For 8-bit samples with the recommended scaling, all the variable
79  * and constant values involved are no more than 16 bits wide, so a
80  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
81  * For 12-bit samples, a full 32-bit multiplication will be needed.
82  */
83 
84 #if BITS_IN_JSAMPLE == 8
85 #define MULTIPLY(var,const) MULTIPLY16C16(var,const)
86 #else
87 #define MULTIPLY(var,const) ((var) * (const))
88 #endif
89 
90 
91 /* Dequantize a coefficient by multiplying it by the multiplier-table
92  * entry; produce an int result. In this module, both inputs and result
93  * are 16 bits or less, so either int or short multiply will work.
94  */
95 
96 #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
97 
98 
99 /*
100  * Perform dequantization and inverse DCT on one block of coefficients,
101  * producing a reduced-size 4x4 output block.
102  */
103 
104 GLOBAL(void)
108 {
109  INT32 tmp0, tmp2, tmp10, tmp12;
110  INT32 z1, z2, z3, z4;
111  JCOEFPTR inptr;
112  ISLOW_MULT_TYPE * quantptr;
113  int * wsptr;
114  JSAMPROW outptr;
115  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
116  int ctr;
117  int workspace[DCTSIZE*4]; /* buffers data between passes */
119 
120  /* Pass 1: process columns from input, store into work array. */
121 
122  inptr = coef_block;
123  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
124  wsptr = workspace;
125  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
126  /* Don't bother to process column 4, because second pass won't use it */
127  if (ctr == DCTSIZE-4)
128  continue;
129  if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
130  inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
131  inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
132  /* AC terms all zero; we need not examine term 4 for 4x4 output */
133  int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
134 
135  wsptr[DCTSIZE*0] = dcval;
136  wsptr[DCTSIZE*1] = dcval;
137  wsptr[DCTSIZE*2] = dcval;
138  wsptr[DCTSIZE*3] = dcval;
139 
140  continue;
141  }
142 
143  /* Even part */
144 
145  tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
146  tmp0 <<= (CONST_BITS+1);
147 
148  z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
149  z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
150 
151  tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
152 
153  tmp10 = tmp0 + tmp2;
154  tmp12 = tmp0 - tmp2;
155 
156  /* Odd part */
157 
158  z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
159  z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
160  z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
161  z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
162 
163  tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
164  + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
165  + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
166  + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
167 
168  tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
169  + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
170  + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
171  + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
172 
173  /* Final output stage */
174 
175  wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
176  wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
177  wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
178  wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
179  }
180 
181  /* Pass 2: process 4 rows from work array, store into output array. */
182 
183  wsptr = workspace;
184  for (ctr = 0; ctr < 4; ctr++) {
185  outptr = output_buf[ctr] + output_col;
186  /* It's not clear whether a zero row test is worthwhile here ... */
187 
188 #ifndef NO_ZERO_ROW_TEST
189  if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
190  wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
191  /* AC terms all zero */
192  JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
193  & RANGE_MASK];
194 
195  outptr[0] = dcval;
196  outptr[1] = dcval;
197  outptr[2] = dcval;
198  outptr[3] = dcval;
199 
200  wsptr += DCTSIZE; /* advance pointer to next row */
201  continue;
202  }
203 #endif
204 
205  /* Even part */
206 
207  tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
208 
209  tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
210  + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
211 
212  tmp10 = tmp0 + tmp2;
213  tmp12 = tmp0 - tmp2;
214 
215  /* Odd part */
216 
217  z1 = (INT32) wsptr[7];
218  z2 = (INT32) wsptr[5];
219  z3 = (INT32) wsptr[3];
220  z4 = (INT32) wsptr[1];
221 
222  tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
223  + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
224  + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
225  + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
226 
227  tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
228  + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
229  + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
230  + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
231 
232  /* Final output stage */
233 
234  outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
236  & RANGE_MASK];
237  outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
239  & RANGE_MASK];
240  outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
242  & RANGE_MASK];
243  outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
245  & RANGE_MASK];
246 
247  wsptr += DCTSIZE; /* advance pointer to next row */
248  }
249 }
250 
251 
252 /*
253  * Perform dequantization and inverse DCT on one block of coefficients,
254  * producing a reduced-size 2x2 output block.
255  */
256 
257 GLOBAL(void)
261 {
262  INT32 tmp0, tmp10, z1;
263  JCOEFPTR inptr;
264  ISLOW_MULT_TYPE * quantptr;
265  int * wsptr;
266  JSAMPROW outptr;
267  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
268  int ctr;
269  int workspace[DCTSIZE*2]; /* buffers data between passes */
271 
272  /* Pass 1: process columns from input, store into work array. */
273 
274  inptr = coef_block;
275  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
276  wsptr = workspace;
277  for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
278  /* Don't bother to process columns 2,4,6 */
279  if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
280  continue;
281  if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
282  inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
283  /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
284  int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
285 
286  wsptr[DCTSIZE*0] = dcval;
287  wsptr[DCTSIZE*1] = dcval;
288 
289  continue;
290  }
291 
292  /* Even part */
293 
294  z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
295  tmp10 = z1 << (CONST_BITS+2);
296 
297  /* Odd part */
298 
299  z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
300  tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
301  z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
302  tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
303  z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
304  tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
305  z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
306  tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
307 
308  /* Final output stage */
309 
310  wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
311  wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
312  }
313 
314  /* Pass 2: process 2 rows from work array, store into output array. */
315 
316  wsptr = workspace;
317  for (ctr = 0; ctr < 2; ctr++) {
318  outptr = output_buf[ctr] + output_col;
319  /* It's not clear whether a zero row test is worthwhile here ... */
320 
321 #ifndef NO_ZERO_ROW_TEST
322  if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
323  /* AC terms all zero */
324  JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
325  & RANGE_MASK];
326 
327  outptr[0] = dcval;
328  outptr[1] = dcval;
329 
330  wsptr += DCTSIZE; /* advance pointer to next row */
331  continue;
332  }
333 #endif
334 
335  /* Even part */
336 
337  tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
338 
339  /* Odd part */
340 
341  tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
342  + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
343  + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
344  + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
345 
346  /* Final output stage */
347 
348  outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
350  & RANGE_MASK];
351  outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
353  & RANGE_MASK];
354 
355  wsptr += DCTSIZE; /* advance pointer to next row */
356  }
357 }
358 
359 
360 /*
361  * Perform dequantization and inverse DCT on one block of coefficients,
362  * producing a reduced-size 1x1 output block.
363  */
364 
365 GLOBAL(void)
369 {
370  int dcval;
371  ISLOW_MULT_TYPE * quantptr;
372  JSAMPLE *range_limit = IDCT_range_limit(cinfo);
374 
375  /* We hardly need an inverse DCT routine for this: just take the
376  * average pixel value, which is one-eighth of the DC coefficient.
377  */
378  quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
379  dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
380  dcval = (int) DESCALE((INT32) dcval, 3);
381 
382  output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
383 }
384 
385 #endif /* IDCT_SCALING_SUPPORTED */
#define DESCALE(x, n)
Definition: jdct.h:141
#define IDCT_range_limit(cinfo)
Definition: jdct.h:71
char JSAMPLE
Definition: jmorecfg.h:61
#define FIX_0_850430095
Definition: jidctred.cpp:50
jpeg_idct_2x2(j_decompress_ptr cinfo, jpeg_component_info *compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)
Definition: jidctred.cpp:258
#define DCTSIZE
Definition: mrpt_jpeglib.h:38
jpeg_component_info JCOEFPTR coef_block
Definition: jdct.h:97
#define RANGE_MASK
Definition: jdct.h:73
#define FIX_0_211164243
Definition: jidctred.cpp:45
#define FIX_1_061594337
Definition: jidctred.cpp:52
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:63
#define FIX_1_272758580
Definition: jidctred.cpp:53
long INT32
Definition: jmorecfg.h:158
#define SHIFT_TEMPS
Definition: jpegint.h:286
jpeg_component_info * compptr
Definition: jdct.h:97
#define MULTIPLY(var, const)
Definition: jidctred.cpp:85
#define FIX_0_899976223
Definition: jidctred.cpp:51
#define FIX_0_765366865
Definition: jidctred.cpp:49
jpeg_component_info JCOEFPTR JSAMPARRAY JDIMENSION output_col
Definition: jdct.h:97
#define FIX_2_172734803
Definition: jidctred.cpp:56
#define CONST_BITS
Definition: jidctred.cpp:30
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:64
#define FIX_3_624509785
Definition: jidctred.cpp:58
JCOEF FAR * JCOEFPTR
Definition: mrpt_jpeglib.h:72
#define FIX_2_562915447
Definition: jidctred.cpp:57
MULTIPLIER ISLOW_MULT_TYPE
Definition: jdct.h:51
jpeg_idct_4x4(j_decompress_ptr cinfo, jpeg_component_info *compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)
Definition: jidctred.cpp:105
#define DEQUANTIZE(coef, quantval)
Definition: jidctred.cpp:96
Definition: inftrees.h:28
#define FIX_0_601344887
Definition: jidctred.cpp:47
#define FIX_0_509795579
Definition: jidctred.cpp:46
JSAMPIMAGE output_buf
Definition: jdcoefct.cpp:59
#define GLOBAL(type)
Definition: jmorecfg.h:185
jpeg_idct_1x1(j_decompress_ptr cinfo, jpeg_component_info *compptr, JCOEFPTR coef_block, JSAMPARRAY output_buf, JDIMENSION output_col)
Definition: jidctred.cpp:366
#define FIX_1_451774981
Definition: jidctred.cpp:54
#define FIX_1_847759065
Definition: jidctred.cpp:55
unsigned int JDIMENSION
Definition: jmorecfg.h:168
#define PASS1_BITS
Definition: jidctred.cpp:31
#define FIX_0_720959822
Definition: jidctred.cpp:48



Page generated by Doxygen 1.8.14 for MRPT 1.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019