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.

400 lines
13 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jidctred.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 inverse-DCT routines that produce reduced-size output:
  11. * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
  12. *
  13. * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
  14. * algorithm used in jidctint.c. We simply replace each 8-to-8 1-D IDCT step
  15. * with an 8-to-4 step that produces the four averages of two adjacent outputs
  16. * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
  17. * These steps were derived by computing the corresponding values at the end
  18. * of the normal LL&M code, then simplifying as much as possible.
  19. *
  20. * 1x1 is trivial: just take the DC coefficient divided by 8.
  21. *
  22. * See jidctint.c for additional comments.
  23. */
  24. #define JPEG_INTERNALS
  25. #include "jinclude.h"
  26. #include "jpeglib.h"
  27. #include "jdct.h" /* Private declarations for DCT subsystem */
  28. #ifdef IDCT_SCALING_SUPPORTED
  29. /*
  30. * This module is specialized to the case DCTSIZE = 8.
  31. */
  32. #if DCTSIZE != 8
  33. Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  34. #endif
  35. /* Scaling is the same as in jidctint.c. */
  36. #if BITS_IN_JSAMPLE == 8
  37. #define CONST_BITS 13
  38. #define PASS1_BITS 2
  39. #else
  40. #define CONST_BITS 13
  41. #define PASS1_BITS 1 /* lose a little precision to avoid overflow */
  42. #endif
  43. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  44. * causing a lot of useless floating-point operations at run time.
  45. * To get around this we use the following pre-calculated constants.
  46. * If you change CONST_BITS you may want to add appropriate values.
  47. * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  48. */
  49. #if CONST_BITS == 13
  50. #define FIX_0_211164243 ((INT32) 1730) /* FIX(0.211164243) */
  51. #define FIX_0_509795579 ((INT32) 4176) /* FIX(0.509795579) */
  52. #define FIX_0_601344887 ((INT32) 4926) /* FIX(0.601344887) */
  53. #define FIX_0_720959822 ((INT32) 5906) /* FIX(0.720959822) */
  54. #define FIX_0_765366865 ((INT32) 6270) /* FIX(0.765366865) */
  55. #define FIX_0_850430095 ((INT32) 6967) /* FIX(0.850430095) */
  56. #define FIX_0_899976223 ((INT32) 7373) /* FIX(0.899976223) */
  57. #define FIX_1_061594337 ((INT32) 8697) /* FIX(1.061594337) */
  58. #define FIX_1_272758580 ((INT32) 10426) /* FIX(1.272758580) */
  59. #define FIX_1_451774981 ((INT32) 11893) /* FIX(1.451774981) */
  60. #define FIX_1_847759065 ((INT32) 15137) /* FIX(1.847759065) */
  61. #define FIX_2_172734803 ((INT32) 17799) /* FIX(2.172734803) */
  62. #define FIX_2_562915447 ((INT32) 20995) /* FIX(2.562915447) */
  63. #define FIX_3_624509785 ((INT32) 29692) /* FIX(3.624509785) */
  64. #else
  65. #define FIX_0_211164243 FIX(0.211164243)
  66. #define FIX_0_509795579 FIX(0.509795579)
  67. #define FIX_0_601344887 FIX(0.601344887)
  68. #define FIX_0_720959822 FIX(0.720959822)
  69. #define FIX_0_765366865 FIX(0.765366865)
  70. #define FIX_0_850430095 FIX(0.850430095)
  71. #define FIX_0_899976223 FIX(0.899976223)
  72. #define FIX_1_061594337 FIX(1.061594337)
  73. #define FIX_1_272758580 FIX(1.272758580)
  74. #define FIX_1_451774981 FIX(1.451774981)
  75. #define FIX_1_847759065 FIX(1.847759065)
  76. #define FIX_2_172734803 FIX(2.172734803)
  77. #define FIX_2_562915447 FIX(2.562915447)
  78. #define FIX_3_624509785 FIX(3.624509785)
  79. #endif
  80. /* Multiply an INT32 variable by an INT32 constant to yield an INT32 result.
  81. * For 8-bit samples with the recommended scaling, all the variable
  82. * and constant values involved are no more than 16 bits wide, so a
  83. * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  84. * For 12-bit samples, a full 32-bit multiplication will be needed.
  85. */
  86. #if BITS_IN_JSAMPLE == 8
  87. #define MULTIPLY(var,const) MULTIPLY16C16(var,const)
  88. #else
  89. #define MULTIPLY(var,const) ((var) * (const))
  90. #endif
  91. /* Dequantize a coefficient by multiplying it by the multiplier-table
  92. * entry; produce an int result. In this module, both inputs and result
  93. * are 16 bits or less, so either int or short multiply will work.
  94. */
  95. #define DEQUANTIZE(coef,quantval) (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  96. /*
  97. * Perform dequantization and inverse DCT on one block of coefficients,
  98. * producing a reduced-size 4x4 output block.
  99. */
  100. GLOBAL(void)
  101. jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  102. JCOEFPTR coef_block,
  103. JSAMPARRAY output_buf, JDIMENSION output_col)
  104. {
  105. INT32 tmp0, tmp2, tmp10, tmp12;
  106. INT32 z1, z2, z3, z4;
  107. JCOEFPTR inptr;
  108. ISLOW_MULT_TYPE * quantptr;
  109. int * wsptr;
  110. JSAMPROW outptr;
  111. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  112. int ctr;
  113. int workspace[DCTSIZE*4]; /* buffers data between passes */
  114. SHIFT_TEMPS
  115. /* Pass 1: process columns from input, store into work array. */
  116. inptr = coef_block;
  117. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  118. wsptr = workspace;
  119. for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  120. /* Don't bother to process column 4, because second pass won't use it */
  121. if (ctr == DCTSIZE-4)
  122. continue;
  123. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  124. inptr[DCTSIZE*5] | inptr[DCTSIZE*6] | inptr[DCTSIZE*7]) == 0) {
  125. /* AC terms all zero; we need not examine term 4 for 4x4 output */
  126. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  127. wsptr[DCTSIZE*0] = dcval;
  128. wsptr[DCTSIZE*1] = dcval;
  129. wsptr[DCTSIZE*2] = dcval;
  130. wsptr[DCTSIZE*3] = dcval;
  131. continue;
  132. }
  133. /* Even part */
  134. tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  135. tmp0 <<= (CONST_BITS+1);
  136. z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  137. z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  138. tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  139. tmp10 = tmp0 + tmp2;
  140. tmp12 = tmp0 - tmp2;
  141. /* Odd part */
  142. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  143. z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  144. z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  145. z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  146. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  147. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  148. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  149. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  150. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  151. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  152. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  153. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  154. /* Final output stage */
  155. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  156. wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  157. wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  158. wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  159. }
  160. /* Pass 2: process 4 rows from work array, store into output array. */
  161. wsptr = workspace;
  162. for (ctr = 0; ctr < 4; ctr++) {
  163. outptr = output_buf[ctr] + output_col;
  164. /* It's not clear whether a zero row test is worthwhile here ... */
  165. #ifndef NO_ZERO_ROW_TEST
  166. if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[5] | wsptr[6] |
  167. wsptr[7]) == 0) {
  168. /* AC terms all zero */
  169. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  170. & RANGE_MASK];
  171. outptr[0] = dcval;
  172. outptr[1] = dcval;
  173. outptr[2] = dcval;
  174. outptr[3] = dcval;
  175. wsptr += DCTSIZE; /* advance pointer to next row */
  176. continue;
  177. }
  178. #endif
  179. /* Even part */
  180. tmp0 = ((INT32) wsptr[0]) << (CONST_BITS+1);
  181. tmp2 = MULTIPLY((INT32) wsptr[2], FIX_1_847759065)
  182. + MULTIPLY((INT32) wsptr[6], - FIX_0_765366865);
  183. tmp10 = tmp0 + tmp2;
  184. tmp12 = tmp0 - tmp2;
  185. /* Odd part */
  186. z1 = (INT32) wsptr[7];
  187. z2 = (INT32) wsptr[5];
  188. z3 = (INT32) wsptr[3];
  189. z4 = (INT32) wsptr[1];
  190. tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  191. + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  192. + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  193. + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  194. tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  195. + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  196. + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  197. + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  198. /* Final output stage */
  199. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  200. CONST_BITS+PASS1_BITS+3+1)
  201. & RANGE_MASK];
  202. outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  203. CONST_BITS+PASS1_BITS+3+1)
  204. & RANGE_MASK];
  205. outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  206. CONST_BITS+PASS1_BITS+3+1)
  207. & RANGE_MASK];
  208. outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  209. CONST_BITS+PASS1_BITS+3+1)
  210. & RANGE_MASK];
  211. wsptr += DCTSIZE; /* advance pointer to next row */
  212. }
  213. }
  214. /*
  215. * Perform dequantization and inverse DCT on one block of coefficients,
  216. * producing a reduced-size 2x2 output block.
  217. */
  218. GLOBAL(void)
  219. jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  220. JCOEFPTR coef_block,
  221. JSAMPARRAY output_buf, JDIMENSION output_col)
  222. {
  223. INT32 tmp0, tmp10, z1;
  224. JCOEFPTR inptr;
  225. ISLOW_MULT_TYPE * quantptr;
  226. int * wsptr;
  227. JSAMPROW outptr;
  228. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  229. int ctr;
  230. int workspace[DCTSIZE*2]; /* buffers data between passes */
  231. SHIFT_TEMPS
  232. /* Pass 1: process columns from input, store into work array. */
  233. inptr = coef_block;
  234. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  235. wsptr = workspace;
  236. for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  237. /* Don't bother to process columns 2,4,6 */
  238. if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  239. continue;
  240. if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*3] |
  241. inptr[DCTSIZE*5] | inptr[DCTSIZE*7]) == 0) {
  242. /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  243. int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  244. wsptr[DCTSIZE*0] = dcval;
  245. wsptr[DCTSIZE*1] = dcval;
  246. continue;
  247. }
  248. /* Even part */
  249. z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  250. tmp10 = z1 << (CONST_BITS+2);
  251. /* Odd part */
  252. z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  253. tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  254. z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  255. tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  256. z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  257. tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  258. z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  259. tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  260. /* Final output stage */
  261. wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  262. wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  263. }
  264. /* Pass 2: process 2 rows from work array, store into output array. */
  265. wsptr = workspace;
  266. for (ctr = 0; ctr < 2; ctr++) {
  267. outptr = output_buf[ctr] + output_col;
  268. /* It's not clear whether a zero row test is worthwhile here ... */
  269. #ifndef NO_ZERO_ROW_TEST
  270. if ((wsptr[1] | wsptr[3] | wsptr[5] | wsptr[7]) == 0) {
  271. /* AC terms all zero */
  272. JSAMPLE dcval = range_limit[(int) DESCALE((INT32) wsptr[0], PASS1_BITS+3)
  273. & RANGE_MASK];
  274. outptr[0] = dcval;
  275. outptr[1] = dcval;
  276. wsptr += DCTSIZE; /* advance pointer to next row */
  277. continue;
  278. }
  279. #endif
  280. /* Even part */
  281. tmp10 = ((INT32) wsptr[0]) << (CONST_BITS+2);
  282. /* Odd part */
  283. tmp0 = MULTIPLY((INT32) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  284. + MULTIPLY((INT32) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  285. + MULTIPLY((INT32) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  286. + MULTIPLY((INT32) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  287. /* Final output stage */
  288. outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  289. CONST_BITS+PASS1_BITS+3+2)
  290. & RANGE_MASK];
  291. outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  292. CONST_BITS+PASS1_BITS+3+2)
  293. & RANGE_MASK];
  294. wsptr += DCTSIZE; /* advance pointer to next row */
  295. }
  296. }
  297. /*
  298. * Perform dequantization and inverse DCT on one block of coefficients,
  299. * producing a reduced-size 1x1 output block.
  300. */
  301. GLOBAL(void)
  302. jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  303. JCOEFPTR coef_block,
  304. JSAMPARRAY output_buf, JDIMENSION output_col)
  305. {
  306. int dcval;
  307. ISLOW_MULT_TYPE * quantptr;
  308. JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  309. SHIFT_TEMPS
  310. /* We hardly need an inverse DCT routine for this: just take the
  311. * average pixel value, which is one-eighth of the DC coefficient.
  312. */
  313. quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  314. dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  315. dcval = (int) DESCALE((INT32) dcval, 3);
  316. output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  317. }
  318. #endif /* IDCT_SCALING_SUPPORTED */