jdk
402 строки · 13.8 Кб
1/*
2* reserved comment block
3* DO NOT REMOVE OR ALTER!
4*/
5/*
6* jidctred.c
7*
8* Copyright (C) 1994-1998, Thomas G. Lane.
9* This file is part of the Independent JPEG Group's software.
10* For conditions of distribution and use, see the accompanying README file.
11*
12* This file contains inverse-DCT routines that produce reduced-size output:
13* either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
14*
15* The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
16* algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step
17* with an 8-to-4 step that produces the four averages of two adjacent outputs
18* (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
19* These steps were derived by computing the corresponding values at the end
20* of the normal LL&M code, then simplifying as much as possible.
21*
22* 1x1 is trivial: just take the DC coefficient divided by 8.
23*
24* See jidctint.c for additional comments.
25*/
26
27#define JPEG_INTERNALS
28#include "jinclude.h"
29#include "jpeglib.h"
30#include "jdct.h" /* Private declarations for DCT subsystem */
31
32#ifdef IDCT_SCALING_SUPPORTED
33
34
35/*
36* This module is specialized to the case DCTSIZE = 8.
37*/
38
39#if DCTSIZE != 8
40Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
41#endif
42
43
44/* Scaling is the same as in jidctint.c. */
45
46#if BITS_IN_JSAMPLE == 8
47#define CONST_BITS 13
48#define PASS1_BITS 2
49#else
50#define CONST_BITS 13
51#define PASS1_BITS 1 /* lose a little precision to avoid overflow */
52#endif
53
54/* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
55* causing a lot of useless floating-point operations at run time.
56* To get around this we use the following pre-calculated constants.
57* If you change CONST_BITS you may want to add appropriate values.
58* (With a reasonable C compiler, you can just rely on the FIX() macro...)
59*/
60
61#if CONST_BITS == 13
62#define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */
63#define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */
64#define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */
65#define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */
66#define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
67#define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */
68#define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
69#define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */
70#define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */
71#define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */
72#define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
73#define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */
74#define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
75#define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */
76#else
77#define FIX_0_211164243 FIX(0.211164243)
78#define FIX_0_509795579 FIX(0.509795579)
79#define FIX_0_601344887 FIX(0.601344887)
80#define FIX_0_720959822 FIX(0.720959822)
81#define FIX_0_765366865 FIX(0.765366865)
82#define FIX_0_850430095 FIX(0.850430095)
83#define FIX_0_899976223 FIX(0.899976223)
84#define FIX_1_061594337 FIX(1.061594337)
85#define FIX_1_272758580 FIX(1.272758580)
86#define FIX_1_451774981 FIX(1.451774981)
87#define FIX_1_847759065 FIX(1.847759065)
88#define FIX_2_172734803 FIX(2.172734803)
89#define FIX_2_562915447 FIX(2.562915447)
90#define FIX_3_624509785 FIX(3.624509785)
91#endif
92
93
94/* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
95* For 8-bit samples with the recommended scaling, all the variable
96* and constant values involved are no more than 16 bits wide, so a
97* 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
98* For 12-bit samples, a full 32-bit multiplication will be needed.
99*/
100
101#if BITS_IN_JSAMPLE == 8
102#define MULTIPLY(var,const) MULTIPLY16C16(var,const)
103#else
104#define MULTIPLY(var,const) ((var) * (const))
105#endif
106
107
108/* Dequantize a coefficient by multiplying it by the multiplier-table
109* entry; produce an int result. In this module, both inputs and result
110* are 16 bits or less, so either int or short multiply will work.
111*/
112
113#define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
114
115
116/*
117* Perform dequantization and inverse DCT on one block of coefficients,
118* producing a reduced-size 4x4 output block.
119*/
120
121GLOBAL(void)
122jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
123JCOEFPTR coef_block,
124JSAMPARRAY output_buf, JDIMENSION output_col)
125{
126INT32 tmp0, tmp2, tmp10, tmp12;
127INT32 z1, z2, z3, z4;
128JCOEFPTR inptr;
129ISLOW_MULT_TYPE * quantptr;
130int * wsptr;
131JSAMPROW outptr;
132JSAMPLE *range_limit = IDCT_range_limit(cinfo);
133int ctr;
134int workspace[DCTSIZE*4]; /* buffers data between passes */
135SHIFT_TEMPS
136
137/* Pass 1: process columns from input, store into work array. */
138
139inptr = coef_block;
140quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
141wsptr = workspace;
142for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
143/* Don't bother to process column 4, because second pass won't use it */
144if (ctr == DCTSIZE-4)
145continue;
146if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
147inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
148inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
149/* AC terms all zero; we need not examine term 4 for 4x4 output */
150int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
151
152wsptr[DCTSIZE*0] = dcval;
153wsptr[DCTSIZE*1] = dcval;
154wsptr[DCTSIZE*2] = dcval;
155wsptr[DCTSIZE*3] = dcval;
156
157continue;
158}
159
160/* Even part */
161
162tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
163tmp0 <<= (CONST_BITS+1);
164
165z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
166z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
167
168tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
169
170tmp10 = tmp0 + tmp2;
171tmp12 = tmp0 - tmp2;
172
173/* Odd part */
174
175z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
176z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
177z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
178z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
179
180tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
181+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
182+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
183+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
184
185tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
186+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
187+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
188+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
189
190/* Final output stage */
191
192wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
193wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
194wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
195wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
196}
197
198/* Pass 2: process 4 rows from work array, store into output array. */
199
200wsptr = workspace;
201for (ctr = 0; ctr < 4; ctr++) {
202outptr = output_buf[ctr] + output_col;
203/* It's not clear whether a zero row test is worthwhile here ... */
204
205#ifndef NO_ZERO_ROW_TEST
206if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
207wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
208/* AC terms all zero */
209JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
210& RANGE_MASK];
211
212outptr[0] = dcval;
213outptr[1] = dcval;
214outptr[2] = dcval;
215outptr[3] = dcval;
216
217wsptr += DCTSIZE; /* advance pointer to next row */
218continue;
219}
220#endif
221
222/* Even part */
223
224tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
225
226tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
227+ MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
228
229tmp10 = tmp0 + tmp2;
230tmp12 = tmp0 - tmp2;
231
232/* Odd part */
233
234z1 = (INT32) wsptr[7];
235z2 = (INT32) wsptr[5];
236z3 = (INT32) wsptr[3];
237z4 = (INT32) wsptr[1];
238
239tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
240+ MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
241+ MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
242+ MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
243
244tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
245+ MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
246+ MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
247+ MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
248
249/* Final output stage */
250
251outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
252CONST_BITS+PASS1_BITS+3+1)
253& RANGE_MASK];
254outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
255CONST_BITS+PASS1_BITS+3+1)
256& RANGE_MASK];
257outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
258CONST_BITS+PASS1_BITS+3+1)
259& RANGE_MASK];
260outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
261CONST_BITS+PASS1_BITS+3+1)
262& RANGE_MASK];
263
264wsptr += DCTSIZE; /* advance pointer to next row */
265}
266}
267
268
269/*
270* Perform dequantization and inverse DCT on one block of coefficients,
271* producing a reduced-size 2x2 output block.
272*/
273
274GLOBAL(void)
275jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
276JCOEFPTR coef_block,
277JSAMPARRAY output_buf, JDIMENSION output_col)
278{
279INT32 tmp0, tmp10, z1;
280JCOEFPTR inptr;
281ISLOW_MULT_TYPE * quantptr;
282int * wsptr;
283JSAMPROW outptr;
284JSAMPLE *range_limit = IDCT_range_limit(cinfo);
285int ctr;
286int workspace[DCTSIZE*2]; /* buffers data between passes */
287SHIFT_TEMPS
288
289/* Pass 1: process columns from input, store into work array. */
290
291inptr = coef_block;
292quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
293wsptr = workspace;
294for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
295/* Don't bother to process columns 2,4,6 */
296if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
297continue;
298if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
299inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
300/* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
301int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
302
303wsptr[DCTSIZE*0] = dcval;
304wsptr[DCTSIZE*1] = dcval;
305
306continue;
307}
308
309/* Even part */
310
311z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
312tmp10 = z1 << (CONST_BITS+2);
313
314/* Odd part */
315
316z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
317tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
318z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
319tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
320z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
321tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
322z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
323tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
324
325/* Final output stage */
326
327wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
328wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
329}
330
331/* Pass 2: process 2 rows from work array, store into output array. */
332
333wsptr = workspace;
334for (ctr = 0; ctr < 2; ctr++) {
335outptr = output_buf[ctr] + output_col;
336/* It's not clear whether a zero row test is worthwhile here ... */
337
338#ifndef NO_ZERO_ROW_TEST
339if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
340/* AC terms all zero */
341JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
342& RANGE_MASK];
343
344outptr[0] = dcval;
345outptr[1] = dcval;
346
347wsptr += DCTSIZE; /* advance pointer to next row */
348continue;
349}
350#endif
351
352/* Even part */
353
354tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
355
356/* Odd part */
357
358tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
359+ MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
360+ MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
361+ MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
362
363/* Final output stage */
364
365outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
366CONST_BITS+PASS1_BITS+3+2)
367& RANGE_MASK];
368outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
369CONST_BITS+PASS1_BITS+3+2)
370& RANGE_MASK];
371
372wsptr += DCTSIZE; /* advance pointer to next row */
373}
374}
375
376
377/*
378* Perform dequantization and inverse DCT on one block of coefficients,
379* producing a reduced-size 1x1 output block.
380*/
381
382GLOBAL(void)
383jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
384JCOEFPTR coef_block,
385JSAMPARRAY output_buf, JDIMENSION output_col)
386{
387int dcval;
388ISLOW_MULT_TYPE * quantptr;
389JSAMPLE *range_limit = IDCT_range_limit(cinfo);
390SHIFT_TEMPS
391
392/* We hardly need an inverse DCT routine for this: just take the
393* average pixel value, which is one-eighth of the DC coefficient.
394*/
395quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
396dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
397dcval = (int) DESCALE((INT32) dcval, 3);
398
399output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
400}
401
402#endif /* IDCT_SCALING_SUPPORTED */
403