JIDCTINT.c
上传用户:qiutianh
上传日期:2022-08-08
资源大小:939k
文件大小:16k
源码类别:

图形图象

开发平台:

Visual C++

  1. ////////////////////////////////////////////////////////////////////////
  2. //
  3. // Note : this file is included as part of the Smaller Animals Software
  4. // JpegFile package. Though this file has not been modified from it's 
  5. // original IJG 6a form, it is not the responsibility on the Independent
  6. // JPEG Group to answer questions regarding this code.
  7. //
  8. // Any questions you have about this code should be addressed to :
  9. //
  10. // CHRISDL@PAGESZ.NET - the distributor of this package.
  11. //
  12. // Remember, by including this code in the JpegFile package, Smaller 
  13. // Animals Software assumes all responsibilities for answering questions
  14. // about it. If we (SA Software) can't answer your questions ourselves, we 
  15. // will direct you to people who can.
  16. //
  17. // Thanks, CDL.
  18. //
  19. ////////////////////////////////////////////////////////////////////////
  20. /*
  21.  * jidctint.c
  22.  *
  23.  * Copyright (C) 1991-1996, Thomas G. Lane.
  24.  * This file is part of the Independent JPEG Group's software.
  25.  * For conditions of distribution and use, see the accompanying README file.
  26.  *
  27.  * This file contains a slow-but-accurate integer implementation of the
  28.  * inverse DCT (Discrete Cosine Transform).  In the IJG code, this routine
  29.  * must also perform dequantization of the input coefficients.
  30.  *
  31.  * A 2-D IDCT can be done by 1-D IDCT on each column followed by 1-D IDCT
  32.  * on each row (or vice versa, but it's more convenient to emit a row at
  33.  * a time).  Direct algorithms are also available, but they are much more
  34.  * complex and seem not to be any faster when reduced to code.
  35.  *
  36.  * This implementation is based on an algorithm described in
  37.  *   C. Loeffler, A. Ligtenberg and G. Moschytz, "Practical Fast 1-D DCT
  38.  *   Algorithms with 11 Multiplications", Proc. Int'l. Conf. on Acoustics,
  39.  *   Speech, and Signal Processing 1989 (ICASSP '89), pp. 988-991.
  40.  * The primary algorithm described there uses 11 multiplies and 29 adds.
  41.  * We use their alternate method with 12 multiplies and 32 adds.
  42.  * The advantage of this method is that no data path contains more than one
  43.  * multiplication; this allows a very simple and accurate implementation in
  44.  * scaled fixed-point arithmetic, with a minimal number of shifts.
  45.  */
  46. #define JPEG_INTERNALS
  47. #include "jinclude.h"
  48. #include "jpeglib.h"
  49. #include "jdct.h" /* Private declarations for DCT subsystem */
  50. #ifdef DCT_ISLOW_SUPPORTED
  51. /*
  52.  * This module is specialized to the case DCTSIZE = 8.
  53.  */
  54. #if DCTSIZE != 8
  55.   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  56. #endif
  57. /*
  58.  * The poop on this scaling stuff is as follows:
  59.  *
  60.  * Each 1-D IDCT step produces outputs which are a factor of sqrt(N)
  61.  * larger than the true IDCT outputs.  The final outputs are therefore
  62.  * a factor of N larger than desired; since N=8 this can be cured by
  63.  * a simple right shift at the end of the algorithm.  The advantage of
  64.  * this arrangement is that we save two multiplications per 1-D IDCT,
  65.  * because the y0 and y4 inputs need not be divided by sqrt(N).
  66.  *
  67.  * We have to do addition and subtraction of the integer inputs, which
  68.  * is no problem, and multiplication by fractional constants, which is
  69.  * a problem to do in integer arithmetic.  We multiply all the constants
  70.  * by CONST_SCALE and convert them to integer constants (thus retaining
  71.  * CONST_BITS bits of precision in the constants).  After doing a
  72.  * multiplication we have to divide the product by CONST_SCALE, with proper
  73.  * rounding, to produce the correct output.  This division can be done
  74.  * cheaply as a right shift of CONST_BITS bits.  We postpone shifting
  75.  * as long as possible so that partial sums can be added together with
  76.  * full fractional precision.
  77.  *
  78.  * The outputs of the first pass are scaled up by PASS1_BITS bits so that
  79.  * they are represented to better-than-integral precision.  These outputs
  80.  * require BITS_IN_JSAMPLE + PASS1_BITS + 3 bits; this fits in a 16-bit word
  81.  * with the recommended scaling.  (To scale up 12-bit sample data further, an
  82.  * intermediate long array would be needed.)
  83.  *
  84.  * To avoid overflow of the 32-bit intermediate results in pass 2, we must
  85.  * have BITS_IN_JSAMPLE + CONST_BITS + PASS1_BITS <= 26.  Error analysis
  86.  * shows that the values given below are the most effective.
  87.  */
  88. #if BITS_IN_JSAMPLE == 8
  89. #define CONST_BITS  13
  90. #define PASS1_BITS  2
  91. #else
  92. #define CONST_BITS  13
  93. #define PASS1_BITS  1 /* lose a little precision to avoid overflow */
  94. #endif
  95. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  96.  * causing a lot of useless floating-point operations at run time.
  97.  * To get around this we use the following pre-calculated constants.
  98.  * If you change CONST_BITS you may want to add appropriate values.
  99.  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  100.  */
  101. #if CONST_BITS == 13
  102. #define FIX_0_298631336  ((long)  2446) /* FIX(0.298631336) */
  103. #define FIX_0_390180644  ((long)  3196) /* FIX(0.390180644) */
  104. #define FIX_0_541196100  ((long)  4433) /* FIX(0.541196100) */
  105. #define FIX_0_765366865  ((long)  6270) /* FIX(0.765366865) */
  106. #define FIX_0_899976223  ((long)  7373) /* FIX(0.899976223) */
  107. #define FIX_1_175875602  ((long)  9633) /* FIX(1.175875602) */
  108. #define FIX_1_501321110  ((long)  12299) /* FIX(1.501321110) */
  109. #define FIX_1_847759065  ((long)  15137) /* FIX(1.847759065) */
  110. #define FIX_1_961570560  ((long)  16069) /* FIX(1.961570560) */
  111. #define FIX_2_053119869  ((long)  16819) /* FIX(2.053119869) */
  112. #define FIX_2_562915447  ((long)  20995) /* FIX(2.562915447) */
  113. #define FIX_3_072711026  ((long)  25172) /* FIX(3.072711026) */
  114. #else
  115. #define FIX_0_298631336  FIX(0.298631336)
  116. #define FIX_0_390180644  FIX(0.390180644)
  117. #define FIX_0_541196100  FIX(0.541196100)
  118. #define FIX_0_765366865  FIX(0.765366865)
  119. #define FIX_0_899976223  FIX(0.899976223)
  120. #define FIX_1_175875602  FIX(1.175875602)
  121. #define FIX_1_501321110  FIX(1.501321110)
  122. #define FIX_1_847759065  FIX(1.847759065)
  123. #define FIX_1_961570560  FIX(1.961570560)
  124. #define FIX_2_053119869  FIX(2.053119869)
  125. #define FIX_2_562915447  FIX(2.562915447)
  126. #define FIX_3_072711026  FIX(3.072711026)
  127. #endif
  128. /* Multiply an long variable by an long constant to yield an long result.
  129.  * For 8-bit samples with the recommended scaling, all the variable
  130.  * and constant values involved are no more than 16 bits wide, so a
  131.  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  132.  * For 12-bit samples, a full 32-bit multiplication will be needed.
  133.  */
  134. #if BITS_IN_JSAMPLE == 8
  135. #define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  136. #else
  137. #define MULTIPLY(var,const)  ((var) * (const))
  138. #endif
  139. /* Dequantize a coefficient by multiplying it by the multiplier-table
  140.  * entry; produce an int result.  In this module, both inputs and result
  141.  * are 16 bits or less, so either int or short multiply will work.
  142.  */
  143. #define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  144. /*
  145.  * Perform dequantization and inverse DCT on one block of coefficients.
  146.  */
  147. GLOBAL(void)
  148. jpeg_idct_islow (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  149.  JCOEFPTR coef_block,
  150.  JSAMPARRAY output_buf, JDIMENSION output_col)
  151. {
  152.   long tmp0, tmp1, tmp2, tmp3;
  153.   long tmp10, tmp11, tmp12, tmp13;
  154.   long z1, z2, z3, z4, z5;
  155.   JCOEFPTR inptr;
  156.   ISLOW_MULT_TYPE * quantptr;
  157.   int * wsptr;
  158.   JSAMPROW outptr;
  159.   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  160.   int ctr;
  161.   int workspace[DCTSIZE2]; /* buffers data between passes */
  162.   SHIFT_TEMPS
  163.   /* Pass 1: process columns from input, store into work array. */
  164.   /* Note results are scaled up by sqrt(8) compared to a true IDCT; */
  165.   /* furthermore, we scale the results by 2**PASS1_BITS. */
  166.   inptr = coef_block;
  167.   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  168.   wsptr = workspace;
  169.   for (ctr = DCTSIZE; ctr > 0; ctr--) {
  170.     /* Due to quantization, we will usually find that many of the input
  171.      * coefficients are zero, especially the AC terms.  We can exploit this
  172.      * by short-circuiting the IDCT calculation for any column in which all
  173.      * the AC terms are zero.  In that case each output is equal to the
  174.      * DC coefficient (with scale factor as needed).
  175.      * With typical images and quantization tables, half or more of the
  176.      * column DCT calculations can be simplified this way.
  177.      */
  178.     
  179.     if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  180.  inptr[DCTSIZE*4] | inptr[DCTSIZE*5] | inptr[DCTSIZE*6] |
  181.  inptr[DCTSIZE*7]) == 0) {
  182.       /* AC terms all zero */
  183.       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  184.       
  185.       wsptr[DCTSIZE*0] = dcval;
  186.       wsptr[DCTSIZE*1] = dcval;
  187.       wsptr[DCTSIZE*2] = dcval;
  188.       wsptr[DCTSIZE*3] = dcval;
  189.       wsptr[DCTSIZE*4] = dcval;
  190.       wsptr[DCTSIZE*5] = dcval;
  191.       wsptr[DCTSIZE*6] = dcval;
  192.       wsptr[DCTSIZE*7] = dcval;
  193.       
  194.       inptr++; /* advance pointers to next column */
  195.       quantptr++;
  196.       wsptr++;
  197.       continue;
  198.     }
  199.     
  200.     /* Even part: reverse the even part of the forward DCT. */
  201.     /* The rotator is sqrt(2)*c(-6). */
  202.     
  203.     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  204.     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  205.     
  206.     z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  207.     tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  208.     tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  209.     
  210.     z2 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  211.     z3 = DEQUANTIZE(inptr[DCTSIZE*4], quantptr[DCTSIZE*4]);
  212.     tmp0 = (z2 + z3) << CONST_BITS;
  213.     tmp1 = (z2 - z3) << CONST_BITS;
  214.     
  215.     tmp10 = tmp0 + tmp3;
  216.     tmp13 = tmp0 - tmp3;
  217.     tmp11 = tmp1 + tmp2;
  218.     tmp12 = tmp1 - tmp2;
  219.     
  220.     /* Odd part per figure 8; the matrix is unitary and hence its
  221.      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  222.      */
  223.     
  224.     tmp0 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  225.     tmp1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  226.     tmp2 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  227.     tmp3 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  228.     
  229.     z1 = tmp0 + tmp3;
  230.     z2 = tmp1 + tmp2;
  231.     z3 = tmp0 + tmp2;
  232.     z4 = tmp1 + tmp3;
  233.     z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  234.     
  235.     tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  236.     tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  237.     tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  238.     tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  239.     z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  240.     z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  241.     z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  242.     z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  243.     
  244.     z3 += z5;
  245.     z4 += z5;
  246.     
  247.     tmp0 += z1 + z3;
  248.     tmp1 += z2 + z4;
  249.     tmp2 += z2 + z3;
  250.     tmp3 += z1 + z4;
  251.     
  252.     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  253.     
  254.     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp3, CONST_BITS-PASS1_BITS);
  255.     wsptr[DCTSIZE*7] = (int) DESCALE(tmp10 - tmp3, CONST_BITS-PASS1_BITS);
  256.     wsptr[DCTSIZE*1] = (int) DESCALE(tmp11 + tmp2, CONST_BITS-PASS1_BITS);
  257.     wsptr[DCTSIZE*6] = (int) DESCALE(tmp11 - tmp2, CONST_BITS-PASS1_BITS);
  258.     wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 + tmp1, CONST_BITS-PASS1_BITS);
  259.     wsptr[DCTSIZE*5] = (int) DESCALE(tmp12 - tmp1, CONST_BITS-PASS1_BITS);
  260.     wsptr[DCTSIZE*3] = (int) DESCALE(tmp13 + tmp0, CONST_BITS-PASS1_BITS);
  261.     wsptr[DCTSIZE*4] = (int) DESCALE(tmp13 - tmp0, CONST_BITS-PASS1_BITS);
  262.     
  263.     inptr++; /* advance pointers to next column */
  264.     quantptr++;
  265.     wsptr++;
  266.   }
  267.   
  268.   /* Pass 2: process rows from work array, store into output array. */
  269.   /* Note that we must descale the results by a factor of 8 == 2**3, */
  270.   /* and also undo the PASS1_BITS scaling. */
  271.   wsptr = workspace;
  272.   for (ctr = 0; ctr < DCTSIZE; ctr++) {
  273.     outptr = output_buf[ctr] + output_col;
  274.     /* Rows of zeroes can be exploited in the same way as we did with columns.
  275.      * However, the column calculation has created many nonzero AC terms, so
  276.      * the simplification applies less often (typically 5% to 10% of the time).
  277.      * On machines with very fast multiplication, it's possible that the
  278.      * test takes more time than it's worth.  In that case this section
  279.      * may be commented out.
  280.      */
  281.     
  282. #ifndef NO_ZERO_ROW_TEST
  283.     if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[4] | wsptr[5] | wsptr[6] |
  284.  wsptr[7]) == 0) {
  285.       /* AC terms all zero */
  286.       JSAMPLE dcval = range_limit[(int) DESCALE((long) wsptr[0], PASS1_BITS+3)
  287.   & RANGE_MASK];
  288.       
  289.       outptr[0] = dcval;
  290.       outptr[1] = dcval;
  291.       outptr[2] = dcval;
  292.       outptr[3] = dcval;
  293.       outptr[4] = dcval;
  294.       outptr[5] = dcval;
  295.       outptr[6] = dcval;
  296.       outptr[7] = dcval;
  297.       wsptr += DCTSIZE; /* advance pointer to next row */
  298.       continue;
  299.     }
  300. #endif
  301.     
  302.     /* Even part: reverse the even part of the forward DCT. */
  303.     /* The rotator is sqrt(2)*c(-6). */
  304.     
  305.     z2 = (long) wsptr[2];
  306.     z3 = (long) wsptr[6];
  307.     
  308.     z1 = MULTIPLY(z2 + z3, FIX_0_541196100);
  309.     tmp2 = z1 + MULTIPLY(z3, - FIX_1_847759065);
  310.     tmp3 = z1 + MULTIPLY(z2, FIX_0_765366865);
  311.     
  312.     tmp0 = ((long) wsptr[0] + (long) wsptr[4]) << CONST_BITS;
  313.     tmp1 = ((long) wsptr[0] - (long) wsptr[4]) << CONST_BITS;
  314.     
  315.     tmp10 = tmp0 + tmp3;
  316.     tmp13 = tmp0 - tmp3;
  317.     tmp11 = tmp1 + tmp2;
  318.     tmp12 = tmp1 - tmp2;
  319.     
  320.     /* Odd part per figure 8; the matrix is unitary and hence its
  321.      * transpose is its inverse.  i0..i3 are y7,y5,y3,y1 respectively.
  322.      */
  323.     
  324.     tmp0 = (long) wsptr[7];
  325.     tmp1 = (long) wsptr[5];
  326.     tmp2 = (long) wsptr[3];
  327.     tmp3 = (long) wsptr[1];
  328.     
  329.     z1 = tmp0 + tmp3;
  330.     z2 = tmp1 + tmp2;
  331.     z3 = tmp0 + tmp2;
  332.     z4 = tmp1 + tmp3;
  333.     z5 = MULTIPLY(z3 + z4, FIX_1_175875602); /* sqrt(2) * c3 */
  334.     
  335.     tmp0 = MULTIPLY(tmp0, FIX_0_298631336); /* sqrt(2) * (-c1+c3+c5-c7) */
  336.     tmp1 = MULTIPLY(tmp1, FIX_2_053119869); /* sqrt(2) * ( c1+c3-c5+c7) */
  337.     tmp2 = MULTIPLY(tmp2, FIX_3_072711026); /* sqrt(2) * ( c1+c3+c5-c7) */
  338.     tmp3 = MULTIPLY(tmp3, FIX_1_501321110); /* sqrt(2) * ( c1+c3-c5-c7) */
  339.     z1 = MULTIPLY(z1, - FIX_0_899976223); /* sqrt(2) * (c7-c3) */
  340.     z2 = MULTIPLY(z2, - FIX_2_562915447); /* sqrt(2) * (-c1-c3) */
  341.     z3 = MULTIPLY(z3, - FIX_1_961570560); /* sqrt(2) * (-c3-c5) */
  342.     z4 = MULTIPLY(z4, - FIX_0_390180644); /* sqrt(2) * (c5-c3) */
  343.     
  344.     z3 += z5;
  345.     z4 += z5;
  346.     
  347.     tmp0 += z1 + z3;
  348.     tmp1 += z2 + z4;
  349.     tmp2 += z2 + z3;
  350.     tmp3 += z1 + z4;
  351.     
  352.     /* Final output stage: inputs are tmp10..tmp13, tmp0..tmp3 */
  353.     
  354.     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp3,
  355.   CONST_BITS+PASS1_BITS+3)
  356.     & RANGE_MASK];
  357.     outptr[7] = range_limit[(int) DESCALE(tmp10 - tmp3,
  358.   CONST_BITS+PASS1_BITS+3)
  359.     & RANGE_MASK];
  360.     outptr[1] = range_limit[(int) DESCALE(tmp11 + tmp2,
  361.   CONST_BITS+PASS1_BITS+3)
  362.     & RANGE_MASK];
  363.     outptr[6] = range_limit[(int) DESCALE(tmp11 - tmp2,
  364.   CONST_BITS+PASS1_BITS+3)
  365.     & RANGE_MASK];
  366.     outptr[2] = range_limit[(int) DESCALE(tmp12 + tmp1,
  367.   CONST_BITS+PASS1_BITS+3)
  368.     & RANGE_MASK];
  369.     outptr[5] = range_limit[(int) DESCALE(tmp12 - tmp1,
  370.   CONST_BITS+PASS1_BITS+3)
  371.     & RANGE_MASK];
  372.     outptr[3] = range_limit[(int) DESCALE(tmp13 + tmp0,
  373.   CONST_BITS+PASS1_BITS+3)
  374.     & RANGE_MASK];
  375.     outptr[4] = range_limit[(int) DESCALE(tmp13 - tmp0,
  376.   CONST_BITS+PASS1_BITS+3)
  377.     & RANGE_MASK];
  378.     
  379.     wsptr += DCTSIZE; /* advance pointer to next row */
  380.   }
  381. }
  382. #endif /* DCT_ISLOW_SUPPORTED */