Ryba3310

Edges

Apr 20th, 2022 (edited)
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.03 KB | None | 0 0
  1. void edges(int height, int width, RGBTRIPLE image[height][width])
  2. {
  3.     RGBTRIPLE copy[height][width];
  4.     for (int i = 0; i < height; i++)
  5.     {
  6.         for (int j = 0; j < width; j++)
  7.         {
  8.             copy[i][j] = image[i][j];
  9.         }
  10.     }
  11.  
  12.     for (int i = 0; i < height; i++)
  13.     {
  14.         for (int j = 0; j < width; j++)
  15.         {
  16.             image[i][j].rgbtRed = color_value('r', i, j, height, width, copy);
  17.             image[i][j].rgbtGreen = color_value('g', i, j, height, width, copy);
  18.             image[i][j].rgbtBlue = color_value('b', i, j, height, width, copy);
  19.         }
  20.     }
  21.     return;
  22. }
  23.  
  24. int color_value(char color, int i, int j, int height, int width, RGBTRIPLE pixels[height][width])
  25. {
  26.     //Arrays for Gx and Gy for weighted sum
  27.     int Gx[] = {-1, 0, 1, -2, 0, 2, -1, 0, 1};
  28.     int Gy[] = {-1, -2, -1, 0, 0, 0, 1, 2, 1};
  29.     int Gx_value = 0;
  30.     int Gy_value = 0;
  31.  
  32.     for (int k = 0; k < 9; k++)
  33.     {
  34.         int box_height = i + (k % 3) - 1; // "-1" is offset from pixel
  35.         int box_width = j + (k / 3) - 1;
  36.         // Check if pixel is out of border
  37.         if (box_height < 0 || box_width < 0 || box_height >= height || box_width >= width)
  38.         {
  39.             Gx_value += 0;
  40.             Gy_value += 0;
  41.             continue;
  42.         }
  43.         switch(color)
  44.         {
  45.             case 'r':
  46.             Gx_value += Gx[k] * pixels[box_height][box_width].rgbtRed;
  47.             Gy_value += Gy[k] * pixels[box_height][box_width].rgbtRed;
  48.             break;
  49.             case 'g':
  50.             Gx_value += Gx[k] * pixels[box_height][box_width].rgbtGreen;
  51.             Gy_value += Gy[k] * pixels[box_height][box_width].rgbtGreen;
  52.             break;
  53.             case 'b':
  54.             Gx_value += Gx[k] * pixels[box_height][box_width].rgbtBlue;
  55.             Gy_value += Gy[k] * pixels[box_height][box_width].rgbtBlue;
  56.             break;
  57.         }
  58.     }
  59.     return (round(sqrt(Gx_value * Gx_value + Gy_value * Gy_value)) < 256) ? round(sqrt(Gx_value * Gx_value + Gy_value * Gy_value)) : 255;
  60. }
Add Comment
Please, Sign In to add comment