Advertisement
shamzed

Untitled

Mar 18th, 2022
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. #include "helpers.h"
  2. #include <math.h>
  3.  
  4. void blur(int height, int width, RGBTRIPLE image[height][width])
  5. {
  6.     // make a copy
  7.     RGBTRIPLE copy[height][width];
  8.     for (int i = 0; i < height; i++)
  9.     {
  10.         for (int j = 0; j < width; j++)
  11.         {
  12.             copy[i][j].rgbtRed = image[i][j].rgbtRed;
  13.             copy[i][j].rgbtGreen = image[i][j].rgbtGreen;
  14.             copy[i][j].rgbtBlue = image[i][j].rgbtBlue;
  15.         }
  16.     }
  17.  
  18.     for (int i = 0; i < height; i++)
  19.     {
  20.         for (int j = 0; j < width; j++)
  21.         {
  22.             int pixelCount = 1, sumRed = 0, sumGreen = 0, sumBlue = 0, avgRed, avgGreen, avgBlue;
  23.             for (int a = i - 1; a < i + 2; a++)
  24.             {
  25.                 for (int b = j - 1; b < j + 2; b++)
  26.                 {
  27.                     // check whether the pixel lies inside the image
  28.                     if (a < 0 || a > height - 1 || b < 0 || b > width - 1)
  29.                     {
  30.                         continue;
  31.                     }
  32.                     // add values of each channel
  33.                     sumRed += copy[a][b].rgbtRed;
  34.                     sumGreen += copy[a][b].rgbtGreen;
  35.                     sumBlue += copy[a][b].rgbtBlue;
  36.                     pixelCount++;
  37.                 }
  38.             }
  39.             // calculate the average
  40.             avgRed = round(sumRed / (float) pixelCount);
  41.             avgGreen = round(sumBlue / (float) pixelCount);
  42.             avgBlue = round(sumGreen / (float) pixelCount);
  43.  
  44.             // change the values of each pixel
  45.             image[i][j].rgbtRed = avgRed;
  46.             image[i][j].rgbtGreen = avgGreen;
  47.             image[i][j].rgbtBlue = avgBlue;
  48.         }
  49.     }
  50.     return;
  51. }
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement