Main MRPT website > C++ reference for MRPT 1.9.9
jidctint.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 DCT_ISLOW_SUPPORTED
16 
17 /*
18  * This module is specialized to the case DCTSIZE = 8.
19  */
20 
21 #if DCTSIZE != 8
22 Sorry, this code only copes with 8x8 DCTs./* deliberate syntax err */
23 #endif
24 
25 /*
26  * The poop on this scaling stuff is as follows:
27  *
28  * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
29  * larger than the true IDCT outputs. The final outputs are therefore
30  * a factor of N larger than desired; since N=8 this can be cured by
31  * a simple right shift at the end of the algorithm. The advantage of
32  * this arrangement is that we save two multiplications per 1-D IDCT,
33  * because the y0 and y4 inputs need not be divided by sqrt(N).
34  *
35  * We have to do addition and subtraction of the integer inputs, which
36  * is no problem, and multiplication by fractional constants, which is
37  * a problem to do in integer arithmetic. We multiply all the constants
38  * by CONST_SCALE and convert them to integer constants (thus retaining
39  * CONST_BITS bits of precision in the constants). After doing a
40  * multiplication we have to divide the product by CONST_SCALE, with proper
41  * rounding, to produce the correct output. This division can be done
42  * cheaply as a right shift of CONST_BITS bits. We postpone shifting
43  * as long as possible so that partial sums can be added together with
44  * full fractional precision.
45  *
46  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
47  * they are represented to better-than-integral precision. These outputs
48  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
49  * with the recommended scaling. (To scale up 12-bit sample data further, an
50  * intermediate INT32 array would be needed.)
51  *
52  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
53  * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
54  * shows that the values given below are the most effective.
55  */
56 
57 #if BITS_IN_JSAMPLE == 8
58 #define CONST_BITS 13
59 #define PASS1_BITS 2
60 #else
61 #define CONST_BITS 13
62 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
63 #endif
64 
65 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
66  * causing a lot of useless floating-point operations at run time.
67  * To get around this we use the following pre-calculated constants.
68  * If you change CONST_BITS you may want to add appropriate values.
69  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
70  */
71 
72 #if CONST_BITS == 13
73 #define FIX_0_298631336 ((INT32)2446) /* FIX(0.298631336) */
74 #define FIX_0_390180644 ((INT32)3196) /* FIX(0.390180644) */
75 #define FIX_0_541196100 ((INT32)4433) /* FIX(0.541196100) */
76 #define FIX_0_765366865 ((INT32)6270) /* FIX(0.765366865) */
77 #define FIX_0_899976223 ((INT32)7373) /* FIX(0.899976223) */
78 #define FIX_1_175875602 ((INT32)9633) /* FIX(1.175875602) */
79 #define FIX_1_501321110 ((INT32)12299) /* FIX(1.501321110) */
80 #define FIX_1_847759065 ((INT32)15137) /* FIX(1.847759065) */
81 #define FIX_1_961570560 ((INT32)16069) /* FIX(1.961570560) */
82 #define FIX_2_053119869 ((INT32)16819) /* FIX(2.053119869) */
83 #define FIX_2_562915447 ((INT32)20995) /* FIX(2.562915447) */
84 #define FIX_3_072711026 ((INT32)25172) /* FIX(3.072711026) */
85 #else
86 #define FIX_0_298631336 FIX(0.298631336)
87 #define FIX_0_390180644 FIX(0.390180644)
88 #define FIX_0_541196100 FIX(0.541196100)
89 #define FIX_0_765366865 FIX(0.765366865)
90 #define FIX_0_899976223 FIX(0.899976223)
91 #define FIX_1_175875602 FIX(1.175875602)
92 #define FIX_1_501321110 FIX(1.501321110)
93 #define FIX_1_847759065 FIX(1.847759065)
94 #define FIX_1_961570560 FIX(1.961570560)
95 #define FIX_2_053119869 FIX(2.053119869)
96 #define FIX_2_562915447 FIX(2.562915447)
97 #define FIX_3_072711026 FIX(3.072711026)
98 #endif
99 
100 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
101  * For 8-bit samples with the recommended scaling, all the variable
102  * and constant values involved are no more than 16 bits wide, so a
103  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
104  * For 12-bit samples, a full 32-bit multiplication will be needed.
105  */
106 
107 #if BITS_IN_JSAMPLE == 8
108 #define MULTIPLY(var, const) MULTIPLY16C16(var, const)
109 #else
110 #define MULTIPLY(var, const) ((var) * (const))
111 #endif
112 
113 /* Dequantize a coefficient by multiplying it by the multiplier-table
114  * entry; produce an int result. In this module, both inputs and result
115  * are 16 bits or less, so either int or short multiply will work.
116  */
117 
118 #define DEQUANTIZE(coef, quantval) (((ISLOW_MULT_TYPE)(coef)) * (quantval))
119 
120  /*
121  * Perform dequantization and inverse DCT on one block of coefficients.
122  */
123 
124  GLOBAL(void) jpeg_idct_islow(
127 {
128  INT32 tmp0, tmp1, tmp2, tmp3;
130  INT32 z1, z2, z3, z4, z5;
133  int* wsptr;
136  int ctr;
137  int workspace[DCTSIZE2]; /* buffers data between passes */
139 
140  /* Pass 1: process columns from input, store into work array. */
141  /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
142  /* furthermore, we scale the results by 2**PASS1_BITS. */
143 
144  inptr = coef_block;
146  wsptr = workspace;
147  for (ctr = DCTSIZE; ctr > 0; ctr--)
148  {
149  /* Due to quantization, we will usually find that many of the input
150  * coefficients are zero, especially the AC terms. We can exploit this
151  * by short-circuiting the IDCT calculation for any column in which all
152  * the AC terms are zero. In that case each output is equal to the
153  * DC coefficient (with scale factor as needed).
154  * With typical images and quantization tables, half or more of the
155  * column DCT calculations can be simplified this way.
156  */
157 
158  if (inptr[DCTSIZE * 1] == 0 && inptr[DCTSIZE * 2] == 0 &&
159  inptr[DCTSIZE * 3] == 0 && inptr[DCTSIZE * 4] == 0 &&
160  inptr[DCTSIZE * 5] == 0 && inptr[DCTSIZE * 6] == 0 &&
161  inptr[DCTSIZE * 7] == 0)
162  {
163  /* AC terms all zero */
164  int dcval = DEQUANTIZE(inptr[DCTSIZE * 0], quantptr[DCTSIZE * 0])
165  << PASS1_BITS;
166 
167  wsptr[DCTSIZE * 0] = dcval;
168  wsptr[DCTSIZE * 1] = dcval;
169  wsptr[DCTSIZE * 2] = dcval;
170  wsptr[DCTSIZE * 3] = dcval;
171  wsptr[DCTSIZE * 4] = dcval;
172  wsptr[DCTSIZE * 5] = dcval;
173  wsptr[DCTSIZE * 6] = dcval;
174  wsptr[DCTSIZE * 7] = dcval;
175 
176  inptr++; /* advance pointers to next column */
177  quantptr++;
178  wsptr++;
179  continue;
180  }
181 
182  /* Even part: reverse the even part of the forward DCT. */
183  /* The rotator is sqrt(2)*c(-6). */
184 
185  z2 = DEQUANTIZE(inptr[DCTSIZE * 2], quantptr[DCTSIZE * 2]);
186  z3 = DEQUANTIZE(inptr[DCTSIZE * 6], quantptr[DCTSIZE * 6]);
187 
189  tmp2 = z1 + MULTIPLY(z3, -FIX_1_847759065);
190  tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
191 
192  z2 = DEQUANTIZE(inptr[DCTSIZE * 0], quantptr[DCTSIZE * 0]);
193  z3 = DEQUANTIZE(inptr[DCTSIZE * 4], quantptr[DCTSIZE * 4]);
194 
195  tmp0 = (z2 + z3) << CONST_BITS;
196  tmp1 = (z2 - z3) << CONST_BITS;
197 
198  tmp10 = tmp0 + tmp3;
199  tmp13 = tmp0 - tmp3;
200  tmp11 = tmp1 + tmp2;
201  tmp12 = tmp1 - tmp2;
202 
203  /* Odd part per figure 8; the matrix is unitary and hence its
204  * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
205  */
206 
207  tmp0 = DEQUANTIZE(inptr[DCTSIZE * 7], quantptr[DCTSIZE * 7]);
208  tmp1 = DEQUANTIZE(inptr[DCTSIZE * 5], quantptr[DCTSIZE * 5]);
209  tmp2 = DEQUANTIZE(inptr[DCTSIZE * 3], quantptr[DCTSIZE * 3]);
210  tmp3 = DEQUANTIZE(inptr[DCTSIZE * 1], quantptr[DCTSIZE * 1]);
211 
212  z1 = tmp0 + tmp3;
213  z2 = tmp1 + tmp2;
214  z3 = tmp0 + tmp2;
215  z4 = tmp1 + tmp3;
216  z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
217 
218  tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
219  tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
220  tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
221  tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
222  z1 = MULTIPLY(z1, -FIX_0_899976223); /* sqrt(2) * (c7-c3) */
223  z2 = MULTIPLY(z2, -FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
224  z3 = MULTIPLY(z3, -FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
225  z4 = MULTIPLY(z4, -FIX_0_390180644); /* sqrt(2) * (c5-c3) */
226 
227  z3 += z5;
228  z4 += z5;
229 
230  tmp0 += z1 + z3;
231  tmp1 += z2 + z4;
232  tmp2 += z2 + z3;
233  tmp3 += z1 + z4;
234 
235  /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
236 
237  wsptr[DCTSIZE * 0] =
238  (int)DESCALE(tmp10 + tmp3, CONST_BITS - PASS1_BITS);
239  wsptr[DCTSIZE * 7] =
240  (int)DESCALE(tmp10 - tmp3, CONST_BITS - PASS1_BITS);
241  wsptr[DCTSIZE * 1] =
242  (int)DESCALE(tmp11 + tmp2, CONST_BITS - PASS1_BITS);
243  wsptr[DCTSIZE * 6] =
244  (int)DESCALE(tmp11 - tmp2, CONST_BITS - PASS1_BITS);
245  wsptr[DCTSIZE * 2] =
246  (int)DESCALE(tmp12 + tmp1, CONST_BITS - PASS1_BITS);
247  wsptr[DCTSIZE * 5] =
248  (int)DESCALE(tmp12 - tmp1, CONST_BITS - PASS1_BITS);
249  wsptr[DCTSIZE * 3] =
250  (int)DESCALE(tmp13 + tmp0, CONST_BITS - PASS1_BITS);
251  wsptr[DCTSIZE * 4] =
252  (int)DESCALE(tmp13 - tmp0, CONST_BITS - PASS1_BITS);
253 
254  inptr++; /* advance pointers to next column */
255  quantptr++;
256  wsptr++;
257  }
258 
259  /* Pass 2: process rows from work array, store into output array. */
260  /* Note that we must descale the results by a factor of 8 == 2**3, */
261  /* and also undo the PASS1_BITS scaling. */
262 
263  wsptr = workspace;
264  for (ctr = 0; ctr < DCTSIZE; ctr++)
265  {
267 /* Rows of zeroes can be exploited in the same way as we did with columns.
268  * However, the column calculation has created many nonzero AC terms, so
269  * the simplification applies less often (typically 5% to 10% of the time).
270  * On machines with very fast multiplication, it's possible that the
271  * test takes more time than it's worth. In that case this section
272  * may be commented out.
273  */
274 
275 #ifndef NO_ZERO_ROW_TEST
276  if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 && wsptr[4] == 0 &&
277  wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0)
278  {
279  /* AC terms all zero */
280  JSAMPLE dcval =
281  range_limit[(int)DESCALE((INT32)wsptr[0], PASS1_BITS + 3) &
282  RANGE_MASK];
283 
284  outptr[0] = dcval;
285  outptr[1] = dcval;
286  outptr[2] = dcval;
287  outptr[3] = dcval;
288  outptr[4] = dcval;
289  outptr[5] = dcval;
290  outptr[6] = dcval;
291  outptr[7] = dcval;
292 
293  wsptr += DCTSIZE; /* advance pointer to next row */
294  continue;
295  }
296 #endif
297 
298  /* Even part: reverse the even part of the forward DCT. */
299  /* The rotator is sqrt(2)*c(-6). */
300 
301  z2 = (INT32)wsptr[2];
302  z3 = (INT32)wsptr[6];
303 
305  tmp2 = z1 + MULTIPLY(z3, -FIX_1_847759065);
306  tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
307 
308  tmp0 = ((INT32)wsptr[0] + (INT32)wsptr[4]) << CONST_BITS;
309  tmp1 = ((INT32)wsptr[0] - (INT32)wsptr[4]) << CONST_BITS;
310 
311  tmp10 = tmp0 + tmp3;
312  tmp13 = tmp0 - tmp3;
313  tmp11 = tmp1 + tmp2;
314  tmp12 = tmp1 - tmp2;
315 
316  /* Odd part per figure 8; the matrix is unitary and hence its
317  * transpose is its inverse. i0..i3 are y7,y5,y3,y1 respectively.
318  */
319 
320  tmp0 = (INT32)wsptr[7];
321  tmp1 = (INT32)wsptr[5];
322  tmp2 = (INT32)wsptr[3];
323  tmp3 = (INT32)wsptr[1];
324 
325  z1 = tmp0 + tmp3;
326  z2 = tmp1 + tmp2;
327  z3 = tmp0 + tmp2;
328  z4 = tmp1 + tmp3;
329  z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
330 
331  tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
332  tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
333  tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
334  tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
335  z1 = MULTIPLY(z1, -FIX_0_899976223); /* sqrt(2) * (c7-c3) */
336  z2 = MULTIPLY(z2, -FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
337  z3 = MULTIPLY(z3, -FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
338  z4 = MULTIPLY(z4, -FIX_0_390180644); /* sqrt(2) * (c5-c3) */
339 
340  z3 += z5;
341  z4 += z5;
342 
343  tmp0 += z1 + z3;
344  tmp1 += z2 + z4;
345  tmp2 += z2 + z3;
346  tmp3 += z1 + z4;
347 
348  /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
349 
350  outptr[0] = range_limit[(int)DESCALE(
351  tmp10 + tmp3, CONST_BITS + PASS1_BITS + 3) &
352  RANGE_MASK];
353  outptr[7] = range_limit[(int)DESCALE(
354  tmp10 - tmp3, CONST_BITS + PASS1_BITS + 3) &
355  RANGE_MASK];
356  outptr[1] = range_limit[(int)DESCALE(
357  tmp11 + tmp2, CONST_BITS + PASS1_BITS + 3) &
358  RANGE_MASK];
359  outptr[6] = range_limit[(int)DESCALE(
360  tmp11 - tmp2, CONST_BITS + PASS1_BITS + 3) &
361  RANGE_MASK];
362  outptr[2] = range_limit[(int)DESCALE(
363  tmp12 + tmp1, CONST_BITS + PASS1_BITS + 3) &
364  RANGE_MASK];
365  outptr[5] = range_limit[(int)DESCALE(
366  tmp12 - tmp1, CONST_BITS + PASS1_BITS + 3) &
367  RANGE_MASK];
368  outptr[3] = range_limit[(int)DESCALE(
369  tmp13 + tmp0, CONST_BITS + PASS1_BITS + 3) &
370  RANGE_MASK];
371  outptr[4] = range_limit[(int)DESCALE(
372  tmp13 - tmp0, CONST_BITS + PASS1_BITS + 3) &
373  RANGE_MASK];
374 
375  wsptr += DCTSIZE; /* advance pointer to next row */
376  }
377 }
378 
379 #endif /* DCT_ISLOW_SUPPORTED */
#define DESCALE(x, n)
Definition: jdct.h:142
#define FIX_0_298631336
Definition: jidctint.cpp:73
#define IDCT_range_limit(cinfo)
Definition: jdct.h:68
char JSAMPLE
Definition: jmorecfg.h:58
#define FIX_0_541196100
Definition: jidctint.cpp:75
int workspace[DCTSIZE2]
Definition: jidctint.cpp:137
#define FIX_3_072711026
Definition: jidctint.cpp:84
#define DCTSIZE
Definition: mrpt_jpeglib.h:36
INT32 z2
Definition: jidctint.cpp:130
jpeg_component_info JCOEFPTR coef_block
Definition: jidctint.cpp:125
#define RANGE_MASK
Definition: jdct.h:70
INT32 tmp12
Definition: jidctint.cpp:129
JSAMPLE FAR * JSAMPROW
Definition: mrpt_jpeglib.h:60
INT32 z3
Definition: jidctint.cpp:130
INT32 tmp11
Definition: jidctint.cpp:129
long INT32
Definition: jmorecfg.h:151
#define SHIFT_TEMPS
Definition: jpegint.h:301
int ctr
Definition: jidctint.cpp:136
#define DEQUANTIZE(coef, quantval)
Definition: jidctint.cpp:118
INT32 z4
Definition: jidctint.cpp:130
#define CONST_BITS
Definition: jidctint.cpp:58
#define FIX_2_053119869
Definition: jidctint.cpp:82
#define FIX_0_390180644
Definition: jidctint.cpp:74
INT32 tmp10
Definition: jidctint.cpp:129
#define MULTIPLY(var, const)
Definition: jidctint.cpp:108
INT32 tmp13
Definition: jidctint.cpp:129
JSAMPROW * JSAMPARRAY
Definition: mrpt_jpeglib.h:61
JCOEF FAR * JCOEFPTR
Definition: mrpt_jpeglib.h:69
jpeg_component_info JCOEFPTR JSAMPARRAY JDIMENSION output_col
Definition: jidctint.cpp:127
int * wsptr
Definition: jidctint.cpp:133
#define DCTSIZE2
Definition: mrpt_jpeglib.h:37
MULTIPLIER ISLOW_MULT_TYPE
Definition: jdct.h:49
#define PASS1_BITS
Definition: jidctint.cpp:59
JSAMPLE * range_limit
Definition: jidctint.cpp:135
#define FIX_1_847759065
Definition: jidctint.cpp:80
jpeg_component_info * compptr
Definition: jidctint.cpp:125
Definition: inftrees.h:28
#define FIX_1_501321110
Definition: jidctint.cpp:79
JCOEFPTR inptr
Definition: jidctint.cpp:131
JSAMPROW outptr
Definition: jidctint.cpp:134
INT32 z5
Definition: jidctint.cpp:130
#define FIX_1_961570560
Definition: jidctint.cpp:81
unsigned int JDIMENSION
Definition: jmorecfg.h:161
#define FIX_1_175875602
Definition: jidctint.cpp:78
jpeg_component_info JCOEFPTR JSAMPARRAY output_buf
Definition: jidctint.cpp:125
INT32 z1
Definition: jidctint.cpp:130
#define FIX_2_562915447
Definition: jidctint.cpp:83
#define FIX_0_899976223
Definition: jidctint.cpp:77
ISLOW_MULT_TYPE * quantptr
Definition: jidctint.cpp:132
#define FIX_0_765366865
Definition: jidctint.cpp:76
for(ctr=DCTSIZE;ctr > 0;ctr--)
Definition: jidctint.cpp:147
GLOBAL(void) jpeg_idct_islow(j_decompress_ptr cinfo



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