Guest User

PSET4

a guest
Jun 1st, 2020
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include "helpers.h"
  2. #include <math.h>
  3.  
  4. // Convert image to grayscale
  5. void grayscale(int height, int width, RGBTRIPLE image[height][width])
  6. {
  7.  
  8.     for (int i = 0; i < height; ++i)
  9.     {
  10.         for (int j = 0; j < width; ++j)
  11.         {
  12.  
  13.             float meanRGBVal = (image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
  14.  
  15.             image[i][j].rgbtRed = round(meanRGBVal);
  16.             image[i][j].rgbtGreen = round(meanRGBVal);
  17.             image[i][j].rgbtBlue = round(meanRGBVal);
  18.         }
  19.     }
  20.  
  21.  
  22.     return;
  23. }
  24.  
  25. // Convert image to sepia
  26. void sepia(int height, int width, RGBTRIPLE image[height][width])
  27. {
  28.  
  29.     int originalRed, originalGreen, originalBlue, sepiaRed, sepiaBlue, sepiaGreen;
  30.  
  31.     for (int i = 0; i < height; ++i)
  32.     {
  33.         for (int j = 0; j < width; ++j)
  34.         {
  35.             originalRed = image[i][j].rgbtRed;
  36.             originalGreen = image[i][j].rgbtGreen;
  37.             originalBlue = image[i][j].rgbtBlue;
  38.  
  39.             sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
  40.             sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
  41.             sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);
  42.  
  43.             if (sepiaRed > 255)
  44.             {
  45.                 sepiaRed = 255;
  46.             }
  47.             if (sepiaGreen > 255)
  48.             {
  49.                 sepiaGreen = 255;
  50.             }
  51.             if (sepiaBlue > 255)
  52.             {
  53.                 sepiaBlue = 255;
  54.             }
  55.  
  56.             image[i][j].rgbtRed = sepiaRed;
  57.             image[i][j].rgbtGreen = sepiaGreen;
  58.             image[i][j].rgbtBlue = sepiaBlue;
  59.         }
  60.     }
  61.  
  62.     return;
  63. }
  64.  
  65. // Swap function used for Reflection of images
  66. void swap(BYTE *x, BYTE *y)
  67. {
  68.     BYTE tmp = *x;
  69.     *x = *y;
  70.     *y = tmp;
  71. }
  72.  
  73. // Reflect image horizontally
  74. void reflect(int height, int width, RGBTRIPLE image[height][width])
  75. {
  76.  
  77.     for (int i = 0; i < height; ++i)
  78.     {
  79.         for (int j = 0; j < width / 2; ++j)
  80.         {
  81.             swap(&image[i][j].rgbtRed,   &image[i][width - j - 1].rgbtRed);
  82.             swap(&image[i][j].rgbtGreen, &image[i][width - j - 1].rgbtGreen);
  83.             swap(&image[i][j].rgbtBlue,  &image[i][width - j - 1].rgbtBlue);
  84.         }
  85.     }
  86.  
  87.     return;
  88. }
  89.  
  90. // Blur image
  91. void blur(int height, int width, RGBTRIPLE image[height][width])
  92. {
  93.  
  94.     for (int i = 0; i < height; i++)
  95.     {
  96.         for (int j = 0; j < width; j++)
  97.         {
  98.             // Top Left
  99.             if (i == 0 && j == 0)
  100.             {
  101.  
  102.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 4);
  103.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 4);
  104.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 4);
  105.             }
  106.  
  107.             // Top Right
  108.             else if (i == 0 && j == (width - 1))
  109.             {
  110.  
  111.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 4);
  112.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 4);
  113.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 4);
  114.             }
  115.  
  116.  
  117.             // Bottom Left
  118.             else if (i == (height - 1) && j == 0)
  119.             {
  120.  
  121.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed) / 4);
  122.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 4);
  123.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 4);
  124.             }
  125.  
  126.             // Bottom Right
  127.             else if (i == (height - 1) && j == width)
  128.             {
  129.  
  130.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed) / 4);
  131.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen) / 4);
  132.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue) / 4);
  133.  
  134.             }
  135.  
  136.             // Top Border
  137.             else if (i == 0)
  138.             {
  139.  
  140.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6);
  141.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6);
  142.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6);
  143.  
  144.             }
  145.  
  146.  
  147.  
  148.             // Left Border
  149.             else if (j == 0)
  150.             {
  151.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 6);
  152.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 6);
  153.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 6);
  154.  
  155.             }
  156.  
  157.             // Right Border
  158.             else if (j == (width - 1))
  159.             {
  160.  
  161.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i][j - 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed) / 6);
  162.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i][j - 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen) / 6);
  163.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i][j - 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue) / 6);
  164.             }
  165.  
  166.             // Bottom Border
  167.             else if (i == (height - 1))
  168.             {
  169.  
  170.                 image[i][j].rgbtRed = round((image[i][j].rgbtRed + image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i][j + 1].rgbtRed) / 6);
  171.                 image[i][j].rgbtGreen = round((image[i][j].rgbtGreen + image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j + 1].rgbtGreen) / 6);
  172.                 image[i][j].rgbtBlue = round((image[i][j].rgbtBlue + image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j + 1].rgbtBlue) / 6);
  173.  
  174.             }
  175.  
  176.             // No Border
  177.             else {
  178.  
  179.                 image[i][j].rgbtRed = round((image[i - 1][j - 1].rgbtRed + image[i - 1][j].rgbtRed + image[i - 1][j + 1].rgbtRed + image[i][j - 1].rgbtRed + image[i][j].rgbtRed + image[i][j + 1].rgbtRed + image[i + 1][j - 1].rgbtRed + image[i + 1][j].rgbtRed + image[i + 1][j + 1].rgbtRed) / 9);
  180.                 image[i][j].rgbtGreen = round((image[i - 1][j - 1].rgbtGreen + image[i - 1][j].rgbtGreen + image[i - 1][j + 1].rgbtGreen + image[i][j - 1].rgbtGreen + image[i][j].rgbtGreen + image[i][j + 1].rgbtGreen + image[i + 1][j - 1].rgbtGreen + image[i + 1][j].rgbtGreen + image[i + 1][j + 1].rgbtGreen) / 9);
  181.                 image[i][j].rgbtBlue = round((image[i - 1][j - 1].rgbtBlue + image[i - 1][j].rgbtBlue + image[i - 1][j + 1].rgbtBlue + image[i][j - 1].rgbtBlue + image[i][j].rgbtBlue + image[i][j + 1].rgbtBlue + image[i + 1][j - 1].rgbtBlue + image[i + 1][j].rgbtBlue + image[i + 1][j + 1].rgbtBlue) / 9);
  182.             }
  183.         }
  184.     }
  185.  
  186.     return;
  187. }
  188.  
  189. /*
  190.  
  191. -- ERROR MESSAGES ONLY --
  192. :( grayscale correctly filters single pixel without whole number average
  193.     expected "28 28 28\n", not "27 27 27\n"
  194. :( grayscale correctly filters more complex 3x3 image
  195.     expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
  196. :( grayscale correctly filters 4x4 image
  197.     expected "20 20 20\n50 5...", not "20 20 20\n50 5..."
  198. :( blur correctly filters middle pixel
  199.     expected "127 140 149\n", not "145 159 169\n"
  200. :( blur correctly filters pixel on edge
  201.     expected "80 95 105\n", not "90 105 115\n"
  202. :( blur correctly filters 3x3 image
  203.     expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
  204. :( blur correctly filters 4x4 image
  205.     expected "70 85 95\n80 9...", not "70 85 95\n90 1..."
  206. */
RAW Paste Data