Ryba3310

helpers.c

Apr 16th, 2022 (edited)
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.70 KB | None | 0 0
  1. #include "helpers.h"
  2. #include "math.h"
  3.  
  4. int new_blur_pix(RGBTRIPLE *copy);
  5. int round_triple(int a, int b, int c);
  6. // Convert image to grayscale
  7. void grayscale(int height, int width, RGBTRIPLE image[height][width])
  8. {
  9.     for (int i = 0; i < height; i++)
  10.     {
  11.         for (int j = 0; j < width; j++)
  12.         {
  13.             RGBTRIPLE tmp = image[i][j];
  14.             int tmp_color = round_triple(tmp.rgbtBlue, tmp.rgbtGreen, tmp.rgbtRed);
  15.             tmp.rgbtBlue = tmp_color;
  16.             tmp.rgbtGreen = tmp_color;
  17.             tmp.rgbtRed = tmp_color;
  18.             image[i][j] = tmp;
  19.         }
  20.     }
  21.     return;
  22. }
  23.  
  24. // Convert image to sepia
  25. void sepia(int height, int width, RGBTRIPLE image[height][width])
  26. {
  27.     for (int i = 0; i < height; i++)
  28.     {
  29.         for (int j = 0; j < width; j++)
  30.         {
  31.             RGBTRIPLE tmp = image[i][j];
  32.             int red = image[i][j].rgbtRed;
  33.             int green = image[i][j].rgbtGreen;
  34.             int blue = image[i][j].rgbtBlue;
  35.             tmp.rgbtRed = ((round(0.393f * image[i][j].rgbtRed + 0.769f * image[i][j].rgbtGreen + 0.189f * image[i][j].rgbtBlue)) < 255) ? round(0.393f * image[i][j].rgbtRed + 0.769f * image[i][j].rgbtGreen + 0.189f * image[i][j].rgbtBlue) : 255;
  36.             tmp.rgbtGreen = ((round(0.349 * image[i][j].rgbtRed + 0.686 * image[i][j].rgbtGreen + 0.168 * image[i][j].rgbtBlue)) < 255) ? round(0.349 * image[i][j].rgbtRed + 0.686 * image[i][j].rgbtGreen + 0.168 * image[i][j].rgbtBlue) : 255;
  37.             tmp.rgbtBlue = ((round(0.272 * image[i][j].rgbtRed + 0.534 * image[i][j].rgbtGreen + 0.131 * image[i][j].rgbtBlue)) < 255) ? round(0.272 * image[i][j].rgbtRed + 0.534 * image[i][j].rgbtGreen + 0.131 * image[i][j].rgbtBlue) : 255;
  38.             image[i][j] = tmp;
  39.         }
  40.     }
  41.     return;
  42. }
  43.  
  44. // Reflect image horizontally
  45. void reflect(int height, int width, RGBTRIPLE image[height][width])
  46. {
  47.     for (int i = 0; i < height; i++)
  48.     {
  49.         for (int j = 0; j < width / 2; j++)
  50.         {
  51.             RGBTRIPLE tmp = image[i][j];
  52.             image[i][j] = image[i][width - 1 - j];
  53.             image[i][width - 1 - j] = tmp;
  54.         }
  55.     }
  56.     return;
  57. }
  58.  
  59. // Blur image
  60. void blur(int height, int width, RGBTRIPLE image[height][width])
  61. {
  62.     RGBTRIPLE copy[height][width];
  63.     for (int i = 0; i < height; i++)
  64.     {
  65.         for (int j = 0; j < width; j++)
  66.         {
  67.             copy[i][j] = image[i][j];
  68.         }
  69.     }
  70.     for (int i = 0; i < height; i++)
  71.     {
  72.         for (int j = 0; j < width; j++)
  73.         {
  74.             int averageRed = 0;
  75.             int averageGreen = 0;
  76.             int averageBlue = 0;
  77.             // Keep track of pixels added in case of edges
  78.             int pixels_count = 0;
  79.             //Iterate over every index of box of size 3x3
  80.             for (int k = 0; k < 9; k++)
  81.             {
  82.                 int box_height = i + (k % 3) - 1;
  83.                 int box_width = j + (k / 3) - 1;
  84.  
  85.                 if ((box_height >= 0 && box_width >= 0) && (box_height < height && box_width < width))
  86.                 {
  87.                     averageRed += copy[box_height][box_width].rgbtRed;
  88.                     averageGreen += copy[box_height][box_width].rgbtGreen;
  89.                     averageBlue += copy[box_height][box_width].rgbtBlue;
  90.                     pixels_count++;
  91.                 }
  92.             }
  93.             image[i][j].rgbtRed = round((double)averageRed / pixels_count);
  94.             image[i][j].rgbtGreen = round((double)averageGreen / pixels_count);
  95.             image[i][j].rgbtBlue = round((double)averageBlue / pixels_count);
  96.         }
  97.     }
  98.  
  99.     return;
  100. }
  101.  
  102. int round_triple(int a, int b, int c)
  103. {
  104.     float rounded = ((double)a + b + c) / 3;
  105.  
  106.     return rounded + 0.5f;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment