cs50gespinoza

Untitled

Aug 11th, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.40 KB | None | 0 0
  1. // Blur image
  2. void blur(int height, int width, RGBTRIPLE image[height][width])
  3. {
  4.     // Creates the temporary variable to store the data in before we change it; as well as introduces variables RGB sum & incrementing variable 'elements' to divide by
  5.     RGBTRIPLE temp[height][width];
  6.     float redsum;
  7.     float greensum;
  8.     float bluesum;
  9.     float elements;
  10.  
  11.     // Enters row
  12.     for (int i = 0; i < height; i++)
  13.     {
  14.         // Enters column
  15.         for (int j = 0; j < width; j++)
  16.         {
  17.             // Nullifies the variables
  18.             redsum = 0;
  19.             greensum = 0;
  20.             bluesum = 0;
  21.             elements = 0;
  22.             // Restricts to 1 row below through to 1 above
  23.             for (int x = i - 1; x <= i + 1; x++)
  24.             {
  25.                 // Restricts to 1 column to the side through to 1 over
  26.                 for (int y = j - 1; y <= j + 1; y++)
  27.                 {
  28.                     // Restrict the x and y variables so that they are never < 0 or > matrix_size
  29.                     if ((x >= 0 && x <= height - 1) && (y >= 0 && y <= width - 1))
  30.                     {
  31.                         // Sum up all original red values in the defined matrix
  32.                         redsum += image[x][y].rgbtRed;
  33.                         // Sum up all original blue values in the defined matrix
  34.                         greensum += image[x][y].rgbtGreen;
  35.                         // Sum up all original green values in the defined matrix
  36.                         bluesum += image[x][y].rgbtBlue;
  37.                         // Increment the pixels/elements used
  38.                         elements++;
  39.                     }
  40.                 }
  41.             }
  42.             // Store the average RGB values in the temporary variable
  43.             temp[i][j].rgbtRed = round(redsum / elements);
  44.             temp[i][j].rgbtGreen = round(greensum / elements);
  45.             temp[i][j].rgbtBlue = round(bluesum / elements);
  46.         }
  47.     }
  48.  
  49.     // Enters column
  50.     for (int i = 0; i < height; i++)
  51.     {
  52.         // Enters row
  53.         for (int j = 0; j < width; j++)
  54.         {
  55.                     // Sets the 'selected pixel' to the temporary variables we stored in previously
  56.                     image[i][j].rgbtRed = temp[i][j].rgbtRed;
  57.                     image[i][j].rgbtGreen = temp[i][j].rgbtGreen;
  58.                     image[i][j].rgbtBlue = temp[i][j].rgbtBlue;
  59.         }
  60.     }
  61.     return;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment