Back to index

lightning-sunbird  0.9+nobinonly
jidctred.c
Go to the documentation of this file.
00001 /*
00002  * jidctred.c
00003  *
00004  * Copyright (C) 1994-1998, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains inverse-DCT routines that produce reduced-size output:
00009  * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
00010  *
00011  * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
00012  * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
00013  * with an 8-to-4 step that produces the four averages of two adjacent outputs
00014  * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
00015  * These steps were derived by computing the corresponding values at the end
00016  * of the normal LL&M code, then simplifying as much as possible.
00017  *
00018  * 1x1 is trivial: just take the DC coefficient divided by 8.
00019  *
00020  * See jidctint.c for additional comments.
00021  */
00022 
00023 #define JPEG_INTERNALS
00024 #include "jinclude.h"
00025 #include "jpeglib.h"
00026 #include "jdct.h"           /* Private declarations for DCT subsystem */
00027 
00028 #ifdef IDCT_SCALING_SUPPORTED
00029 
00030 
00031 /*
00032  * This module is specialized to the case DCTSIZE = 8.
00033  */
00034 
00035 #if DCTSIZE != 8
00036   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
00037 #endif
00038 
00039 
00040 /* Scaling is the same as in jidctint.c. */
00041 
00042 #if BITS_IN_JSAMPLE == 8
00043 #define CONST_BITS  13
00044 #define PASS1_BITS  2
00045 #else
00046 #define CONST_BITS  13
00047 #define PASS1_BITS  1              /* lose a little precision to avoid overflow */
00048 #endif
00049 
00050 /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
00051  * causing a lot of useless floating-point operations at run time.
00052  * To get around this we use the following pre-calculated constants.
00053  * If you change CONST_BITS you may want to add appropriate values.
00054  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
00055  */
00056 
00057 #if CONST_BITS == 13
00058 #define FIX_0_211164243  ((INT32)  1730)  /* FIX(0.211164243) */
00059 #define FIX_0_509795579  ((INT32)  4176)  /* FIX(0.509795579) */
00060 #define FIX_0_601344887  ((INT32)  4926)  /* FIX(0.601344887) */
00061 #define FIX_0_720959822  ((INT32)  5906)  /* FIX(0.720959822) */
00062 #define FIX_0_765366865  ((INT32)  6270)  /* FIX(0.765366865) */
00063 #define FIX_0_850430095  ((INT32)  6967)  /* FIX(0.850430095) */
00064 #define FIX_0_899976223  ((INT32)  7373)  /* FIX(0.899976223) */
00065 #define FIX_1_061594337  ((INT32)  8697)  /* FIX(1.061594337) */
00066 #define FIX_1_272758580  ((INT32)  10426) /* FIX(1.272758580) */
00067 #define FIX_1_451774981  ((INT32)  11893) /* FIX(1.451774981) */
00068 #define FIX_1_847759065  ((INT32)  15137) /* FIX(1.847759065) */
00069 #define FIX_2_172734803  ((INT32)  17799) /* FIX(2.172734803) */
00070 #define FIX_2_562915447  ((INT32)  20995) /* FIX(2.562915447) */
00071 #define FIX_3_624509785  ((INT32)  29692) /* FIX(3.624509785) */
00072 #else
00073 #define FIX_0_211164243  FIX(0.211164243)
00074 #define FIX_0_509795579  FIX(0.509795579)
00075 #define FIX_0_601344887  FIX(0.601344887)
00076 #define FIX_0_720959822  FIX(0.720959822)
00077 #define FIX_0_765366865  FIX(0.765366865)
00078 #define FIX_0_850430095  FIX(0.850430095)
00079 #define FIX_0_899976223  FIX(0.899976223)
00080 #define FIX_1_061594337  FIX(1.061594337)
00081 #define FIX_1_272758580  FIX(1.272758580)
00082 #define FIX_1_451774981  FIX(1.451774981)
00083 #define FIX_1_847759065  FIX(1.847759065)
00084 #define FIX_2_172734803  FIX(2.172734803)
00085 #define FIX_2_562915447  FIX(2.562915447)
00086 #define FIX_3_624509785  FIX(3.624509785)
00087 #endif
00088 
00089 
00090 /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
00091  * For 8-bit samples with the recommended scaling, all the variable
00092  * and constant values involved are no more than 16 bits wide, so a
00093  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
00094  * For 12-bit samples, a full 32-bit multiplication will be needed.
00095  */
00096 
00097 #if BITS_IN_JSAMPLE == 8
00098 #define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
00099 #else
00100 #define MULTIPLY(var,const)  ((var) * (const))
00101 #endif
00102 
00103 
00104 /* Dequantize a coefficient by multiplying it by the multiplier-table
00105  * entry; produce an int result.  In this module, both inputs and result
00106  * are 16 bits or less, so either int or short multiply will work.
00107  */
00108 
00109 #define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
00110 
00111 
00112 /*
00113  * Perform dequantization and inverse DCT on one block of coefficients,
00114  * producing a reduced-size 4x4 output block.
00115  */
00116 
00117 GLOBAL(void)
00118 jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00119               JCOEFPTR coef_block,
00120               JSAMPARRAY output_buf, JDIMENSION output_col)
00121 {
00122   INT32 tmp0, tmp2, tmp10, tmp12;
00123   INT32 z1, z2, z3, z4;
00124   JCOEFPTR inptr;
00125   ISLOW_MULT_TYPE * quantptr;
00126   int * wsptr;
00127   JSAMPROW outptr;
00128   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
00129   int ctr;
00130   int workspace[DCTSIZE*4]; /* buffers data between passes */
00131   SHIFT_TEMPS
00132 
00133   /* Pass 1: process columns from input, store into work array. */
00134 
00135   inptr = coef_block;
00136   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
00137   wsptr = workspace;
00138   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
00139     /* Don't bother to process column 4, because second pass won't use it */
00140     if (ctr == DCTSIZE-4)
00141       continue;
00142     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
00143        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*5] == 0 &&
00144        inptr[DCTSIZE*6] == 0 && inptr[DCTSIZE*7] == 0) {
00145       /* AC terms all zero; we need not examine term 4 for 4x4 output */
00146       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
00147       
00148       wsptr[DCTSIZE*0] = dcval;
00149       wsptr[DCTSIZE*1] = dcval;
00150       wsptr[DCTSIZE*2] = dcval;
00151       wsptr[DCTSIZE*3] = dcval;
00152       
00153       continue;
00154     }
00155     
00156     /* Even part */
00157     
00158     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00159     tmp0 <<= (CONST_BITS+1);
00160     
00161     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
00162     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
00163 
00164     tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
00165     
00166     tmp10 = tmp0 + tmp2;
00167     tmp12 = tmp0 - tmp2;
00168     
00169     /* Odd part */
00170     
00171     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
00172     z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
00173     z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
00174     z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
00175     
00176     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
00177         + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
00178         + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
00179         + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
00180     
00181     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
00182         + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
00183         + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
00184         + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
00185 
00186     /* Final output stage */
00187     
00188     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
00189     wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
00190     wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
00191     wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
00192   }
00193   
00194   /* Pass 2: process 4 rows from work array, store into output array. */
00195 
00196   wsptr = workspace;
00197   for (ctr = 0; ctr < 4; ctr++) {
00198     outptr = output_buf[ctr] + output_col;
00199     /* It's not clear whether a zero row test is worthwhile here ... */
00200 
00201 #ifndef NO_ZERO_ROW_TEST
00202     if (wsptr[1] == 0 && wsptr[2] == 0 && wsptr[3] == 0 &&
00203        wsptr[5] == 0 && wsptr[6] == 0 && wsptr[7] == 0) {
00204       /* AC terms all zero */
00205       JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
00206                               & RANGE_MASK];
00207       
00208       outptr[0] = dcval;
00209       outptr[1] = dcval;
00210       outptr[2] = dcval;
00211       outptr[3] = dcval;
00212       
00213       wsptr += DCTSIZE;            /* advance pointer to next row */
00214       continue;
00215     }
00216 #endif
00217     
00218     /* Even part */
00219     
00220     tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
00221     
00222     tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
00223         + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
00224     
00225     tmp10 = tmp0 + tmp2;
00226     tmp12 = tmp0 - tmp2;
00227     
00228     /* Odd part */
00229     
00230     z1 = (INT32) wsptr[7];
00231     z2 = (INT32) wsptr[5];
00232     z3 = (INT32) wsptr[3];
00233     z4 = (INT32) wsptr[1];
00234     
00235     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
00236         + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
00237         + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
00238         + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
00239     
00240     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
00241         + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
00242         + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
00243         + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
00244 
00245     /* Final output stage */
00246     
00247     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
00248                                      CONST_BITS+PASS1_BITS+3+1)
00249                          & RANGE_MASK];
00250     outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
00251                                      CONST_BITS+PASS1_BITS+3+1)
00252                          & RANGE_MASK];
00253     outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
00254                                      CONST_BITS+PASS1_BITS+3+1)
00255                          & RANGE_MASK];
00256     outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
00257                                      CONST_BITS+PASS1_BITS+3+1)
00258                          & RANGE_MASK];
00259     
00260     wsptr += DCTSIZE;              /* advance pointer to next row */
00261   }
00262 }
00263 
00264 
00265 /*
00266  * Perform dequantization and inverse DCT on one block of coefficients,
00267  * producing a reduced-size 2x2 output block.
00268  */
00269 
00270 GLOBAL(void)
00271 jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00272               JCOEFPTR coef_block,
00273               JSAMPARRAY output_buf, JDIMENSION output_col)
00274 {
00275   INT32 tmp0, tmp10, z1;
00276   JCOEFPTR inptr;
00277   ISLOW_MULT_TYPE * quantptr;
00278   int * wsptr;
00279   JSAMPROW outptr;
00280   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
00281   int ctr;
00282   int workspace[DCTSIZE*2]; /* buffers data between passes */
00283   SHIFT_TEMPS
00284 
00285   /* Pass 1: process columns from input, store into work array. */
00286 
00287   inptr = coef_block;
00288   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
00289   wsptr = workspace;
00290   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
00291     /* Don't bother to process columns 2,4,6 */
00292     if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
00293       continue;
00294     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*3] == 0 &&
00295        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*7] == 0) {
00296       /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
00297       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
00298       
00299       wsptr[DCTSIZE*0] = dcval;
00300       wsptr[DCTSIZE*1] = dcval;
00301       
00302       continue;
00303     }
00304     
00305     /* Even part */
00306     
00307     z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00308     tmp10 = z1 << (CONST_BITS+2);
00309     
00310     /* Odd part */
00311 
00312     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
00313     tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
00314     z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
00315     tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
00316     z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
00317     tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
00318     z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
00319     tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
00320 
00321     /* Final output stage */
00322     
00323     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
00324     wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
00325   }
00326   
00327   /* Pass 2: process 2 rows from work array, store into output array. */
00328 
00329   wsptr = workspace;
00330   for (ctr = 0; ctr < 2; ctr++) {
00331     outptr = output_buf[ctr] + output_col;
00332     /* It's not clear whether a zero row test is worthwhile here ... */
00333 
00334 #ifndef NO_ZERO_ROW_TEST
00335     if (wsptr[1] == 0 && wsptr[3] == 0 && wsptr[5] == 0 && wsptr[7] == 0) {
00336       /* AC terms all zero */
00337       JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
00338                               & RANGE_MASK];
00339       
00340       outptr[0] = dcval;
00341       outptr[1] = dcval;
00342       
00343       wsptr += DCTSIZE;            /* advance pointer to next row */
00344       continue;
00345     }
00346 #endif
00347     
00348     /* Even part */
00349     
00350     tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
00351     
00352     /* Odd part */
00353 
00354     tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
00355         + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
00356         + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
00357         + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
00358 
00359     /* Final output stage */
00360     
00361     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
00362                                      CONST_BITS+PASS1_BITS+3+2)
00363                          & RANGE_MASK];
00364     outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
00365                                      CONST_BITS+PASS1_BITS+3+2)
00366                          & RANGE_MASK];
00367     
00368     wsptr += DCTSIZE;              /* advance pointer to next row */
00369   }
00370 }
00371 
00372 
00373 /*
00374  * Perform dequantization and inverse DCT on one block of coefficients,
00375  * producing a reduced-size 1x1 output block.
00376  */
00377 
00378 GLOBAL(void)
00379 jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00380               JCOEFPTR coef_block,
00381               JSAMPARRAY output_buf, JDIMENSION output_col)
00382 {
00383   int dcval;
00384   ISLOW_MULT_TYPE * quantptr;
00385   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
00386   SHIFT_TEMPS
00387 
00388   /* We hardly need an inverse DCT routine for this: just take the
00389    * average pixel value, which is one-eighth of the DC coefficient.
00390    */
00391   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
00392   dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
00393   dcval = (int) DESCALE((INT32) dcval, 3);
00394 
00395   output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
00396 }
00397 
00398 #endif /* IDCT_SCALING_SUPPORTED */