-
Notifications
You must be signed in to change notification settings - Fork 359
/
helpers.c
131 lines (115 loc) · 3.73 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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// inside this file there are all the functions that get used to apply a filter to a picture
#include "helpers.h"
#include <math.h>
// Convert image to grayscale
void grayscale(int height, int width, RGBTRIPLE image[height][width])
{
float rgbGray;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
// averages the color intensity and then applies the same value to all the colors to get gray
rgbGray = round((image[j][i].rgbtBlue + image[j][i].rgbtGreen + image[j][i].rgbtRed) / 3.000);
image[j][i].rgbtBlue = rgbGray;
image[j][i].rgbtGreen = rgbGray;
image[j][i].rgbtRed = rgbGray;
}
}
}
// stops max value at 255 preventing overflow
int limit(int RGB)
{
if (RGB > 255)
{
RGB = 255;
}
return RGB;
}
// Convert image to sepia
void sepia(int height, int width, RGBTRIPLE image[height][width])
{
int sepiaBlue;
int sepiaRed;
int sepiaGreen;
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
sepiaBlue = limit(round(0.272 * image[j][i].rgbtRed + 0.534 * image[j][i].rgbtGreen + 0.131 * image[j][i].rgbtBlue));
sepiaGreen = limit(round(0.349 * image[j][i].rgbtRed + 0.686 * image[j][i].rgbtGreen + 0.168 * image[j][i].rgbtBlue));
sepiaRed = limit(round(0.393 * image[j][i].rgbtRed + 0.769 * image[j][i].rgbtGreen + 0.189 * image[j][i].rgbtBlue));
image[j][i].rgbtBlue = sepiaBlue;
image[j][i].rgbtGreen = sepiaGreen;
image[j][i].rgbtRed = sepiaRed;
}
}
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
//use of a temporary array to swap values
RGBTRIPLE temp;
for (int j = 0; j < width / 2; j++)
{
for (int i = 0; i < height; i++)
{
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])
{
int sumBlue;
int sumGreen;
int sumRed;
float counter;
//create a temporary table of colors to not alter the calculations
RGBTRIPLE temp[height][width];
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
sumBlue = 0;
sumGreen = 0;
sumRed = 0;
counter = 0.00;
// sums values of the pixel and 8 neighboring ones, skips iteration if it goes outside the pic
for (int k = -1; k < 2; k++)
{
if (j + k < 0 || j + k > height - 1)
{
continue;
}
for (int h = -1; h < 2; h++)
{
if (i + h < 0 || i + h > width - 1)
{
continue;
}
sumBlue += image[j + k][i + h].rgbtBlue;
sumGreen += image[j + k][i + h].rgbtGreen;
sumRed += image[j + k][i + h].rgbtRed;
counter++;
}
}
// averages the sum to make picture look blurrier
temp[j][i].rgbtBlue = round(sumBlue / counter);
temp[j][i].rgbtGreen = round(sumGreen / counter);
temp[j][i].rgbtRed = round(sumRed / counter);
}
}
//copies values from temporary table
for (int i = 0; i < width; i++)
{
for (int j = 0; j < height; j++)
{
image[j][i].rgbtBlue = temp[j][i].rgbtBlue;
image[j][i].rgbtGreen = temp[j][i].rgbtGreen;
image[j][i].rgbtRed = temp[j][i].rgbtRed;
}
}
}