Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Blur image
- void blur(int height, int width, RGBTRIPLE image[height][width])
- {
- int i, j;
- // create a copy of array to new image to blur
- RGBTRIPLE newImage[height][width];
- for (i = 0; i < height; i++) {
- for (j = 0; j < width; j++) {
- newImage[i][j] = image[i][j];
- }
- }
- // interate through rows
- for (i = 0; i < height; i++)
- // iterate through columns
- {
- for (j = 0; j < width; j++)
- {
- int sumRed = 0, sumGreen = 0, sumBlue = 0;
- int counter = 0; // count total pixels
- // start off with center pixel = 2
- // want to check if areas around exist and if they do calculate
- // 5 | 4 | 1
- // ---------
- // 7 | 2 | 6
- // ---------
- // 3 | 0 | 8
- // check if there is a row above it (4)
- if ((i - 1 > -1) && (0 => j <= height)){
- sumRed += newImage[i - 1][j].rgbtRed;
- sumGreen += newImage[i - 1][j].rgbtGreen;
- sumBlue += newImage[i - 1][j].rgbtBlue;
- counter ++;
- }
- // check if there columnn left hand side (7)
- if ((j - 1 > -1 ) && (0 => i <= height)){
- sumRed += newImage[i][j - 1].rgbtRed;
- sumGreen += newImage[i][j - 1].rgbtGreen;
- sumBlue += newImage[i][j - 1].rgbtBlue;
- counter ++;
- }
- // check if there is a column to the right (6)
- if ((j + 1 < width) && (newImage[i][j + 1])){
- sumRed += newImage[i][j + 1].rgbtRed;
- sumGreen += newImage[i][j + 1].rgbtGreen;
- sumBlue += newImage[i][j + 1].rgbtBlue;
- counter ++;
- }
- // check if there is a row below pixel (0)
- if ((i + 1 <= height) && (0 >= j <= width)){
- sumRed += newImage[i + 1][j].rgbtRed;
- sumGreen += newImage[i + 1][j].rgbtGreen;
- sumBlue += newImage[i + 1][j].rgbtBlue;
- counter ++;
- }
- // if there is a top left corner (5)
- if ((i - 1 >= 0) && (j - 1 >= 0)) {
- sumRed += newImage[i - 1 ][j - 1].rgbtRed;
- sumGreen += newImage[i - 1][j - 1].rgbtGreen;
- sumBlue += newImage[i - 1][j - 1].rgbtBlue;
- counter ++;
- }
- // if there is a right corner (1)
- if ((i + 1 <= 0) && (j + 1 <= width)) {
- sumRed += newImage[i + 1][j + 1].rgbtRed;
- sumGreen += newImage[i + 1][j + 1].rgbtGreen;
- sumBlue += newImage[i + 1][j + 1].rgbtBlue;
- counter ++;
- }
- // if there is a bottom left corner (3)
- if ((i + 1 <= height) && (j - 1 >= 0)) {
- sumRed += newImage[i + 1][j - 1].rgbtRed;
- sumGreen += newImage[i + 1][j - 1].rgbtGreen;
- sumBlue += newImage[i + 1][j - 1].rgbtBlue;
- counter ++;
- }
- // if there is a bottom right corner (8)
- if ((i + 1 <= height) && (j + 1 <= width)) {
- sumRed += newImage[i - 1][j - 1].rgbtRed;
- sumGreen += newImage[i - 1][j - 1].rgbtGreen;
- sumBlue += newImage[i - 1][j - 1].rgbtBlue;
- counter ++;
- }
- // add the middle pixel
- sumRed += newImage[i][j].rgbtRed;
- sumGreen += newImage[i][j].rgbtGreen;
- sumBlue += newImage[i][j].rgbtBlue;
- // find average of everything
- newImage[i][j].rgbtRed = round(sumRed / counter);
- newImage[i][j].rgbtBlue = round(sumBlue / counter);
- newImage[i][j].rgbtGreen = round(sumGreen / counter);
- // replace old image with new blurred pixels
- image[i][j].rgbtRed = newImage[i][j].rgbtRed;
- image[i][j].rgbtBlue = newImage[i][j].rgbtBlue;
- image[i][j].rgbtGreen = newImage[i][j].rgbtGreen;
- }
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement