jddctmgr.cpp 10 KB

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