Main MRPT website > C++ reference for MRPT 1.5.7
jfdctint.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 /*
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 /*
28  * The poop on this scaling stuff is as follows:
29  *
30  * Each 1-D DCT step produces outputs which are a factor of sqrt(N)
31  * larger than the true DCT outputs. The final outputs are therefore
32  * a factor of N larger than desired; since N=8 this can be cured by
33  * a simple right shift at the end of the algorithm. The advantage of
34  * this arrangement is that we save two multiplications per 1-D DCT,
35  * because the y0 and y4 outputs need not be divided by sqrt(N).
36  * In the IJG code, this factor of 8 is removed by the quantization step
37  * (in jcdctmgr.c), NOT in this module.
38  *
39  * We have to do addition and subtraction of the integer inputs, which
40  * is no problem, and multiplication by fractional constants, which is
41  * a problem to do in integer arithmetic. We multiply all the constants
42  * by CONST_SCALE and convert them to integer constants (thus retaining
43  * CONST_BITS bits of precision in the constants). After doing a
44  * multiplication we have to divide the product by CONST_SCALE, with proper
45  * rounding, to produce the correct output. This division can be done
46  * cheaply as a right shift of CONST_BITS bits. We postpone shifting
47  * as long as possible so that partial sums can be added together with
48  * full fractional precision.
49  *
50  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
51  * they are represented to better-than-integral precision. These outputs
52  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
53  * with the recommended scaling. (For 12-bit sample data, the intermediate
54  * array is INT32 anyway.)
55  *
56  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
57  * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26. Error analysis
58  * shows that the values given below are the most effective.
59  */
60 
61 #if BITS_IN_JSAMPLE == 8
62 #define CONST_BITS 13
63 #define PASS1_BITS 2
64 #else
65 #define CONST_BITS 13
66 #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
67 #endif
68 
69 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
70  * causing a lot of useless floating-point operations at run time.
71  * To get around this we use the following pre-calculated constants.
72  * If you change CONST_BITS you may want to add appropriate values.
73  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
74  */
75 
76 #if CONST_BITS == 13
77 #define FIX_0_298631336 ((INT32) 2446) /* FIX(0.298631336) */
78 #define FIX_0_390180644 ((INT32) 3196) /* FIX(0.390180644) */
79 #define FIX_0_541196100 ((INT32) 4433) /* FIX(0.541196100) */
80 #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
81 #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
82 #define FIX_1_175875602 ((INT32) 9633) /* FIX(1.175875602) */
83 #define FIX_1_501321110 ((INT32) 12299) /* FIX(1.501321110) */
84 #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
85 #define FIX_1_961570560 ((INT32) 16069) /* FIX(1.961570560) */
86 #define FIX_2_053119869 ((INT32) 16819) /* FIX(2.053119869) */
87 #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
88 #define FIX_3_072711026 ((INT32) 25172) /* FIX(3.072711026) */
89 #else
90 #define FIX_0_298631336 FIX(0.298631336)
91 #define FIX_0_390180644 FIX(0.390180644)
92 #define FIX_0_541196100 FIX(0.541196100)
93 #define FIX_0_765366865 FIX(0.765366865)
94 #define FIX_0_899976223 FIX(0.899976223)
95 #define FIX_1_175875602 FIX(1.175875602)
96 #define FIX_1_501321110 FIX(1.501321110)
97 #define FIX_1_847759065 FIX(1.847759065)
98 #define FIX_1_961570560 FIX(1.961570560)
99 #define FIX_2_053119869 FIX(2.053119869)
100 #define FIX_2_562915447 FIX(2.562915447)
101 #define FIX_3_072711026 FIX(3.072711026)
102 #endif
103 
104 
105 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
106  * For 8-bit samples with the recommended scaling, all the variable
107  * and constant values involved are no more than 16 bits wide, so a
108  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
109  * For 12-bit samples, a full 32-bit multiplication will be needed.
110  */
111 
112 #if BITS_IN_JSAMPLE == 8
113 #define MULTIPLY(var,const) MULTIPLY16C16(var,const)
114 #else
115 #define MULTIPLY(var,const) ((var) * (const))
116 #endif
117 
118 
119 /*
120  * Perform the forward DCT on one block of samples.
121  */
122 
123 GLOBAL(void)
125 {
126  INT32 tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
127  INT32 tmp10, tmp11, tmp12, tmp13;
128  INT32 z1, z2, z3, z4, z5;
129  DCTELEM *dataptr;
130  int ctr;
132 
133  /* Pass 1: process rows. */
134  /* Note results are scaled up by sqrt(8) compared to a true DCT; */
135  /* furthermore, we scale the results by 2**PASS1_BITS. */
136 
137  dataptr = data;
138  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
139  tmp0 = dataptr[0] + dataptr[7];
140  tmp7 = dataptr[0] - dataptr[7];
141  tmp1 = dataptr[1] + dataptr[6];
142  tmp6 = dataptr[1] - dataptr[6];
143  tmp2 = dataptr[2] + dataptr[5];
144  tmp5 = dataptr[2] - dataptr[5];
145  tmp3 = dataptr[3] + dataptr[4];
146  tmp4 = dataptr[3] - dataptr[4];
147 
148  /* Even part per LL&M figure 1 --- note that published figure is faulty;
149  * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
150  */
151 
152  tmp10 = tmp0 + tmp3;
153  tmp13 = tmp0 - tmp3;
154  tmp11 = tmp1 + tmp2;
155  tmp12 = tmp1 - tmp2;
156 
157  dataptr[0] = (DCTELEM) ((tmp10 + tmp11) << PASS1_BITS);
158  dataptr[4] = (DCTELEM) ((tmp10 - tmp11) << PASS1_BITS);
159 
160  z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
161  dataptr[2] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp13, FIX_0_765366865),
163  dataptr[6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
165 
166  /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
167  * cK represents cos(K*pi/16).
168  * i0..i3 in the paper are tmp4..tmp7 here.
169  */
170 
171  z1 = tmp4 + tmp7;
172  z2 = tmp5 + tmp6;
173  z3 = tmp4 + tmp6;
174  z4 = tmp5 + tmp7;
175  z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
176 
177  tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
178  tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
179  tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
180  tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
181  z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
182  z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
183  z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
184  z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
185 
186  z3 += z5;
187  z4 += z5;
188 
189  dataptr[7] = (DCTELEM) DESCALE(tmp4 + z1 + z3, CONST_BITS-PASS1_BITS);
190  dataptr[5] = (DCTELEM) DESCALE(tmp5 + z2 + z4, CONST_BITS-PASS1_BITS);
191  dataptr[3] = (DCTELEM) DESCALE(tmp6 + z2 + z3, CONST_BITS-PASS1_BITS);
192  dataptr[1] = (DCTELEM) DESCALE(tmp7 + z1 + z4, CONST_BITS-PASS1_BITS);
193 
194  dataptr += DCTSIZE; /* advance pointer to next row */
195  }
196 
197  /* Pass 2: process columns.
198  * We remove the PASS1_BITS scaling, but leave the results scaled up
199  * by an overall factor of 8.
200  */
201 
202  dataptr = data;
203  for (ctr = DCTSIZE-1; ctr >= 0; ctr--) {
204  tmp0 = dataptr[DCTSIZE*0] + dataptr[DCTSIZE*7];
205  tmp7 = dataptr[DCTSIZE*0] - dataptr[DCTSIZE*7];
206  tmp1 = dataptr[DCTSIZE*1] + dataptr[DCTSIZE*6];
207  tmp6 = dataptr[DCTSIZE*1] - dataptr[DCTSIZE*6];
208  tmp2 = dataptr[DCTSIZE*2] + dataptr[DCTSIZE*5];
209  tmp5 = dataptr[DCTSIZE*2] - dataptr[DCTSIZE*5];
210  tmp3 = dataptr[DCTSIZE*3] + dataptr[DCTSIZE*4];
211  tmp4 = dataptr[DCTSIZE*3] - dataptr[DCTSIZE*4];
212 
213  /* Even part per LL&M figure 1 --- note that published figure is faulty;
214  * rotator "sqrt(2)*c1" should be "sqrt(2)*c6".
215  */
216 
217  tmp10 = tmp0 + tmp3;
218  tmp13 = tmp0 - tmp3;
219  tmp11 = tmp1 + tmp2;
220  tmp12 = tmp1 - tmp2;
221 
222  dataptr[DCTSIZE*0] = (DCTELEM) DESCALE(tmp10 + tmp11, PASS1_BITS);
223  dataptr[DCTSIZE*4] = (DCTELEM) DESCALE(tmp10 - tmp11, PASS1_BITS);
224 
225  z1 = MULTIPLY(tmp12 + tmp13, FIX_0_541196100);
228  dataptr[DCTSIZE*6] = (DCTELEM) DESCALE(z1 + MULTIPLY(tmp12, - FIX_1_847759065),
230 
231  /* Odd part per figure 8 --- note paper omits factor of sqrt(2).
232  * cK represents cos(K*pi/16).
233  * i0..i3 in the paper are tmp4..tmp7 here.
234  */
235 
236  z1 = tmp4 + tmp7;
237  z2 = tmp5 + tmp6;
238  z3 = tmp4 + tmp6;
239  z4 = tmp5 + tmp7;
240  z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
241 
242  tmp4 = MULTIPLY(tmp4, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
243  tmp5 = MULTIPLY(tmp5, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
244  tmp6 = MULTIPLY(tmp6, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
245  tmp7 = MULTIPLY(tmp7, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
246  z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
247  z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
248  z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
249  z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
250 
251  z3 += z5;
252  z4 += z5;
253 
254  dataptr[DCTSIZE*7] = (DCTELEM) DESCALE(tmp4 + z1 + z3,
256  dataptr[DCTSIZE*5] = (DCTELEM) DESCALE(tmp5 + z2 + z4,
258  dataptr[DCTSIZE*3] = (DCTELEM) DESCALE(tmp6 + z2 + z3,
260  dataptr[DCTSIZE*1] = (DCTELEM) DESCALE(tmp7 + z1 + z4,
262 
263  dataptr++; /* advance pointer to next column */
264  }
265 }
266 
267 #endif /* DCT_ISLOW_SUPPORTED */
#define DESCALE(x, n)
Definition: jdct.h:141
#define FIX_0_298631336
Definition: jfdctint.cpp:77
int const JOCTET * dataptr
Definition: mrpt_jpeglib.h:947
#define FIX_0_541196100
Definition: jfdctint.cpp:79
#define FIX_3_072711026
Definition: jfdctint.cpp:88
#define DCTSIZE
Definition: mrpt_jpeglib.h:38
INT32 DCTELEM
Definition: jdct.h:27
long INT32
Definition: jmorecfg.h:158
#define SHIFT_TEMPS
Definition: jpegint.h:286
#define MULTIPLY(var, const)
Definition: jfdctint.cpp:113
#define CONST_BITS
Definition: jfdctint.cpp:62
#define FIX_2_053119869
Definition: jfdctint.cpp:86
#define FIX_0_390180644
Definition: jfdctint.cpp:78
#define PASS1_BITS
Definition: jfdctint.cpp:63
jpeg_fdct_islow(DCTELEM *data)
Definition: jfdctint.cpp:124
#define FIX_1_847759065
Definition: jfdctint.cpp:84
Definition: inftrees.h:28
#define FIX_1_501321110
Definition: jfdctint.cpp:83
#define GLOBAL(type)
Definition: jmorecfg.h:185
#define FIX_1_961570560
Definition: jfdctint.cpp:85
#define FIX_2_562915447
Definition: jfdctint.cpp:87
#define FIX_1_175875602
Definition: jfdctint.cpp:82
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: glext.h:3520
#define FIX_0_765366865
Definition: jfdctint.cpp:80
#define FIX_0_899976223
Definition: jfdctint.cpp:81



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