-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
105 lines (98 loc) · 3.1 KB
/
helpers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "helpers.h"
#include <math.h>
#include <cs50.h>
// 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 pixel = image[i][j];
int avg = round((pixel.rgbtRed + pixel.rgbtGreen + pixel.rgbtBlue) / 3.0);
image[i][j].rgbtRed = image[i][j].rgbtGreen = image[i][j].rgbtBlue = avg;
}
}
}
int cap(int value)
{
return value > 255 ? 255 : value;
}
// 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 pixel = image[i][j];
int originalRed = pixel.rgbtRed;
int originalGreen = pixel.rgbtGreen;
int originalBlue = pixel.rgbtBlue;
image[i][j].rgbtRed = cap(round(0.393 * originalRed + 0.769 * originalGreen + 0.189 * originalBlue));
image[i][j].rgbtGreen = cap(round(0.349 * originalRed + 0.686 * originalGreen + 0.168 * originalBlue));
image[i][j].rgbtBlue = cap(round(0.272 * originalRed + 0.534 * originalGreen + 0.131 * originalBlue));
}
}
}
void swap(RGBTRIPLE *pixel1, RGBTRIPLE *pixel2)
{
RGBTRIPLE temp = *pixel1;
*pixel1 = *pixel2;
*pixel2 = temp;
}
// 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++)
{
swap(&image[i][j], &image[i][width - 1 - j]);
}
}
}
bool is_valid_pixel(int i, int j, int height, int width)
{
return i >= 0 && i < height &&j >= 0 && j < width;
}
RGBTRIPLE get_blurred_pixel(int i, int j, int height, int width, RGBTRIPLE image[height][width])
{
int redValue, blueValue, greenValue; redValue = blueValue = greenValue = 0;
int numOfValidPixels = 0;
for (int di = -1; di <= 1; di++)
{
for (int dj = -1; dj <= 1; dj++)
{
int new_i = i + di;
int new_j = j +dj;
if (is_valid_pixel(new_i, new_j, height, width))
{
numOfValidPixels++;
redValue += image[new_i][new_j].rgbtRed;
blueValue += image[new_i][new_j].rgbtBlue;
greenValue += image[new_i][new_j].rgbtGreen;
}
}
}
RGBTRIPLE blurred_pixel;
blurred_pixel.rgbtRed = round((float) redValue / numOfValidPixels);
blurred_pixel.rgbtGreen = round((float) greenValue / numOfValidPixels);
blurred_pixel.rgbtBlue = round((float) blueValue / numOfValidPixels);
return blurred_pixel;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE new_image [height][width];
for (int i = 0; i <height ; i++)
{
for (int j = 0; j < width; j ++)
{
new_image[i][j] = get_blurred_pixel(i, j, height, width, image);
}
}
for (int i = 0; i < height; i ++)
for (int j = 0; j < width; j++)
image[i][j] = new_image[i][j];
}