home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / jpeg / jcdctmgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  12.8 KB  |  390 lines

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