home *** CD-ROM | disk | FTP | other *** search
/ Phoenix Heaven Sunny 2 / APPARE2.BIN / oh_towns / tetujin / src.lzh / G_EFF / YMEDFLT.C < prev    next >
Text File  |  1994-12-26  |  5KB  |  274 lines

  1. /*
  2.         graphic effect lib.
  3.           Noise Filter
  4.  
  5.         h. Toda 1994 5 9
  6. */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <egb.h>
  11. #include "g_eff.h"
  12.  
  13. #define NOERR 0        /* no error */
  14.  
  15. static int mx ;
  16. static int aSen ;
  17. static int mSen ;
  18. static int cMax ;
  19. static int aMax ;
  20. static int x1 ;
  21. static int y1 ;
  22. static int x2 ;
  23. static int y2 ;
  24. static int (*read1)() ;
  25. static int (*write)() ;
  26. static int (*mask)() ;
  27.  
  28. g_yMedianFilter( BASICPARA *para, int mode )
  29. {
  30.     mx = para->mix ;
  31.     aSen = para->alphaSen ;
  32.     mSen = para->maskSen ;
  33.     cMax = para->colorMax ;
  34.     aMax = para->alphaMax ;
  35.     x1 = para->lupx ;
  36.     y1 = para->lupy ;
  37.     x2 = para->rdwx ;
  38.     y2 = para->rdwy ;
  39.     read1 = para->read1 ;
  40.     write = para->write ;
  41.     mask = para->mask ;
  42.  
  43.     if( mode == 0 )
  44.         mode0() ;
  45.     else
  46.         mode1() ;
  47.  
  48.     return NOERR ;
  49. }
  50.  
  51. static int mode0()
  52. {
  53.     unsigned char a[10][4] ;
  54.     int b[10][4] ;
  55.     int r0, g0, b0 ;
  56.     int i, x, y ;
  57.     int n ;
  58.  
  59.     for( y = y1 ; y <= y2 ; y++ )
  60.     {
  61.         for( x=x1 ; x <= x2 ; x++ )
  62.         {
  63.             read1( x-1, y-1, a[0] ) ;
  64.             read1( x,   y-1, a[1] ) ;
  65.             read1( x+1, y-1, a[2] ) ;
  66.             read1( x-1, y,   a[3] ) ;
  67.             read1( x,   y,   a[4] ) ;
  68.             read1( x+1, y,   a[5] ) ;
  69.             read1( x-1, y+1, a[6] ) ;
  70.             read1( x,   y+1, a[7] ) ;
  71.             read1( x+1, y+1, a[8] ) ;
  72.  
  73.             for( i=0 ; i<9 ; i++ )
  74.             {
  75.                 rgbToYuv( a[i][0], a[i][1], a[i][2],
  76.                         &(b[i][0]), &(b[i][1]), &(b[i][2]), cMax ) ;
  77.             }
  78.  
  79.             n = ck9(
  80.                      b[0][0], b[1][0], b[2][0],
  81.                      b[3][0], b[4][0], b[5][0],
  82.                      b[6][0], b[7][0], b[8][0]
  83.                    ) ;
  84.  
  85.             yuvToRgb( b[n][0], b[4][1], b[4][2], &r0, &g0, &b0, cMax ) ;
  86.             a[9][0] = r0 ;
  87.             a[9][1] = g0 ;
  88.             a[9][2] = b0 ;
  89.  
  90.             mixWrite( x, y, a[9], a[4] ) ;
  91.         }
  92.     }
  93.  
  94.     return NOERR ;
  95. }
  96.  
  97. static int mode1()
  98. {
  99.     unsigned char a[6][4] ;
  100.     int b[6][4] ;
  101.     int r0, g0, b0 ;
  102.     int i, x, y ;
  103.     int n ;
  104.  
  105.     for( y = y1 ; y <= y2 ; y++ )
  106.     {
  107.         for( x=x1 ; x <= x2 ; x++ )
  108.         {
  109.             read1( x,   y-1, a[0] ) ;
  110.             read1( x-1, y,   a[1] ) ;
  111.             read1( x,   y,   a[2] ) ;
  112.             read1( x+1, y,   a[3] ) ;
  113.             read1( x,   y+1, a[4] ) ;
  114.  
  115.             for( i=0 ; i<5 ; i++ )
  116.             {
  117.                 rgbToYuv( a[i][0], a[i][1], a[i][2],
  118.                         &(b[i][0]), &(b[i][1]), &(b[i][2]), cMax ) ;
  119.             }
  120.  
  121.             n = ck5(
  122.                      b[0][0], b[1][0], b[2][0],
  123.                      b[3][0], b[4][0]
  124.                    ) ;
  125.  
  126.             yuvToRgb( b[n][0], b[2][1], b[2][2], &r0, &g0, &b0, cMax ) ;
  127.             a[5][0] = r0 ;
  128.             a[5][1] = g0 ;
  129.             a[5][2] = b0 ;
  130.  
  131.             mixWrite( x, y, a[5], a[2] ) ;
  132.         }
  133.     }
  134.  
  135.     return NOERR ;
  136. }
  137.  
  138. static mixWrite( int x, int y, unsigned char *a, unsigned char *b )
  139. {
  140.     unsigned char c[4] ;
  141.     int mix ;
  142.  
  143.     if( mSen )
  144.     {
  145.         if( mask( x, y ) >= mSen )
  146.             return NOERR ;
  147.     }
  148.  
  149.     mix = mx ;
  150.     if( aSen )
  151.     {
  152.         mix = mix * b[3] / aMax ;
  153.     }
  154.  
  155.     c[0] = ( a[0] * mix + b[0] * ( 256 - mix ) + 0x80 ) >> 8 ;
  156.     c[1] = ( a[1] * mix + b[1] * ( 256 - mix ) + 0x80 ) >> 8 ;
  157.     c[2] = ( a[2] * mix + b[2] * ( 256 - mix ) + 0x80 ) >> 8 ;
  158.     c[3] = b[3] ;
  159.  
  160.     if( mix )
  161.     {
  162.         write( x, y, c ) ;
  163.     }
  164.  
  165.     return NOERR ;
  166. }
  167.  
  168. static ck9( int a0, int a1, int a2, int a3,
  169.             int a4, int a5, int a6, int a7, int a8 )
  170. {
  171.     int temp ;
  172.     int m[9] ;
  173.     int a[9] ;
  174.     int i, j ;
  175.  
  176.     for( i=0 ; i<9 ; i++ )m[i] = i ;
  177.  
  178.     a[0] = a0 ; a[1] = a1 ; a[2] = a2 ; a[3] = a3 ; a[4] = a4 ;
  179.     a[5] = a5 ; a[6] = a6 ; a[7] = a7 ; a[8] = a8 ;
  180.  
  181.     for( j=1 ; j<6 ; j++ )
  182.     {
  183.         for( i=j ; i<9 ; i++ )
  184.         {
  185.             if( a[j-1] > a[i] )
  186.             {
  187.                 temp = a[i] ;
  188.                 a[i] = a[j-1] ;
  189.                 a[j-1] =temp ;
  190.  
  191.                 temp = m[i] ;
  192.                 m[i] = m[j-1] ;
  193.                 m[j-1] =temp ;
  194.             }
  195.         }
  196.     }
  197.  
  198.     return m[4] ;
  199. }
  200.  
  201. static ck5( int a0, int a1, int a2, int a3, int a4 )
  202. {
  203.     int temp ;
  204.     int m[5] ;
  205.     int a[5] ;
  206.     int i, j ;
  207.  
  208.     for( i=0 ; i<5 ; i++ )m[i] = i ;
  209.  
  210.     a[0] = a0 ; a[1] = a1 ; a[2] = a2 ; a[3] = a3 ; a[4] = a4 ;
  211.  
  212.     for( j=1 ; j<4 ; j++ )
  213.     {
  214.         for( i=j ; i<5 ; i++ )
  215.         {
  216.             if( a[j-1] > a[i] )
  217.             {
  218.                 temp = a[i] ;
  219.                 a[i] = a[j-1] ;
  220.                 a[j-1] =temp ;
  221.  
  222.                 temp = m[i] ;
  223.                 m[i] = m[j-1] ;
  224.                 m[j-1] =temp ;
  225.             }
  226.         }
  227.     }
  228.  
  229.     return m[2] ;
  230. }
  231.  
  232. static rgbToYuv( int r0, int g0, int b0, int *y0, int *u0, int *v0, int max )
  233. {
  234.     int y, cb, cr ;
  235.  
  236.     y  =  19595*r0 + 38470*g0 + 7471*b0 ;
  237.     cb = -11049*r0 - 21699*g0 + 32748*b0 ;
  238.     cr =  32755*r0 -27427*g0 -5328*b0 ;
  239.  
  240.     *y0 = ( ((y/max)*219) >> 16 ) + 16 ;
  241.     *u0 = ( ((cb/max)*224) >> 16 ) + 128 ;
  242.     *v0 = ( ((cr/max)*224) >> 16 ) + 128 ;
  243.  
  244.     if( *y0 < 0 )*y0 = 0 ;
  245.     if( *y0 > 255 )*y0 = 255 ;
  246.     if( *u0 < 0 )*u0 = 0 ;
  247.     if( *u0 > 255 )*u0 = 255 ;
  248.     if( *v0 < 0 )*v0 = 0 ;
  249.     if( *v0 > 255 )*v0 = 255 ;
  250.  
  251.     return NOERR ;
  252. }
  253.  
  254. static yuvToRgb( int y0, int u0, int v0, int *r0, int *g0, int *b0, int max )
  255. {
  256.     y0 = (( y0 - 16 ) * max )/219 ;
  257.     u0 = (( u0 -128 ) * max )/224 ;
  258.     v0 = (( v0 -128 ) * max )/224 ;
  259.  
  260.     *r0 = ( 65536*y0 + 4*u0 + 91920*v0 ) >> 16 ;
  261.     *g0 = ( 65536*y0 - 22569*u0 - 46819*v0 ) >> 16 ;
  262.     *b0 = ( 65536*y0 + 116198*u0 - 9*v0 ) >> 16 ;
  263.  
  264.     if( *r0 < 0 )*r0 = 0 ;
  265.     if( *r0 > max )*r0 = max ;
  266.     if( *g0 < 0 )*g0 = 0 ;
  267.     if( *g0 > max )*g0 = max ;
  268.     if( *b0 < 0 )*b0 = 0 ;
  269.     if( *b0 > max )*b0 = max ;
  270.  
  271.     return NOERR ;
  272. }
  273.  
  274.