Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "helpers.h"
- #include "math.h"
- int new_blur_pix(RGBTRIPLE *copy);
- int round_triple(int a, int b, int c);
- // 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++)
- {
- RGBTRIPLE tmp = image[i][j];
- int tmp_color = round_triple(tmp.rgbtBlue, tmp.rgbtGreen, tmp.rgbtRed);
- tmp.rgbtBlue = tmp_color;
- tmp.rgbtGreen = tmp_color;
- tmp.rgbtRed = tmp_color;
- image[i][j] = tmp;
- }
- }
- return;
- }
- // Convert image to sepia
- void sepia(int height, int width, RGBTRIPLE image[height][width])
- {
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- RGBTRIPLE tmp = image[i][j];
- int red = image[i][j].rgbtRed;
- int green = image[i][j].rgbtGreen;
- int blue = image[i][j].rgbtBlue;
- tmp.rgbtRed = ((round(0.393f * image[i][j].rgbtRed + 0.769f * image[i][j].rgbtGreen + 0.189f * image[i][j].rgbtBlue)) < 255) ? round(0.393f * image[i][j].rgbtRed + 0.769f * image[i][j].rgbtGreen + 0.189f * image[i][j].rgbtBlue) : 255;
- tmp.rgbtGreen = ((round(0.349 * image[i][j].rgbtRed + 0.686 * image[i][j].rgbtGreen + 0.168 * image[i][j].rgbtBlue)) < 255) ? round(0.349 * image[i][j].rgbtRed + 0.686 * image[i][j].rgbtGreen + 0.168 * image[i][j].rgbtBlue) : 255;
- tmp.rgbtBlue = ((round(0.272 * image[i][j].rgbtRed + 0.534 * image[i][j].rgbtGreen + 0.131 * image[i][j].rgbtBlue)) < 255) ? round(0.272 * image[i][j].rgbtRed + 0.534 * image[i][j].rgbtGreen + 0.131 * image[i][j].rgbtBlue) : 255;
- image[i][j] = tmp;
- }
- }
- 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 < width / 2; j++)
- {
- RGBTRIPLE tmp = image[i][j];
- image[i][j] = image[i][width - 1 - j];
- image[i][width - 1 - j] = tmp;
- }
- }
- return;
- }
- // Blur image
- void blur(int height, int width, RGBTRIPLE image[height][width])
- {
- RGBTRIPLE copy[height][width];
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- copy[i][j] = image[i][j];
- }
- }
- for (int i = 0; i < height; i++)
- {
- for (int j = 0; j < width; j++)
- {
- int averageRed = 0;
- int averageGreen = 0;
- int averageBlue = 0;
- // Keep track of pixels added in case of edges
- int pixels_count = 0;
- //Iterate over every index of box of size 3x3
- for (int k = 0; k < 9; k++)
- {
- int box_height = i + (k % 3) - 1;
- int box_width = j + (k / 3) - 1;
- if ((box_height >= 0 && box_width >= 0) && (box_height < height && box_width < width))
- {
- averageRed += copy[box_height][box_width].rgbtRed;
- averageGreen += copy[box_height][box_width].rgbtGreen;
- averageBlue += copy[box_height][box_width].rgbtBlue;
- pixels_count++;
- }
- }
- image[i][j].rgbtRed = round((double)averageRed / pixels_count);
- image[i][j].rgbtGreen = round((double)averageGreen / pixels_count);
- image[i][j].rgbtBlue = round((double)averageBlue / pixels_count);
- }
- }
- return;
- }
- int round_triple(int a, int b, int c)
- {
- float rounded = ((double)a + b + c) / 3;
- return rounded + 0.5f;
- }
Advertisement
Add Comment
Please, Sign In to add comment