This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathspread.cpp
More file actions
341 lines (287 loc) · 9.31 KB
/
spread.cpp
File metadata and controls
341 lines (287 loc) · 9.31 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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>. */
#include "graphics.h"
#include "graphics-internal.h"
#include <assert.h>
#include <SkMath.h>
// Dilate the input by using a distance transform. A pixel in the new mask
// is taken to be in the dilated mask if it is within the ellipse with radii
// xradius and yradius of a set pixel in the original mask. The maximum
// radii is 254 pixels.
void dilateDistanceXY(const uint8_t *src, uint8_t *dst, int xradius, int yradius, int width, int height, int& r_new_width, int& r_new_height)
{
/*
int new_width = width + 2 * xradius;
int new_height = height + 2 * yradius;
// Compute the x distance of each pixel from the nearest set pixel.
uint8_t *xd;
xd = new (nothrow) uint8_t[new_width * height];
for(int y = 0; y < height; y++)
{
uint8_t *xdptr;
xdptr = xd + new_width * y;
const uint8_t *sptr;
sptr = src + width * y;
for(int x = 0; x < width; x++)
{
// If there is a value at x, take it
if (sptr[x] != 0)
{
xdptr[x + xradius] = 0;
continue;
}
// Otherwise search back...
int db;
db = 255;
for(int z = x; z >= 0 && (x - z) < 255; z--)
{
if (sptr[z] != 0)
{
db = x - z;
break;
}
}
// Now search forwards...
int df;
df = 255;
for(int z = x; (z < width) && (z - x) < 255; z++)
{
if (sptr[z] != 0)
{
df = z - x;
break;
}
}
// Distance is the minimum.
int d;
d = SkMin32(db, df);
xdptr[x + xradius] = d;
}
// Now expand the fringes.
for(int x = 0; x < xradius; x++)
{
if (xdptr[xradius] + (xradius - x) < 255)
xdptr[x] = ((xradius - x) + xdptr[xradius]);
else
xdptr[x] = 255;
if (xdptr[width + xradius - 1] + x + 1 < 255)
xdptr[width + xradius + x] = xdptr[width + xradius - 1] + x + 1;
else
xdptr[width + xradius + x] = 255;
}
}
unsigned int rf;
rf = xradius * xradius * yradius * yradius;
memset(dst, 0, new_width * new_height);
// Now use xd to compute the spread mask.
for(int x = 0; x < new_width; x++)
{
for(int y = 0; y < new_height; y++)
{
uint8_t *dptr = dst + y * new_width + x;
// If the distance at x, y is 0 then we are done.
if (y >= yradius && y < (new_height - yradius) && xd[(y - yradius) * new_width + x] == 0)
{
*dptr = 255;
continue;
}
// Otherwise, search up.
*dptr = 0;
for(int z = y; z >= 0; z--)
{
unsigned int yf;
yf = (y - z) * xradius;
yf *= yf;
if (yf >= rf)
break;
unsigned int xf;
if (z >= yradius && z < (new_height - yradius))
xf = yradius * xd[(z - yradius) * new_width + x];
else
xf = yradius * 255;
xf *= xf;
if (xf < rf - yf)
{
*dptr = 255;
break;
}
}
if (*dptr == 255)
continue;
// Search downwards
for(int z = y; z < new_height; z++)
{
unsigned int yf;
yf = (z - y) * xradius;
yf *= yf;
if (yf >= rf)
break;
unsigned int xf;
if (z >= yradius && z < (new_height - yradius))
xf = yradius * xd[(z - yradius) * new_width + x];
else
xf = yradius * 255;
xf *= xf;
if (xf < rf - yf)
{
*dptr = 255;
break;
}
}
}
}
r_new_width = new_width;
r_new_height = new_height;
*/
int new_width = width + 2 * xradius;
int new_height = height + 2 * yradius;
// Allocate memory for the loop
size_t t_mem_size = new_width * new_height;
uint8_t *buffer;
/* UNCHECKED */ buffer = (uint8_t*)malloc(t_mem_size);
// All the destination pixels are set to the infinite distance initially
memset(buffer, 255, t_mem_size);
// Only this part of the buffer can have non-infinite x distances
uint8_t *xd = buffer + new_width * yradius;
// Compute the x distance of each pixel from the nearest set pixel
for(int y = 0; y < height; y++)
{
uint8_t *xdptr;
xdptr = xd + new_width * y;
const uint8_t *sptr;
sptr = src + width * y;
int x = 0;
int next = 0;
while (x < new_width)
{
// Scan along the line for the next pixel that is set in the source mask
while (next < width && sptr[next] == 0)
next++;
// If no more set pixels in the source mask, go to the end of the line
int dnext;
if (next >= width)
dnext = new_width;
else
dnext = next + xradius; // Account for position shift between src and dst
// If we're starting at the left edge, there isn't a mask point there
int distance = dnext - x;
int halfway = distance / 2;
if (x == 0)
halfway = 0;
// If we're ending at the right edge, there isn't a mask point there
if (dnext >= new_width)
halfway = distance;
// Ensure that we do nothing for empty lines
if (next >= width && x == 0)
halfway = distance = 0;
int i;
for (i = 0; i < halfway; i++)
xdptr[x + i] = SkMin32(i, 255);
for (; i < distance; i++)
xdptr[x + i] = SkMin32(distance - i, 255);
// Move on to the next pixel
//
// Optimisation: scan for the next clear pixel in the mask
// Avoids excessive processing for rows of non-zero pixels
x = dnext;
next = next + 1;
// MW-2014-06-24: [[ Bug ]] Only loop up to (width - 1), otherwise we get
// an access overflow.
while (next < (width - 1) && sptr[next + 1] != 0)
{
xdptr[x] = 0;
x++, next++;
}
// MW-2014-08-27: [[ Bug 13221 ]] If we reached the edge of the source, then we
// assume the next pixel is clear.
if (next == width - 1)
{
xdptr[x] = 0;
x++, next++;
}
}
}
// Destination pointer stride
int ydstride = new_width;
// a-squared, b-squared and a-squared*b-squared
uint32_t aa, bb, aabb;
aa = xradius * xradius;
bb = yradius * yradius;
aabb = aa*bb;
// Look-up tables for pre-calculated multiplications
uint32_t t_xlut[256], t_ylut[256];
for (int i = 0; i < 256; i++)
t_xlut[i] = bb*i*i, t_ylut[i] = aa*i*i;
// Ensure that the "infinite" distance never compares < anything else
t_xlut[255] = t_ylut[255] = 0xFFFFFFFF;
memset(dst, 0, new_width * new_height);
// X distance array
const uint8_t *xdist = buffer;
// Be careful:
//
// Minor, seemingly inconsequential, changes to this loop can slow it down
// considerably. On the other hand, they could speed it up...
int offset = 0;
for (int y = 0; y < new_height; y++)
{
for (int x = 0; x < new_width; x++, offset++)
{
// Starting at this position, scan down for a pixel that is within
// the dilation radius.
int ny = y;
int noffset = offset;
int max = SkMin32(new_height, y + yradius);
do
{
uint8_t xval, yval;
xval = xdist[noffset];
yval = ny - y;
// Note that care is needed to avoid overflow errors
uint64_t t_dist64 = uint64_t(t_xlut[xval]) + uint64_t(t_ylut[yval]);
if (t_dist64 == uint32_t(t_dist64) && uint32_t(t_dist64) < aabb)
{
dst[offset] = 255;
break;
}
ny += 1;
noffset += ydstride;
}
while (ny < max);
// Early escape to avoid processing another loop
if (dst[offset])
continue;
// Starting at this position, scan up for a pixel that is within
// the dilation radius
ny = y - 1;
noffset = offset - ydstride;
int min = SkMax32(0, y - yradius);
while (ny >= min)
{
uint8_t xval, yval;
xval = xdist[noffset];
yval = y - ny;
// Note that care is needed to avoid overflow errors
uint64_t t_dist64 = uint64_t(t_xlut[xval]) + uint64_t(t_ylut[yval]);
if (t_dist64 == uint32_t(t_dist64) && uint32_t(t_dist64) < aabb)
{
dst[offset] = 255;
break;
}
ny -= 1;
noffset -= ydstride;
}
}
}
free(buffer);
r_new_width = new_width;
r_new_height = new_height;
}