7 * Copyright (C) 1994, Thomas G. Lane.
\r
9 * This file is part of the Independent JPEG Group's software.
\r
11 * For conditions of distribution and use, see the accompanying README file.
\r
15 * This file contains a floating-point implementation of the
\r
17 * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
\r
19 * must also perform dequantization of the input coefficients.
\r
23 * This implementation should be more accurate than either of the integer
\r
25 * IDCT implementations. However, it may not give the same results on all
\r
27 * machines because of differences in roundoff behavior. Speed will depend
\r
29 * on the hardware's floating point capacity.
\r
33 * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
\r
35 * on each row (or vice versa, but it's more convenient to emit a row at
\r
37 * a time). Direct algorithms are also available, but they are much more
\r
39 * complex and seem not to be any faster when reduced to code.
\r
43 * This implementation is based on Arai, Agui, and Nakajima's algorithm for
\r
45 * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
\r
47 * Japanese, but the algorithm is described in the Pennebaker & Mitchell
\r
49 * JPEG textbook (see REFERENCES section in file README). The following code
\r
51 * is based directly on figure 4-8 in P&M.
\r
53 * While an 8-point DCT cannot be done in less than 11 multiplies, it is
\r
55 * possible to arrange the computation so that many of the multiplies are
\r
57 * simple scalings of the final outputs. These multiplies can then be
\r
59 * folded into the multiplications or divisions by the JPEG quantization
\r
61 * table entries. The AA&N method leaves only 5 multiplies and 29 adds
\r
63 * to be done in the DCT itself.
\r
65 * The primary disadvantage of this method is that with a fixed-point
\r
67 * implementation, accuracy is lost due to imprecise representation of the
\r
69 * scaled quantization values. However, that problem does not arise if
\r
71 * we use floating point arithmetic.
\r
77 #define JPEG_INTERNALS
\r
79 #include "jinclude.h"
\r
81 #include "radiant_jpeglib.h"
\r
83 #include "jdct.h" /* Private declarations for DCT subsystem */
\r
87 #ifdef DCT_FLOAT_SUPPORTED
\r
95 * This module is specialized to the case DCTSIZE = 8.
\r
103 Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
\r
111 /* Dequantize a coefficient by multiplying it by the multiplier-table
\r
113 * entry; produce a float result.
\r
119 #define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval))
\r
127 * Perform dequantization and inverse DCT on one block of coefficients.
\r
135 jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
\r
137 JCOEFPTR coef_block,
\r
139 JSAMPARRAY output_buf, JDIMENSION output_col)
\r
143 FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
\r
145 FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
\r
147 FAST_FLOAT z5, z10, z11, z12, z13;
\r
151 FLOAT_MULT_TYPE * quantptr;
\r
153 FAST_FLOAT * wsptr;
\r
157 JSAMPLE *range_limit = IDCT_range_limit(cinfo);
\r
161 FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
\r
167 /* Pass 1: process columns from input, store into work array. */
\r
171 inptr = coef_block;
\r
173 quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
\r
177 for (ctr = DCTSIZE; ctr > 0; ctr--) {
\r
179 /* Due to quantization, we will usually find that many of the input
\r
181 * coefficients are zero, especially the AC terms. We can exploit this
\r
183 * by short-circuiting the IDCT calculation for any column in which all
\r
185 * the AC terms are zero. In that case each output is equal to the
\r
187 * DC coefficient (with scale factor as needed).
\r
189 * With typical images and quantization tables, half or more of the
\r
191 * column DCT calculations can be simplified this way.
\r
197 if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
\r
199 inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
\r
201 inptr[DCTSIZE*7]) == 0) {
\r
203 /* AC terms all zero */
\r
205 FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
\r
209 wsptr[DCTSIZE*0] = dcval;
\r
211 wsptr[DCTSIZE*1] = dcval;
\r
213 wsptr[DCTSIZE*2] = dcval;
\r
215 wsptr[DCTSIZE*3] = dcval;
\r
217 wsptr[DCTSIZE*4] = dcval;
\r
219 wsptr[DCTSIZE*5] = dcval;
\r
221 wsptr[DCTSIZE*6] = dcval;
\r
223 wsptr[DCTSIZE*7] = dcval;
\r
227 inptr++; /* advance pointers to next column */
\r
243 tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
\r
245 tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
\r
247 tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
\r
249 tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
\r
253 tmp10 = tmp0 + tmp2; /* phase 3 */
\r
255 tmp11 = tmp0 - tmp2;
\r
259 tmp13 = tmp1 + tmp3; /* phases 5-3 */
\r
261 tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
\r
265 tmp0 = tmp10 + tmp13; /* phase 2 */
\r
267 tmp3 = tmp10 - tmp13;
\r
269 tmp1 = tmp11 + tmp12;
\r
271 tmp2 = tmp11 - tmp12;
\r
279 tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
\r
281 tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
\r
283 tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
\r
285 tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
\r
289 z13 = tmp6 + tmp5; /* phase 6 */
\r
299 tmp7 = z11 + z13; /* phase 5 */
\r
301 tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
\r
305 z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
\r
307 tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
\r
309 tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
\r
313 tmp6 = tmp12 - tmp7; /* phase 2 */
\r
315 tmp5 = tmp11 - tmp6;
\r
317 tmp4 = tmp10 + tmp5;
\r
321 wsptr[DCTSIZE*0] = tmp0 + tmp7;
\r
323 wsptr[DCTSIZE*7] = tmp0 - tmp7;
\r
325 wsptr[DCTSIZE*1] = tmp1 + tmp6;
\r
327 wsptr[DCTSIZE*6] = tmp1 - tmp6;
\r
329 wsptr[DCTSIZE*2] = tmp2 + tmp5;
\r
331 wsptr[DCTSIZE*5] = tmp2 - tmp5;
\r
333 wsptr[DCTSIZE*4] = tmp3 + tmp4;
\r
335 wsptr[DCTSIZE*3] = tmp3 - tmp4;
\r
339 inptr++; /* advance pointers to next column */
\r
349 /* Pass 2: process rows from work array, store into output array. */
\r
351 /* Note that we must descale the results by a factor of 8 == 2**3. */
\r
357 for (ctr = 0; ctr < DCTSIZE; ctr++) {
\r
359 outptr = output_buf[ctr] + output_col;
\r
361 /* Rows of zeroes can be exploited in the same way as we did with columns.
\r
363 * However, the column calculation has created many nonzero AC terms, so
\r
365 * the simplification applies less often (typically 5% to 10% of the time).
\r
367 * And testing floats for zero is relatively expensive, so we don't bother.
\r
377 tmp10 = wsptr[0] + wsptr[4];
\r
379 tmp11 = wsptr[0] - wsptr[4];
\r
383 tmp13 = wsptr[2] + wsptr[6];
\r
385 tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
\r
389 tmp0 = tmp10 + tmp13;
\r
391 tmp3 = tmp10 - tmp13;
\r
393 tmp1 = tmp11 + tmp12;
\r
395 tmp2 = tmp11 - tmp12;
\r
403 z13 = wsptr[5] + wsptr[3];
\r
405 z10 = wsptr[5] - wsptr[3];
\r
407 z11 = wsptr[1] + wsptr[7];
\r
409 z12 = wsptr[1] - wsptr[7];
\r
415 tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
\r
419 z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
\r
421 tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
\r
423 tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
\r
427 tmp6 = tmp12 - tmp7;
\r
429 tmp5 = tmp11 - tmp6;
\r
431 tmp4 = tmp10 + tmp5;
\r
435 /* Final output stage: scale down by a factor of 8 and range-limit */
\r
439 outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
\r
443 outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
\r
447 outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
\r
451 outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
\r
455 outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
\r
459 outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
\r
463 outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
\r
467 outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
\r
473 wsptr += DCTSIZE; /* advance pointer to next row */
\r
481 #endif /* DCT_FLOAT_SUPPORTED */
\r