Skip to content

Instantly share code, notes, and snippets.

@ysc3839
Last active July 21, 2016 04:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ysc3839/2d86e61a88589625912ca99d07ac22d7 to your computer and use it in GitHub Desktop.
Save ysc3839/2d86e61a88589625912ca99d07ac22d7 to your computer and use it in GitHub Desktop.
Image Stroke Algorithm
#include <stdint.h>
void stroke(void *srcBmp, void *dstBmp, size_t width, size_t height)
{
for (size_t x = 0; x < width; x++)
{
for (size_t y = 0; y < height; y++)
{
uint32_t *src = (uint32_t *)srcBmp + y * width;
uint32_t currPixel = src[x];
float a, b;
if (x < 0 || x >= width || y < 0 || y >= height)
a = 1.0f;
else
{
uint8_t alpha = ((uint8_t *)&currPixel)[3];
a = (255.0f - (float)alpha) / 255.0f;
}
float c = 1.0f - a;
for (size_t x1 = x - 1; x1 < x + 2; x1++)
{
for (size_t y1 = y - 1; y1 < y + 2; y1++)
{
if (x1 != x || y1 != y)
{
if (x1 < 0 || x1 >= width || y1 < 0 || y1 >= height)
b = 1.0f;
else
{
src = (uint32_t *)srcBmp + y1 * width;
currPixel = src[x1];
uint8_t alpha = ((uint8_t *)&currPixel)[3];
b = (255.0f - (float)alpha) / 255.0f;
}
a *= b;
}
}
}
if (a != 1.0f)
c = c / (1.0f - a);
uint8_t gray = (uint8_t)floorf((c * 255.0f) + 0.5f);
uint8_t alpha = (uint8_t)floorf(((1.0f - a) * 255.0f) + 0.5f);
uint8_t color[4];
color[0] = color[1] = color[2] = gray;
color[3] = alpha;
uint32_t *dst = (uint32_t *)dstBmp + y * width;
dst[x] = *(uint32_t *)color;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment