Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > VSs > VSs__jpeg_decoder__Proj
comparison VSs_tinyjpeg/jidctflt.c @ 4:62350c40504f
running in sequential mode
| author | Nina Engelhardt <nengel@mailbox.tu-berlin.de> |
|---|---|
| date | Mon, 20 Aug 2012 16:56:27 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:5bf80e58dbe7 |
|---|---|
| 1 /* | |
| 2 * jidctflt.c | |
| 3 * | |
| 4 * Copyright (C) 1994-1998, Thomas G. Lane. | |
| 5 * This file is part of the Independent JPEG Group's software. | |
| 6 * | |
| 7 * The authors make NO WARRANTY or representation, either express or implied, | |
| 8 * with respect to this software, its quality, accuracy, merchantability, or | |
| 9 * fitness for a particular purpose. This software is provided "AS IS", and you, | |
| 10 * its user, assume the entire risk as to its quality and accuracy. | |
| 11 * | |
| 12 * This software is copyright (C) 1991-1998, Thomas G. Lane. | |
| 13 * All Rights Reserved except as specified below. | |
| 14 * | |
| 15 * Permission is hereby granted to use, copy, modify, and distribute this | |
| 16 * software (or portions thereof) for any purpose, without fee, subject to these | |
| 17 * conditions: | |
| 18 * (1) If any part of the source code for this software is distributed, then this | |
| 19 * README file must be included, with this copyright and no-warranty notice | |
| 20 * unaltered; and any additions, deletions, or changes to the original files | |
| 21 * must be clearly indicated in accompanying documentation. | |
| 22 * (2) If only executable code is distributed, then the accompanying | |
| 23 * documentation must state that "this software is based in part on the work of | |
| 24 * the Independent JPEG Group". | |
| 25 * (3) Permission for use of this software is granted only if the user accepts | |
| 26 * full responsibility for any undesirable consequences; the authors accept | |
| 27 * NO LIABILITY for damages of any kind. | |
| 28 * | |
| 29 * These conditions apply to any software derived from or based on the IJG code, | |
| 30 * not just to the unmodified library. If you use our work, you ought to | |
| 31 * acknowledge us. | |
| 32 * | |
| 33 * Permission is NOT granted for the use of any IJG author's name or company name | |
| 34 * in advertising or publicity relating to this software or products derived from | |
| 35 * it. This software may be referred to only as "the Independent JPEG Group's | |
| 36 * software". | |
| 37 * | |
| 38 * We specifically permit and encourage the use of this software as the basis of | |
| 39 * commercial products, provided that all warranty or liability claims are | |
| 40 * assumed by the product vendor. | |
| 41 * | |
| 42 * | |
| 43 * This file contains a floating-point implementation of the | |
| 44 * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine | |
| 45 * must also perform dequantization of the input coefficients. | |
| 46 * | |
| 47 * This implementation should be more accurate than either of the integer | |
| 48 * IDCT implementations. However, it may not give the same results on all | |
| 49 * machines because of differences in roundoff behavior. Speed will depend | |
| 50 * on the hardware's floating point capacity. | |
| 51 * | |
| 52 * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT | |
| 53 * on each row (or vice versa, but it's more convenient to emit a row at | |
| 54 * a time). Direct algorithms are also available, but they are much more | |
| 55 * complex and seem not to be any faster when reduced to code. | |
| 56 * | |
| 57 * This implementation is based on Arai, Agui, and Nakajima's algorithm for | |
| 58 * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in | |
| 59 * Japanese, but the algorithm is described in the Pennebaker & Mitchell | |
| 60 * JPEG textbook (see REFERENCES section in file README). The following code | |
| 61 * is based directly on figure 4-8 in P&M. | |
| 62 * While an 8-point DCT cannot be done in less than 11 multiplies, it is | |
| 63 * possible to arrange the computation so that many of the multiplies are | |
| 64 * simple scalings of the final outputs. These multiplies can then be | |
| 65 * folded into the multiplications or divisions by the JPEG quantization | |
| 66 * table entries. The AA&N method leaves only 5 multiplies and 29 adds | |
| 67 * to be done in the DCT itself. | |
| 68 * The primary disadvantage of this method is that with a fixed-point | |
| 69 * implementation, accuracy is lost due to imprecise representation of the | |
| 70 * scaled quantization values. However, that problem does not arise if | |
| 71 * we use floating point arithmetic. | |
| 72 */ | |
| 73 | |
| 74 #include <stdint.h> | |
| 75 #include "tinyjpeg-internal.h" | |
| 76 | |
| 77 #define FAST_FLOAT float | |
| 78 #define DCTSIZE 8 | |
| 79 #define DCTSIZE2 (DCTSIZE*DCTSIZE) | |
| 80 | |
| 81 #define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval)) | |
| 82 | |
| 83 static inline unsigned char descale_and_clamp(int x, int shift) | |
| 84 { | |
| 85 x += (1UL<<(shift-1)); | |
| 86 if (x<0) | |
| 87 x = (x >> shift) | ((~(0UL)) << (32-(shift))); | |
| 88 else | |
| 89 x >>= shift; | |
| 90 x += 128; | |
| 91 if (x>255) | |
| 92 return 255; | |
| 93 else if (x<0) | |
| 94 return 0; | |
| 95 else | |
| 96 return x; | |
| 97 } | |
| 98 | |
| 99 /* | |
| 100 * Perform dequantization and inverse DCT on one block of coefficients. | |
| 101 */ | |
| 102 void tinyjpeg_idct_float (struct component *compptr, uint8_t *output_buf, int stride) | |
| 103 { | |
| 104 FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7; | |
| 105 FAST_FLOAT tmp10, tmp11, tmp12, tmp13; | |
| 106 FAST_FLOAT z5, z10, z11, z12, z13; | |
| 107 int16_t *inptr; | |
| 108 FAST_FLOAT *quantptr; | |
| 109 FAST_FLOAT *wsptr; | |
| 110 uint8_t *outptr; | |
| 111 int ctr; | |
| 112 FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */ | |
| 113 | |
| 114 /* Pass 1: process columns from input, store into work array. */ | |
| 115 | |
| 116 inptr = compptr->DCT; | |
| 117 quantptr = compptr->Q_table; | |
| 118 wsptr = workspace; | |
| 119 for (ctr = DCTSIZE; ctr > 0; ctr--) { | |
| 120 /* Due to quantization, we will usually find that many of the input | |
| 121 * coefficients are zero, especially the AC terms. We can exploit this | |
| 122 * by short-circuiting the IDCT calculation for any column in which all | |
| 123 * the AC terms are zero. In that case each output is equal to the | |
| 124 * DC coefficient (with scale factor as needed). | |
| 125 * With typical images and quantization tables, half or more of the | |
| 126 * column DCT calculations can be simplified this way. | |
| 127 */ | |
| 128 | |
| 129 if (inptr[DCTSIZE*1] == 0 && inptr[DCTSIZE*2] == 0 && | |
| 130 inptr[DCTSIZE*3] == 0 && inptr[DCTSIZE*4] == 0 && | |
| 131 inptr[DCTSIZE*5] == 0 && inptr[DCTSIZE*6] == 0 && | |
| 132 inptr[DCTSIZE*7] == 0) { | |
| 133 /* AC terms all zero */ | |
| 134 FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); | |
| 135 | |
| 136 wsptr[DCTSIZE*0] = dcval; | |
| 137 wsptr[DCTSIZE*1] = dcval; | |
| 138 wsptr[DCTSIZE*2] = dcval; | |
| 139 wsptr[DCTSIZE*3] = dcval; | |
| 140 wsptr[DCTSIZE*4] = dcval; | |
| 141 wsptr[DCTSIZE*5] = dcval; | |
| 142 wsptr[DCTSIZE*6] = dcval; | |
| 143 wsptr[DCTSIZE*7] = dcval; | |
| 144 | |
| 145 inptr++; /* advance pointers to next column */ | |
| 146 quantptr++; | |
| 147 wsptr++; | |
| 148 continue; | |
| 149 } | |
| 150 | |
| 151 /* Even part */ | |
| 152 | |
| 153 tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]); | |
| 154 tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]); | |
| 155 tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]); | |
| 156 tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]); | |
| 157 | |
| 158 tmp10 = tmp0 + tmp2; /* phase 3 */ | |
| 159 tmp11 = tmp0 - tmp2; | |
| 160 | |
| 161 tmp13 = tmp1 + tmp3; /* phases 5-3 */ | |
| 162 tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */ | |
| 163 | |
| 164 tmp0 = tmp10 + tmp13; /* phase 2 */ | |
| 165 tmp3 = tmp10 - tmp13; | |
| 166 tmp1 = tmp11 + tmp12; | |
| 167 tmp2 = tmp11 - tmp12; | |
| 168 | |
| 169 /* Odd part */ | |
| 170 | |
| 171 tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]); | |
| 172 tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]); | |
| 173 tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]); | |
| 174 tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]); | |
| 175 | |
| 176 z13 = tmp6 + tmp5; /* phase 6 */ | |
| 177 z10 = tmp6 - tmp5; | |
| 178 z11 = tmp4 + tmp7; | |
| 179 z12 = tmp4 - tmp7; | |
| 180 | |
| 181 tmp7 = z11 + z13; /* phase 5 */ | |
| 182 tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */ | |
| 183 | |
| 184 z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ | |
| 185 tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ | |
| 186 tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ | |
| 187 | |
| 188 tmp6 = tmp12 - tmp7; /* phase 2 */ | |
| 189 tmp5 = tmp11 - tmp6; | |
| 190 tmp4 = tmp10 + tmp5; | |
| 191 | |
| 192 wsptr[DCTSIZE*0] = tmp0 + tmp7; | |
| 193 wsptr[DCTSIZE*7] = tmp0 - tmp7; | |
| 194 wsptr[DCTSIZE*1] = tmp1 + tmp6; | |
| 195 wsptr[DCTSIZE*6] = tmp1 - tmp6; | |
| 196 wsptr[DCTSIZE*2] = tmp2 + tmp5; | |
| 197 wsptr[DCTSIZE*5] = tmp2 - tmp5; | |
| 198 wsptr[DCTSIZE*4] = tmp3 + tmp4; | |
| 199 wsptr[DCTSIZE*3] = tmp3 - tmp4; | |
| 200 | |
| 201 inptr++; /* advance pointers to next column */ | |
| 202 quantptr++; | |
| 203 wsptr++; | |
| 204 } | |
| 205 | |
| 206 /* Pass 2: process rows from work array, store into output array. */ | |
| 207 /* Note that we must descale the results by a factor of 8 == 2**3. */ | |
| 208 | |
| 209 wsptr = workspace; | |
| 210 outptr = output_buf; | |
| 211 for (ctr = 0; ctr < DCTSIZE; ctr++) { | |
| 212 /* Rows of zeroes can be exploited in the same way as we did with columns. | |
| 213 * However, the column calculation has created many nonzero AC terms, so | |
| 214 * the simplification applies less often (typically 5% to 10% of the time). | |
| 215 * And testing floats for zero is relatively expensive, so we don't bother. | |
| 216 */ | |
| 217 | |
| 218 /* Even part */ | |
| 219 | |
| 220 tmp10 = wsptr[0] + wsptr[4]; | |
| 221 tmp11 = wsptr[0] - wsptr[4]; | |
| 222 | |
| 223 tmp13 = wsptr[2] + wsptr[6]; | |
| 224 tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13; | |
| 225 | |
| 226 tmp0 = tmp10 + tmp13; | |
| 227 tmp3 = tmp10 - tmp13; | |
| 228 tmp1 = tmp11 + tmp12; | |
| 229 tmp2 = tmp11 - tmp12; | |
| 230 | |
| 231 /* Odd part */ | |
| 232 | |
| 233 z13 = wsptr[5] + wsptr[3]; | |
| 234 z10 = wsptr[5] - wsptr[3]; | |
| 235 z11 = wsptr[1] + wsptr[7]; | |
| 236 z12 = wsptr[1] - wsptr[7]; | |
| 237 | |
| 238 tmp7 = z11 + z13; | |
| 239 tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); | |
| 240 | |
| 241 z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */ | |
| 242 tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */ | |
| 243 tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */ | |
| 244 | |
| 245 tmp6 = tmp12 - tmp7; | |
| 246 tmp5 = tmp11 - tmp6; | |
| 247 tmp4 = tmp10 + tmp5; | |
| 248 | |
| 249 /* Final output stage: scale down by a factor of 8 and range-limit */ | |
| 250 | |
| 251 outptr[0] = descale_and_clamp((int)(tmp0 + tmp7), 3); | |
| 252 outptr[7] = descale_and_clamp((int)(tmp0 - tmp7), 3); | |
| 253 outptr[1] = descale_and_clamp((int)(tmp1 + tmp6), 3); | |
| 254 outptr[6] = descale_and_clamp((int)(tmp1 - tmp6), 3); | |
| 255 outptr[2] = descale_and_clamp((int)(tmp2 + tmp5), 3); | |
| 256 outptr[5] = descale_and_clamp((int)(tmp2 - tmp5), 3); | |
| 257 outptr[4] = descale_and_clamp((int)(tmp3 + tmp4), 3); | |
| 258 outptr[3] = descale_and_clamp((int)(tmp3 - tmp4), 3); | |
| 259 | |
| 260 | |
| 261 wsptr += DCTSIZE; /* advance pointer to next row */ | |
| 262 outptr += stride; | |
| 263 } | |
| 264 } | |
| 265 |
