Skip to content

Instantly share code, notes, and snippets.

@m0r13
Created December 29, 2014 19:06
Show Gist options
  • Select an option

  • Save m0r13/4b75b0c52e03b9949cf7 to your computer and use it in GitHub Desktop.

Select an option

Save m0r13/4b75b0c52e03b9949cf7 to your computer and use it in GitHub Desktop.
RGBAPixel gaussBlur(const RGBAImage& image, int x, int y, int radius) {
int count = 0;
int r = 0, g = 0, b = 0, a = 0;
for (int dx = -radius; dx < radius; dx++)
for (int dy = -radius; dy < radius; dy++) {
int x2 = x + dx;
int y2 = y + dy;
if (x2 < 0 || y2 < 0 || x2 >= image.getWidth() || y2 >= image.getHeight())
continue;
count++;
r += rgba_red(image.getPixel(x2, y2));
g += rgba_green(image.getPixel(x2, y2));
b += rgba_blue(image.getPixel(x2, y2));
a += rgba_alpha(image.getPixel(x2, y2));
}
return rgba(r / count, g / count, b / count, a / count);
}
void RGBAImage::blur(RGBAImage& dest, int radius) const {
dest.setSize(width, height);
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
dest.pixel(x, y) = gaussBlur(*this, x, y, radius);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment