-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpatch.c
More file actions
165 lines (159 loc) · 5.04 KB
/
Copy pathpatch.c
File metadata and controls
165 lines (159 loc) · 5.04 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "patch.h"
#include <math.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++)
{
// Get the average of the pixel's RGB values
int avg = round(((double)image[i][j].rgbtRed
+ (double)image[i][j].rgbtGreen
+ (double)image[i][j].rgbtBlue) / 3);
// Set the grayscale value to the RGB values of the pixel
image[i][j].rgbtRed = avg;
image[i][j].rgbtGreen = avg;
image[i][j].rgbtBlue = 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; 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 temp array
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++)
{
float sum_red;
float sum_blue;
float sum_green;
int counter;
sum_red = sum_blue = sum_green = counter = 0;
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
if (i + k < 0 || i + k >= height)
{
continue;
}
if (j + l < 0 || j + l >= width)
{
continue;
}
sum_red += temp[i + k][j + l].rgbtRed;
sum_blue += temp[i + k][j + l].rgbtBlue;
sum_green += temp[i + k][j + l].rgbtGreen;
counter++;
}
}
image[i][j].rgbtRed = round(sum_red / counter);
image[i][j].rgbtGreen = round(sum_green / counter);
image[i][j].rgbtBlue = round(sum_blue / counter);
}
}
return;
}
// Detect edges
void edges(int height, int width, RGBTRIPLE image[height][width])
{
// Create temp array
RGBTRIPLE temp[height][width];
for (int i = 0; i < height; i++)
{
for (int j = 0; j < width; j++)
{
temp[i][j] = image[i][j];
}
}
// Initialise Sobel arrays
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}};
// Loop through rows
for (int i = 0; i < height; i++)
{
// Loop through columns
for (int j = 0; j < width; j++)
{
// Initialise ints
float Gx_red;
float Gx_blue;
float Gx_green;
float Gy_red;
float Gy_blue;
float Gy_green;
Gx_red = Gx_blue = Gx_green = Gy_red = Gy_blue = Gy_green = 0;
// For each pixel, loop vertical and horizontal
for (int k = -1; k < 2; k++)
{
for (int l = -1; l < 2; l++)
{
// Check if pixel is outside rows
if (i + k < 0 || i + k >= height)
{
continue;
}
// Check if pixel is outside columns
if (j + l < 0 || j + l >= width)
{
continue;
}
// Otherwise add to sums
Gx_red += temp[i + k][j + l].rgbtRed * Gx[k + 1][l + 1];
Gx_green += temp[i + k][j + l].rgbtGreen * Gx[k + 1][l + 1];
Gx_blue += temp[i + k][j + l].rgbtBlue * Gx[k + 1][l + 1];
Gy_red += temp[i + k][j + l].rgbtRed * Gy[k + 1][l + 1];
Gy_green += temp[i + k][j + l].rgbtGreen * Gy[k + 1][l + 1];
Gy_blue += temp[i + k][j + l].rgbtBlue * Gy[k + 1][l + 1];
}
}
// Calculate Sobel operator
int red = round(sqrt(Gx_red * Gx_red + Gy_red * Gy_red));
int green = round(sqrt(Gx_green * Gx_green + Gy_green * Gy_green));
int blue = round(sqrt(Gx_blue * Gx_blue + Gy_blue * Gy_blue));
// Cap at 255
if (red > 255)
{
red = 255;
}
if (green > 255)
{
green = 255;
}
if (blue > 255)
{
blue = 255;
}
// Assign new values to pixels
image[i][j].rgbtRed = red;
image[i][j].rgbtGreen = green;
image[i][j].rgbtBlue = blue;
}
}
return;
}