Repository navigation
Expand file tree
/
Copy pathhelpers.c
More file actions
150 lines (141 loc) · 4.41 KB
/
Copy pathhelpers.c
File metadata and controls
150 lines (141 loc) · 4.41 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
143
144
145
146
147
148
149
150
#include "helpers.h"
#include "math.h"
#include "stdio.h"
typedef uint8_t BYTE;
int check_in_scope(int value, int min, int max);
// 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++)
{
int avg = (int) round(
(image[i][j].rgbtBlue + image[i][j].rgbtGreen + image[i][j].rgbtRed) / 3.0);
image[i][j].rgbtBlue = image[i][j].rgbtGreen = image[i][j].rgbtRed = avg;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
for (int j = 0, half = width / 2; j < half; j++)
{
int backward_index = width - 1 - j;
RGBTRIPLE temp = {image[i][j].rgbtBlue, image[i][j].rgbtGreen, image[i][j].rgbtRed};
image[i][j] =
(RGBTRIPLE){image[i][backward_index].rgbtBlue, image[i][backward_index].rgbtGreen,
image[i][backward_index].rgbtRed};
image[i][backward_index] = temp;
}
}
return;
}
// Blur image
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++)
{
int sum_blue = 0;
int sum_green = 0;
int sum_red = 0;
int q = 0;
for (int di = -1; di < 2; di++)
{
for (int dj = -1; dj < 2; dj++)
{
int ni = i + di;
int nj = j + dj;
if (ni >= 0 && ni < height && nj >= 0 && nj < width)
{
sum_blue += image[i + di][j + dj].rgbtBlue;
sum_green += image[i + di][j + dj].rgbtGreen;
sum_red += image[i + di][j + dj].rgbtRed;
q += 1;
}
}
}
BYTE avg_blue = (BYTE) round((float) sum_blue / q);
BYTE avg_green = (BYTE) round((float) sum_green / q);
BYTE avg_red = (BYTE) round((float) sum_red / q);
copy[i][j] = (RGBTRIPLE){avg_blue, avg_green, avg_red};
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = copy[i][j];
}
}
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE copy[height][width];
int GX[3][3] = {{-1, 0, 1}, {-2, 0, 2}, {-1, 0, 1}};
int GY[3][3] = {{-1, -2, -1}, {0, 0, 0}, {1, 2, 1}};
int gxb, gxg, gxr, gyb, gyg, gyr;
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
gxb = gxg = gxr = gyb = gyg = gyr = 0;
for (int di = -1; di < 2; di++)
{
for (int dj = -1; dj < 2; dj++)
{
int ni = i + di;
int nj = j + dj;
if (ni < 0 || ni > height - 1)
{
continue;
}
else if (nj < 0 || nj > width - 1)
{
continue;
}
gxb += image[ni][nj].rgbtBlue * GX[di + 1][dj + 1];
gxg += image[ni][nj].rgbtGreen * GX[di + 1][dj + 1];
gxr += image[ni][nj].rgbtRed * GX[di + 1][dj + 1];
gyb += image[ni][nj].rgbtBlue * GY[di + 1][dj + 1];
gyg += image[ni][nj].rgbtGreen * GY[di + 1][dj + 1];
gyr += image[ni][nj].rgbtRed * GY[di + 1][dj + 1];
}
}
int blue = fmin(round(sqrt(gxb * gxb + gyb * gyb)), 255);
int green = fmin(round(sqrt(gxg * gxg + gyg * gyg)), 255);
int red = fmin(round(sqrt(gxr * gxr + gyr * gyr)), 255);
copy[i][j] = (RGBTRIPLE){blue, green, red};
}
}
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
image[i][j] = copy[i][j];
}
}
return;
}
int check_in_scope(int value, int min, int max)
{
if (value < min)
{
return min;
}
else if (value > max)
{
return max;
}
else
{
return value;
}
}