Facebook
From Morose Tapir, 3 Years ago, written in C.
Embed
Download Paste or View Raw
Hits: 105
  1. #include "helpers.h"
  2. #include <math.h>
  3. #include <stdio.h>
  4. // function prototype
  5. void swap(RGBTRIPLE *lp, RGBTRIPLE *rp);
  6.  
  7. // Convert image to grayscale
  8. void grayscale(int height, int width, RGBTRIPLE image[height][width])
  9. {
  10.     int R, G, B, Gr;
  11.     for (int i = 0; i < height; i++)
  12.     {
  13.         for (int j = 0; j < width; j++)
  14.         {
  15.             // calculate average of R G B values. for each pixel.
  16.             R = image[i][j].rgbtRed;
  17.             G = image[i][j].rgbtGreen;
  18.             B = image[i][j].rgbtBlue;
  19.             Gr = round((R + B + G) / (double) 3);
  20.             // put new values to original array
  21.             image[i][j].rgbtRed = Gr;
  22.             image[i][j].rgbtGreen = Gr;
  23.             image[i][j].rgbtBlue = Gr;
  24.         }
  25.     }
  26.     return;
  27. }
  28.  
  29. // Reflect image horizontally
  30. void reflect(int height, int width, RGBTRIPLE image[height][width])
  31. {
  32.     for (int i = 0; i < height; i++)
  33.     {
  34.         for (int j = 0; j < (width / 2); j++)
  35.         {
  36.             // swap the pixels
  37.             swap(&image[i][j], &image[i][width - j - 1]);
  38.         }
  39.     }
  40.     return;
  41. }
  42.  
  43. // Blur image
  44. void blur(int height, int width, RGBTRIPLE image[height][width])
  45. {
  46.     RGBTRIPLE imagetemp[height][width];
  47.     int Rblur, Gblur, Bblur;
  48.     double neighborcount;
  49.     for (int i = 0; i < height; i++)
  50.     {
  51.         for (int j = 0; j < width; j++)
  52.         {
  53.             // calculate red green blue amount of pixel at row i, column j
  54.             Rblur = 0, Gblur = 0, Bblur = 0, neighborcount = 0;
  55.             for (int row = -1; row < 2; row++)
  56.             {
  57.                 if (i + row > -1 && i + row < height)
  58.                 {
  59.                     for (int column = -1; column < 2; column++)
  60.                     {
  61.                         if (j + column > -1 && j + column < width)
  62.                         {
  63.                             Rblur += image[i + row][j + column].rgbtRed;
  64.                             Gblur += image[i + row][j + column].rgbtGreen;
  65.                             Bblur += image[i + row][j + column].rgbtBlue;
  66.                             neighborcount++;
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.             // put new values to temporary array
  72.             imagetemp[i][j].rgbtRed = round(Rblur / neighborcount);
  73.             imagetemp[i][j].rgbtGreen = round(Gblur / neighborcount);
  74.             imagetemp[i][j].rgbtBlue = round(Bblur / neighborcount);
  75.         }
  76.     }
  77.     // put new values in temporary array into the original array
  78.     for (int i = 0; i < height; i++)
  79.     {
  80.         for (int j = 0; j < width; j++)
  81.         {
  82.             image[i][j].rgbtRed = imagetemp[i][j].rgbtRed;
  83.             image[i][j].rgbtGreen = imagetemp[i][j].rgbtGreen;
  84.             image[i][j].rgbtBlue = imagetemp[i][j].rgbtBlue;
  85.         }
  86.     }
  87.     return;
  88. }
  89.  
  90. // Detect edges
  91. void edges(int height, int width, RGBTRIPLE image[height][width])
  92. {
  93.     RGBTRIPLE imagetemp[height][width];
  94.     float RtempGx, GtempGx, BtempGx, RtempGy, GtempGy, BtempGy;
  95.     int R, G, B;
  96.     // defining & initializing arrays according to Gx and Gy kernels
  97.     int Gx[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
  98.     int Gy[3][3] = {{-1, -2, -1}, {0, 0, 0}, {-1, -2, -1}};
  99.  
  100.     // iterate through every pixel in image
  101.     for (int i = 0; i < height; i++)
  102.     {
  103.         for (int j = 0; j < width; j++)
  104.         {
  105.             // zeroing buffer arrays and temp variables for each iteration
  106.             RtempGx = 0, GtempGx = 0, BtempGx = 0, RtempGy = 0, GtempGy = 0, BtempGy = 0;
  107.  
  108.             for (int row = -1; row < 2; row++)
  109.             {
  110.                 int rowplus = row + 1;
  111.                 if (i + row > -1 && i + row < height)
  112.                 {
  113.                     for (int column = -1; column < 2; column++)
  114.                     {
  115.                         int columnplus = column + 1;
  116.                         if (j + column > -1 && j + column < width)
  117.                         {
  118.                             RtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtRed;
  119.                             GtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtGreen;
  120.                             BtempGx += Gx[rowplus][columnplus] * image[i + row][j + column].rgbtBlue;
  121. // printf("%f\n", Xbufferbox[rowplus][columnplus][2]);
  122.                             RtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtRed;
  123.                             GtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtGreen;
  124.                             BtempGy += Gy[rowplus][columnplus] * image[i + row][j + column].rgbtBlue;
  125.                         }
  126.                     }
  127.                 }
  128.             }
  129.  
  130.             R = round(sqrt((RtempGx * RtempGx) + (RtempGy * RtempGy)));
  131.             G = round(sqrt((GtempGx * GtempGx) + (GtempGy * GtempGy)));
  132.             B = round(sqrt((BtempGx * BtempGx) + (BtempGy * BtempGy)));
  133.  
  134.             if (R > 255)
  135.             {
  136.                 R = 255;
  137.             }
  138.             if (G > 255)
  139.             {
  140.                 G = 255;
  141.             }
  142.             if (B > 255)
  143.             {
  144.                 B = 255;
  145.             }
  146.  
  147.             // put new values into temporary array
  148.             imagetemp[i][j].rgbtRed = R;
  149.             imagetemp[i][j].rgbtGreen = G;
  150.             imagetemp[i][j].rgbtBlue = B;
  151.         }
  152.     }
  153.     // put new values in temporary array into the original array
  154.     for (int i = 0; i < height; i++)
  155.     {
  156.         for (int j = 0; j < width; j++)
  157.         {
  158.             image[i][j].rgbtRed = imagetemp[i][j].rgbtRed;
  159.             image[i][j].rgbtGreen = imagetemp[i][j].rgbtGreen;
  160.             image[i][j].rgbtBlue = imagetemp[i][j].rgbtBlue;
  161.         }
  162.     }
  163.     return;
  164. }
  165.  
  166. // swap arrays using pointers
  167. void swap(RGBTRIPLE *lp, RGBTRIPLE *rp)
  168. {
  169.     RGBTRIPLE temp = *lp;
  170.     *lp = *rp;
  171.     *rp = temp;
  172. }
  173.  
  174.