source: golgotha/src/i4/loaders/jpg/jidctred.cc @ 608

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