Advertisement
Guest User

Untitled

a guest
Nov 28th, 2017
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.30 KB | None | 0 0
  1. void alpha_bleeding(ubyte *image, int width, int height)
  2. {
  3.     const uint N = width * height;
  4.  
  5.     auto opaque = new byte[N];
  6.     auto loose = new bool[N];
  7.     uint[] pending;
  8.     uint[] pendingNext;
  9.  
  10.  
  11.     int[2][8] offsets = [
  12.         [-1, -1],
  13.         [ 0, -1],
  14.         [ 1, -1],
  15.         [-1,  0],
  16.         [ 1,  0],
  17.         [-1,  1],
  18.         [ 0,  1],
  19.         [ 1,  1]
  20.     ];
  21.  
  22.     for (uint i = 0, j = 3; i < N; i++, j += 4)
  23.     {
  24.         if (image[j] == 0)
  25.         {
  26.             bool isLoose = true;
  27.  
  28.             int x = i % width;
  29.             int y = i / width;
  30.  
  31.             for (int k = 0; k < 8; k++)
  32.             {
  33.                 int s = offsets[k][0];
  34.                 int t = offsets[k][1];
  35.  
  36.                 if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
  37.                 {
  38.                     uint index = j + 4 * (s + t * width);
  39.  
  40.                     if (image[index + 3] != 0)
  41.                     {
  42.                         isLoose = false;
  43.                         break;
  44.                     }
  45.                 }
  46.             }
  47.  
  48.             if (!isLoose)
  49.                 pending ~= i;
  50.             else
  51.                 loose[i] = true;
  52.         }
  53.         else
  54.         {
  55.             opaque[i] = -1;
  56.         }
  57.     }
  58.  
  59.     while (pending.length > 0)
  60.     {
  61.         pendingNext = null;
  62.  
  63.         for (uint p = 0; p < pending.length; p++)
  64.         {
  65.             uint i = pending[p] * 4;
  66.             uint j = pending[p];
  67.  
  68.             int x = j % width;
  69.             int y = j / width;
  70.  
  71.             int r = 0;
  72.             int g = 0;
  73.             int b = 0;
  74.  
  75.             int count = 0;
  76.  
  77.             for (uint k = 0; k < 8; k++)
  78.             {
  79.                 int s = offsets[k][0];
  80.                 int t = offsets[k][1];
  81.  
  82.                 if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
  83.                 {
  84.                     t *= width;
  85.  
  86.                     if (opaque[j + s + t] & 1)
  87.                     {
  88.                         uint index = i + 4 * (s + t);
  89.  
  90.                         r += image[index + 0];
  91.                         g += image[index + 1];
  92.                         b += image[index + 2];
  93.  
  94.                         count++;
  95.                     }
  96.                 }
  97.             }
  98.  
  99.             if (count > 0)
  100.             {
  101.                 image[i + 0] = cast(ubyte)(r / count);
  102.                 image[i + 1] = cast(ubyte)(g / count);
  103.                 image[i + 2] = cast(ubyte)(b / count);
  104.  
  105.                 opaque[j] = cast(byte)0xFE;
  106.  
  107.                 for (uint k = 0; k < 8; k++)
  108.                 {
  109.                     int s = offsets[k][0];
  110.                     int t = offsets[k][1];
  111.  
  112.                     if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
  113.                     {
  114.                         uint index = j + s + t * width;
  115.  
  116.                         if (loose[index])
  117.                         {
  118.                             pendingNext ~= index;
  119.                             loose[index] = false;
  120.                         }
  121.                     }
  122.                 }
  123.             }
  124.             else
  125.             {
  126.                 pendingNext ~= j;
  127.             }
  128.         }
  129.  
  130.         if (pendingNext.length > 0)
  131.         {
  132.             for (uint p = 0; p < pending.length; p++)
  133.                 opaque[pending[p]] >>= 1;
  134.         }
  135.  
  136.         pending.swap(pendingNext);
  137.     }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement