Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void alpha_bleeding(ubyte *image, int width, int height)
- {
- const uint N = width * height;
- auto opaque = new byte[N];
- auto loose = new bool[N];
- uint[] pending;
- uint[] pendingNext;
- int[2][8] offsets = [
- [-1, -1],
- [ 0, -1],
- [ 1, -1],
- [-1, 0],
- [ 1, 0],
- [-1, 1],
- [ 0, 1],
- [ 1, 1]
- ];
- for (uint i = 0, j = 3; i < N; i++, j += 4)
- {
- if (image[j] == 0)
- {
- bool isLoose = true;
- int x = i % width;
- int y = i / width;
- for (int k = 0; k < 8; k++)
- {
- int s = offsets[k][0];
- int t = offsets[k][1];
- if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
- {
- uint index = j + 4 * (s + t * width);
- if (image[index + 3] != 0)
- {
- isLoose = false;
- break;
- }
- }
- }
- if (!isLoose)
- pending ~= i;
- else
- loose[i] = true;
- }
- else
- {
- opaque[i] = -1;
- }
- }
- while (pending.length > 0)
- {
- pendingNext = null;
- for (uint p = 0; p < pending.length; p++)
- {
- uint i = pending[p] * 4;
- uint j = pending[p];
- int x = j % width;
- int y = j / width;
- int r = 0;
- int g = 0;
- int b = 0;
- int count = 0;
- for (uint k = 0; k < 8; k++)
- {
- int s = offsets[k][0];
- int t = offsets[k][1];
- if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
- {
- t *= width;
- if (opaque[j + s + t] & 1)
- {
- uint index = i + 4 * (s + t);
- r += image[index + 0];
- g += image[index + 1];
- b += image[index + 2];
- count++;
- }
- }
- }
- if (count > 0)
- {
- image[i + 0] = cast(ubyte)(r / count);
- image[i + 1] = cast(ubyte)(g / count);
- image[i + 2] = cast(ubyte)(b / count);
- opaque[j] = cast(byte)0xFE;
- for (uint k = 0; k < 8; k++)
- {
- int s = offsets[k][0];
- int t = offsets[k][1];
- if (x + s >= 0 && x + s < width && y + t >= 0 && y + t < height)
- {
- uint index = j + s + t * width;
- if (loose[index])
- {
- pendingNext ~= index;
- loose[index] = false;
- }
- }
- }
- }
- else
- {
- pendingNext ~= j;
- }
- }
- if (pendingNext.length > 0)
- {
- for (uint p = 0; p < pending.length; p++)
- opaque[pending[p]] >>= 1;
- }
- pending.swap(pendingNext);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement