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

图形图象

开发平台:

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.  * jidctred.c
  22.  *
  23.  * Copyright (C) 1994-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 inverse-DCT routines that produce reduced-size output:
  28.  * either 4x4, 2x2, or 1x1 pixels from an 8x8 DCT block.
  29.  *
  30.  * The implementation is based on the Loeffler, Ligtenberg and Moschytz (LL&M)
  31.  * algorithm used in jidctint.c.  We simply replace each 8-to-8 1-D IDCT step
  32.  * with an 8-to-4 step that produces the four averages of two adjacent outputs
  33.  * (or an 8-to-2 step producing two averages of four outputs, for 2x2 output).
  34.  * These steps were derived by computing the corresponding values at the end
  35.  * of the normal LL&M code, then simplifying as much as possible.
  36.  *
  37.  * 1x1 is trivial: just take the DC coefficient divided by 8.
  38.  *
  39.  * See jidctint.c for additional comments.
  40.  */
  41. #define JPEG_INTERNALS
  42. #include "jinclude.h"
  43. #include "jpeglib.h"
  44. #include "jdct.h" /* Private declarations for DCT subsystem */
  45. #ifdef IDCT_SCALING_SUPPORTED
  46. /*
  47.  * This module is specialized to the case DCTSIZE = 8.
  48.  */
  49. #if DCTSIZE != 8
  50.   Sorry, this code only copes with 8x8 DCTs. /* deliberate syntax err */
  51. #endif
  52. /* Scaling is the same as in jidctint.c. */
  53. #if BITS_IN_JSAMPLE == 8
  54. #define CONST_BITS  13
  55. #define PASS1_BITS  2
  56. #else
  57. #define CONST_BITS  13
  58. #define PASS1_BITS  1 /* lose a little precision to avoid overflow */
  59. #endif
  60. /* Some C compilers fail to reduce "FIX(constant)" at compile time, thus
  61.  * causing a lot of useless floating-point operations at run time.
  62.  * To get around this we use the following pre-calculated constants.
  63.  * If you change CONST_BITS you may want to add appropriate values.
  64.  * (With a reasonable C compiler, you can just rely on the FIX() macro...)
  65.  */
  66. #if CONST_BITS == 13
  67. #define FIX_0_211164243  ((long)  1730) /* FIX(0.211164243) */
  68. #define FIX_0_509795579  ((long)  4176) /* FIX(0.509795579) */
  69. #define FIX_0_601344887  ((long)  4926) /* FIX(0.601344887) */
  70. #define FIX_0_720959822  ((long)  5906) /* FIX(0.720959822) */
  71. #define FIX_0_765366865  ((long)  6270) /* FIX(0.765366865) */
  72. #define FIX_0_850430095  ((long)  6967) /* FIX(0.850430095) */
  73. #define FIX_0_899976223  ((long)  7373) /* FIX(0.899976223) */
  74. #define FIX_1_061594337  ((long)  8697) /* FIX(1.061594337) */
  75. #define FIX_1_272758580  ((long)  10426) /* FIX(1.272758580) */
  76. #define FIX_1_451774981  ((long)  11893) /* FIX(1.451774981) */
  77. #define FIX_1_847759065  ((long)  15137) /* FIX(1.847759065) */
  78. #define FIX_2_172734803  ((long)  17799) /* FIX(2.172734803) */
  79. #define FIX_2_562915447  ((long)  20995) /* FIX(2.562915447) */
  80. #define FIX_3_624509785  ((long)  29692) /* FIX(3.624509785) */
  81. #else
  82. #define FIX_0_211164243  FIX(0.211164243)
  83. #define FIX_0_509795579  FIX(0.509795579)
  84. #define FIX_0_601344887  FIX(0.601344887)
  85. #define FIX_0_720959822  FIX(0.720959822)
  86. #define FIX_0_765366865  FIX(0.765366865)
  87. #define FIX_0_850430095  FIX(0.850430095)
  88. #define FIX_0_899976223  FIX(0.899976223)
  89. #define FIX_1_061594337  FIX(1.061594337)
  90. #define FIX_1_272758580  FIX(1.272758580)
  91. #define FIX_1_451774981  FIX(1.451774981)
  92. #define FIX_1_847759065  FIX(1.847759065)
  93. #define FIX_2_172734803  FIX(2.172734803)
  94. #define FIX_2_562915447  FIX(2.562915447)
  95. #define FIX_3_624509785  FIX(3.624509785)
  96. #endif
  97. /* Multiply an long variable by an long constant to yield an long result.
  98.  * For 8-bit samples with the recommended scaling, all the variable
  99.  * and constant values involved are no more than 16 bits wide, so a
  100.  * 16x16->32 bit multiply can be used instead of a full 32x32 multiply.
  101.  * For 12-bit samples, a full 32-bit multiplication will be needed.
  102.  */
  103. #if BITS_IN_JSAMPLE == 8
  104. #define MULTIPLY(var,const)  MULTIPLY16C16(var,const)
  105. #else
  106. #define MULTIPLY(var,const)  ((var) * (const))
  107. #endif
  108. /* Dequantize a coefficient by multiplying it by the multiplier-table
  109.  * entry; produce an int result.  In this module, both inputs and result
  110.  * are 16 bits or less, so either int or short multiply will work.
  111.  */
  112. #define DEQUANTIZE(coef,quantval)  (((ISLOW_MULT_TYPE) (coef)) * (quantval))
  113. /*
  114.  * Perform dequantization and inverse DCT on one block of coefficients,
  115.  * producing a reduced-size 4x4 output block.
  116.  */
  117. GLOBAL(void)
  118. jpeg_idct_4x4 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  119.        JCOEFPTR coef_block,
  120.        JSAMPARRAY output_buf, JDIMENSION output_col)
  121. {
  122.   long tmp0, tmp2, tmp10, tmp12;
  123.   long z1, z2, z3, z4;
  124.   JCOEFPTR inptr;
  125.   ISLOW_MULT_TYPE * quantptr;
  126.   int * wsptr;
  127.   JSAMPROW outptr;
  128.   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  129.   int ctr;
  130.   int workspace[DCTSIZE*4]; /* buffers data between passes */
  131.   SHIFT_TEMPS
  132.   /* Pass 1: process columns from input, store into work array. */
  133.   inptr = coef_block;
  134.   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  135.   wsptr = workspace;
  136.   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  137.     /* Don't bother to process column 4, because second pass won't use it */
  138.     if (ctr == DCTSIZE-4)
  139.       continue;
  140.     if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*2] | inptr[DCTSIZE*3] |
  141.  inptr[DCTSIZE*5] | inptr[DCTSIZE*6] | inptr[DCTSIZE*7]) == 0) {
  142.       /* AC terms all zero; we need not examine term 4 for 4x4 output */
  143.       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  144.       
  145.       wsptr[DCTSIZE*0] = dcval;
  146.       wsptr[DCTSIZE*1] = dcval;
  147.       wsptr[DCTSIZE*2] = dcval;
  148.       wsptr[DCTSIZE*3] = dcval;
  149.       
  150.       continue;
  151.     }
  152.     
  153.     /* Even part */
  154.     
  155.     tmp0 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  156.     tmp0 <<= (CONST_BITS+1);
  157.     
  158.     z2 = DEQUANTIZE(inptr[DCTSIZE*2], quantptr[DCTSIZE*2]);
  159.     z3 = DEQUANTIZE(inptr[DCTSIZE*6], quantptr[DCTSIZE*6]);
  160.     tmp2 = MULTIPLY(z2, FIX_1_847759065) + MULTIPLY(z3, - FIX_0_765366865);
  161.     
  162.     tmp10 = tmp0 + tmp2;
  163.     tmp12 = tmp0 - tmp2;
  164.     
  165.     /* Odd part */
  166.     
  167.     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  168.     z2 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  169.     z3 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  170.     z4 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  171.     
  172.     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  173.  + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  174.  + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  175.  + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  176.     
  177.     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  178.  + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  179.  + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  180.  + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  181.     /* Final output stage */
  182.     
  183.     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp2, CONST_BITS-PASS1_BITS+1);
  184.     wsptr[DCTSIZE*3] = (int) DESCALE(tmp10 - tmp2, CONST_BITS-PASS1_BITS+1);
  185.     wsptr[DCTSIZE*1] = (int) DESCALE(tmp12 + tmp0, CONST_BITS-PASS1_BITS+1);
  186.     wsptr[DCTSIZE*2] = (int) DESCALE(tmp12 - tmp0, CONST_BITS-PASS1_BITS+1);
  187.   }
  188.   
  189.   /* Pass 2: process 4 rows from work array, store into output array. */
  190.   wsptr = workspace;
  191.   for (ctr = 0; ctr < 4; ctr++) {
  192.     outptr = output_buf[ctr] + output_col;
  193.     /* It's not clear whether a zero row test is worthwhile here ... */
  194. #ifndef NO_ZERO_ROW_TEST
  195.     if ((wsptr[1] | wsptr[2] | wsptr[3] | wsptr[5] | wsptr[6] |
  196.  wsptr[7]) == 0) {
  197.       /* AC terms all zero */
  198.       JSAMPLE dcval = range_limit[(int) DESCALE((long) wsptr[0], PASS1_BITS+3)
  199.   & RANGE_MASK];
  200.       
  201.       outptr[0] = dcval;
  202.       outptr[1] = dcval;
  203.       outptr[2] = dcval;
  204.       outptr[3] = dcval;
  205.       
  206.       wsptr += DCTSIZE; /* advance pointer to next row */
  207.       continue;
  208.     }
  209. #endif
  210.     
  211.     /* Even part */
  212.     
  213.     tmp0 = ((long) wsptr[0]) << (CONST_BITS+1);
  214.     
  215.     tmp2 = MULTIPLY((long) wsptr[2], FIX_1_847759065)
  216.  + MULTIPLY((long) wsptr[6], - FIX_0_765366865);
  217.     
  218.     tmp10 = tmp0 + tmp2;
  219.     tmp12 = tmp0 - tmp2;
  220.     
  221.     /* Odd part */
  222.     
  223.     z1 = (long) wsptr[7];
  224.     z2 = (long) wsptr[5];
  225.     z3 = (long) wsptr[3];
  226.     z4 = (long) wsptr[1];
  227.     
  228.     tmp0 = MULTIPLY(z1, - FIX_0_211164243) /* sqrt(2) * (c3-c1) */
  229.  + MULTIPLY(z2, FIX_1_451774981) /* sqrt(2) * (c3+c7) */
  230.  + MULTIPLY(z3, - FIX_2_172734803) /* sqrt(2) * (-c1-c5) */
  231.  + MULTIPLY(z4, FIX_1_061594337); /* sqrt(2) * (c5+c7) */
  232.     
  233.     tmp2 = MULTIPLY(z1, - FIX_0_509795579) /* sqrt(2) * (c7-c5) */
  234.  + MULTIPLY(z2, - FIX_0_601344887) /* sqrt(2) * (c5-c1) */
  235.  + MULTIPLY(z3, FIX_0_899976223) /* sqrt(2) * (c3-c7) */
  236.  + MULTIPLY(z4, FIX_2_562915447); /* sqrt(2) * (c1+c3) */
  237.     /* Final output stage */
  238.     
  239.     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp2,
  240.   CONST_BITS+PASS1_BITS+3+1)
  241.     & RANGE_MASK];
  242.     outptr[3] = range_limit[(int) DESCALE(tmp10 - tmp2,
  243.   CONST_BITS+PASS1_BITS+3+1)
  244.     & RANGE_MASK];
  245.     outptr[1] = range_limit[(int) DESCALE(tmp12 + tmp0,
  246.   CONST_BITS+PASS1_BITS+3+1)
  247.     & RANGE_MASK];
  248.     outptr[2] = range_limit[(int) DESCALE(tmp12 - tmp0,
  249.   CONST_BITS+PASS1_BITS+3+1)
  250.     & RANGE_MASK];
  251.     
  252.     wsptr += DCTSIZE; /* advance pointer to next row */
  253.   }
  254. }
  255. /*
  256.  * Perform dequantization and inverse DCT on one block of coefficients,
  257.  * producing a reduced-size 2x2 output block.
  258.  */
  259. GLOBAL(void)
  260. jpeg_idct_2x2 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  261.        JCOEFPTR coef_block,
  262.        JSAMPARRAY output_buf, JDIMENSION output_col)
  263. {
  264.   long tmp0, tmp10, z1;
  265.   JCOEFPTR inptr;
  266.   ISLOW_MULT_TYPE * quantptr;
  267.   int * wsptr;
  268.   JSAMPROW outptr;
  269.   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  270.   int ctr;
  271.   int workspace[DCTSIZE*2]; /* buffers data between passes */
  272.   SHIFT_TEMPS
  273.   /* Pass 1: process columns from input, store into work array. */
  274.   inptr = coef_block;
  275.   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  276.   wsptr = workspace;
  277.   for (ctr = DCTSIZE; ctr > 0; inptr++, quantptr++, wsptr++, ctr--) {
  278.     /* Don't bother to process columns 2,4,6 */
  279.     if (ctr == DCTSIZE-2 || ctr == DCTSIZE-4 || ctr == DCTSIZE-6)
  280.       continue;
  281.     if ((inptr[DCTSIZE*1] | inptr[DCTSIZE*3] |
  282.  inptr[DCTSIZE*5] | inptr[DCTSIZE*7]) == 0) {
  283.       /* AC terms all zero; we need not examine terms 2,4,6 for 2x2 output */
  284.       int dcval = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]) << PASS1_BITS;
  285.       
  286.       wsptr[DCTSIZE*0] = dcval;
  287.       wsptr[DCTSIZE*1] = dcval;
  288.       
  289.       continue;
  290.     }
  291.     
  292.     /* Even part */
  293.     
  294.     z1 = DEQUANTIZE(inptr[DCTSIZE*0], quantptr[DCTSIZE*0]);
  295.     tmp10 = z1 << (CONST_BITS+2);
  296.     
  297.     /* Odd part */
  298.     z1 = DEQUANTIZE(inptr[DCTSIZE*7], quantptr[DCTSIZE*7]);
  299.     tmp0 = MULTIPLY(z1, - FIX_0_720959822); /* sqrt(2) * (c7-c5+c3-c1) */
  300.     z1 = DEQUANTIZE(inptr[DCTSIZE*5], quantptr[DCTSIZE*5]);
  301.     tmp0 += MULTIPLY(z1, FIX_0_850430095); /* sqrt(2) * (-c1+c3+c5+c7) */
  302.     z1 = DEQUANTIZE(inptr[DCTSIZE*3], quantptr[DCTSIZE*3]);
  303.     tmp0 += MULTIPLY(z1, - FIX_1_272758580); /* sqrt(2) * (-c1+c3-c5-c7) */
  304.     z1 = DEQUANTIZE(inptr[DCTSIZE*1], quantptr[DCTSIZE*1]);
  305.     tmp0 += MULTIPLY(z1, FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  306.     /* Final output stage */
  307.     
  308.     wsptr[DCTSIZE*0] = (int) DESCALE(tmp10 + tmp0, CONST_BITS-PASS1_BITS+2);
  309.     wsptr[DCTSIZE*1] = (int) DESCALE(tmp10 - tmp0, CONST_BITS-PASS1_BITS+2);
  310.   }
  311.   
  312.   /* Pass 2: process 2 rows from work array, store into output array. */
  313.   wsptr = workspace;
  314.   for (ctr = 0; ctr < 2; ctr++) {
  315.     outptr = output_buf[ctr] + output_col;
  316.     /* It's not clear whether a zero row test is worthwhile here ... */
  317. #ifndef NO_ZERO_ROW_TEST
  318.     if ((wsptr[1] | wsptr[3] | wsptr[5] | wsptr[7]) == 0) {
  319.       /* AC terms all zero */
  320.       JSAMPLE dcval = range_limit[(int) DESCALE((long) wsptr[0], PASS1_BITS+3)
  321.   & RANGE_MASK];
  322.       
  323.       outptr[0] = dcval;
  324.       outptr[1] = dcval;
  325.       
  326.       wsptr += DCTSIZE; /* advance pointer to next row */
  327.       continue;
  328.     }
  329. #endif
  330.     
  331.     /* Even part */
  332.     
  333.     tmp10 = ((long) wsptr[0]) << (CONST_BITS+2);
  334.     
  335.     /* Odd part */
  336.     tmp0 = MULTIPLY((long) wsptr[7], - FIX_0_720959822) /* sqrt(2) * (c7-c5+c3-c1) */
  337.  + MULTIPLY((long) wsptr[5], FIX_0_850430095) /* sqrt(2) * (-c1+c3+c5+c7) */
  338.  + MULTIPLY((long) wsptr[3], - FIX_1_272758580) /* sqrt(2) * (-c1+c3-c5-c7) */
  339.  + MULTIPLY((long) wsptr[1], FIX_3_624509785); /* sqrt(2) * (c1+c3+c5+c7) */
  340.     /* Final output stage */
  341.     
  342.     outptr[0] = range_limit[(int) DESCALE(tmp10 + tmp0,
  343.   CONST_BITS+PASS1_BITS+3+2)
  344.     & RANGE_MASK];
  345.     outptr[1] = range_limit[(int) DESCALE(tmp10 - tmp0,
  346.   CONST_BITS+PASS1_BITS+3+2)
  347.     & RANGE_MASK];
  348.     
  349.     wsptr += DCTSIZE; /* advance pointer to next row */
  350.   }
  351. }
  352. /*
  353.  * Perform dequantization and inverse DCT on one block of coefficients,
  354.  * producing a reduced-size 1x1 output block.
  355.  */
  356. GLOBAL(void)
  357. jpeg_idct_1x1 (j_decompress_ptr cinfo, jpeg_component_info * compptr,
  358.        JCOEFPTR coef_block,
  359.        JSAMPARRAY output_buf, JDIMENSION output_col)
  360. {
  361.   int dcval;
  362.   ISLOW_MULT_TYPE * quantptr;
  363.   JSAMPLE *range_limit = IDCT_range_limit(cinfo);
  364.   SHIFT_TEMPS
  365.   /* We hardly need an inverse DCT routine for this: just take the
  366.    * average pixel value, which is one-eighth of the DC coefficient.
  367.    */
  368.   quantptr = (ISLOW_MULT_TYPE *) compptr->dct_table;
  369.   dcval = DEQUANTIZE(coef_block[0], quantptr[0]);
  370.   dcval = (int) DESCALE((long) dcval, 3);
  371.   output_buf[0][output_col] = range_limit[dcval & RANGE_MASK];
  372. }
  373. #endif /* IDCT_SCALING_SUPPORTED */