kusano 7d535a
/*
kusano 7d535a
 * jidctflt.c
kusano 7d535a
 *
kusano 7d535a
 * Copyright (C) 1994-1998, Thomas G. Lane.
kusano 7d535a
 * Modified 2010 by Guido Vollbeding.
kusano 7d535a
 * This file is part of the Independent JPEG Group's software.
kusano 7d535a
 * For conditions of distribution and use, see the accompanying README file.
kusano 7d535a
 *
kusano 7d535a
 * This file contains a floating-point implementation of the
kusano 7d535a
 * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
kusano 7d535a
 * must also perform dequantization of the input coefficients.
kusano 7d535a
 *
kusano 7d535a
 * This implementation should be more accurate than either of the integer
kusano 7d535a
 * IDCT implementations.  However, it may not give the same results on all
kusano 7d535a
 * machines because of differences in roundoff behavior.  Speed will depend
kusano 7d535a
 * on the hardware's floating point capacity.
kusano 7d535a
 *
kusano 7d535a
 * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
kusano 7d535a
 * on each row (or vice versa, but it's more convenient to emit a row at
kusano 7d535a
 * a time).  Direct algorithms are also available, but they are much more
kusano 7d535a
 * complex and seem not to be any faster when reduced to code.
kusano 7d535a
 *
kusano 7d535a
 * This implementation is based on Arai, Agui, and Nakajima's algorithm for
kusano 7d535a
 * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
kusano 7d535a
 * Japanese, but the algorithm is described in the Pennebaker & Mitchell
kusano 7d535a
 * JPEG textbook (see REFERENCES section in file README).  The following code
kusano 7d535a
 * is based directly on figure 4-8 in P&M.
kusano 7d535a
 * While an 8-point DCT cannot be done in less than 11 multiplies, it is
kusano 7d535a
 * possible to arrange the computation so that many of the multiplies are
kusano 7d535a
 * simple scalings of the final outputs.  These multiplies can then be
kusano 7d535a
 * folded into the multiplications or divisions by the JPEG quantization
kusano 7d535a
 * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
kusano 7d535a
 * to be done in the DCT itself.
kusano 7d535a
 * The primary disadvantage of this method is that with a fixed-point
kusano 7d535a
 * implementation, accuracy is lost due to imprecise representation of the
kusano 7d535a
 * scaled quantization values.  However, that problem does not arise if
kusano 7d535a
 * we use floating point arithmetic.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define JPEG_INTERNALS
kusano 7d535a
#include "jinclude.h"
kusano 7d535a
#include "jpeglib.h"
kusano 7d535a
#include "jdct.h"		/* Private declarations for DCT subsystem */
kusano 7d535a
kusano 7d535a
#ifdef DCT_FLOAT_SUPPORTED
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This module is specialized to the case DCTSIZE = 8.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#if DCTSIZE != 8
kusano 7d535a
  Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Dequantize a coefficient by multiplying it by the multiplier-table
kusano 7d535a
 * entry; produce a float result.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Perform dequantization and inverse DCT on one block of coefficients.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
GLOBAL(void)
kusano 7d535a
jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
kusano 7d535a
		 JCOEFPTR coef_block,
kusano 7d535a
		 JSAMPARRAY output_buf, JDIMENSION output_col)
kusano 7d535a
{
kusano 7d535a
  FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
kusano 7d535a
  FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
kusano 7d535a
  FAST_FLOAT z5, z10, z11, z12, z13;
kusano 7d535a
  JCOEFPTR inptr;
kusano 7d535a
  FLOAT_MULT_TYPE * quantptr;
kusano 7d535a
  FAST_FLOAT * wsptr;
kusano 7d535a
  JSAMPROW outptr;
kusano 7d535a
  JSAMPLE *range_limit = cinfo->sample_range_limit;
kusano 7d535a
  int ctr;
kusano 7d535a
  FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
kusano 7d535a
kusano 7d535a
  /* Pass 1: process columns from input, store into work array. */
kusano 7d535a
kusano 7d535a
  inptr = coef_block;
kusano 7d535a
  quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
kusano 7d535a
  wsptr = workspace;
kusano 7d535a
  for (ctr = DCTSIZE; ctr > 0; ctr--) {
kusano 7d535a
    /* Due to quantization, we will usually find that many of the input
kusano 7d535a
     * coefficients are zero, especially the AC terms.  We can exploit this
kusano 7d535a
     * by short-circuiting the IDCT calculation for any column in which all
kusano 7d535a
     * the AC terms are zero.  In that case each output is equal to the
kusano 7d535a
     * DC coefficient (with scale factor as needed).
kusano 7d535a
     * With typical images and quantization tables, half or more of the
kusano 7d535a
     * column DCT calculations can be simplified this way.
kusano 7d535a
     */
kusano 7d535a
    
kusano 7d535a
    if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
kusano 7d535a
	inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
kusano 7d535a
	inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
kusano 7d535a
	inptr[DCTSIZE*7] == 0) {
kusano 7d535a
      /* AC terms all zero */
kusano 7d535a
      FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
kusano 7d535a
      
kusano 7d535a
      wsptr[DCTSIZE*0] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*1] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*2] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*3] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*4] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*5] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*6] = dcval;
kusano 7d535a
      wsptr[DCTSIZE*7] = dcval;
kusano 7d535a
      
kusano 7d535a
      inptr++;			/* advance pointers to next column */
kusano 7d535a
      quantptr++;
kusano 7d535a
      wsptr++;
kusano 7d535a
      continue;
kusano 7d535a
    }
kusano 7d535a
    
kusano 7d535a
    /* Even part */
kusano 7d535a
kusano 7d535a
    tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
kusano 7d535a
    tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
kusano 7d535a
    tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
kusano 7d535a
    tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
kusano 7d535a
kusano 7d535a
    tmp10 = tmp0 + tmp2;	/* phase 3 */
kusano 7d535a
    tmp11 = tmp0 - tmp2;
kusano 7d535a
kusano 7d535a
    tmp13 = tmp1 + tmp3;	/* phases 5-3 */
kusano 7d535a
    tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
kusano 7d535a
kusano 7d535a
    tmp0 = tmp10 + tmp13;	/* phase 2 */
kusano 7d535a
    tmp3 = tmp10 - tmp13;
kusano 7d535a
    tmp1 = tmp11 + tmp12;
kusano 7d535a
    tmp2 = tmp11 - tmp12;
kusano 7d535a
    
kusano 7d535a
    /* Odd part */
kusano 7d535a
kusano 7d535a
    tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
kusano 7d535a
    tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
kusano 7d535a
    tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
kusano 7d535a
    tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
kusano 7d535a
kusano 7d535a
    z13 = tmp6 + tmp5;		/* phase 6 */
kusano 7d535a
    z10 = tmp6 - tmp5;
kusano 7d535a
    z11 = tmp4 + tmp7;
kusano 7d535a
    z12 = tmp4 - tmp7;
kusano 7d535a
kusano 7d535a
    tmp7 = z11 + z13;		/* phase 5 */
kusano 7d535a
    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
kusano 7d535a
kusano 7d535a
    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
kusano 7d535a
    tmp10 = z5 - z12 * ((FAST_FLOAT) 1.082392200); /* 2*(c2-c6) */
kusano 7d535a
    tmp12 = z5 - z10 * ((FAST_FLOAT) 2.613125930); /* 2*(c2+c6) */
kusano 7d535a
kusano 7d535a
    tmp6 = tmp12 - tmp7;	/* phase 2 */
kusano 7d535a
    tmp5 = tmp11 - tmp6;
kusano 7d535a
    tmp4 = tmp10 - tmp5;
kusano 7d535a
kusano 7d535a
    wsptr[DCTSIZE*0] = tmp0 + tmp7;
kusano 7d535a
    wsptr[DCTSIZE*7] = tmp0 - tmp7;
kusano 7d535a
    wsptr[DCTSIZE*1] = tmp1 + tmp6;
kusano 7d535a
    wsptr[DCTSIZE*6] = tmp1 - tmp6;
kusano 7d535a
    wsptr[DCTSIZE*2] = tmp2 + tmp5;
kusano 7d535a
    wsptr[DCTSIZE*5] = tmp2 - tmp5;
kusano 7d535a
    wsptr[DCTSIZE*3] = tmp3 + tmp4;
kusano 7d535a
    wsptr[DCTSIZE*4] = tmp3 - tmp4;
kusano 7d535a
kusano 7d535a
    inptr++;			/* advance pointers to next column */
kusano 7d535a
    quantptr++;
kusano 7d535a
    wsptr++;
kusano 7d535a
  }
kusano 7d535a
  
kusano 7d535a
  /* Pass 2: process rows from work array, store into output array. */
kusano 7d535a
kusano 7d535a
  wsptr = workspace;
kusano 7d535a
  for (ctr = 0; ctr < DCTSIZE; ctr++) {
kusano 7d535a
    outptr = output_buf[ctr] + output_col;
kusano 7d535a
    /* Rows of zeroes can be exploited in the same way as we did with columns.
kusano 7d535a
     * However, the column calculation has created many nonzero AC terms, so
kusano 7d535a
     * the simplification applies less often (typically 5% to 10% of the time).
kusano 7d535a
     * And testing floats for zero is relatively expensive, so we don't bother.
kusano 7d535a
     */
kusano 7d535a
    
kusano 7d535a
    /* Even part */
kusano 7d535a
kusano 7d535a
    /* Apply signed->unsigned and prepare float->int conversion */
kusano 7d535a
    z5 = wsptr[0] + ((FAST_FLOAT) CENTERJSAMPLE + (FAST_FLOAT) 0.5);
kusano 7d535a
    tmp10 = z5 + wsptr[4];
kusano 7d535a
    tmp11 = z5 - wsptr[4];
kusano 7d535a
kusano 7d535a
    tmp13 = wsptr[2] + wsptr[6];
kusano 7d535a
    tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
kusano 7d535a
kusano 7d535a
    tmp0 = tmp10 + tmp13;
kusano 7d535a
    tmp3 = tmp10 - tmp13;
kusano 7d535a
    tmp1 = tmp11 + tmp12;
kusano 7d535a
    tmp2 = tmp11 - tmp12;
kusano 7d535a
kusano 7d535a
    /* Odd part */
kusano 7d535a
kusano 7d535a
    z13 = wsptr[5] + wsptr[3];
kusano 7d535a
    z10 = wsptr[5] - wsptr[3];
kusano 7d535a
    z11 = wsptr[1] + wsptr[7];
kusano 7d535a
    z12 = wsptr[1] - wsptr[7];
kusano 7d535a
kusano 7d535a
    tmp7 = z11 + z13;
kusano 7d535a
    tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
kusano 7d535a
kusano 7d535a
    z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
kusano 7d535a
    tmp10 = z5 - z12 * ((FAST_FLOAT) 1.082392200); /* 2*(c2-c6) */
kusano 7d535a
    tmp12 = z5 - z10 * ((FAST_FLOAT) 2.613125930); /* 2*(c2+c6) */
kusano 7d535a
kusano 7d535a
    tmp6 = tmp12 - tmp7;
kusano 7d535a
    tmp5 = tmp11 - tmp6;
kusano 7d535a
    tmp4 = tmp10 - tmp5;
kusano 7d535a
kusano 7d535a
    /* Final output stage: float->int conversion and range-limit */
kusano 7d535a
kusano 7d535a
    outptr[0] = range_limit[((int) (tmp0 + tmp7)) & RANGE_MASK];
kusano 7d535a
    outptr[7] = range_limit[((int) (tmp0 - tmp7)) & RANGE_MASK];
kusano 7d535a
    outptr[1] = range_limit[((int) (tmp1 + tmp6)) & RANGE_MASK];
kusano 7d535a
    outptr[6] = range_limit[((int) (tmp1 - tmp6)) & RANGE_MASK];
kusano 7d535a
    outptr[2] = range_limit[((int) (tmp2 + tmp5)) & RANGE_MASK];
kusano 7d535a
    outptr[5] = range_limit[((int) (tmp2 - tmp5)) & RANGE_MASK];
kusano 7d535a
    outptr[3] = range_limit[((int) (tmp3 + tmp4)) & RANGE_MASK];
kusano 7d535a
    outptr[4] = range_limit[((int) (tmp3 - tmp4)) & RANGE_MASK];
kusano 7d535a
    
kusano 7d535a
    wsptr += DCTSIZE;		/* advance pointer to next row */
kusano 7d535a
  }
kusano 7d535a
}
kusano 7d535a
kusano 7d535a
#endif /* DCT_FLOAT_SUPPORTED */