Advertisement
Guest User

Untitled

a guest
Apr 21st, 2020
884
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.08 KB | None | 0 0
  1.  
  2. // Blur image
  3. void blur(int height, int width, RGBTRIPLE image[height][width])
  4. {
  5.  
  6.  
  7. int i, j;
  8.     // create a copy of array to new image to blur
  9.     RGBTRIPLE newImage[height][width];
  10.  
  11.     for (i = 0; i < height; i++) {
  12.         for (j = 0; j < width; j++) {
  13.             newImage[i][j] = image[i][j];
  14.         }
  15.     }
  16.  
  17.  
  18.  
  19.     // interate through rows
  20.     for (i = 0; i < height; i++)
  21.     // iterate through columns
  22.     {
  23.  
  24.         for (j = 0; j < width; j++)
  25.         {
  26.                 int sumRed = 0, sumGreen = 0, sumBlue = 0;
  27.                 int counter = 0; // count total pixels
  28.  
  29.             // start off with center pixel = 2
  30.             // want to check if areas around exist and if they do calculate
  31. // 5 | 4 | 1
  32. // ---------
  33. // 7 | 2 | 6
  34. // ---------
  35. // 3 | 0 | 8
  36.  
  37.             // check if there is a row above it (4)
  38.             if ((i - 1 > -1) && (0 => j <= height)){
  39.                 sumRed += newImage[i - 1][j].rgbtRed;
  40.                 sumGreen += newImage[i - 1][j].rgbtGreen;
  41.                 sumBlue += newImage[i - 1][j].rgbtBlue;
  42.                 counter ++;
  43.             }
  44.  
  45.             // check if there columnn left hand side (7)
  46.             if ((j - 1 > -1 ) && (0 => i <= height)){
  47.                 sumRed += newImage[i][j - 1].rgbtRed;
  48.                 sumGreen += newImage[i][j - 1].rgbtGreen;
  49.                 sumBlue += newImage[i][j - 1].rgbtBlue;
  50.                 counter ++;
  51.             }
  52.  
  53.  
  54.             // check if there is a column to the right (6)
  55.             if ((j + 1 < width) && (newImage[i][j + 1])){
  56.                 sumRed += newImage[i][j + 1].rgbtRed;
  57.                 sumGreen += newImage[i][j + 1].rgbtGreen;
  58.                 sumBlue += newImage[i][j + 1].rgbtBlue;
  59.                 counter ++;
  60.             }
  61.  
  62.             // check if there is a row below pixel (0)
  63.             if ((i + 1 <= height) && (0 >= j <= width)){
  64.                 sumRed += newImage[i + 1][j].rgbtRed;
  65.                 sumGreen += newImage[i + 1][j].rgbtGreen;
  66.                 sumBlue += newImage[i + 1][j].rgbtBlue;
  67.                 counter ++;
  68.             }
  69.  
  70.             // if there is a top left corner (5)
  71.             if ((i - 1 >= 0) && (j - 1 >= 0)) {
  72.                 sumRed += newImage[i - 1 ][j - 1].rgbtRed;
  73.                 sumGreen += newImage[i - 1][j - 1].rgbtGreen;
  74.                 sumBlue += newImage[i - 1][j - 1].rgbtBlue;
  75.                 counter ++;
  76.             }
  77.  
  78.             // if there is a right corner (1)
  79.             if ((i + 1 <= 0) && (j + 1 <= width)) {
  80.                 sumRed += newImage[i + 1][j + 1].rgbtRed;
  81.                 sumGreen += newImage[i + 1][j + 1].rgbtGreen;
  82.                 sumBlue += newImage[i + 1][j + 1].rgbtBlue;
  83.                 counter ++;
  84.  
  85.             }
  86.  
  87.             // if there is a bottom left corner (3)
  88.             if ((i + 1 <= height) && (j - 1 >= 0)) {
  89.                 sumRed += newImage[i + 1][j - 1].rgbtRed;
  90.                 sumGreen += newImage[i + 1][j - 1].rgbtGreen;
  91.                 sumBlue += newImage[i + 1][j - 1].rgbtBlue;
  92.                 counter ++;
  93.  
  94.             }
  95.  
  96.             // if there is a bottom right corner (8)
  97.             if ((i + 1 <= height) && (j + 1 <= width)) {
  98.                 sumRed += newImage[i - 1][j - 1].rgbtRed;
  99.                 sumGreen += newImage[i - 1][j - 1].rgbtGreen;
  100.                 sumBlue += newImage[i - 1][j - 1].rgbtBlue;
  101.                 counter ++;
  102.  
  103.             }
  104.  
  105.             // add the middle pixel
  106.  
  107.             sumRed += newImage[i][j].rgbtRed;
  108.             sumGreen += newImage[i][j].rgbtGreen;
  109.             sumBlue += newImage[i][j].rgbtBlue;
  110.  
  111.             // find average of everything
  112.  
  113.             newImage[i][j].rgbtRed = round(sumRed / counter);
  114.             newImage[i][j].rgbtBlue = round(sumBlue / counter);
  115.             newImage[i][j].rgbtGreen = round(sumGreen / counter);
  116.  
  117.             // replace old image with new blurred pixels
  118.             image[i][j].rgbtRed = newImage[i][j].rgbtRed;
  119.             image[i][j].rgbtBlue = newImage[i][j].rgbtBlue;
  120.             image[i][j].rgbtGreen = newImage[i][j].rgbtGreen;
  121.  
  122.  
  123.         }
  124.     }
  125.  
  126.  
  127.  
  128.    return;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement