home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / software / unix / jpeg / jpegv4a.tar / jbsmooth.c < prev    next >
C/C++ Source or Header  |  1992-11-01  |  3KB  |  119 lines

  1. /*
  2.  * jbsmooth.c
  3.  *
  4.  * Copyright (C) 1991, 1992, 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 cross-block smoothing routines.
  9.  * These routines are invoked via the smooth_coefficients method.
  10.  */
  11.  
  12. #include "jinclude.h"
  13.  
  14. #ifdef BLOCK_SMOOTHING_SUPPORTED
  15.  
  16.  
  17. /*
  18.  * Cross-block coefficient smoothing.
  19.  */
  20.  
  21. METHODDEF void
  22. smooth_coefficients (decompress_info_ptr cinfo,
  23.              jpeg_component_info *compptr,
  24.              JBLOCKROW above,
  25.              JBLOCKROW currow,
  26.              JBLOCKROW below,
  27.              JBLOCKROW output)
  28. {
  29.   QUANT_TBL_PTR Qptr = cinfo->quant_tbl_ptrs[compptr->quant_tbl_no];
  30.   long blocks_in_row = compptr->downsampled_width / DCTSIZE;
  31.   long col;
  32.  
  33.   /* First, copy the block row as-is.
  34.    * This takes care of the first & last blocks in the row, the top/bottom
  35.    * special cases, and the higher-order coefficients in each block.
  36.    */
  37.   jcopy_block_row(currow, output, blocks_in_row);
  38.  
  39.   /* Now apply the smoothing calculation, but not to any blocks on the
  40.    * edges of the image.
  41.    */
  42.  
  43.   if (above != NULL && below != NULL) {
  44.     for (col = 1; col < blocks_in_row-1; col++) {
  45.  
  46.       /* See section K.8 of the JPEG standard.
  47.        *
  48.        * As I understand it, this produces approximations
  49.        * for the low frequency AC components, based on the
  50.        * DC values of the block and its eight neighboring blocks.
  51.        * (Thus it can't be used for blocks on the image edges.)
  52.        */
  53.  
  54.       /* The layout of these variables corresponds to text and figure in K.8 */
  55.       
  56.       JCOEF DC1, DC2, DC3;
  57.       JCOEF DC4, DC5, DC6;
  58.       JCOEF DC7, DC8, DC9;
  59.       
  60.       long       AC01, AC02;
  61.       long AC10, AC11;
  62.       long AC20;
  63.       
  64.       DC1 = above [col-1][0];
  65.       DC2 = above [col  ][0];
  66.       DC3 = above [col+1][0];
  67.       DC4 = currow[col-1][0];
  68.       DC5 = currow[col  ][0];
  69.       DC6 = currow[col+1][0];
  70.       DC7 = below [col-1][0];
  71.       DC8 = below [col  ][0];
  72.       DC9 = below [col+1][0];
  73.       
  74. #define DIVIDE_256(x)    x = ( (x) < 0 ? -((128-(x))/256) : ((x)+128)/256 )
  75.       
  76.       AC01 = (36 * (DC4 - DC6));
  77.       DIVIDE_256(AC01);
  78.       AC10 = (36 * (DC2 - DC8));
  79.       DIVIDE_256(AC10);
  80.       AC20 = (9 * (DC2 + DC8 - 2*DC5));
  81.       DIVIDE_256(AC20);
  82.       AC11 = (5 * ((DC1 - DC3) - (DC7 - DC9)));
  83.       DIVIDE_256(AC11);
  84.       AC02 = (9 * (DC4 + DC6 - 2*DC5));
  85.       DIVIDE_256(AC02);
  86.       
  87.       /* I think that this checks to see if the quantisation
  88.        * on the transmitting side would have produced this
  89.        * answer. If so, then we use our (hopefully better)
  90.        * estimate.
  91.        */
  92.  
  93. #define ABS(x)    ((x) < 0 ? -(x) : (x))
  94.  
  95. #define COND_ASSIGN(_ac,_n,_z)   if ((ABS(output[col][_n] - (_ac))<<1) <= Qptr[_z]) output[col][_n] = (JCOEF) (_ac)
  96.  
  97.       COND_ASSIGN(AC01,  1, 1);
  98.       COND_ASSIGN(AC02,  2, 5);
  99.       COND_ASSIGN(AC10,  8, 2);
  100.       COND_ASSIGN(AC11,  9, 4);
  101.       COND_ASSIGN(AC20, 16, 3);
  102.     }
  103.   }
  104. }
  105.  
  106.  
  107. /*
  108.  * The method selection routine for cross-block smoothing.
  109.  */
  110.  
  111. GLOBAL void
  112. jselbsmooth (decompress_info_ptr cinfo)
  113. {
  114.   /* just one implementation for now */
  115.   cinfo->methods->smooth_coefficients = smooth_coefficients;
  116. }
  117.  
  118. #endif /* BLOCK_SMOOTHING_SUPPORTED */
  119.