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



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