Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "helpers.h"
- #include <math.h>
- #include <stdlib.h>
- #include <stdio.h>
- void sum(int *b_avg, int *g_avg, int *r_avg, RGBTRIPLE pixel);
- // Convert image to grayscale
- void grayscale(int height, int width, RGBTRIPLE image[height][width])
- {
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- int avg = round((image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3.0);
- image[i][j].rgbtBlue = avg;
- image[i][j].rgbtGreen = avg;
- image[i][j].rgbtRed = avg;
- }
- }
- return;
- }
- // Reflect image horizontally
- void reflect(int height, int width, RGBTRIPLE image[height][width])
- {
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < round(width / 2); j++)
- {
- // swap j with width - j
- RGBTRIPLE temp = image[i][j];
- image[i][j] = image[i][width - (j + 1)];
- image[i][width - (j + 1)] = temp;
- }
- }
- return;
- }
- // Blur image
- void blur(int height, int width, RGBTRIPLE image[height][width])
- {
- RGBTRIPLE(*image_cp)[width] = calloc(height, width * sizeof(RGBTRIPLE));
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- image_cp[i][j] = image[i][j];
- }
- }
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- int b_avg, g_avg, r_avg = 0;
- int pixel_count = 0;
- for (int c = -1; c < 2; c++)
- {
- if (i + c < 0 || i + c >= height)
- {
- continue;
- }
- for (int r = -1; r < 2; r++)
- {
- if (j + r < 0 || j + r >= width)
- {
- continue;
- }
- sum(&b_avg, &g_avg, &r_avg, image_cp[i + c][j + r]);
- pixel_count++;
- }
- }
- RGBTRIPLE avg;
- avg.rgbtBlue = b_avg / pixel_count;
- avg.rgbtGreen = g_avg / pixel_count;
- avg.rgbtRed = r_avg / pixel_count;
- image[i][j] = avg;
- }
- }
- free(image_cp);
- return;
- }
- // Detect edges
- void edges(int height, int width, RGBTRIPLE image[height][width])
- {
- return;
- }
- // adds rgb triple to averages
- void sum(int *b_avg, int *g_avg, int *r_avg, RGBTRIPLE pixel)
- {
- *b_avg += pixel.rgbtBlue;
- *g_avg += pixel.rgbtGreen;
- *r_avg += pixel.rgbtRed;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement