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
| #include "helpers.h"
#include <math.h>
#include <string.h>
// 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++)
{
float pixel = (float)(image[i][j].rgbtRed + image[i][j].rgbtGreen + image[i][j].rgbtBlue) / 3;
pixel = roundf(pixel);
image[i][j].rgbtRed = pixel;
image[i][j].rgbtGreen = pixel;
image[i][j].rgbtBlue = pixel;
}
}
return;
}
// Reflect image horizontally
void reflect(int height, int width, RGBTRIPLE image[height][width])
{
for (int i = 0; i < height; i++)
{
RGBTRIPLE aux[width];
memcpy(aux, image[i], sizeof(RGBTRIPLE) * width);
for (int j = 0; j < width; j++)
{
image[i][j].rgbtRed = aux[width - j - 1].rgbtRed;
image[i][j].rgbtGreen = aux[width - j - 1].rgbtGreen;
image[i][j].rgbtBlue = aux[width - j - 1].rgbtBlue;
}
}
return;
}
// Blur image
void blur(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE aux[height][width];
memcpy(aux, image, sizeof(RGBTRIPLE) * height * width);
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int r = 0, g = 0, b = 0;
float pixel_count = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
if (!(i + k < 0 || i + k >= height || j + l < 0 || j + l >= width))
{
r += aux[i + k][j + l].rgbtRed;
g += aux[i + k][j + l].rgbtGreen;
b += aux[i + k][j + l].rgbtBlue;
pixel_count++;
}
}
}
float pixel_blur_r = roundf((float)r / pixel_count);
float pixel_blur_g = roundf((float)g / pixel_count);
float pixel_blur_b = roundf((float)b / pixel_count);
image[i][j].rgbtRed = pixel_blur_r;
image[i][j].rgbtGreen = pixel_blur_g;
image[i][j].rgbtBlue = pixel_blur_b;
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
RGBTRIPLE aux[height][width];
memcpy(aux, image, sizeof(RGBTRIPLE) * height * width);
int g_x[3][3] =
{
{-1, 0, 1},
{-2, 0, 2},
{-1, 0, 1}
};
int g_y[3][3] =
{
{-1, -2, -1},
{0, 0, 0},
{1, 2, 1}
};
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
int rx = 0, gx = 0, bx = 0;
int ry = 0, gy = 0, by = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
if (!(i + k < 0 || i + k >= height || j + l < 0 || j + l >= width))
{
rx += aux[i + k][j + l].rgbtRed * g_x[k + 1][l + 1];
gx += aux[i + k][j + l].rgbtGreen * g_x[k + 1][l + 1];
bx += aux[i + k][j + l].rgbtBlue * g_x[k + 1][l + 1];
ry += aux[i + k][j + l].rgbtRed * g_y[k + 1][l + 1];
gy += aux[i + k][j + l].rgbtGreen * g_y[k + 1][l + 1];
by += aux[i + k][j + l].rgbtBlue * g_y[k + 1][l + 1];
}
}
}
float r = roundf(sqrt((float)(rx * rx + ry * ry)));
float g = roundf(sqrt((float)(gx * gx + gy * gy)));
float b = roundf(sqrt((float)(bx * bx + by * by)));
r = r > 255 ? 255 : r;
g = g > 255 ? 255 : g;
b = b > 255 ? 255 : b;
image[i][j].rgbtRed = r;
image[i][j].rgbtGreen = g;
image[i][j].rgbtBlue = b;
}
}
return;
}
|