-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfilter.c
More file actions
142 lines (122 loc) · 4.44 KB
/
filter.c
File metadata and controls
142 lines (122 loc) · 4.44 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include "helpers.h"
#include <math.h>
// Convert image to grayscale
// Refer to the article below for more information:
// https://cs50.harvard.edu/x/2025/psets/4/filter/less/
// https://tannerhelland.com/2011/10/01/grayscale-image-algorithm-vb6.html
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 red = image[i][j].rgbtRed;
int green = image[i][j].rgbtGreen;
int blue = image[i][j].rgbtBlue;
int gray = round((red + green + blue) / 3.0);
image[i][j].rgbtRed = gray;
image[i][j].rgbtGreen = gray;
image[i][j].rgbtBlue = gray;
}
}
return;
}
// Convert image to sepia
// Refer to these article:
// https://en.wikipedia.org/wiki/Sepia_tone#Sepia_tone_in_photography
// https://stackoverflow.com/questions/1061093/how-is-a-sepia-tone-created
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int originalRed = image[i][j].rgbtRed;
int originalGreen = image[i][j].rgbtGreen;
int originalBlue = image[i][j].rgbtBlue;
int sepiaRed = round(.393 * originalRed + .769 * originalGreen + .189 * originalBlue);
int sepiaGreen = round(.349 * originalRed + .686 * originalGreen + .168 * originalBlue);
int sepiaBlue = round(.272 * originalRed + .534 * originalGreen + .131 * originalBlue);
image[i][j].rgbtRed = sepiaRed > 255 ? 255 : sepiaRed;
image[i][j].rgbtGreen = sepiaGreen > 255 ? 255 : sepiaGreen;
image[i][j].rgbtBlue = sepiaBlue > 255 ? 255 : sepiaBlue;
}
}
return;
}
// Reflect image horizontally (mirror effect)
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 temp = image[i][j];
image[i][j] = image[i][width - 1 - j];
image[i][width - 1 - j] = temp;
}
}
return;
}
// Blur image
// This function averages the color values of each pixel with its neighbors
// Refer to the article below for more information:
// https://cs50.harvard.edu/x/2025/psets/4/filter/less/
// https://en.wikipedia.org/wiki/Box_blur
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 redSum = 0, greenSum = 0, blueSum = 0;
int count = 0;
for (int di = -1; di <= 1; di++)
{
for (int dj = -1; dj <= 1; dj++)
{
int newI = i + di;
int newJ = j + dj;
if (newI >= 0 && newI < height && newJ >= 0 && newJ < width)
{
redSum += copy[newI][newJ].rgbtRed;
greenSum += copy[newI][newJ].rgbtGreen;
blueSum += copy[newI][newJ].rgbtBlue;
count++;
}
}
}
image[i][j].rgbtRed = round(redSum / (float) count);
image[i][j].rgbtGreen = round(greenSum / (float) count);
image[i][j].rgbtBlue = round(blueSum / (float) count);
}
}
return;
}
// Brighten image
// This function increases the brightness of each pixel by a fixed amount (50 in this case)
// Refer to the article below for more information:
// https://ie.nitk.ac.in/blog/2020/01/19/algorithms-for-adjusting-brightness-and-contrast-of-an-image/#:~:text=To%20change%20the%20brightness%20of,every%20pixel%20of%20the%20image.
void brighten(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int red = image[i][j].rgbtRed + 50;
int green = image[i][j].rgbtGreen + 50;
int blue = image[i][j].rgbtBlue + 50;
image[i][j].rgbtRed = red > 255 ? 255 : red;
image[i][j].rgbtGreen = green > 255 ? 255 : green;
image[i][j].rgbtBlue = blue > 255 ? 255 : blue;
}
}
}