Advertisement
Guest User

helpers.c

a guest
Apr 9th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include "helpers.h"
  2. #include <math.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. void sum(int *b_avg, int *g_avg, int *r_avg, RGBTRIPLE pixel);
  7.  
  8. // Convert image to grayscale
  9. void grayscale(int height, int width, RGBTRIPLE image[height][width])
  10. {
  11.     for (int i = 0; i < height; i++)
  12.     {
  13.         for (int j = 0; j < width; j++)
  14.         {
  15.             int avg = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3.0);
  16.             image[i][j].rgbtBlue = avg;
  17.             image[i][j].rgbtGreen = avg;
  18.             image[i][j].rgbtRed = avg;
  19.         }
  20.     }
  21.     return;
  22. }
  23.  
  24. // Reflect image horizontally
  25. void reflect(int height, int width, RGBTRIPLE image[height][width])
  26. {
  27.     for (int i = 0; i < height; i++)
  28.     {
  29.         for (int j = 0; j < round(width / 2); j++)
  30.         {
  31.             // swap j with width - j
  32.             RGBTRIPLE temp = image[i][j];
  33.             image[i][j] = image[i][width - (j + 1)];
  34.             image[i][width - (j + 1)] = temp;
  35.         }
  36.     }
  37.     return;
  38. }
  39.  
  40. // Blur image
  41. void blur(int height, int width, RGBTRIPLE image[height][width])
  42. {
  43.     RGBTRIPLE(*image_cp)[width] = calloc(height, width * sizeof(RGBTRIPLE));
  44.  
  45.     for (int i = 0; i < height; i++)
  46.     {
  47.         for (int j = 0; j < width; j++)
  48.         {
  49.             image_cp[i][j] = image[i][j];
  50.         }
  51.     }
  52.  
  53.     for (int i = 0; i < height; i++)
  54.     {
  55.         for (int j = 0; j < width; j++)
  56.         {
  57.             int b_avg, g_avg, r_avg = 0;
  58.             int pixel_count = 0;
  59.             for (int c = -1; c < 2; c++)
  60.             {
  61.                 if (i + c < 0 || i + c >= height)
  62.                 {
  63.                     continue;
  64.                 }
  65.                 for (int r = -1; r < 2; r++)
  66.                 {
  67.                     if (j + r < 0 || j + r >= width)
  68.                     {
  69.                         continue;
  70.                     }
  71.                     sum(&b_avg, &g_avg, &r_avg, image_cp[i + c][j + r]);
  72.                     pixel_count++;
  73.                 }
  74.             }
  75.  
  76.             RGBTRIPLE avg;
  77.             avg.rgbtBlue = b_avg / pixel_count;
  78.             avg.rgbtGreen = g_avg / pixel_count;
  79.             avg.rgbtRed = r_avg / pixel_count;
  80.  
  81.             image[i][j] = avg;
  82.         }
  83.     }
  84.     free(image_cp);
  85.     return;
  86. }
  87.  
  88. // Detect edges
  89. void edges(int height, int width, RGBTRIPLE image[height][width])
  90. {
  91.     return;
  92. }
  93.  
  94. // adds rgb triple to averages
  95. void sum(int *b_avg, int *g_avg, int *r_avg, RGBTRIPLE pixel)
  96. {
  97.     *b_avg += pixel.rgbtBlue;
  98.     *g_avg += pixel.rgbtGreen;
  99.     *r_avg += pixel.rgbtRed;
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement