Skip to content

Instantly share code, notes, and snippets.

@Darker
Created April 18, 2017 21:17
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 Darker/d971d184970f041d53bb131343de1fc0 to your computer and use it in GitHub Desktop.
Save Darker/d971d184970f041d53bb131343de1fc0 to your computer and use it in GitHub Desktop.
void Image::convolute(const int matrix[3][3], Image& target)
{
target.setSize(getWidth(), getHeight());
for(size_t y=0, yl=getHeight(); y<yl; ++y) {
for(size_t x=0, xl=getWidth(); x<xl; ++x) {
if(x==0 || y==0 || x+1==xl || y+1==yl) {
target.pixels[y][x] = pixels[y][x];
}
else {
target.pixels[y][x] = ((pixels[y-1][x-1]*matrix[0][0])
+ (pixels[y-1][x ]*matrix[0][1])
+ (pixels[y-1][x+1]*matrix[0][2])
+ (pixels[y ][x-1]*matrix[1][0])
+ (pixels[y ][x ]*matrix[1][1])
+ (pixels[y ][x+1]*matrix[1][2])
+ (pixels[y+1][x-1]*matrix[2][0])
+ (pixels[y+1][x ]*matrix[2][1])
+ (pixels[y+1][x+1]*matrix[2][2]));
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment