Back to index

plt-scheme  4.2.1
jidctflt.c
Go to the documentation of this file.
00001 /*
00002  * jidctflt.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 a floating-point implementation of the
00009  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
00010  * must also perform dequantization of the input coefficients.
00011  *
00012  * This implementation should be more accurate than either of the integer
00013  * IDCT implementations.  However, it may not give the same results on all
00014  * machines because of differences in roundoff behavior.  Speed will depend
00015  * on the hardware's floating point capacity.
00016  *
00017  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
00018  * on each row (or vice versa, but it's more convenient to emit a row at
00019  * a time).  Direct algorithms are also available, but they are much more
00020  * complex and seem not to be any faster when reduced to code.
00021  *
00022  * This implementation is based on Arai, Agui, and Nakajima's algorithm for
00023  * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
00024  * Japanese, but the algorithm is described in the Pennebaker & Mitchell
00025  * JPEG textbook (see REFERENCES section in file README).  The following code
00026  * is based directly on figure 4-8 in P&M.
00027  * While an 8-point DCT cannot be done in less than 11 multiplies, it is
00028  * possible to arrange the computation so that many of the multiplies are
00029  * simple scalings of the final outputs.  These multiplies can then be
00030  * folded into the multiplications or divisions by the JPEG quantization
00031  * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
00032  * to be done in the DCT itself.
00033  * The primary disadvantage of this method is that with a fixed-point
00034  * implementation, accuracy is lost due to imprecise representation of the
00035  * scaled quantization values.  However, that problem does not arise if
00036  * we use floating point arithmetic.
00037  */
00038 
00039 #define JPEG_INTERNALS
00040 #include "jinclude.h"
00041 #include "jpeglib.h"
00042 #include "jdct.h"           /* Private declarations for DCT subsystem */
00043 
00044 #ifdef DCT_FLOAT_SUPPORTED
00045 
00046 
00047 /*
00048  * This module is specialized to the case DCTSIZE = 8.
00049  */
00050 
00051 #if DCTSIZE != 8
00052   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
00053 #endif
00054 
00055 
00056 /* Dequantize a coefficient by multiplying it by the multiplier-table
00057  * entry; produce a float result.
00058  */
00059 
00060 #define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
00061 
00062 
00063 /*
00064  * Perform dequantization and inverse DCT on one block of coefficients.
00065  */
00066 
00067 GLOBAL(void)
00068 jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
00069                JCOEFPTR coef_block,
00070                JSAMPARRAY output_buf, JDIMENSION output_col)
00071 {
00072   FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
00073   FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
00074   FAST_FLOAT z5, z10, z11, z12, z13;
00075   JCOEFPTR inptr;
00076   FLOAT_MULT_TYPE * quantptr;
00077   FAST_FLOAT * wsptr;
00078   JSAMPROW outptr;
00079   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
00080   int ctr;
00081   FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
00082   SHIFT_TEMPS
00083 
00084   /* Pass 1: process columns from input, store into work array. */
00085 
00086   inptr = coef_block;
00087   quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
00088   wsptr = workspace;
00089   for (ctr = DCTSIZE; ctr > 0; ctr--) {
00090     /* Due to quantization, we will usually find that many of the input
00091      * coefficients are zero, especially the AC terms.  We can exploit this
00092      * by short-circuiting the IDCT calculation for any column in which all
00093      * the AC terms are zero.  In that case each output is equal to the
00094      * DC coefficient (with scale factor as needed).
00095      * With typical images and quantization tables, half or more of the
00096      * column DCT calculations can be simplified this way.
00097      */
00098     
00099     if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
00100        inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
00101        inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
00102        inptr[DCTSIZE*7] == 0) {
00103       /* AC terms all zero */
00104       FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00105       
00106       wsptr[DCTSIZE*0] = dcval;
00107       wsptr[DCTSIZE*1] = dcval;
00108       wsptr[DCTSIZE*2] = dcval;
00109       wsptr[DCTSIZE*3] = dcval;
00110       wsptr[DCTSIZE*4] = dcval;
00111       wsptr[DCTSIZE*5] = dcval;
00112       wsptr[DCTSIZE*6] = dcval;
00113       wsptr[DCTSIZE*7] = dcval;
00114       
00115       inptr++;                     /* advance pointers to next column */
00116       quantptr++;
00117       wsptr++;
00118       continue;
00119     }
00120     
00121     /* Even part */
00122 
00123     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
00124     tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
00125     tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
00126     tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
00127 
00128     tmp10 = tmp0 + tmp2;    /* phase 3 */
00129     tmp11 = tmp0 - tmp2;
00130 
00131     tmp13 = tmp1 + tmp3;    /* phases 5-3 */
00132     tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
00133 
00134     tmp0 = tmp10 + tmp13;   /* phase 2 */
00135     tmp3 = tmp10 - tmp13;
00136     tmp1 = tmp11 + tmp12;
00137     tmp2 = tmp11 - tmp12;
00138     
00139     /* Odd part */
00140 
00141     tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
00142     tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
00143     tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
00144     tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
00145 
00146     z13 = tmp6 + tmp5;             /* phase 6 */
00147     z10 = tmp6 - tmp5;
00148     z11 = tmp4 + tmp7;
00149     z12 = tmp4 - tmp7;
00150 
00151     tmp7 = z11 + z13;              /* phase 5 */
00152     tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
00153 
00154     z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
00155     tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
00156     tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
00157 
00158     tmp6 = tmp12 - tmp7;    /* phase 2 */
00159     tmp5 = tmp11 - tmp6;
00160     tmp4 = tmp10 + tmp5;
00161 
00162     wsptr[DCTSIZE*0] = tmp0 + tmp7;
00163     wsptr[DCTSIZE*7] = tmp0 - tmp7;
00164     wsptr[DCTSIZE*1] = tmp1 + tmp6;
00165     wsptr[DCTSIZE*6] = tmp1 - tmp6;
00166     wsptr[DCTSIZE*2] = tmp2 + tmp5;
00167     wsptr[DCTSIZE*5] = tmp2 - tmp5;
00168     wsptr[DCTSIZE*4] = tmp3 + tmp4;
00169     wsptr[DCTSIZE*3] = tmp3 - tmp4;
00170 
00171     inptr++;                /* advance pointers to next column */
00172     quantptr++;
00173     wsptr++;
00174   }
00175   
00176   /* Pass 2: process rows from work array, store into output array. */
00177   /* Note that we must descale the results by a factor of 8 == 2**3. */
00178 
00179   wsptr = workspace;
00180   for (ctr = 0; ctr < DCTSIZE; ctr++) {
00181     outptr = output_buf[ctr] + output_col;
00182     /* Rows of zeroes can be exploited in the same way as we did with columns.
00183      * However, the column calculation has created many nonzero AC terms, so
00184      * the simplification applies less often (typically 5% to 10% of the time).
00185      * And testing floats for zero is relatively expensive, so we don't bother.
00186      */
00187     
00188     /* Even part */
00189 
00190     tmp10 = wsptr[0] + wsptr[4];
00191     tmp11 = wsptr[0] - wsptr[4];
00192 
00193     tmp13 = wsptr[2] + wsptr[6];
00194     tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
00195 
00196     tmp0 = tmp10 + tmp13;
00197     tmp3 = tmp10 - tmp13;
00198     tmp1 = tmp11 + tmp12;
00199     tmp2 = tmp11 - tmp12;
00200 
00201     /* Odd part */
00202 
00203     z13 = wsptr[5] + wsptr[3];
00204     z10 = wsptr[5] - wsptr[3];
00205     z11 = wsptr[1] + wsptr[7];
00206     z12 = wsptr[1] - wsptr[7];
00207 
00208     tmp7 = z11 + z13;
00209     tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
00210 
00211     z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
00212     tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
00213     tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
00214 
00215     tmp6 = tmp12 - tmp7;
00216     tmp5 = tmp11 - tmp6;
00217     tmp4 = tmp10 + tmp5;
00218 
00219     /* Final output stage: scale down by a factor of 8 and range-limit */
00220 
00221     outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
00222                          & RANGE_MASK];
00223     outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
00224                          & RANGE_MASK];
00225     outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
00226                          & RANGE_MASK];
00227     outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
00228                          & RANGE_MASK];
00229     outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
00230                          & RANGE_MASK];
00231     outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
00232                          & RANGE_MASK];
00233     outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
00234                          & RANGE_MASK];
00235     outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
00236                          & RANGE_MASK];
00237     
00238     wsptr += DCTSIZE;              /* advance pointer to next row */
00239   }
00240 }
00241 
00242 #endif /* DCT_FLOAT_SUPPORTED */