Advertisement
jamestha3d

cs50 filter more

Oct 7th, 2022
871
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.66 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdint.h>
  5. #include "helpers.h"
  6.  
  7. //const int BITMAPINFOHEADER = 14;
  8. //const int BITMAPFILEHEADER = 40;
  9.  
  10. typedef uint8_t  BYTE;
  11. typedef uint32_t DWORD;
  12. typedef int32_t  LONG;
  13. typedef uint16_t WORD;
  14.  
  15. // Convert image to grayscale
  16. void grayscale(int height, int width, RGBTRIPLE image[height][width])
  17. {
  18.     for (int i = 0; i < height; i++)
  19.     {
  20.         for (int j = 0; j < width; j++)
  21.         {
  22.             //get RGBTRIPLE average
  23.             BYTE blue = image[i][j].rgbtBlue;
  24.             BYTE green = image[i][j].rgbtGreen;
  25.             BYTE red = image[i][j].rgbtRed;
  26.             double avg = round((red + blue + green) / 3.0);
  27.  
  28.             //update
  29.             image[i][j].rgbtBlue = avg;
  30.             image[i][j].rgbtGreen = avg;
  31.             image[i][j].rgbtRed = avg;
  32.         }
  33.     }
  34.     return;
  35. }
  36.  
  37. // Reflect image horizontally
  38. void reflect(int height, int width, RGBTRIPLE image[height][width])
  39. {
  40.     for (int i = 0; i < height; i++)
  41.     {
  42.         for (int j = 0; j < width / 2; j++)
  43.         {
  44.             //swap each RGBTRIPLE with its reflective counterpart
  45.             RGBTRIPLE temp = image[i][j];
  46.             image[i][j] = image[i][width - j - 1];
  47.             image[i][width - j - 1] = temp;
  48.         }
  49.     }
  50.     return;
  51. }
  52.  
  53. // Blur image
  54. void blur(int height, int width, RGBTRIPLE image[height][width])
  55. {
  56.     RGBTRIPLE copy[height][width];
  57.     //for each row in the image
  58.     for (int i = 0; i < height; i++)
  59.     {
  60.         //for each pixel in the row
  61.         for (int j = 0; j < width; j++)
  62.         {
  63.             //find the avg of all 9 pixels
  64.             double sum_red = 0;
  65.             double sum_blue = 0;
  66.             double sum_green = 0;
  67.  
  68.             double num_runs = 0;
  69.             //ensure that we do not exceed array in +ve or -ve direction
  70.             //creates a loop that goes through -1, 0, 1, enabling us to check the rows before, current, and after
  71.             for (int k = -1; k < 2; k++)
  72.  
  73.             {
  74.                 //creates loop that goes from -1,0,1 enabling to check the pixels before, current and after
  75.                 for (int l = -1; l < 2; l++)
  76.                 {
  77.                     // only do the following if we have not exceeded array indexes for row and column
  78.                     if ((i + k) < 0 || (i + k) >= height || (j + l) < 0 || (j + l) >= width)
  79.                     {
  80.                         continue;
  81.                         //printf("%i\n", num_runs);
  82.                     }
  83.  
  84.                     sum_red += image[i + k][ j + l].rgbtRed;
  85.                     sum_blue += image[i + k][ j + l].rgbtBlue;
  86.                     sum_green += image[i + k][ j + l].rgbtGreen;
  87.  
  88.                     num_runs++;
  89.  
  90.                 }
  91.  
  92.             }
  93.  
  94.  
  95.             copy[i][j].rgbtRed = round(sum_red / num_runs);
  96.             copy[i][j].rgbtBlue = round(sum_blue / num_runs);
  97.             copy[i][j].rgbtGreen = round(sum_green / num_runs);
  98.  
  99.  
  100.         }
  101.     }
  102.     for (int i = 0; i < height; i++)
  103.     {
  104.         for (int j = 0; j < width; j++)
  105.         {
  106.             image[i][j] = copy[i][j];
  107.         }
  108.     }
  109.     return;
  110. }
  111.  
  112. // Detect edges
  113. void edges(int height, int width, RGBTRIPLE image[height][width])
  114. {
  115.     RGBTRIPLE copy[height][width];
  116.     int gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
  117.     int gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
  118.     for (int i = 0; i < height; i++)
  119.     {
  120.         for (int j = 0; j < width; j++)
  121.         {
  122.             //find the avg of all 9 pixels
  123.             long gx_red = 0;
  124.             long gx_blue = 0;
  125.             long gx_green = 0;
  126.             long gy_red = 0;
  127.             long gy_blue = 0;
  128.             long gy_green = 0;
  129.  
  130.             //loop from -1 to 1, to enable checking of items before and after current item
  131.             for (int k = -1; k < 2; k++)
  132.             {
  133.                 for (int l = -1; l < 2; l++)
  134.                 {
  135.                     //if item goes out of bound of array, skip
  136.                     if ((i + k) < 0 || (i + k) >= height || (j + l) < 0 || (j + l) >= width)
  137.                     {
  138.                         continue;
  139.  
  140.                     }
  141.  
  142.                     //calculate sobel edge sum
  143.                     gx_red += image[i + k][ j + l].rgbtRed * gx[k + 1][l + 1];
  144.                     gx_blue += image[i + k][ j + l].rgbtBlue * gx[k + 1][l + 1];
  145.                     gx_green += image[i + k][ j + l].rgbtGreen * gx[k + 1][l + 1];
  146.                     gy_red += image[i + k][ j + l].rgbtRed * gy[k + 1][l + 1];
  147.                     gy_blue += image[i + k][ j + l].rgbtBlue * gy[k + 1][l + 1];
  148.                     gy_green += image[i + k][ j + l].rgbtGreen * gy[k + 1][l + 1];
  149.  
  150.  
  151.                 }
  152.  
  153.             }
  154.  
  155.             //calculate the sobel edge detection sq root
  156.             double red_final = round(sqrt(gx_red * gx_red + gy_red * gy_red));
  157.             double blue_final = round(sqrt(gx_blue * gx_blue + gy_blue * gy_blue));
  158.             double green_final = round(sqrt(gx_green * gx_green + gy_green * gy_green));
  159.  
  160.             if (red_final > 255)
  161.             {
  162.                 red_final = 255;
  163.             }
  164.  
  165.             if (blue_final > 255)
  166.             {
  167.                 blue_final = 255;
  168.             }
  169.  
  170.             if (green_final > 255)
  171.             {
  172.                 green_final = 255;
  173.             }
  174.  
  175.             copy[i][j].rgbtRed = red_final;
  176.             copy[i][j].rgbtBlue = blue_final;
  177.             copy[i][j].rgbtGreen = green_final;
  178.  
  179.         }
  180.  
  181.  
  182.     }
  183.  
  184.     for (int i = 0; i < height; i++)
  185.     {
  186.         for (int j = 0; j < width; j++)
  187.         {
  188.             image[i][j] = copy[i][j];
  189.         }
  190.     }
  191.  
  192.     return;
  193. }
  194.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement