Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "helpers.h"
- #include <math.h>
- void blur(int height, int width, RGBTRIPLE image[height][width])
- {
- // make a copy
- RGBTRIPLE copy[height][width];
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- copy[i][j].rgbtRed = image[i][j].rgbtRed;
- copy[i][j].rgbtGreen = image[i][j].rgbtGreen;
- copy[i][j].rgbtBlue = image[i][j].rgbtBlue;
- }
- }
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- int pixelCount = 1, sumRed = 0, sumGreen = 0, sumBlue = 0, avgRed, avgGreen, avgBlue;
- for (int a = i - 1; a < i + 2; a++)
- {
- for (int b = j - 1; b < j + 2; b++)
- {
- // check whether the pixel lies inside the image
- if (a < 0 || a > height - 1 || b < 0 || b > width - 1)
- {
- continue;
- }
- // add values of each channel
- sumRed += copy[a][b].rgbtRed;
- sumGreen += copy[a][b].rgbtGreen;
- sumBlue += copy[a][b].rgbtBlue;
- pixelCount++;
- }
- }
- // calculate the average
- avgRed = round(sumRed / (float) pixelCount);
- avgGreen = round(sumBlue / (float) pixelCount);
- avgBlue = round(sumGreen / (float) pixelCount);
- // change the values of each pixel
- image[i][j].rgbtRed = avgRed;
- image[i][j].rgbtGreen = avgGreen;
- image[i][j].rgbtBlue = avgBlue;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement