Guest User

helpers.c

a guest
Apr 21st, 2020
942
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.17 KB | None | 0 0
  1. #include "helpers.h"
  2. #include "math.h"
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5.  
  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.             float r = image[i][j].rgbtRed;
  14.             float g = image[i][j].rgbtGreen;
  15.             float b = image[i][j].rgbtBlue;
  16.  
  17.             float c = (r + g + b) / 3;
  18.  
  19.             image[i][j].rgbtRed = round(c);
  20.             image[i][j].rgbtGreen = round(c);
  21.             image[i][j].rgbtBlue = round(c);
  22.         }
  23.     }
  24.     return;
  25. }
  26.  
  27. // Convert image to sepia
  28. void sepia(int height, int width, RGBTRIPLE image[height][width])
  29. {
  30.     for (int i = 0; i < height; i++)
  31.     {
  32.         for (int j = 0; j < width; j++)
  33.         {
  34.             float r = image[i][j].rgbtRed;
  35.             float g = image[i][j].rgbtGreen;
  36.             float b = image[i][j].rgbtBlue;
  37.  
  38.             float red = .393 * r + .769 * g + .189 * b;
  39.             float green = .349 * r + .686 * g + .168 * b;
  40.             float blue = .272 * r + .534 * g + .131 * b;
  41.  
  42.             if (red > 255)
  43.             {
  44.                 red = 255;
  45.             }
  46.  
  47.             if (green > 255)
  48.             {
  49.                 green = 255;
  50.             }
  51.  
  52.             if (blue > 255)
  53.             {
  54.                 blue = 255;
  55.             }
  56.  
  57.             image[i][j].rgbtRed = round(red);
  58.             image[i][j].rgbtGreen = round(green);
  59.             image[i][j].rgbtBlue = round(blue);
  60.         }
  61.     }
  62.  
  63.     return;
  64. }
  65.  
  66. // Reflect image horizontally
  67. void reflect(int height, int width, RGBTRIPLE image[height][width])
  68. {
  69.     // initialize new temp array with RGB struct
  70.     RGBTRIPLE newimage[height][width];
  71.  
  72.     for (int i = 0; i < height; i++)
  73.     {
  74.         for (int j = 0; j < width; j++)
  75.         {
  76.             //flip the j value
  77.             int fliph = (width - 1 - j);
  78.  
  79.             // getting RGB values of original
  80.             newimage[i][fliph] = image[i][j];
  81.         }
  82.     }
  83.  
  84.     for (int i = 0; i < height; i++)
  85.     {
  86.         for (int j = 0; j < width; j++)
  87.         {
  88.             // place the newly flipped values into the old array
  89.             image[i][j] = newimage[i][j];
  90.         }
  91.     }
  92.     return;
  93. }
  94.  
  95. // Blur image
  96. void blur(int height, int width, RGBTRIPLE image[height][width])
  97. {
  98.     RGBTRIPLE newimage[height][width]; // Gets mirror image to work on
  99.  
  100.     for (int i = 0; i < height; i++)
  101.     {
  102.         for (int j = 0; j < width; j++)
  103.         {
  104.             newimage[i][j] = image[i][j];
  105.         }
  106.     }                                   // done with image mapping
  107.    
  108.     for (int i = 0; i < height; i++) // goes over entire image i for rows (height), j for columns (width)
  109.     {
  110.         for (int j = 0; j < width; j++)
  111.         {
  112.             float sumr = 0; // initalize the sum values of the rgbt's
  113.             float sumg = 0;
  114.             float sumb = 0;
  115.             int pixel_count = 0; // used to count how many pixels are done for each rotation
  116.            
  117.             for (int k = -1; k < 2; k++) // checks each pixel within -1 to + 1 of each [i]  (ie left to right)
  118.             {
  119.                 for (int l = -1; l < 2; l++) // checks each pixel withint -1 to +1 of each j pixel (ie up to down)
  120.                 {
  121.                     if (i + k >= 0  && j + l >= 0 && i + k <= height - 1 && j + l <= width - 1) // if statement to check
  122.                     {                                                                           // if pixel checke is
  123.                         sumr = newimage[i + k][j + l].rgbtRed + sumr;                           // within bounds
  124.                         sumg = newimage[i + k][j + l].rgbtGreen + sumg;
  125.                         sumb = newimage[i + k][j + l].rgbtBlue + sumb;
  126.                         pixel_count++; //adds one to the pixel count to divide by within each [i][j] loop
  127.                     }
  128.                 }
  129.             }
  130.            
  131.             image[i][j].rgbtRed = round(sumr / pixel_count);
  132.             image[i][j].rgbtGreen = round(sumg / pixel_count);
  133.             image[i][j].rgbtBlue = round(sumb / pixel_count);
  134.         }
  135.     }
  136.     return;
  137. }
Add Comment
Please, Sign In to add comment