-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclose-open.cpp
202 lines (183 loc) · 10.3 KB
/
close-open.cpp
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#include <iostream>
#include <algorithm>
#include <cassert>
#include "include/bitmapRW.h"
#include "border_checker.h"
//n = 1 for 3x3
//n = 2 for 5x5
//n = 3 for 7x7
typedef struct coords {
int64_t x, y;
} coords_t;
void erode(byte *pixels, byte *pixels_eroded, uint32_t width, uint32_t height, uint32_t bytesPerPixel, signed short n) {
for (int64_t i = 0; i < width * height * bytesPerPixel; i += bytesPerPixel) {
byte chunk_size = (1+2*n)*(1+2*n);
byte chunk[chunk_size];
border_t border = checkBorder(i, (int)width, (int)height);
chunk[0] = pixels[i];
for (int8_t j = -n; j < n; j++) {
}
// byte chunk[9];
// border_t border = checkBorder(i, (int)width, (int)height);
// chunk[0] = pixels[i];
// //on the L and R sides
// chunk[1] = (border.widthBorderDistance != -1) ? pixels[i - bytesPerPixel] : 0;
// chunk[2] = (border.widthBorderDistance != 1) ? pixels[i + bytesPerPixel] : 0;
// //on the U and D sides
// chunk[3] = (border.heightBorderDistance != -1) ? pixels[i + bytesPerPixel * width] : 0;
// chunk[4] = (border.heightBorderDistance != 1) ? pixels[i - bytesPerPixel * width] : 0;
// //upper corners
// chunk[5] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != 1)) ? pixels[i - bytesPerPixel * width + bytesPerPixel] : 0;
// chunk[6] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != -1)) ? pixels[i - bytesPerPixel * width - bytesPerPixel] : 0;
// //bottom corners
// chunk[7] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != 1)) ? pixels[i + bytesPerPixel * width + bytesPerPixel] : 0;
// chunk[8] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != -1)) ? pixels[i + bytesPerPixel * width - bytesPerPixel] : 0;
//
//erosion
bool flag = false;
for (byte q = 0; q < n; q++) {
if (chunk[q] == 0xff) flag = true;
else {
flag = false;
break;
}
}
if (flag) {
pixels_eroded[i] = 0xff;
pixels_eroded[i+1] = 0xff;
pixels_eroded[i+2] = 0xff;
}
}
}
void dilatate(byte *pixels, byte *pixels_dilatated, uint32_t width, uint32_t height, uint32_t bytesPerPixel, signed short n) {
// for (int64_t i = 0; i < width * height * bytesPerPixel; i += bytesPerPixel) {
// if (pixels[i]) {
// border_t border = checkBorder(i, (int) width, (int) height);
// for (short j = 0; j < 3; j++) {
// pixels_dilatated[i+j] = 0xff;
//
// //LR sides
// pixels_dilatated[i+j - bytesPerPixel] = (border.widthBorderDistance != -1) ? 0xff : 0;
// pixels_dilatated[i+j + bytesPerPixel] = (border.widthBorderDistance != 1) ? 0xff : 0;
// //on the U and D sides
// pixels_dilatated[i+j + bytesPerPixel * width] = (border.heightBorderDistance != -1) ? 0xff : 0;
// pixels_dilatated[i+j - bytesPerPixel * width] = (border.heightBorderDistance != 1) ? 0xff : 0;
// //upper corners
// pixels_dilatated[i+j - bytesPerPixel * width + bytesPerPixel] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != 1)) ? 0xff : 0;
// pixels_dilatated[i+j - bytesPerPixel * width - bytesPerPixel] = ((border.heightBorderDistance != 1) && (border.widthBorderDistance != -1)) ? 0xff : 0;
// //bottom corners
// pixels_dilatated[i+j + bytesPerPixel * width + bytesPerPixel] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != 1)) ? 0xff : 0;
// pixels_dilatated[i+j + bytesPerPixel * width - bytesPerPixel] = ((border.heightBorderDistance != -1) && (border.widthBorderDistance != -1)) ? 0xff : 0;
// }
// }
// }
}
int main(int argc, char *argv[])
{
byte* pixels; //array for data to be read from image
byte* pixels_eroded; //array for data to be written to
byte* pixels_dilatated;
int32_t width;
int32_t height;
uint32_t bytesPerPixel;
char* input = argv[1];
char* output = argv[2];
clock_t start, end;
start = clock();
ReadImage(input, &pixels, &width, &height, &bytesPerPixel);
pixels_eroded = (byte*)malloc(width * height * bytesPerPixel);
pixels_dilatated = (byte*)malloc(width * height * bytesPerPixel);
for (int64_t i = 0; i < width * height * bytesPerPixel; i++) pixels_eroded[i] = 0;
for (int64_t i = 0; i < width * height * bytesPerPixel; i++) pixels_dilatated[i] = 0;
erode(pixels, pixels_eroded, width, height, bytesPerPixel, 1);
dilatate(pixels_eroded, pixels_dilatated, width, height, bytesPerPixel, 1);
WriteImage(output, pixels_dilatated, width, height, bytesPerPixel);
end = clock();
double runtime = double(end - start) / double(CLOCKS_PER_SEC);
printf("%f\n", runtime);
free(pixels);
free(pixels_dilatated);
free(pixels_eroded);
}
//
//chunk[0] = (border.widthBorder != Left) ? pixels[i - bytesPerPixel] : 0;
//chunk[1] = (border.widthBorder != Left2) ? pixels[i - 2*bytesPerPixel] : 0;
//chunk[2] = (border.widthBorder != Right) ? pixels[i + bytesPerPixel] : 0;
//chunk[3] = (border.widthBorder != Right2) ? pixels[i + 2*bytesPerPixel] : 0;
////UD
//chunk[4] = (border.heightBorder != Dwn) ? pixels[i + bytesPerPixel * width] : 0;
//chunk[5] = (border.heightBorder != Up) ? pixels[i - bytesPerPixel * width] : 0;
//chunk[6] = (border.heightBorder != Dwn2) && (border.heightBorder != Dwn) ? pixels[i+ 2 * bytesPerPixel * width] : 0;
//chunk[7] = (border.heightBorder != Up2) && (border.heightBorder != Up) ? pixels[i - 2 * bytesPerPixel * width] : 0;
//
//chunk[8] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Right2)) ? pixels[i - bytesPerPixel * width + bytesPerPixel] : 0;
//chunk[9] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Right2)) ? pixels[i - bytesPerPixel * 2*width + 2*bytesPerPixel] : 0;
//chunk[10] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Right2)) ? pixels[i - bytesPerPixel * 2 * width + bytesPerPixel] : 0;
//chunk[11] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Right2)) ? pixels[i - bytesPerPixel * width + 2*bytesPerPixel] : 0;
//
//chunk[12] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Left2)) ? pixels[i - bytesPerPixel * width - bytesPerPixel] : 0;
//chunk[13] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Left2)) ? pixels[i - bytesPerPixel * width -2* bytesPerPixel] : 0;
//chunk[14] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Left2)) ? pixels[i - 2*bytesPerPixel * width - bytesPerPixel] : 0;
//chunk[15] = ((border.heightBorder != Up2) && (border.heightBorder != Up) && (border.widthBorder != Left2)) ? pixels[i - 2*bytesPerPixel * width - 2*bytesPerPixel] : 0;
//
//chunk[16] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Right2)) ? pixels[i + bytesPerPixel * width + bytesPerPixel] : 0;
//chunk[17] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Right2)) ? pixels[i + bytesPerPixel * width + 2*bytesPerPixel] : 0;
//chunk[18] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Right2)) ? pixels[i + 2*bytesPerPixel * width + bytesPerPixel] : 0;
//chunk[19] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Right2)) ? pixels[i + 2*bytesPerPixel * width + 2*bytesPerPixel] : 0;
//
//chunk[20] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Left2)) ? pixels[i + bytesPerPixel * width - bytesPerPixel] : 0;
//chunk[21] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Left2)) ? pixels[i + bytesPerPixel * width - 2*bytesPerPixel] : 0;
//chunk[22] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Left2)) ? pixels[i + 2*bytesPerPixel * width - bytesPerPixel] : 0;
//chunk[23] = ((border.heightBorder != Dwn2) && (border.heightBorder != Dwn) && (border.widthBorder != Left2)) ? pixels[i + 2*bytesPerPixel * width - 2*bytesPerPixel] : 0;
//
//for (int64_t j = 0; j <= 23; j++)
//{
//if (chunk[j] == 0)
//{
//pixels_prefiltered[i] = 0;
//pixels_prefiltered[i + 1] = 0;
//pixels_prefiltered[i + 2] = 0;
//}
//}
//
//}
//
//for (int64_t i = 0; i < width * height * bytesPerPixel; i += bytesPerPixel) {
//if (pixels_prefiltered[i] != 0)
//{
//for (int64_t j = i; j <= i + 2; j++)
//{
//pixels_filtered[j] = 0xff;
//
//pixels_filtered[j - bytesPerPixel] = 0xff;
//pixels_filtered[j - 2 * bytesPerPixel] = 0xff;
//pixels_filtered[j + bytesPerPixel] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel] = 0xff;
//
//
//pixels_filtered[j + bytesPerPixel * width] = 0xff;
//pixels_filtered[j - bytesPerPixel * width] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel * width] = 0xff;
//pixels_filtered[j - 2*bytesPerPixel * width] = 0xff;
//
//pixels_filtered[j - bytesPerPixel * width + bytesPerPixel] = 0xff;
//pixels_filtered[j - bytesPerPixel * width + 2*bytesPerPixel] = 0xff;
//pixels_filtered[j - 2*bytesPerPixel * width + bytesPerPixel] = 0xff;
//pixels_filtered[j - 2*bytesPerPixel * width + 2*bytesPerPixel] = 0xff;
//
//pixels_filtered[j - bytesPerPixel * width - bytesPerPixel] = 0xff;
//pixels_filtered[j - bytesPerPixel * width - 2*bytesPerPixel] = 0xff;
//pixels_filtered[j - 2*bytesPerPixel * width - bytesPerPixel] = 0xff;
//pixels_filtered[j - 2*bytesPerPixel * width - 2*bytesPerPixel] = 0xff;
//
//pixels_filtered[j + bytesPerPixel * width + bytesPerPixel] = 0xff;
//pixels_filtered[j + bytesPerPixel * width + 2*bytesPerPixel] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel * width + bytesPerPixel] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel * width + 2*bytesPerPixel] = 0xff;
//
//pixels_filtered[j + bytesPerPixel * width - bytesPerPixel] = 0xff;
//pixels_filtered[j + bytesPerPixel * width - 2*bytesPerPixel] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel * width - bytesPerPixel] = 0xff;
//pixels_filtered[j + 2*bytesPerPixel * width - 2*bytesPerPixel] = 0xff;
//}
//}