Advertisement
Anachronos

Untitled

Nov 9th, 2020 (edited)
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.53 KB | None | 0 0
  1. void blur(int height, int width, RGBTRIPLE image[height][width])
  2. {
  3. RGBTRIPLE(*alt_img)[width] = calloc(height, width * sizeof(RGBTRIPLE));
  4.  
  5. //3x3 radius
  6. /*       +-----------------------------------------+
  7.          |             |             |             |
  8.          | N[h-1][w-1] |  N[h-1][w]  | N[h-1][w+1] |
  9.          |             |             |             |
  10.          |-------------|-------------|-------------|
  11.          |             |             |             |
  12.          |  N[h][w-1]  |   N[h][w]   |  N[h][w+1]  |
  13.          |             |             |             |
  14.          |-------------|-------------|-------------|
  15.          |             |             |             |
  16.          | N[h+1][w-1] | N[h+1][w-1] | N[h+1][w+1] |
  17.          |             |             |             |
  18.          +-----------------------------------------+
  19. */
  20. reduce(height, width, image, alt_img, 1);
  21. reduce(height, width, image, alt_img, 0);
  22. free(alt_img);
  23. }
  24.  
  25. //loop over image, execute radius for each pixel, dumb idea but it look neat.
  26. void reduce(int height, int width, RGBTRIPLE (*pixel)[width], RGBTRIPLE (*alt_pixel)[width], int alt)
  27. {
  28. for (int h = 0; h < height; h++)
  29.     {
  30.         for (int w = 0; w < width; w++)
  31.         {
  32.           if (alt)
  33.           {
  34.              radius(h, w, height, width, pixel);
  35.           }
  36.           else
  37.           {
  38.               pixel[h][w] = alt_pixel[h][w];
  39.           }
  40.         }
  41.     }
  42. }
  43.  
  44. void radius(int h, int w, int height, int width, RGBTRIPLE (*pixel)[width])
  45. {
  46.     int rad_h;
  47.     int rad_w;
  48.  
  49.     int total[] = {pixel[h][w].rgbtRed,pixel[h][w].rgbtGreen,pixel[h][w].rgbtBlue};
  50.     int n = 0;
  51.     //select pixels in 3x3 area.
  52.     for (int i = 0; i < 3; i++)
  53.     {
  54.         for (int j = 0; j < 3; j++)
  55.         {
  56.         rad_h = h + (i - 1);
  57.         rad_w = w + (j - 1);
  58.         //if rad_w,rad_h is not out of bound, increase total.
  59.             if (rad_w >= 0 && rad_h >= 0)
  60.             {
  61.                 if(rad_h < height && rad_w < width)
  62.                 {
  63.                     n++;
  64.                     total[0] += pixel[rad_h][rad_w].rgbtRed;
  65.                     total[1] += pixel[rad_h][rad_w].rgbtGreen;
  66.                     total[2] += pixel[rad_h][rad_w].rgbtBlue;
  67.                 }
  68.             }
  69.         }
  70.     }
  71.     //get avg value of each pixel's color and convert to it BYTE
  72.     for (int k = 0; k < 3; k++)
  73.     {
  74.         total[k] = (BYTE) round(total[k]/n);
  75.     }
  76.     pixel[h][w].rgbtRed = total[0];
  77.     pixel[h][w].rgbtGreen = total[1];
  78.     pixel[h][w].rgbtBlue = total[2];
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement