diff VSs_tinyjpeg/jidctflt.c @ 0:a8af8b3fc99d

initial commit
author Nina Engelhardt <nengel@mailbox.tu-berlin.de>
date Thu, 05 Jul 2012 11:35:03 +0200
parents
children
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/VSs_tinyjpeg/jidctflt.c	Thu Jul 05 11:35:03 2012 +0200
     1.3 @@ -0,0 +1,265 @@
     1.4 +/*
     1.5 + * jidctflt.c
     1.6 + *
     1.7 + * Copyright (C) 1994-1998, Thomas G. Lane.
     1.8 + * This file is part of the Independent JPEG Group's software.
     1.9 + *
    1.10 + * The authors make NO WARRANTY or representation, either express or implied,
    1.11 + * with respect to this software, its quality, accuracy, merchantability, or
    1.12 + * fitness for a particular purpose.  This software is provided "AS IS", and you,
    1.13 + * its user, assume the entire risk as to its quality and accuracy.
    1.14 + *
    1.15 + * This software is copyright (C) 1991-1998, Thomas G. Lane.
    1.16 + * All Rights Reserved except as specified below.
    1.17 + *
    1.18 + * Permission is hereby granted to use, copy, modify, and distribute this
    1.19 + * software (or portions thereof) for any purpose, without fee, subject to these
    1.20 + * conditions:
    1.21 + * (1) If any part of the source code for this software is distributed, then this
    1.22 + * README file must be included, with this copyright and no-warranty notice
    1.23 + * unaltered; and any additions, deletions, or changes to the original files
    1.24 + * must be clearly indicated in accompanying documentation.
    1.25 + * (2) If only executable code is distributed, then the accompanying
    1.26 + * documentation must state that "this software is based in part on the work of
    1.27 + * the Independent JPEG Group".
    1.28 + * (3) Permission for use of this software is granted only if the user accepts
    1.29 + * full responsibility for any undesirable consequences; the authors accept
    1.30 + * NO LIABILITY for damages of any kind.
    1.31 + *
    1.32 + * These conditions apply to any software derived from or based on the IJG code,
    1.33 + * not just to the unmodified library.  If you use our work, you ought to
    1.34 + * acknowledge us.
    1.35 + *
    1.36 + * Permission is NOT granted for the use of any IJG author's name or company name
    1.37 + * in advertising or publicity relating to this software or products derived from
    1.38 + * it.  This software may be referred to only as "the Independent JPEG Group's
    1.39 + * software".
    1.40 + *
    1.41 + * We specifically permit and encourage the use of this software as the basis of
    1.42 + * commercial products, provided that all warranty or liability claims are
    1.43 + * assumed by the product vendor.
    1.44 + *
    1.45 + *
    1.46 + * This file contains a floating-point implementation of the
    1.47 + * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
    1.48 + * must also perform dequantization of the input coefficients.
    1.49 + *
    1.50 + * This implementation should be more accurate than either of the integer
    1.51 + * IDCT implementations.  However, it may not give the same results on all
    1.52 + * machines because of differences in roundoff behavior.  Speed will depend
    1.53 + * on the hardware's floating point capacity.
    1.54 + *
    1.55 + * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
    1.56 + * on each row (or vice versa, but it's more convenient to emit a row at
    1.57 + * a time).  Direct algorithms are also available, but they are much more
    1.58 + * complex and seem not to be any faster when reduced to code.
    1.59 + *
    1.60 + * This implementation is based on Arai, Agui, and Nakajima's algorithm for
    1.61 + * scaled DCT.  Their original paper (Trans. IEICE E-71(11):1095) is in
    1.62 + * Japanese, but the algorithm is described in the Pennebaker & Mitchell
    1.63 + * JPEG textbook (see REFERENCES section in file README).  The following code
    1.64 + * is based directly on figure 4-8 in P&M.
    1.65 + * While an 8-point DCT cannot be done in less than 11 multiplies, it is
    1.66 + * possible to arrange the computation so that many of the multiplies are
    1.67 + * simple scalings of the final outputs.  These multiplies can then be
    1.68 + * folded into the multiplications or divisions by the JPEG quantization
    1.69 + * table entries.  The AA&N method leaves only 5 multiplies and 29 adds
    1.70 + * to be done in the DCT itself.
    1.71 + * The primary disadvantage of this method is that with a fixed-point
    1.72 + * implementation, accuracy is lost due to imprecise representation of the
    1.73 + * scaled quantization values.  However, that problem does not arise if
    1.74 + * we use floating point arithmetic.
    1.75 + */
    1.76 +
    1.77 +#include <stdint.h>
    1.78 +#include "tinyjpeg-internal.h"
    1.79 +
    1.80 +#define FAST_FLOAT float
    1.81 +#define DCTSIZE	   8
    1.82 +#define DCTSIZE2   (DCTSIZE*DCTSIZE)
    1.83 +
    1.84 +#define DEQUANTIZE(coef,quantval)  (((FAST_FLOAT) (coef)) * (quantval))
    1.85 +
    1.86 +static inline unsigned char descale_and_clamp(int x, int shift)
    1.87 +{
    1.88 +	x += (1UL<<(shift-1));
    1.89 +	if (x<0)
    1.90 +		x = (x >> shift) | ((~(0UL)) << (32-(shift)));
    1.91 +	else
    1.92 +		x >>= shift;
    1.93 +	x += 128;
    1.94 +	if (x>255)
    1.95 +		return 255;
    1.96 +	else if (x<0)
    1.97 +		return 0;
    1.98 +	else
    1.99 +		return x;
   1.100 +}
   1.101 +
   1.102 +/*
   1.103 + * Perform dequantization and inverse DCT on one block of coefficients.
   1.104 + */
   1.105 +void tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride)
   1.106 +{
   1.107 +	FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
   1.108 +	FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
   1.109 +	FAST_FLOAT z5, z10, z11, z12, z13;
   1.110 +	int16_t *inptr;
   1.111 +	FAST_FLOAT *quantptr;
   1.112 +	FAST_FLOAT *wsptr;
   1.113 +	uint8_t *outptr;
   1.114 +	int ctr;
   1.115 +	FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
   1.116 +
   1.117 +	/* Pass 1: process columns from input, store into work array. */
   1.118 +
   1.119 +	inptr = compptr->DCT;
   1.120 +	quantptr = compptr->Q_table;
   1.121 +	wsptr = workspace;
   1.122 +	for (ctr = DCTSIZE; ctr > 0; ctr--) {
   1.123 +		/* Due to quantization, we will usually find that many of the input
   1.124 +		 * coefficients are zero, especially the AC terms.  We can exploit this
   1.125 +		 * by short-circuiting the IDCT calculation for any column in which all
   1.126 +		 * the AC terms are zero.  In that case each output is equal to the
   1.127 +		 * DC coefficient (with scale factor as needed).
   1.128 +		 * With typical images and quantization tables, half or more of the
   1.129 +		 * column DCT calculations can be simplified this way.
   1.130 +		 */
   1.131 +
   1.132 +		if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 &&
   1.133 +			inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 &&
   1.134 +			inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 &&
   1.135 +			inptr[DCTSIZE*7] == 0) {
   1.136 +			/* AC terms all zero */
   1.137 +			FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
   1.138 +
   1.139 +		wsptr[DCTSIZE*0] = dcval;
   1.140 +		wsptr[DCTSIZE*1] = dcval;
   1.141 +		wsptr[DCTSIZE*2] = dcval;
   1.142 +		wsptr[DCTSIZE*3] = dcval;
   1.143 +		wsptr[DCTSIZE*4] = dcval;
   1.144 +		wsptr[DCTSIZE*5] = dcval;
   1.145 +		wsptr[DCTSIZE*6] = dcval;
   1.146 +		wsptr[DCTSIZE*7] = dcval;
   1.147 +
   1.148 +		inptr++;			/* advance pointers to next column */
   1.149 +		quantptr++;
   1.150 +		wsptr++;
   1.151 +		continue;
   1.152 +			}
   1.153 +
   1.154 +			/* Even part */
   1.155 +
   1.156 +			tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
   1.157 +			tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
   1.158 +			tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
   1.159 +			tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
   1.160 +
   1.161 +			tmp10 = tmp0 + tmp2;	/* phase 3 */
   1.162 +			tmp11 = tmp0 - tmp2;
   1.163 +
   1.164 +			tmp13 = tmp1 + tmp3;	/* phases 5-3 */
   1.165 +			tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
   1.166 +
   1.167 +			tmp0 = tmp10 + tmp13;	/* phase 2 */
   1.168 +			tmp3 = tmp10 - tmp13;
   1.169 +			tmp1 = tmp11 + tmp12;
   1.170 +			tmp2 = tmp11 - tmp12;
   1.171 +
   1.172 +			/* Odd part */
   1.173 +
   1.174 +			tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
   1.175 +			tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
   1.176 +			tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
   1.177 +			tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
   1.178 +
   1.179 +			z13 = tmp6 + tmp5;		/* phase 6 */
   1.180 +			z10 = tmp6 - tmp5;
   1.181 +			z11 = tmp4 + tmp7;
   1.182 +			z12 = tmp4 - tmp7;
   1.183 +
   1.184 +			tmp7 = z11 + z13;		/* phase 5 */
   1.185 +			tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
   1.186 +
   1.187 +			z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
   1.188 +			tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
   1.189 +			tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
   1.190 +
   1.191 +			tmp6 = tmp12 - tmp7;	/* phase 2 */
   1.192 +			tmp5 = tmp11 - tmp6;
   1.193 +			tmp4 = tmp10 + tmp5;
   1.194 +
   1.195 +			wsptr[DCTSIZE*0] = tmp0 + tmp7;
   1.196 +			wsptr[DCTSIZE*7] = tmp0 - tmp7;
   1.197 +			wsptr[DCTSIZE*1] = tmp1 + tmp6;
   1.198 +			wsptr[DCTSIZE*6] = tmp1 - tmp6;
   1.199 +			wsptr[DCTSIZE*2] = tmp2 + tmp5;
   1.200 +			wsptr[DCTSIZE*5] = tmp2 - tmp5;
   1.201 +			wsptr[DCTSIZE*4] = tmp3 + tmp4;
   1.202 +			wsptr[DCTSIZE*3] = tmp3 - tmp4;
   1.203 +
   1.204 +			inptr++;			/* advance pointers to next column */
   1.205 +			quantptr++;
   1.206 +			wsptr++;
   1.207 +	}
   1.208 +
   1.209 +	/* Pass 2: process rows from work array, store into output array. */
   1.210 +	/* Note that we must descale the results by a factor of 8 == 2**3. */
   1.211 +
   1.212 +	wsptr = workspace;
   1.213 +	outptr = output_buf;
   1.214 +	for (ctr = 0; ctr < DCTSIZE; ctr++) {
   1.215 +		/* Rows of zeroes can be exploited in the same way as we did with columns.
   1.216 +		 * However, the column calculation has created many nonzero AC terms, so
   1.217 +		 * the simplification applies less often (typically 5% to 10% of the time).
   1.218 +		 * And testing floats for zero is relatively expensive, so we don't bother.
   1.219 +		 */
   1.220 +
   1.221 +		/* Even part */
   1.222 +
   1.223 +		tmp10 = wsptr[0] + wsptr[4];
   1.224 +		tmp11 = wsptr[0] - wsptr[4];
   1.225 +
   1.226 +		tmp13 = wsptr[2] + wsptr[6];
   1.227 +		tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
   1.228 +
   1.229 +		tmp0 = tmp10 + tmp13;
   1.230 +		tmp3 = tmp10 - tmp13;
   1.231 +		tmp1 = tmp11 + tmp12;
   1.232 +		tmp2 = tmp11 - tmp12;
   1.233 +
   1.234 +		/* Odd part */
   1.235 +
   1.236 +		z13 = wsptr[5] + wsptr[3];
   1.237 +		z10 = wsptr[5] - wsptr[3];
   1.238 +		z11 = wsptr[1] + wsptr[7];
   1.239 +		z12 = wsptr[1] - wsptr[7];
   1.240 +
   1.241 +		tmp7 = z11 + z13;
   1.242 +		tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
   1.243 +
   1.244 +		z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
   1.245 +		tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
   1.246 +		tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
   1.247 +
   1.248 +		tmp6 = tmp12 - tmp7;
   1.249 +		tmp5 = tmp11 - tmp6;
   1.250 +		tmp4 = tmp10 + tmp5;
   1.251 +
   1.252 +		/* Final output stage: scale down by a factor of 8 and range-limit */
   1.253 +
   1.254 +		outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3);
   1.255 +		outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3);
   1.256 +		outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3);
   1.257 +		outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3);
   1.258 +		outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3);
   1.259 +		outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3);
   1.260 +		outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3);
   1.261 +		outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3);
   1.262 +
   1.263 +
   1.264 +		wsptr += DCTSIZE;		/* advance pointer to next row */
   1.265 +		outptr += stride;
   1.266 +	}
   1.267 +}
   1.268 +