jcdctmgr.c
上传用户:zlh9724
上传日期:2007-01-04
资源大小:1991k
文件大小:12k
源码类别:

浏览器

开发平台:

Unix_Linux

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