Advertisement
Guest User

Untitled

a guest
Jul 18th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.16 KB | None | 0 0
  1.     static class LinearBlur
  2.     {
  3.         private static float _rSum = 0;
  4.         private static float _gSum = 0;
  5.         private static float _bSum = 0;
  6.  
  7.         private static Texture2D _sourceImage;
  8.         private static int _sourceWidth;
  9.         private static int _sourceHeight;
  10.         private static int _windowSize;
  11.  
  12.         private static Texture2D blurred;
  13.  
  14.         public static Texture2D Blur(Texture2D image, int radius, int iterations)
  15.         {
  16.             _windowSize = radius * 2 + 1;
  17.             _sourceWidth = image.width;
  18.             _sourceHeight = image.height;
  19.             blurred = new Texture2D(image.width, image.height, image.format, false);
  20.  
  21.             var tex = image;
  22.  
  23.             for (var i = 0; i < iterations; i++)
  24.             {
  25.                 tex = OneDimensialBlur(tex, radius, true);
  26.                 tex = OneDimensialBlur(tex, radius, false);
  27.             }
  28.  
  29.             return tex;
  30.         }
  31.  
  32.         private static Texture2D OneDimensialBlur(Texture2D image, int radius, bool horizontal)
  33.         {
  34.             _sourceImage = image;
  35.            
  36.             if (horizontal)
  37.             {
  38.                 for (int imgY = 0; imgY < _sourceHeight; ++imgY)
  39.                 {
  40.                     ResetSum();
  41.  
  42.                     for (int imgX = 0; imgX < _sourceWidth; imgX++)
  43.                     {
  44.                         if (imgX == 0)
  45.                             for (int x = radius * -1; x <= radius; ++x)
  46.                                 AddPixel(GetPixelWithXCheck(x, imgY));
  47.                         else
  48.                         {
  49.                             var toExclude = GetPixelWithXCheck(imgX - radius - 1, imgY);
  50.                             var toInclude = GetPixelWithXCheck(imgX + radius, imgY);
  51.  
  52.                             SubstPixel(toExclude);
  53.                             AddPixel(toInclude);
  54.                         }
  55.  
  56.                         blurred.SetPixel(imgX, imgY, CalcPixelFromSum());
  57.                     }
  58.                 }
  59.             } else
  60.             {
  61.                 for (int imgX = 0; imgX < _sourceWidth; imgX++)
  62.                 {
  63.                     ResetSum();
  64.  
  65.                     for (int imgY = 0; imgY < _sourceHeight; ++imgY)
  66.                     {
  67.                         if (imgY == 0)
  68.                             for (int y = radius * -1; y <= radius; ++y)
  69.                                 AddPixel(GetPixelWithYCheck(imgX, y));
  70.                         else
  71.                         {
  72.                             var toExclude = GetPixelWithYCheck(imgX, imgY - radius - 1);
  73.                             var toInclude = GetPixelWithYCheck(imgX, imgY + radius);
  74.  
  75.                             SubstPixel(toExclude);
  76.                             AddPixel(toInclude);
  77.                         }
  78.  
  79.                         blurred.SetPixel(imgX, imgY, CalcPixelFromSum());
  80.                     }
  81.                 }
  82.             }
  83.  
  84.             blurred.Apply();
  85.             return blurred;
  86.         }
  87.  
  88.         private static Color GetPixelWithXCheck(int x, int y)
  89.         {
  90.             if (x <= 0) return _sourceImage.GetPixel(0, y);
  91.             if (x >= _sourceWidth) return _sourceImage.GetPixel(_sourceWidth - 1, y);
  92.             return _sourceImage.GetPixel(x, y);
  93.         }
  94.  
  95.         private static Color GetPixelWithYCheck(int x, int y)
  96.         {
  97.             if (y <= 0) return _sourceImage.GetPixel(x, 0);
  98.             if (y >= _sourceHeight) return _sourceImage.GetPixel(x, _sourceHeight - 1);
  99.             return _sourceImage.GetPixel(x, y);
  100.         }
  101.  
  102.         private static void AddPixel(Color pixel)
  103.         {
  104.             _rSum += pixel.r;
  105.             _gSum += pixel.g;
  106.             _bSum += pixel.b;
  107.         }
  108.  
  109.         private static void SubstPixel(Color pixel)
  110.         {
  111.             _rSum -= pixel.r;
  112.             _gSum -= pixel.g;
  113.             _bSum -= pixel.b;
  114.         }
  115.  
  116.         private static void ResetSum()
  117.         {
  118.             _rSum = 0.0f;
  119.             _gSum = 0.0f;
  120.             _bSum = 0.0f;
  121.         }
  122.  
  123.         static Color CalcPixelFromSum()
  124.         {
  125.             return new Color(_rSum / _windowSize, _gSum / _windowSize, _bSum / _windowSize);
  126.         }
  127.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement