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
|
/* See LICENSE file for copyright and license details. */
#include "libtracebitmap.h"
#include <stdlib.h>
#include <unistd.h>
#define CLEARED 0
#define SET 1
#define NEGATIVE 2
#define STATUS_MASK 0x03
#define TOP 0x10
#define RIGHT 0x20
#define BOTTOM 0x40
#define LEFT 0x80
#define EDGE_MASK 0xF0
#define INDEX(Y, X) ((size_t)(Y) * bitmap->width + (size_t)(X))
static uint8_t
read_pixel(struct libtracebitmap_bitmap *bitmap, ssize_t y, ssize_t x)
{
if (y < 0 || x < 0 || (size_t)y >= bitmap->height || (size_t)x >= bitmap->width)
return 0;
return bitmap->image[INDEX(y, x)];
}
static int
test_pixel(struct libtracebitmap_bitmap *bitmap, ssize_t y, ssize_t x, uint8_t status)
{
return (read_pixel(bitmap, y, x) & STATUS_MASK) == status;
}
static int
find_component(size_t *yp, size_t *xp, int *negativep, struct libtracebitmap_bitmap *bitmap)
{
size_t y, x;
uint8_t status;
for (y = 0; y < bitmap->height; y++) {
for (x = 0; x < bitmap->width; x++) {
status = (bitmap->image[INDEX(y, x)] & STATUS_MASK);
if (status != CLEARED) {
*yp = y;
*xp = x;
*negativep = (status == NEGATIVE);
return 1;
}
}
}
return 0;
}
static int
trace_edges(struct libtracebitmap_bitmap *bitmap, size_t tl_y, size_t tl_x, int negative,
int (*new_stop)(size_t y, size_t x, void *user_data), void *user_data)
{
uint8_t c = (negative ? NEGATIVE : SET);
uint8_t edge = TOP;
ssize_t y = (ssize_t)tl_y;
ssize_t x = (ssize_t)tl_x;
int r;
if ((r = new_stop((size_t)y, (size_t)x, user_data)))
return r;
do {
bitmap->image[INDEX(y, x)] |= edge;
switch (edge) {
case TOP:
if (test_pixel(bitmap, y - 1, x + 1, c)) {
if ((r = new_stop((size_t)y, (size_t)x + 1, user_data)))
return r;
x += 1;
y -= 1;
edge = LEFT;
} else if (test_pixel(bitmap, y, x + 1, c)) {
x += 1;
} else {
if ((r = new_stop((size_t)y, (size_t)x + 1, user_data)))
return r;
edge = RIGHT;
}
break;
case RIGHT:
if (test_pixel(bitmap, y + 1, x + 1, c)) {
if ((r = new_stop((size_t)y + 1, (size_t)x + 1, user_data)))
return r;
x += 1;
y += 1;
edge = TOP;
} else if (test_pixel(bitmap, y + 1, x, c)) {
y += 1;
} else {
if ((r = new_stop((size_t)y + 1, (size_t)x + 1, user_data)))
return r;
edge = BOTTOM;
}
break;
case BOTTOM:
if (test_pixel(bitmap, y + 1, x - 1, c)) {
if ((r = new_stop((size_t)y + 1, (size_t)x, user_data)))
return r;
x -= 1;
y += 1;
edge = RIGHT;
} else if (test_pixel(bitmap, y, x - 1, c)) {
x -= 1;
} else {
if ((r = new_stop((size_t)y + 1, (size_t)x, user_data)))
return r;
edge = LEFT;
}
break;
case LEFT:
if (test_pixel(bitmap, y - 1, x - 1, c)) {
if ((r = new_stop((size_t)y, (size_t)x, user_data)))
return r;
x -= 1;
y -= 1;
edge = BOTTOM;
} else if (test_pixel(bitmap, y - 1, x, c)) {
y -= 1;
} else {
if ((r = new_stop((size_t)y, (size_t)x, user_data)))
return r;
edge = TOP;
}
break;
default:
abort();
}
} while (y != (ssize_t)tl_y || x != (ssize_t)tl_x || edge != TOP);
return 0;
}
static void
erase_traced_component(struct libtracebitmap_bitmap *bitmap, int negative)
{
size_t y, x;
int inside;
uint8_t c;
for (y = 0; y < bitmap->height; y++) {
inside = 0;
for (x = 0; x < bitmap->width; x++) {
c = bitmap->image[INDEX(y, x)];
bitmap->image[INDEX(y, x)] &= (uint8_t)~EDGE_MASK;
if (c & LEFT)
inside = 1;
if (inside) {
bitmap->image[INDEX(y, x)] &= (uint8_t)~STATUS_MASK;
if ((c & STATUS_MASK) != CLEARED)
bitmap->image[INDEX(y, x)] |= CLEARED;
else if (negative)
bitmap->image[INDEX(y, x)] |= SET;
else
bitmap->image[INDEX(y, x)] |= NEGATIVE;
}
if (c & RIGHT)
inside = 0;
}
}
}
int
libtracebitmap_trace(
struct libtracebitmap_bitmap *bitmap,
int (*new_component)(int negative, void *user_data),
int (*new_stop)(size_t y, size_t x, void *user_data),
int (*component_finished)(void *user_data),
void *user_data)
{
size_t y, x;
int negative;
int r;
for (;;) {
if (!find_component(&y, &x, &negative, bitmap))
return 0;
if ((r = new_component(negative, user_data)))
return r;
if ((r = trace_edges(bitmap, y, x, negative, new_stop, user_data)))
return r;
if ((r = component_finished(user_data)))
return r;
erase_traced_component(bitmap, negative);
}
return 0;
}
|