Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void blur(int height, int width, RGBTRIPLE image[height][width])
- {
- RGBTRIPLE(*alt_img)[width] = calloc(height, width * sizeof(RGBTRIPLE));
- //3x3 radius
- /* +-----------------------------------------+
- | | | |
- | N[h-1][w-1] | N[h-1][w] | N[h-1][w+1] |
- | | | |
- |-------------|-------------|-------------|
- | | | |
- | N[h][w-1] | N[h][w] | N[h][w+1] |
- | | | |
- |-------------|-------------|-------------|
- | | | |
- | N[h+1][w-1] | N[h+1][w-1] | N[h+1][w+1] |
- | | | |
- +-----------------------------------------+
- */
- reduce(height, width, image, alt_img, 1);
- reduce(height, width, image, alt_img, 0);
- free(alt_img);
- }
- //loop over image, execute radius for each pixel, dumb idea but it look neat.
- void reduce(int height, int width, RGBTRIPLE (*pixel)[width], RGBTRIPLE (*alt_pixel)[width], int alt)
- {
- for (int h = 0; h < height; h++)
- {
- for (int w = 0; w < width; w++)
- {
- if (alt)
- {
- radius(h, w, height, width, pixel);
- }
- else
- {
- pixel[h][w] = alt_pixel[h][w];
- }
- }
- }
- }
- void radius(int h, int w, int height, int width, RGBTRIPLE (*pixel)[width])
- {
- int rad_h;
- int rad_w;
- int total[] = {pixel[h][w].rgbtRed,pixel[h][w].rgbtGreen,pixel[h][w].rgbtBlue};
- int n = 0;
- //select pixels in 3x3 area.
- for (int i = 0; i < 3; i++)
- {
- for (int j = 0; j < 3; j++)
- {
- rad_h = h + (i - 1);
- rad_w = w + (j - 1);
- //if rad_w,rad_h is not out of bound, increase total.
- if (rad_w >= 0 && rad_h >= 0)
- {
- if(rad_h < height && rad_w < width)
- {
- n++;
- total[0] += pixel[rad_h][rad_w].rgbtRed;
- total[1] += pixel[rad_h][rad_w].rgbtGreen;
- total[2] += pixel[rad_h][rad_w].rgbtBlue;
- }
- }
- }
- }
- //get avg value of each pixel's color and convert to it BYTE
- for (int k = 0; k < 3; k++)
- {
- total[k] = (BYTE) round(total[k]/n);
- }
- pixel[h][w].rgbtRed = total[0];
- pixel[h][w].rgbtGreen = total[1];
- pixel[h][w].rgbtBlue = total[2];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement