PageRenderTime 26ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/JuceLibraryCode/modules/juce_graphics/image_formats/jpglib/jcdctmgr.c

https://bitbucket.org/teamaxe/markingtool
C | 387 lines | 260 code | 39 blank | 88 comment | 31 complexity | f31b3b0e460302c56fb9a63ede3ca667 MD5 | raw file
  1. /*
  2. * jcdctmgr.c
  3. *
  4. * Copyright (C) 1994-1996, 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 forward-DCT management logic.
  9. * This code selects a particular DCT implementation to be used,
  10. * and it performs related housekeeping chores including coefficient
  11. * quantization.
  12. */
  13. #define JPEG_INTERNALS
  14. #include "jinclude.h"
  15. #include "jpeglib.h"
  16. #include "jdct.h" /* Private declarations for DCT subsystem */
  17. /* Private subobject for this module */
  18. typedef struct {
  19. struct jpeg_forward_dct pub; /* public fields */
  20. /* Pointer to the DCT routine actually in use */
  21. forward_DCT_method_ptr do_dct;
  22. /* The actual post-DCT divisors --- not identical to the quant table
  23. * entries, because of scaling (especially for an unnormalized DCT).
  24. * Each table is given in normal array order.
  25. */
  26. DCTELEM * divisors[NUM_QUANT_TBLS];
  27. #ifdef DCT_FLOAT_SUPPORTED
  28. /* Same as above for the floating-point case. */
  29. float_DCT_method_ptr do_float_dct;
  30. FAST_FLOAT * float_divisors[NUM_QUANT_TBLS];
  31. #endif
  32. } my_fdct_controller;
  33. typedef my_fdct_controller * my_fdct_ptr;
  34. /*
  35. * Initialize for a processing pass.
  36. * Verify that all referenced Q-tables are present, and set up
  37. * the divisor table for each one.
  38. * In the current implementation, DCT of all components is done during
  39. * the first pass, even if only some components will be output in the
  40. * first scan. Hence all components should be examined here.
  41. */
  42. METHODDEF(void)
  43. start_pass_fdctmgr (j_compress_ptr cinfo)
  44. {
  45. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  46. int ci, qtblno, i;
  47. jpeg_component_info *compptr;
  48. JQUANT_TBL * qtbl;
  49. DCTELEM * dtbl;
  50. for (ci = 0, compptr = cinfo->comp_info; ci < cinfo->num_components;
  51. ci++, compptr++) {
  52. qtblno = compptr->quant_tbl_no;
  53. /* Make sure specified quantization table is present */
  54. if (qtblno < 0 || qtblno >= NUM_QUANT_TBLS ||
  55. cinfo->quant_tbl_ptrs[qtblno] == NULL)
  56. ERREXIT1(cinfo, JERR_NO_QUANT_TABLE, qtblno);
  57. qtbl = cinfo->quant_tbl_ptrs[qtblno];
  58. /* Compute divisors for this quant table */
  59. /* We may do this more than once for same table, but it's not a big deal */
  60. switch (cinfo->dct_method) {
  61. #ifdef DCT_ISLOW_SUPPORTED
  62. case JDCT_ISLOW:
  63. /* For LL&M IDCT method, divisors are equal to raw quantization
  64. * coefficients multiplied by 8 (to counteract scaling).
  65. */
  66. if (fdct->divisors[qtblno] == NULL) {
  67. fdct->divisors[qtblno] = (DCTELEM *)
  68. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  69. DCTSIZE2 * SIZEOF(DCTELEM));
  70. }
  71. dtbl = fdct->divisors[qtblno];
  72. for (i = 0; i < DCTSIZE2; i++) {
  73. dtbl[i] = ((DCTELEM) qtbl->quantval[i]) << 3;
  74. }
  75. break;
  76. #endif
  77. #ifdef DCT_IFAST_SUPPORTED
  78. case JDCT_IFAST:
  79. {
  80. /* For AA&N IDCT method, divisors are equal to quantization
  81. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  82. * scalefactor[0] = 1
  83. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  84. * We apply a further scale factor of 8.
  85. */
  86. #define CONST_BITS 14
  87. static const INT16 aanscales[DCTSIZE2] = {
  88. /* precomputed values scaled up by 14 bits */
  89. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  90. 22725, 31521, 29692, 26722, 22725, 17855, 12299, 6270,
  91. 21407, 29692, 27969, 25172, 21407, 16819, 11585, 5906,
  92. 19266, 26722, 25172, 22654, 19266, 15137, 10426, 5315,
  93. 16384, 22725, 21407, 19266, 16384, 12873, 8867, 4520,
  94. 12873, 17855, 16819, 15137, 12873, 10114, 6967, 3552,
  95. 8867, 12299, 11585, 10426, 8867, 6967, 4799, 2446,
  96. 4520, 6270, 5906, 5315, 4520, 3552, 2446, 1247
  97. };
  98. SHIFT_TEMPS
  99. if (fdct->divisors[qtblno] == NULL) {
  100. fdct->divisors[qtblno] = (DCTELEM *)
  101. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  102. DCTSIZE2 * SIZEOF(DCTELEM));
  103. }
  104. dtbl = fdct->divisors[qtblno];
  105. for (i = 0; i < DCTSIZE2; i++) {
  106. dtbl[i] = (DCTELEM)
  107. DESCALE(MULTIPLY16V16((INT32) qtbl->quantval[i],
  108. (INT32) aanscales[i]),
  109. CONST_BITS-3);
  110. }
  111. }
  112. break;
  113. #endif
  114. #ifdef DCT_FLOAT_SUPPORTED
  115. case JDCT_FLOAT:
  116. {
  117. /* For float AA&N IDCT method, divisors are equal to quantization
  118. * coefficients scaled by scalefactor[row]*scalefactor[col], where
  119. * scalefactor[0] = 1
  120. * scalefactor[k] = cos(k*PI/16) * sqrt(2) for k=1..7
  121. * We apply a further scale factor of 8.
  122. * What's actually stored is 1/divisor so that the inner loop can
  123. * use a multiplication rather than a division.
  124. */
  125. FAST_FLOAT * fdtbl;
  126. int row, col;
  127. static const double aanscalefactor[DCTSIZE] = {
  128. 1.0, 1.387039845, 1.306562965, 1.175875602,
  129. 1.0, 0.785694958, 0.541196100, 0.275899379
  130. };
  131. if (fdct->float_divisors[qtblno] == NULL) {
  132. fdct->float_divisors[qtblno] = (FAST_FLOAT *)
  133. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  134. DCTSIZE2 * SIZEOF(FAST_FLOAT));
  135. }
  136. fdtbl = fdct->float_divisors[qtblno];
  137. i = 0;
  138. for (row = 0; row < DCTSIZE; row++) {
  139. for (col = 0; col < DCTSIZE; col++) {
  140. fdtbl[i] = (FAST_FLOAT)
  141. (1.0 / (((double) qtbl->quantval[i] *
  142. aanscalefactor[row] * aanscalefactor[col] * 8.0)));
  143. i++;
  144. }
  145. }
  146. }
  147. break;
  148. #endif
  149. default:
  150. ERREXIT(cinfo, JERR_NOT_COMPILED);
  151. break;
  152. }
  153. }
  154. }
  155. /*
  156. * Perform forward DCT on one or more blocks of a component.
  157. *
  158. * The input samples are taken from the sample_data[] array starting at
  159. * position start_row/start_col, and moving to the right for any additional
  160. * blocks. The quantized coefficients are returned in coef_blocks[].
  161. */
  162. METHODDEF(void)
  163. forward_DCT (j_compress_ptr cinfo, jpeg_component_info * compptr,
  164. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  165. JDIMENSION start_row, JDIMENSION start_col,
  166. JDIMENSION num_blocks)
  167. /* This version is used for integer DCT implementations. */
  168. {
  169. /* This routine is heavily used, so it's worth coding it tightly. */
  170. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  171. forward_DCT_method_ptr do_dct = fdct->do_dct;
  172. DCTELEM * divisors = fdct->divisors[compptr->quant_tbl_no];
  173. DCTELEM workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  174. JDIMENSION bi;
  175. sample_data += start_row; /* fold in the vertical offset once */
  176. for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  177. /* Load data into workspace, applying unsigned->signed conversion */
  178. { register DCTELEM *workspaceptr;
  179. register JSAMPROW elemptr;
  180. register int elemr;
  181. workspaceptr = workspace;
  182. for (elemr = 0; elemr < DCTSIZE; elemr++) {
  183. elemptr = sample_data[elemr] + start_col;
  184. #if DCTSIZE == 8 /* unroll the inner loop */
  185. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  186. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  187. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  188. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  189. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  190. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  191. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  192. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  193. #else
  194. { register int elemc;
  195. for (elemc = DCTSIZE; elemc > 0; elemc--) {
  196. *workspaceptr++ = GETJSAMPLE(*elemptr++) - CENTERJSAMPLE;
  197. }
  198. }
  199. #endif
  200. }
  201. }
  202. /* Perform the DCT */
  203. (*do_dct) (workspace);
  204. /* Quantize/descale the coefficients, and store into coef_blocks[] */
  205. { register DCTELEM temp, qval;
  206. register int i;
  207. register JCOEFPTR output_ptr = coef_blocks[bi];
  208. for (i = 0; i < DCTSIZE2; i++) {
  209. qval = divisors[i];
  210. temp = workspace[i];
  211. /* Divide the coefficient value by qval, ensuring proper rounding.
  212. * Since C does not specify the direction of rounding for negative
  213. * quotients, we have to force the dividend positive for portability.
  214. *
  215. * In most files, at least half of the output values will be zero
  216. * (at default quantization settings, more like three-quarters...)
  217. * so we should ensure that this case is fast. On many machines,
  218. * a comparison is enough cheaper than a divide to make a special test
  219. * a win. Since both inputs will be nonnegative, we need only test
  220. * for a < b to discover whether a/b is 0.
  221. * If your machine's division is fast enough, define FAST_DIVIDE.
  222. */
  223. #ifdef FAST_DIVIDE
  224. #define DIVIDE_BY(a,b) a /= b
  225. #else
  226. #define DIVIDE_BY(a,b) if (a >= b) a /= b; else a = 0
  227. #endif
  228. if (temp < 0) {
  229. temp = -temp;
  230. temp += qval>>1; /* for rounding */
  231. DIVIDE_BY(temp, qval);
  232. temp = -temp;
  233. } else {
  234. temp += qval>>1; /* for rounding */
  235. DIVIDE_BY(temp, qval);
  236. }
  237. output_ptr[i] = (JCOEF) temp;
  238. }
  239. }
  240. }
  241. }
  242. #ifdef DCT_FLOAT_SUPPORTED
  243. METHODDEF(void)
  244. forward_DCT_float (j_compress_ptr cinfo, jpeg_component_info * compptr,
  245. JSAMPARRAY sample_data, JBLOCKROW coef_blocks,
  246. JDIMENSION start_row, JDIMENSION start_col,
  247. JDIMENSION num_blocks)
  248. /* This version is used for floating-point DCT implementations. */
  249. {
  250. /* This routine is heavily used, so it's worth coding it tightly. */
  251. my_fdct_ptr fdct = (my_fdct_ptr) cinfo->fdct;
  252. float_DCT_method_ptr do_dct = fdct->do_float_dct;
  253. FAST_FLOAT * divisors = fdct->float_divisors[compptr->quant_tbl_no];
  254. FAST_FLOAT workspace[DCTSIZE2]; /* work area for FDCT subroutine */
  255. JDIMENSION bi;
  256. sample_data += start_row; /* fold in the vertical offset once */
  257. for (bi = 0; bi < num_blocks; bi++, start_col += DCTSIZE) {
  258. /* Load data into workspace, applying unsigned->signed conversion */
  259. { register FAST_FLOAT *workspaceptr;
  260. register JSAMPROW elemptr;
  261. register int elemr;
  262. workspaceptr = workspace;
  263. for (elemr = 0; elemr < DCTSIZE; elemr++) {
  264. elemptr = sample_data[elemr] + start_col;
  265. #if DCTSIZE == 8 /* unroll the inner loop */
  266. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  267. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  268. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  269. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  270. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  271. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  272. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  273. *workspaceptr++ = (FAST_FLOAT)(GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  274. #else
  275. { register int elemc;
  276. for (elemc = DCTSIZE; elemc > 0; elemc--) {
  277. *workspaceptr++ = (FAST_FLOAT)
  278. (GETJSAMPLE(*elemptr++) - CENTERJSAMPLE);
  279. }
  280. }
  281. #endif
  282. }
  283. }
  284. /* Perform the DCT */
  285. (*do_dct) (workspace);
  286. /* Quantize/descale the coefficients, and store into coef_blocks[] */
  287. { register FAST_FLOAT temp;
  288. register int i;
  289. register JCOEFPTR output_ptr = coef_blocks[bi];
  290. for (i = 0; i < DCTSIZE2; i++) {
  291. /* Apply the quantization and scaling factor */
  292. temp = workspace[i] * divisors[i];
  293. /* Round to nearest integer.
  294. * Since C does not specify the direction of rounding for negative
  295. * quotients, we have to force the dividend positive for portability.
  296. * The maximum coefficient size is +-16K (for 12-bit data), so this
  297. * code should work for either 16-bit or 32-bit ints.
  298. */
  299. output_ptr[i] = (JCOEF) ((int) (temp + (FAST_FLOAT) 16384.5) - 16384);
  300. }
  301. }
  302. }
  303. }
  304. #endif /* DCT_FLOAT_SUPPORTED */
  305. /*
  306. * Initialize FDCT manager.
  307. */
  308. GLOBAL(void)
  309. jinit_forward_dct (j_compress_ptr cinfo)
  310. {
  311. my_fdct_ptr fdct;
  312. int i;
  313. fdct = (my_fdct_ptr)
  314. (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE,
  315. SIZEOF(my_fdct_controller));
  316. cinfo->fdct = (struct jpeg_forward_dct *) fdct;
  317. fdct->pub.start_pass = start_pass_fdctmgr;
  318. switch (cinfo->dct_method) {
  319. #ifdef DCT_ISLOW_SUPPORTED
  320. case JDCT_ISLOW:
  321. fdct->pub.forward_DCT = forward_DCT;
  322. fdct->do_dct = jpeg_fdct_islow;
  323. break;
  324. #endif
  325. #ifdef DCT_IFAST_SUPPORTED
  326. case JDCT_IFAST:
  327. fdct->pub.forward_DCT = forward_DCT;
  328. fdct->do_dct = jpeg_fdct_ifast;
  329. break;
  330. #endif
  331. #ifdef DCT_FLOAT_SUPPORTED
  332. case JDCT_FLOAT:
  333. fdct->pub.forward_DCT = forward_DCT_float;
  334. fdct->do_float_dct = jpeg_fdct_float;
  335. break;
  336. #endif
  337. default:
  338. ERREXIT(cinfo, JERR_NOT_COMPILED);
  339. break;
  340. }
  341. /* Mark divisor tables unallocated */
  342. for (i = 0; i < NUM_QUANT_TBLS; i++) {
  343. fdct->divisors[i] = NULL;
  344. #ifdef DCT_FLOAT_SUPPORTED
  345. fdct->float_divisors[i] = NULL;
  346. #endif
  347. }
  348. }