-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.c
More file actions
135 lines (121 loc) · 4 KB
/
helpers.c
File metadata and controls
135 lines (121 loc) · 4 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
#include <helpers.h>
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
// comb through each row
for (int i = 0; i < height; i++)
{
// comb through each column
{
for (int j = 0; j < width; j++)
{
// convert pixels to float
float Red = image[i][j].rgbtRed;
float Green = image[i][j].rgbtGreen;
float Blue = image[i][j].rgbtBlue;
// Find the average value
int average = round((Red + Green + Blue) / 3);
image[i][j].rgbtRed = image[i][j].rgbtGreen = image[i][j].rgbtBlue = average;
}
}
}
return;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
// Loop through each row
for (int i = 0; i < height; i++)
{
// Loop through each column
for (int j = 0; j < width; j++)
{
// Convert pixels to float for more accurate calculations
float originalRed = image[i][j].rgbtRed;
float originalGreen = image[i][j].rgbtGreen;
float originalBlue = image[i][j].rgbtBlue;
// Calculate sepia values
int sepiaRed = round(0.393 * originalRed + 0.769 * originalGreen + 0.189 * originalBlue);
int sepiaGreen = round(0.349 * originalRed + 0.686 * originalGreen + 0.168 * originalBlue);
int sepiaBlue = round(0.272 * originalRed + 0.534 * originalGreen + 0.131 * originalBlue);
// Cap values at 255 if they exceed
if (sepiaRed > 255)
{
sepiaRed = 255;
}
if (sepiaGreen > 255)
{
sepiaGreen = 255;
}
if (sepiaBlue > 255)
{
sepiaBlue = 255;
}
// Update image with sepia values
image[i][j].rgbtRed = sepiaRed;
image[i][j].rgbtGreen = sepiaGreen;
image[i][j].rgbtBlue = sepiaBlue;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
// comb through each row
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 - (j + 1)];
image[i][width - (j + 1)] = temp;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
// Create a temporary copy of the image
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
// Loop through each pixel
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int totalRed = 0, totalGreen = 0, totalBlue = 0;
float counter = 0.00;
// Loop through neighboring pixels
for (int x = -1; x <= 1; x++)
{
for (int y = -1; y <= 1; y++)
{
int currentX = i + x;
int currentY = j + y;
// Check if neighboring pixel is valid
if (currentX >= 0 && currentX < height && currentY >= 0 && currentY < width)
{
totalRed += temp[currentX][currentY].rgbtRed;
totalGreen += temp[currentX][currentY].rgbtGreen;
totalBlue += temp[currentX][currentY].rgbtBlue;
counter++;
}
}
}
// Calculate average value for each channel and update image
image[i][j].rgbtRed = round(totalRed / counter);
image[i][j].rgbtGreen = round(totalGreen / counter);
image[i][j].rgbtBlue = round(totalBlue / counter);
}
}
return;
}