-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.c
109 lines (98 loc) · 3.48 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
106
107
108
109
#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++)
{
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;
}
}
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
// Each column
for (int j = 0; j < width; j++)
{
// Pixels to float
float originalRed = image[i][j].rgbtRed;
float originalGreen = image[i][j].rgbtGreen;
float originalBlue = image[i][j].rgbtBlue;
// Update pixel value
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);
// Update pixel value if sepiaRed, sepiaGreen, sepiaBlue are greater than 255
image[i][j].rgbtRed = (sepiaRed > 255) ? 255 : sepiaRed;
image[i][j].rgbtGreen = (sepiaGreen > 255) ? 255 : sepiaGreen;
image[i][j].rgbtBlue = (sepiaBlue > 255) ? 255 : sepiaBlue;
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
// 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;
}
}
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
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;
// Neighboring pixels value
for (int x = -1; x < 2; x++)
{
for (int y = -1; y < 2; y++)
{
int currentX = i + x;
int currentY = j + y;
// Neighboring pixels are 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 the average of neighboring values and assign it to the image
image[i][j].rgbtRed = round(totalRed / counter);
image[i][j].rgbtGreen = round(totalGreen / counter);
image[i][j].rgbtBlue = round(totalBlue / counter);
}
}
}