Remove 'oldcode'
[oweals/cde.git] / cde / lib / DtHelp / jpeg / jidctfst.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these libraries and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /* $XConsortium: jidctfst.c /main/2 1996/05/09 03:51:13 drk $ */
24 /*
25  * jidctfst.c
26  *
27  * Copyright (C) 1994-1996, Thomas G. Lane.
28  * This file is part of the Independent JPEG Group's software.
29  * For conditions of distribution and use, see the accompanying README file.
30  *
31  * This file contains a fast, not so accurate integer implementation of the
32  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
33  * must also perform dequantization of the input coefficients.
34  *
35  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
36  * on each row (or vice versa, but it's more convenient to emit a row at
37  * a time).  Direct algorithms are also available, but they are much more
38  * complex and seem not to be any faster when reduced to code.
39  *
40  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
41  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
42  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
43  * JPEG textbook (see REFERENCES section in file README).  The following code
44  * is based directly on figure 4-8 in P&M.
45  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
46  * possible to arrange the computation so that many of the multiplies are
47  * simple scalings of the final outputs.  These multiplies can then be
48  * folded into the multiplications or divisions by the JPEG quantization
49  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
50  * to be done in the DCT itself.
51  * The primary disadvantage of this method is that with fixed-point math,
52  * accuracy is lost due to imprecise representation of the scaled
53  * quantization values.  The smaller the quantization table entry, the less
54  * precise the scaled value, so this implementation does worse with high-
55  * quality-setting files than with low-quality ones.
56  */
57
58 #define JPEG_INTERNALS
59 #include "jinclude.h"
60 #include "jpeglib.h"
61 #include "jdct.h"               /* Private declarations for DCT subsystem */
62
63 #ifdef DCT_IFAST_SUPPORTED
64
65
66 /*
67  * This module is specialized to the case DCTSIZE = 8.
68  */
69
70 #if DCTSIZE != 8
71   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
72 #endif
73
74
75 /* Scaling decisions are generally the same as in the LL&M algorithm;
76  * see jidctint.c for more details.  However, we choose to descale
77  * (right shift) multiplication products as soon as they are formed,
78  * rather than carrying additional fractional bits into subsequent additions.
79  * This compromises accuracy slightly, but it lets us save a few shifts.
80  * More importantly, 16-bit arithmetic is then adequate (for 8-bit samples)
81  * everywhere except in the multiplications proper; this saves a good deal
82  * of work on 16-bit-int machines.
83  *
84  * The dequantized coefficients are not integers because the AA&N scaling
85  * factors have been incorporated.  We represent them scaled up by PASS1_BITS,
86  * so that the first and second IDCT rounds have the same input scaling.
87  * For 8-bit JSAMPLEs, we choose IFAST_SCALE_BITS = PASS1_BITS so as to
88  * avoid a descaling shift; this compromises accuracy rather drastically
89  * for small quantization table entries, but it saves a lot of shifts.
90  * For 12-bit JSAMPLEs, there's no hope of using 16x16 multiplies anyway,
91  * so we use a much larger scaling factor to preserve accuracy.
92  *
93  * A final compromise is to represent the multiplicative constants to only
94  * 8 fractional bits, rather than 13.  This saves some shifting work on some
95  * machines, and may also reduce the cost of multiplication (since there
96  * are fewer one-bits in the constants).
97  */
98
99 #if BITS_IN_JSAMPLE == 8
100 #define CONST_BITS  8
101 #define PASS1_BITS  2
102 #else
103 #define CONST_BITS  8
104 #define PASS1_BITS  1           /* lose a little precision to avoid overflow */
105 #endif
106
107 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
108  * causing a lot of useless floating-point operations at run time.
109  * To get around this we use the following pre-calculated constants.
110  * If you change CONST_BITS you may want to add appropriate values.
111  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
112  */
113
114 #if CONST_BITS == 8
115 #define FIX_1_082392200  ((INT32)  277)         /* FIX(1.082392200) */
116 #define FIX_1_414213562  ((INT32)  362)         /* FIX(1.414213562) */
117 #define FIX_1_847759065  ((INT32)  473)         /* FIX(1.847759065) */
118 #define FIX_2_613125930  ((INT32)  669)         /* FIX(2.613125930) */
119 #else
120 #define FIX_1_082392200  FIX(1.082392200)
121 #define FIX_1_414213562  FIX(1.414213562)
122 #define FIX_1_847759065  FIX(1.847759065)
123 #define FIX_2_613125930  FIX(2.613125930)
124 #endif
125
126
127 /* We can gain a little more speed, with a further compromise in accuracy,
128  * by omitting the addition in a descaling shift.  This yields an incorrectly
129  * rounded result half the time...
130  */
131
132 #ifndef USE_ACCURATE_ROUNDING
133 #undef DESCALE
134 #define DESCALE(x,n)  RIGHT_SHIFT(x, n)
135 #endif
136
137
138 /* Multiply a DCTELEM variable by an INT32 constant, and immediately
139  * descale to yield a DCTELEM result.
140  */
141
142 #define MULTIPLY(var,const)  ((DCTELEM) DESCALE((var) * (const), CONST_BITS))
143
144
145 /* Dequantize a coefficient by multiplying it by the multiplier-table
146  * entry; produce a DCTELEM result.  For 8-bit data a 16x16->16
147  * multiplication will do.  For 12-bit data, the multiplier table is
148  * declared INT32, so a 32-bit multiply will be used.
149  */
150
151 #if BITS_IN_JSAMPLE == 8
152 #define DEQUANTIZE(coef,quantval)  (((IFAST_MULT_TYPE) (coef)) * (quantval))
153 #else
154 #define DEQUANTIZE(coef,quantval)  \
155         DESCALE((coef)*(quantval), IFAST_SCALE_BITS-PASS1_BITS)
156 #endif
157
158
159 /* Like DESCALE, but applies to a DCTELEM and produces an int.
160  * We assume that int right shift is unsigned if INT32 right shift is.
161  */
162
163 #ifdef RIGHT_SHIFT_IS_UNSIGNED
164 #define ISHIFT_TEMPS    DCTELEM ishift_temp;
165 #if BITS_IN_JSAMPLE == 8
166 #define DCTELEMBITS  16         /* DCTELEM may be 16 or 32 bits */
167 #else
168 #define DCTELEMBITS  32         /* DCTELEM must be 32 bits */
169 #endif
170 #define IRIGHT_SHIFT(x,shft)  \
171     ((ishift_temp = (x)) < 0 ? \
172      (ishift_temp >> (shft)) | ((~((DCTELEM) 0)) << (DCTELEMBITS-(shft))) : \
173      (ishift_temp >> (shft)))
174 #else
175 #define ISHIFT_TEMPS
176 #define IRIGHT_SHIFT(x,shft)    ((x) >> (shft))
177 #endif
178
179 #ifdef USE_ACCURATE_ROUNDING
180 #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT((x) + (1 << ((n)-1)), n))
181 #else
182 #define IDESCALE(x,n)  ((int) IRIGHT_SHIFT(x, n))
183 #endif
184
185
186 /*
187  * Perform dequantization and inverse DCT on one block of coefficients.
188  */
189
190 GLOBAL(void)
191 jpeg_idct_ifast (j_decompress_ptr cinfo, jpeg_component_info * compptr,
192                  JCOEFPTR coef_block,
193                  JSAMPARRAY output_buf, JDIMENSION output_col)
194 {
195   DCTELEM tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
196   DCTELEM tmp10, tmp11, tmp12, tmp13;
197   DCTELEM z5, z10, z11, z12, z13;
198   JCOEFPTR inptr;
199   IFAST_MULT_TYPE * quantptr;
200   int * wsptr;
201   JSAMPROW outptr;
202   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
203   int ctr;
204   int workspace[DCTSIZE2];      /* buffers data between passes */
205   SHIFT_TEMPS                   /* for DESCALE */
206   ISHIFT_TEMPS                  /* for IDESCALE */
207
208   /* Pass 1: process columns from input, store into work array. */
209
210   inptr = coef_block;
211   quantptr = (IFAST_MULT_TYPE *) compptr->dct_table;
212   wsptr = workspace;
213   for (ctr = DCTSIZE; ctr > 0; ctr--) {
214     /* Due to quantization, we will usually find that many of the input
215      * coefficients are zero, especially the AC terms.  We can exploit this
216      * by short-circuiting the IDCT calculation for any column in which all
217      * the AC terms are zero.  In that case each output is equal to the
218      * DC coefficient (with scale factor as needed).
219      * With typical images and quantization tables, half or more of the
220      * column DCT calculations can be simplified this way.
221      */
222     
223     if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
224          inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
225          inptr[DCTSIZE*7]) == 0) {
226       /* AC terms all zero */
227       int dcval = (int) DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
228
229       wsptr[DCTSIZE*0] = dcval;
230       wsptr[DCTSIZE*1] = dcval;
231       wsptr[DCTSIZE*2] = dcval;
232       wsptr[DCTSIZE*3] = dcval;
233       wsptr[DCTSIZE*4] = dcval;
234       wsptr[DCTSIZE*5] = dcval;
235       wsptr[DCTSIZE*6] = dcval;
236       wsptr[DCTSIZE*7] = dcval;
237       
238       inptr++;                  /* advance pointers to next column */
239       quantptr++;
240       wsptr++;
241       continue;
242     }
243     
244     /* Even part */
245
246     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
247     tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
248     tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
249     tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
250
251     tmp10 = tmp0 + tmp2;        /* phase 3 */
252     tmp11 = tmp0 - tmp2;
253
254     tmp13 = tmp1 + tmp3;        /* phases 5-3 */
255     tmp12 = MULTIPLY(tmp1 - tmp3, FIX_1_414213562) - tmp13; /* 2*c4 */
256
257     tmp0 = tmp10 + tmp13;       /* phase 2 */
258     tmp3 = tmp10 - tmp13;
259     tmp1 = tmp11 + tmp12;
260     tmp2 = tmp11 - tmp12;
261     
262     /* Odd part */
263
264     tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
265     tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
266     tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
267     tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
268
269     z13 = tmp6 + tmp5;          /* phase 6 */
270     z10 = tmp6 - tmp5;
271     z11 = tmp4 + tmp7;
272     z12 = tmp4 - tmp7;
273
274     tmp7 = z11 + z13;           /* phase 5 */
275     tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
276
277     z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
278     tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
279     tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
280
281     tmp6 = tmp12 - tmp7;        /* phase 2 */
282     tmp5 = tmp11 - tmp6;
283     tmp4 = tmp10 + tmp5;
284
285     wsptr[DCTSIZE*0] = (int) (tmp0 + tmp7);
286     wsptr[DCTSIZE*7] = (int) (tmp0 - tmp7);
287     wsptr[DCTSIZE*1] = (int) (tmp1 + tmp6);
288     wsptr[DCTSIZE*6] = (int) (tmp1 - tmp6);
289     wsptr[DCTSIZE*2] = (int) (tmp2 + tmp5);
290     wsptr[DCTSIZE*5] = (int) (tmp2 - tmp5);
291     wsptr[DCTSIZE*4] = (int) (tmp3 + tmp4);
292     wsptr[DCTSIZE*3] = (int) (tmp3 - tmp4);
293
294     inptr++;                    /* advance pointers to next column */
295     quantptr++;
296     wsptr++;
297   }
298   
299   /* Pass 2: process rows from work array, store into output array. */
300   /* Note that we must descale the results by a factor of 8 == 2**3, */
301   /* and also undo the PASS1_BITS scaling. */
302
303   wsptr = workspace;
304   for (ctr = 0; ctr < DCTSIZE; ctr++) {
305     outptr = output_buf[ctr] + output_col;
306     /* Rows of zeroes can be exploited in the same way as we did with columns.
307      * However, the column calculation has created many nonzero AC terms, so
308      * the simplification applies less often (typically 5% to 10% of the time).
309      * On machines with very fast multiplication, it's possible that the
310      * test takes more time than it's worth.  In that case this section
311      * may be commented out.
312      */
313     
314 #ifndef NO_ZERO_ROW_TEST
315     if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[4] | wsptr[5] | wsptr[6] |
316          wsptr[7]) == 0) {
317       /* AC terms all zero */
318       JSAMPLE dcval = range_limit[IDESCALE(wsptr[0], PASS1_BITS+3)
319                                   & RANGE_MASK];
320       
321       outptr[0] = dcval;
322       outptr[1] = dcval;
323       outptr[2] = dcval;
324       outptr[3] = dcval;
325       outptr[4] = dcval;
326       outptr[5] = dcval;
327       outptr[6] = dcval;
328       outptr[7] = dcval;
329
330       wsptr += DCTSIZE;         /* advance pointer to next row */
331       continue;
332     }
333 #endif
334     
335     /* Even part */
336
337     tmp10 = ((DCTELEM) wsptr[0] + (DCTELEM) wsptr[4]);
338     tmp11 = ((DCTELEM) wsptr[0] - (DCTELEM) wsptr[4]);
339
340     tmp13 = ((DCTELEM) wsptr[2] + (DCTELEM) wsptr[6]);
341     tmp12 = MULTIPLY((DCTELEM) wsptr[2] - (DCTELEM) wsptr[6], FIX_1_414213562)
342             - tmp13;
343
344     tmp0 = tmp10 + tmp13;
345     tmp3 = tmp10 - tmp13;
346     tmp1 = tmp11 + tmp12;
347     tmp2 = tmp11 - tmp12;
348
349     /* Odd part */
350
351     z13 = (DCTELEM) wsptr[5] + (DCTELEM) wsptr[3];
352     z10 = (DCTELEM) wsptr[5] - (DCTELEM) wsptr[3];
353     z11 = (DCTELEM) wsptr[1] + (DCTELEM) wsptr[7];
354     z12 = (DCTELEM) wsptr[1] - (DCTELEM) wsptr[7];
355
356     tmp7 = z11 + z13;           /* phase 5 */
357     tmp11 = MULTIPLY(z11 - z13, FIX_1_414213562); /* 2*c4 */
358
359     z5 = MULTIPLY(z10 + z12, FIX_1_847759065); /* 2*c2 */
360     tmp10 = MULTIPLY(z12, FIX_1_082392200) - z5; /* 2*(c2-c6) */
361     tmp12 = MULTIPLY(z10, - FIX_2_613125930) + z5; /* -2*(c2+c6) */
362
363     tmp6 = tmp12 - tmp7;        /* phase 2 */
364     tmp5 = tmp11 - tmp6;
365     tmp4 = tmp10 + tmp5;
366
367     /* Final output stage: scale down by a factor of 8 and range-limit */
368
369     outptr[0] = range_limit[IDESCALE(tmp0 + tmp7, PASS1_BITS+3)
370                             & RANGE_MASK];
371     outptr[7] = range_limit[IDESCALE(tmp0 - tmp7, PASS1_BITS+3)
372                             & RANGE_MASK];
373     outptr[1] = range_limit[IDESCALE(tmp1 + tmp6, PASS1_BITS+3)
374                             & RANGE_MASK];
375     outptr[6] = range_limit[IDESCALE(tmp1 - tmp6, PASS1_BITS+3)
376                             & RANGE_MASK];
377     outptr[2] = range_limit[IDESCALE(tmp2 + tmp5, PASS1_BITS+3)
378                             & RANGE_MASK];
379     outptr[5] = range_limit[IDESCALE(tmp2 - tmp5, PASS1_BITS+3)
380                             & RANGE_MASK];
381     outptr[4] = range_limit[IDESCALE(tmp3 + tmp4, PASS1_BITS+3)
382                             & RANGE_MASK];
383     outptr[3] = range_limit[IDESCALE(tmp3 - tmp4, PASS1_BITS+3)
384                             & RANGE_MASK];
385
386     wsptr += DCTSIZE;           /* advance pointer to next row */
387   }
388 }
389
390 #endif /* DCT_IFAST_SUPPORTED */