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.

272 lines
8.1 KiB

  1. #include "stdafx.h"
  2. #pragma hdrstop
  3. /*
  4. * jddctmgr.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 the inverse-DCT management logic.
  11. * This code selects a particular IDCT implementation to be used,
  12. * and it performs related housekeeping chores. No code in this file
  13. * is executed per IDCT step, only during output pass setup.
  14. *
  15. * Note that the IDCT routines are responsible for performing coefficient
  16. * dequantization as well as the IDCT proper. This module sets up the
  17. * dequantization multiplier table needed by the IDCT routine.
  18. */
  19. #define JPEG_INTERNALS
  20. #include "jinclude.h"
  21. #include "jpeglib.h"
  22. #include "jdct.h" /* Private declarations for DCT subsystem */
  23. /*
  24. * The decompressor input side (jdinput.c) saves away the appropriate
  25. * quantization table for each component at the start of the first scan
  26. * involving that component. (This is necessary in order to correctly
  27. * decode files that reuse Q-table slots.)
  28. * When we are ready to make an output pass, the saved Q-table is converted
  29. * to a multiplier table that will actually be used by the IDCT routine.
  30. * The multiplier table contents are IDCT-method-dependent. To support
  31. * application changes in IDCT method between scans, we can remake the
  32. * multiplier tables if necessary.
  33. * In buffered-image mode, the first output pass may occur before any data
  34. * has been seen for some components, and thus before their Q-tables have
  35. * been saved away. To handle this case, multiplier tables are preset
  36. * to zeroes; the result of the IDCT will be a neutral gray level.
  37. */
  38. /* Private subobject for this module */
  39. typedef struct {
  40. struct jpeg_inverse_dct pub; /* public fields */
  41. /* This array contains the IDCT method code that each multiplier table
  42. * is currently set up for, or -1 if it's not yet set up.
  43. * The actual multiplier tables are pointed to by dct_table in the
  44. * per-component comp_info structures.
  45. */
  46. int cur_method[MAX_COMPONENTS];
  47. } my_idct_controller;
  48. typedef my_idct_controller * my_idct_ptr;
  49. /* Allocated multiplier tables: big enough for any supported variant */
  50. typedef union {
  51. ISLOW_MULT_TYPE islow_array[DCTSIZE2];
  52. #ifdef DCT_IFAST_SUPPORTED
  53. IFAST_MULT_TYPE ifast_array[DCTSIZE2];
  54. #endif
  55. #ifdef DCT_FLOAT_SUPPORTED
  56. FLOAT_MULT_TYPE float_array[DCTSIZE2];
  57. #endif
  58. } multiplier_table;
  59. /* The current scaled-IDCT routines require ISLOW-style multiplier tables,
  60. * so be sure to compile that code if either ISLOW or SCALING is requested.
  61. */
  62. #ifdef DCT_ISLOW_SUPPORTED
  63. #define PROVIDE_ISLOW_TABLES
  64. #else
  65. #ifdef IDCT_SCALING_SUPPORTED
  66. #define PROVIDE_ISLOW_TABLES
  67. #endif
  68. #endif
  69. /*
  70. * Prepare for an output pass.
  71. * Here we select the proper IDCT routine for each component and build
  72. * a matching multiplier table.
  73. */
  74. METHODDEF(void)
  75. start_pass (j_decompress_ptr cinfo)
  76. {
  77. my_idct_ptr idct = (my_idct_ptr) cinfo->idct;
  78. int ci, i;
  79. jpeg_component_info *compptr;
  80. int method = 0;
  81. inverse_DCT_method_ptr method_ptr = NULL;
  82. JQUANT_TBL * qtbl;
  83. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  84. ci++, compptr++) {
  85. /* Select the proper IDCT routine for this component's scaling */
  86. switch (compptr->DCT_scaled_size) {
  87. #ifdef IDCT_SCALING_SUPPORTED
  88. case 1:
  89. method_ptr = jpeg_idct_1x1;
  90. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  91. break;
  92. case 2:
  93. method_ptr = jpeg_idct_2x2;
  94. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  95. break;
  96. case 4:
  97. method_ptr = jpeg_idct_4x4;
  98. method = JDCT_ISLOW; /* jidctred uses islow-style table */
  99. break;
  100. #endif
  101. case DCTSIZE:
  102. switch (cinfo->dct_method) {
  103. #ifdef DCT_ISLOW_SUPPORTED
  104. case JDCT_ISLOW:
  105. method_ptr = jpeg_idct_islow;
  106. method = JDCT_ISLOW;
  107. break;
  108. #endif
  109. #ifdef DCT_IFAST_SUPPORTED
  110. case JDCT_IFAST:
  111. method_ptr = jpeg_idct_ifast;
  112. method = JDCT_IFAST;
  113. break;
  114. #endif
  115. #ifdef DCT_FLOAT_SUPPORTED
  116. case JDCT_FLOAT:
  117. method_ptr = jpeg_idct_float;
  118. method = JDCT_FLOAT;
  119. break;
  120. #endif
  121. default:
  122. ERREXIT(cinfo, JERR_NOT_COMPILED);
  123. break;
  124. }
  125. break;
  126. default:
  127. ERREXIT1(cinfo, JERR_BAD_DCTSIZE, compptr->DCT_scaled_size);
  128. break;
  129. }
  130. idct->pub.inverse_DCT[ci] = method_ptr;
  131. /* Create multiplier table from quant table.
  132. * However, we can skip this if the component is uninteresting
  133. * or if we already built the table. Also, if no quant table
  134. * has yet been saved for the component, we leave the
  135. * multiplier table all-zero; we'll be reading zeroes from the
  136. * coefficient controller's buffer anyway.
  137. */
  138. if (! compptr->component_needed || idct->cur_method[ci] == method)
  139. continue;
  140. qtbl = compptr->quant_table;
  141. if (qtbl == NULL) /* happens if no data yet for component */
  142. continue;
  143. idct->cur_method[ci] = method;
  144. switch (method) {
  145. #ifdef PROVIDE_ISLOW_TABLES
  146. case JDCT_ISLOW:
  147. {
  148. /* For LL&M IDCT method, multipliers are equal to raw quantization
  149. * coefficients, but are stored as ints to ensure access efficiency.
  150. */
  151. ISLOW_MULT_TYPE * ismtbl = (ISLOW_MULT_TYPE *) compptr->dct_table;
  152. for (i = 0; i < DCTSIZE2; i++) {
  153. ismtbl[i] = (ISLOW_MULT_TYPE) qtbl->quantval[i];
  154. }
  155. }
  156. break;
  157. #endif
  158. #ifdef DCT_IFAST_SUPPORTED
  159. case JDCT_IFAST:
  160. {
  161. /* For AA&N IDCT method, multipliers are equal to quantization
  162. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  163. * scalefactor[0] = 1
  164. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  165. * For integer operation, the multiplier table is to be scaled by
  166. * IFAST_SCALE_BITS.
  167. */
  168. IFAST_MULT_TYPE * ifmtbl = (IFAST_MULT_TYPE *) compptr->dct_table;
  169. #define CONST_BITS 14
  170. static const INT16 aanscales[DCTSIZE2] = {
  171. /* precomputed values scaled up by 14 bits */
  172. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  173. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  174. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  175. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  176. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  177. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  178. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  179. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  180. };
  181. SHIFT_TEMPS
  182. for (i = 0; i < DCTSIZE2; i++) {
  183. ifmtbl[i] = (IFAST_MULT_TYPE)
  184. DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  185. (INT32) aanscales[i]),
  186. CONST_BITS-IFAST_SCALE_BITS);
  187. }
  188. }
  189. break;
  190. #endif
  191. #ifdef DCT_FLOAT_SUPPORTED
  192. case JDCT_FLOAT:
  193. {
  194. /* For float AA&N IDCT method, multipliers are equal to quantization
  195. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  196. * scalefactor[0] = 1
  197. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  198. */
  199. FLOAT_MULT_TYPE * fmtbl = (FLOAT_MULT_TYPE *) compptr->dct_table;
  200. int row, col;
  201. static const double aanscalefactor[DCTSIZE] = {
  202. 1.0, 1.387039845, 1.306562965, 1.175875602,
  203. 1.0, 0.785694958, 0.541196100, 0.275899379
  204. };
  205. i = 0;
  206. for (row = 0; row < DCTSIZE; row++) {
  207. for (col = 0; col < DCTSIZE; col++) {
  208. fmtbl[i] = (FLOAT_MULT_TYPE)
  209. ((double) qtbl->quantval[i] *
  210. aanscalefactor[row] * aanscalefactor[col]);
  211. i++;
  212. }
  213. }
  214. }
  215. break;
  216. #endif
  217. default:
  218. ERREXIT(cinfo, JERR_NOT_COMPILED);
  219. break;
  220. }
  221. }
  222. }
  223. /*
  224. * Initialize IDCT manager.
  225. */
  226. GLOBAL(void)
  227. jinit_inverse_dct (j_decompress_ptr cinfo)
  228. {
  229. my_idct_ptr idct;
  230. int ci;
  231. jpeg_component_info *compptr;
  232. idct = (my_idct_ptr)
  233. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  234. SIZEOF(my_idct_controller));
  235. cinfo->idct = (struct jpeg_inverse_dct *) idct;
  236. idct->pub.start_pass = start_pass;
  237. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  238. ci++, compptr++) {
  239. /* Allocate and pre-zero a multiplier table for each component */
  240. compptr->dct_table =
  241. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  242. SIZEOF(multiplier_table));
  243. MEMZERO(compptr->dct_table, SIZEOF(multiplier_table));
  244. /* Mark multiplier table not yet set up for any method */
  245. idct->cur_method[ci] = -1;
  246. }
  247. }