Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

244 lines
8.3 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jidctflt.c
  5. *
  6. * Copyright (C) 1994-1996, Thomas G. Lane.
  7. * This file is part of the Independent JPEG Group's software.
  8. * For conditions of distribution and use, see the accompanying README file.
  9. *
  10. * This file contains a floating-point implementation of the
  11. * inverse DCT (Discrete Cosine Transform). In the IJG code, this routine
  12. * must also perform dequantization of the input coefficients.
  13. *
  14. * This implementation should be more accurate than either of the integer
  15. * IDCT implementations. However, it may not give the same results on all
  16. * machines because of differences in roundoff behavior. Speed will depend
  17. * on the hardware's floating point capacity.
  18. *
  19. * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  20. * on each row (or vice versa, but it's more convenient to emit a row at
  21. * a time). Direct algorithms are also available, but they are much more
  22. * complex and seem not to be any faster when reduced to code.
  23. *
  24. * This implementation is based on Arai, Agui, and Nakajima's algorithm for
  25. * scaled DCT. Their original paper (Trans. IEICE E-71(11):1095) is in
  26. * Japanese, but the algorithm is described in the Pennebaker & Mitchell
  27. * JPEG textbook (see REFERENCES section in file README). The following code
  28. * is based directly on figure 4-8 in P&M.
  29. * While an 8-point DCT cannot be done in less than 11 multiplies, it is
  30. * possible to arrange the computation so that many of the multiplies are
  31. * simple scalings of the final outputs. These multiplies can then be
  32. * folded into the multiplications or divisions by the JPEG quantization
  33. * table entries. The AA&N method leaves only 5 multiplies and 29 adds
  34. * to be done in the DCT itself.
  35. * The primary disadvantage of this method is that with a fixed-point
  36. * implementation, accuracy is lost due to imprecise representation of the
  37. * scaled quantization values. However, that problem does not arise if
  38. * we use floating point arithmetic.
  39. */
  40. #define JPEG_INTERNALS
  41. #include "jinclude.h"
  42. #include "jpeglib.h"
  43. #include "jdct.h" /* Private declarations for DCT subsystem */
  44. #ifdef DCT_FLOAT_SUPPORTED
  45. /*
  46. * This module is specialized to the case DCTSIZE = 8.
  47. */
  48. #if DCTSIZE != 8
  49. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  50. #endif
  51. /* Dequantize a coefficient by multiplying it by the multiplier-table
  52. * entry; produce a float result.
  53. */
  54. #define DEQUANTIZE(coef,quantval) (((FAST_FLOAT) (coef)) * (quantval))
  55. /*
  56. * Perform dequantization and inverse DCT on one block of coefficients.
  57. */
  58. GLOBAL(void)
  59. jpeg_idct_float (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  60. JCOEFPTR coef_block,
  61. JSAMPARRAY output_buf, JDIMENSION output_col)
  62. {
  63. FAST_FLOAT tmp0, tmp1, tmp2, tmp3, tmp4, tmp5, tmp6, tmp7;
  64. FAST_FLOAT tmp10, tmp11, tmp12, tmp13;
  65. FAST_FLOAT z5, z10, z11, z12, z13;
  66. JCOEFPTR inptr;
  67. FLOAT_MULT_TYPE * quantptr;
  68. FAST_FLOAT * wsptr;
  69. JSAMPROW outptr;
  70. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  71. int ctr;
  72. FAST_FLOAT workspace[DCTSIZE2]; /* buffers data between passes */
  73. SHIFT_TEMPS
  74. /* Pass 1: process columns from input, store into work array. */
  75. inptr = coef_block;
  76. quantptr = (FLOAT_MULT_TYPE *) compptr->dct_table;
  77. wsptr = workspace;
  78. for (ctr = DCTSIZE; ctr > 0; ctr--) {
  79. /* Due to quantization, we will usually find that many of the input
  80. * coefficients are zero, especially the AC terms. We can exploit this
  81. * by short-circuiting the IDCT calculation for any column in which all
  82. * the AC terms are zero. In that case each output is equal to the
  83. * DC coefficient (with scale factor as needed).
  84. * With typical images and quantization tables, half or more of the
  85. * column DCT calculations can be simplified this way.
  86. */
  87. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  88. inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
  89. inptr[DCTSIZE*7]) == 0) {
  90. /* AC terms all zero */
  91. FAST_FLOAT dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  92. wsptr[DCTSIZE*0] = dcval;
  93. wsptr[DCTSIZE*1] = dcval;
  94. wsptr[DCTSIZE*2] = dcval;
  95. wsptr[DCTSIZE*3] = dcval;
  96. wsptr[DCTSIZE*4] = dcval;
  97. wsptr[DCTSIZE*5] = dcval;
  98. wsptr[DCTSIZE*6] = dcval;
  99. wsptr[DCTSIZE*7] = dcval;
  100. inptr++; /* advance pointers to next column */
  101. quantptr++;
  102. wsptr++;
  103. continue;
  104. }
  105. /* Even part */
  106. tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  107. tmp1 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  108. tmp2 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  109. tmp3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  110. tmp10 = tmp0 + tmp2; /* phase 3 */
  111. tmp11 = tmp0 - tmp2;
  112. tmp13 = tmp1 + tmp3; /* phases 5-3 */
  113. tmp12 = (tmp1 - tmp3) * ((FAST_FLOAT) 1.414213562) - tmp13; /* 2*c4 */
  114. tmp0 = tmp10 + tmp13; /* phase 2 */
  115. tmp3 = tmp10 - tmp13;
  116. tmp1 = tmp11 + tmp12;
  117. tmp2 = tmp11 - tmp12;
  118. /* Odd part */
  119. tmp4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  120. tmp5 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  121. tmp6 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  122. tmp7 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  123. z13 = tmp6 + tmp5; /* phase 6 */
  124. z10 = tmp6 - tmp5;
  125. z11 = tmp4 + tmp7;
  126. z12 = tmp4 - tmp7;
  127. tmp7 = z11 + z13; /* phase 5 */
  128. tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562); /* 2*c4 */
  129. z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  130. tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  131. tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  132. tmp6 = tmp12 - tmp7; /* phase 2 */
  133. tmp5 = tmp11 - tmp6;
  134. tmp4 = tmp10 + tmp5;
  135. wsptr[DCTSIZE*0] = tmp0 + tmp7;
  136. wsptr[DCTSIZE*7] = tmp0 - tmp7;
  137. wsptr[DCTSIZE*1] = tmp1 + tmp6;
  138. wsptr[DCTSIZE*6] = tmp1 - tmp6;
  139. wsptr[DCTSIZE*2] = tmp2 + tmp5;
  140. wsptr[DCTSIZE*5] = tmp2 - tmp5;
  141. wsptr[DCTSIZE*4] = tmp3 + tmp4;
  142. wsptr[DCTSIZE*3] = tmp3 - tmp4;
  143. inptr++; /* advance pointers to next column */
  144. quantptr++;
  145. wsptr++;
  146. }
  147. /* Pass 2: process rows from work array, store into output array. */
  148. /* Note that we must descale the results by a factor of 8 == 2**3. */
  149. wsptr = workspace;
  150. for (ctr = 0; ctr < DCTSIZE; ctr++) {
  151. outptr = output_buf[ctr] + output_col;
  152. /* Rows of zeroes can be exploited in the same way as we did with columns.
  153. * However, the column calculation has created many nonzero AC terms, so
  154. * the simplification applies less often (typically 5% to 10% of the time).
  155. * And testing floats for zero is relatively expensive, so we don't bother.
  156. */
  157. /* Even part */
  158. tmp10 = wsptr[0] + wsptr[4];
  159. tmp11 = wsptr[0] - wsptr[4];
  160. tmp13 = wsptr[2] + wsptr[6];
  161. tmp12 = (wsptr[2] - wsptr[6]) * ((FAST_FLOAT) 1.414213562) - tmp13;
  162. tmp0 = tmp10 + tmp13;
  163. tmp3 = tmp10 - tmp13;
  164. tmp1 = tmp11 + tmp12;
  165. tmp2 = tmp11 - tmp12;
  166. /* Odd part */
  167. z13 = wsptr[5] + wsptr[3];
  168. z10 = wsptr[5] - wsptr[3];
  169. z11 = wsptr[1] + wsptr[7];
  170. z12 = wsptr[1] - wsptr[7];
  171. tmp7 = z11 + z13;
  172. tmp11 = (z11 - z13) * ((FAST_FLOAT) 1.414213562);
  173. z5 = (z10 + z12) * ((FAST_FLOAT) 1.847759065); /* 2*c2 */
  174. tmp10 = ((FAST_FLOAT) 1.082392200) * z12 - z5; /* 2*(c2-c6) */
  175. tmp12 = ((FAST_FLOAT) -2.613125930) * z10 + z5; /* -2*(c2+c6) */
  176. tmp6 = tmp12 - tmp7;
  177. tmp5 = tmp11 - tmp6;
  178. tmp4 = tmp10 + tmp5;
  179. /* Final output stage: scale down by a factor of 8 and range-limit */
  180. outptr[0] = range_limit[(int) DESCALE((INT32) (tmp0 + tmp7), 3)
  181. & RANGE_MASK];
  182. outptr[7] = range_limit[(int) DESCALE((INT32) (tmp0 - tmp7), 3)
  183. & RANGE_MASK];
  184. outptr[1] = range_limit[(int) DESCALE((INT32) (tmp1 + tmp6), 3)
  185. & RANGE_MASK];
  186. outptr[6] = range_limit[(int) DESCALE((INT32) (tmp1 - tmp6), 3)
  187. & RANGE_MASK];
  188. outptr[2] = range_limit[(int) DESCALE((INT32) (tmp2 + tmp5), 3)
  189. & RANGE_MASK];
  190. outptr[5] = range_limit[(int) DESCALE((INT32) (tmp2 - tmp5), 3)
  191. & RANGE_MASK];
  192. outptr[4] = range_limit[(int) DESCALE((INT32) (tmp3 + tmp4), 3)
  193. & RANGE_MASK];
  194. outptr[3] = range_limit[(int) DESCALE((INT32) (tmp3 - tmp4), 3)
  195. & RANGE_MASK];
  196. wsptr += DCTSIZE; /* advance pointer to next row */
  197. }
  198. }
  199. #endif /* DCT_FLOAT_SUPPORTED */