Debian Codegraph @

src/LomiriToolkit/tools/3rd_party/edtaa3func.c

raw
1 /*
2 * edtaa3()
3 *
4 * Sweep-and-update Euclidean distance transform of an
5 * image. Positive pixels are treated as object pixels,
6 * zero or negative pixels are treated as background.
7 * An attempt is made to treat antialiased edges correctly.
8 * The input image must have pixels in the range [0,1],
9 * and the antialiased image should be a box-filter
10 * sampling of the ideal, crisp edge.
11 * If the antialias region is more than 1 pixel wide,
12 * the result from this transform will be inaccurate.
13 *
14 * By Stefan Gustavson ([email protected]).
15 *
16 * Originally written in 1994, based on a verbal
17 * description of the SSED8 algorithm published in the
18 * PhD dissertation of Ingemar Ragnemalm. This is his
19 * algorithm, I only implemented it in C.
20 *
21 * Updated in 2004 to treat border pixels correctly,
22 * and cleaned up the code to improve readability.
23 *
24 * Updated in 2009 to handle anti-aliased edges.
25 *
26 * Updated in 2011 to avoid a corner case infinite loop.
27 *
28 * Updated 2012 to change license from LGPL to MIT.
29 */
30
31 /*
32 Copyright (C) 2009-2012 Stefan Gustavson ([email protected])
33 The code in this file is distributed under the MIT license:
34
35 Permission is hereby granted, free of charge, to any person obtaining a copy
36 of this software and associated documentation files (the "Software"), to deal
37 in the Software without restriction, including without limitation the rights
38 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
39 copies of the Software, and to permit persons to whom the Software is
40 furnished to do so, subject to the following conditions:
41
42 The above copyright notice and this permission notice shall be included in
43 all copies or substantial portions of the Software.
44
45 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
46 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
48 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
50 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
51 THE SOFTWARE.
52 */
53
54 /*
55 * Compute the local gradient at edge pixels using convolution filters.
56 * The gradient is computed only at edge pixels. At other places in the
57 * image, it is never used, and it's mostly zero anyway.
58 */
59 void computegradient(double *img, int w, int h, double *gx, double *gy)
60 {
61 int i,j,k,p,q;
62 double glength, phi, phiscaled, ascaled, errsign, pfrac, qfrac, err0, err1, err;
63 #define SQRT2 1.4142136
64 for(i = 1; i < h-1; i++) { // Avoid edges where the kernels would spill over
65 for(j = 1; j < w-1; j++) {
66 k = i*w + j;
67 if((img[k]>0.0) && (img[k]<1.0)) { // Compute gradient for edge pixels only
68 gx[k] = -img[k-w-1] - SQRT2*img[k-1] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+1] + img[k+w+1];
69 gy[k] = -img[k-w-1] - SQRT2*img[k-w] - img[k+w-1] + img[k-w+1] + SQRT2*img[k+w] + img[k+w+1];
70 glength = gx[k]*gx[k] + gy[k]*gy[k];
71 if(glength > 0.0) { // Avoid division by zero
72 glength = sqrt(glength);
73 gx[k]=gx[k]/glength;
74 gy[k]=gy[k]/glength;
75 }
76 }
77 }
78 }
79 // TODO: Compute reasonable values for gx, gy also around the image edges.
80 // (These are zero now, which reduces the accuracy for a 1-pixel wide region
81 // around the image edge.) 2x2 kernels would be suitable for this.
82 }
83
84 /*
85 * A somewhat tricky function to approximate the distance to an edge in a
86 * certain pixel, with consideration to either the local gradient (gx,gy)
87 * or the direction to the pixel (dx,dy) and the pixel greyscale value a.
88 * The latter alternative, using (dx,dy), is the metric used by edtaa2().
89 * Using a local estimate of the edge gradient (gx,gy) yields much better
90 * accuracy at and near edges, and reduces the error even at distant pixels
91 * provided that the gradient direction is accurately estimated.
92 */
93 double edgedf(double gx, double gy, double a)
94 {
95 double df, glength, temp, a1;
96
97 if ((gx == 0) || (gy == 0)) { // Either A) gu or gv are zero, or B) both
98 df = 0.5-a; // Linear approximation is A) correct or B) a fair guess
99 } else {
100 glength = sqrt(gx*gx + gy*gy);
101 if(glength>0) {
102 gx = gx/glength;
103 gy = gy/glength;
104 }
105 /* Everything is symmetric wrt sign and transposition,
106 * so move to first octant (gx>=0, gy>=0, gx>=gy) to
107 * avoid handling all possible edge directions.
108 */
109 gx = fabs(gx);
110 gy = fabs(gy);
111 if(gx<gy) {
112 temp = gx;
113 gx = gy;
114 gy = temp;
115 }
116 a1 = 0.5*gy/gx;
117 if (a < a1) { // 0 <= a < a1
118 df = 0.5*(gx + gy) - sqrt(2.0*gx*gy*a);
119 } else if (a < (1.0-a1)) { // a1 <= a <= 1-a1
120 df = (0.5-a)*gx;
121 } else { // 1-a1 < a <= 1
122 df = -0.5*(gx + gy) + sqrt(2.0*gx*gy*(1.0-a));
123 }
124 }
125 return df;
126 }
127
128 double distaa3(double *img, double *gximg, double *gyimg, int w, int c, int xc, int yc, int xi, int yi)
129 {
130 double di, df, dx, dy, gx, gy, a;
131 int closest;
132
133 closest = c-xc-yc*w; // Index to the edge pixel pointed to from c
134 a = img[closest]; // Grayscale value at the edge pixel
135 gx = gximg[closest]; // X gradient component at the edge pixel
136 gy = gyimg[closest]; // Y gradient component at the edge pixel
137
138 if(a > 1.0) a = 1.0;
139 if(a < 0.0) a = 0.0; // Clip grayscale values outside the range [0,1]
140 if(a == 0.0) return 1000000.0; // Not an object pixel, return "very far" ("don't know yet")
141
142 dx = (double)xi;
143 dy = (double)yi;
144 di = sqrt(dx*dx + dy*dy); // Length of integer vector, like a traditional EDT
145 if(di==0) { // Use local gradient only at edges
146 // Estimate based on local gradient only
147 df = edgedf(gx, gy, a);
148 } else {
149 // Estimate gradient based on direction to edge (accurate for large di)
150 df = edgedf(dx, dy, a);
151 }
152 return di + df; // Same metric as edtaa2, except at edges (where di=0)
153 }
154
155 // Shorthand macro: add ubiquitous parameters dist, gx, gy, img and w and call distaa3()
156 #define DISTAA(c,xc,yc,xi,yi) (distaa3(img, gx, gy, w, c, xc, yc, xi, yi))
157
158 void edtaa3(double *img, double *gx, double *gy, int w, int h, short *distx, short *disty, double *dist)
159 {
160 int x, y, i, c;
161 int offset_u, offset_ur, offset_r, offset_rd,
162 offset_d, offset_dl, offset_l, offset_lu;
163 double olddist, newdist;
164 int cdistx, cdisty, newdistx, newdisty;
165 int changed;
166 double epsilon = 1e-3;
167
168 /* Initialize index offsets for the current image width */
169 offset_u = -w;
170 offset_ur = -w+1;
171 offset_r = 1;
172 offset_rd = w+1;
173 offset_d = w;
174 offset_dl = w-1;
175 offset_l = -1;
176 offset_lu = -w-1;
177
178 /* Initialize the distance images */
179 for(i=0; i<w*h; i++) {
180 distx[i] = 0; // At first, all pixels point to
181 disty[i] = 0; // themselves as the closest known.
182 if(img[i] <= 0.0)
183 {
184 dist[i]= 1000000.0; // Big value, means "not set yet"
185 }
186 else if (img[i]<1.0) {
187 dist[i] = edgedf(gx[i], gy[i], img[i]); // Gradient-assisted estimate
188 }
189 else {
190 dist[i]= 0.0; // Inside the object
191 }
192 }
193
194 /* Perform the transformation */
195 do
196 {
197 changed = 0;
198
199 /* Scan rows, except first row */
200 for(y=1; y<h; y++)
201 {
202
203 /* move index to leftmost pixel of current row */
204 i = y*w;
205
206 /* scan right, propagate distances from above & left */
207
208 /* Leftmost pixel is special, has no left neighbors */
209 olddist = dist[i];
210 if(olddist > 0) // If non-zero distance or not set yet
211 {
212 c = i + offset_u; // Index of candidate for testing
213 cdistx = distx[c];
214 cdisty = disty[c];
215 newdistx = cdistx;
216 newdisty = cdisty+1;
217 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
218 if(newdist < olddist-epsilon)
219 {
220 distx[i]=newdistx;
221 disty[i]=newdisty;
222 dist[i]=newdist;
223 olddist=newdist;
224 changed = 1;
225 }
226
227 c = i+offset_ur;
228 cdistx = distx[c];
229 cdisty = disty[c];
230 newdistx = cdistx-1;
231 newdisty = cdisty+1;
232 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
233 if(newdist < olddist-epsilon)
234 {
235 distx[i]=newdistx;
236 disty[i]=newdisty;
237 dist[i]=newdist;
238 changed = 1;
239 }
240 }
241 i++;
242
243 /* Middle pixels have all neighbors */
244 for(x=1; x<w-1; x++, i++)
245 {
246 olddist = dist[i];
247 if(olddist <= 0) continue; // No need to update further
248
249 c = i+offset_l;
250 cdistx = distx[c];
251 cdisty = disty[c];
252 newdistx = cdistx+1;
253 newdisty = cdisty;
254 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
255 if(newdist < olddist-epsilon)
256 {
257 distx[i]=newdistx;
258 disty[i]=newdisty;
259 dist[i]=newdist;
260 olddist=newdist;
261 changed = 1;
262 }
263
264 c = i+offset_lu;
265 cdistx = distx[c];
266 cdisty = disty[c];
267 newdistx = cdistx+1;
268 newdisty = cdisty+1;
269 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
270 if(newdist < olddist-epsilon)
271 {
272 distx[i]=newdistx;
273 disty[i]=newdisty;
274 dist[i]=newdist;
275 olddist=newdist;
276 changed = 1;
277 }
278
279 c = i+offset_u;
280 cdistx = distx[c];
281 cdisty = disty[c];
282 newdistx = cdistx;
283 newdisty = cdisty+1;
284 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
285 if(newdist < olddist-epsilon)
286 {
287 distx[i]=newdistx;
288 disty[i]=newdisty;
289 dist[i]=newdist;
290 olddist=newdist;
291 changed = 1;
292 }
293
294 c = i+offset_ur;
295 cdistx = distx[c];
296 cdisty = disty[c];
297 newdistx = cdistx-1;
298 newdisty = cdisty+1;
299 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
300 if(newdist < olddist-epsilon)
301 {
302 distx[i]=newdistx;
303 disty[i]=newdisty;
304 dist[i]=newdist;
305 changed = 1;
306 }
307 }
308
309 /* Rightmost pixel of row is special, has no right neighbors */
310 olddist = dist[i];
311 if(olddist > 0) // If not already zero distance
312 {
313 c = i+offset_l;
314 cdistx = distx[c];
315 cdisty = disty[c];
316 newdistx = cdistx+1;
317 newdisty = cdisty;
318 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
319 if(newdist < olddist-epsilon)
320 {
321 distx[i]=newdistx;
322 disty[i]=newdisty;
323 dist[i]=newdist;
324 olddist=newdist;
325 changed = 1;
326 }
327
328 c = i+offset_lu;
329 cdistx = distx[c];
330 cdisty = disty[c];
331 newdistx = cdistx+1;
332 newdisty = cdisty+1;
333 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
334 if(newdist < olddist-epsilon)
335 {
336 distx[i]=newdistx;
337 disty[i]=newdisty;
338 dist[i]=newdist;
339 olddist=newdist;
340 changed = 1;
341 }
342
343 c = i+offset_u;
344 cdistx = distx[c];
345 cdisty = disty[c];
346 newdistx = cdistx;
347 newdisty = cdisty+1;
348 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
349 if(newdist < olddist-epsilon)
350 {
351 distx[i]=newdistx;
352 disty[i]=newdisty;
353 dist[i]=newdist;
354 changed = 1;
355 }
356 }
357
358 /* Move index to second rightmost pixel of current row. */
359 /* Rightmost pixel is skipped, it has no right neighbor. */
360 i = y*w + w-2;
361
362 /* scan left, propagate distance from right */
363 for(x=w-2; x>=0; x--, i--)
364 {
365 olddist = dist[i];
366 if(olddist <= 0) continue; // Already zero distance
367
368 c = i+offset_r;
369 cdistx = distx[c];
370 cdisty = disty[c];
371 newdistx = cdistx-1;
372 newdisty = cdisty;
373 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
374 if(newdist < olddist-epsilon)
375 {
376 distx[i]=newdistx;
377 disty[i]=newdisty;
378 dist[i]=newdist;
379 changed = 1;
380 }
381 }
382 }
383
384 /* Scan rows in reverse order, except last row */
385 for(y=h-2; y>=0; y--)
386 {
387 /* move index to rightmost pixel of current row */
388 i = y*w + w-1;
389
390 /* Scan left, propagate distances from below & right */
391
392 /* Rightmost pixel is special, has no right neighbors */
393 olddist = dist[i];
394 if(olddist > 0) // If not already zero distance
395 {
396 c = i+offset_d;
397 cdistx = distx[c];
398 cdisty = disty[c];
399 newdistx = cdistx;
400 newdisty = cdisty-1;
401 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
402 if(newdist < olddist-epsilon)
403 {
404 distx[i]=newdistx;
405 disty[i]=newdisty;
406 dist[i]=newdist;
407 olddist=newdist;
408 changed = 1;
409 }
410
411 c = i+offset_dl;
412 cdistx = distx[c];
413 cdisty = disty[c];
414 newdistx = cdistx+1;
415 newdisty = cdisty-1;
416 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
417 if(newdist < olddist-epsilon)
418 {
419 distx[i]=newdistx;
420 disty[i]=newdisty;
421 dist[i]=newdist;
422 changed = 1;
423 }
424 }
425 i--;
426
427 /* Middle pixels have all neighbors */
428 for(x=w-2; x>0; x--, i--)
429 {
430 olddist = dist[i];
431 if(olddist <= 0) continue; // Already zero distance
432
433 c = i+offset_r;
434 cdistx = distx[c];
435 cdisty = disty[c];
436 newdistx = cdistx-1;
437 newdisty = cdisty;
438 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
439 if(newdist < olddist-epsilon)
440 {
441 distx[i]=newdistx;
442 disty[i]=newdisty;
443 dist[i]=newdist;
444 olddist=newdist;
445 changed = 1;
446 }
447
448 c = i+offset_rd;
449 cdistx = distx[c];
450 cdisty = disty[c];
451 newdistx = cdistx-1;
452 newdisty = cdisty-1;
453 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
454 if(newdist < olddist-epsilon)
455 {
456 distx[i]=newdistx;
457 disty[i]=newdisty;
458 dist[i]=newdist;
459 olddist=newdist;
460 changed = 1;
461 }
462
463 c = i+offset_d;
464 cdistx = distx[c];
465 cdisty = disty[c];
466 newdistx = cdistx;
467 newdisty = cdisty-1;
468 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
469 if(newdist < olddist-epsilon)
470 {
471 distx[i]=newdistx;
472 disty[i]=newdisty;
473 dist[i]=newdist;
474 olddist=newdist;
475 changed = 1;
476 }
477
478 c = i+offset_dl;
479 cdistx = distx[c];
480 cdisty = disty[c];
481 newdistx = cdistx+1;
482 newdisty = cdisty-1;
483 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
484 if(newdist < olddist-epsilon)
485 {
486 distx[i]=newdistx;
487 disty[i]=newdisty;
488 dist[i]=newdist;
489 changed = 1;
490 }
491 }
492 /* Leftmost pixel is special, has no left neighbors */
493 olddist = dist[i];
494 if(olddist > 0) // If not already zero distance
495 {
496 c = i+offset_r;
497 cdistx = distx[c];
498 cdisty = disty[c];
499 newdistx = cdistx-1;
500 newdisty = cdisty;
501 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
502 if(newdist < olddist-epsilon)
503 {
504 distx[i]=newdistx;
505 disty[i]=newdisty;
506 dist[i]=newdist;
507 olddist=newdist;
508 changed = 1;
509 }
510
511 c = i+offset_rd;
512 cdistx = distx[c];
513 cdisty = disty[c];
514 newdistx = cdistx-1;
515 newdisty = cdisty-1;
516 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
517 if(newdist < olddist-epsilon)
518 {
519 distx[i]=newdistx;
520 disty[i]=newdisty;
521 dist[i]=newdist;
522 olddist=newdist;
523 changed = 1;
524 }
525
526 c = i+offset_d;
527 cdistx = distx[c];
528 cdisty = disty[c];
529 newdistx = cdistx;
530 newdisty = cdisty-1;
531 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
532 if(newdist < olddist-epsilon)
533 {
534 distx[i]=newdistx;
535 disty[i]=newdisty;
536 dist[i]=newdist;
537 changed = 1;
538 }
539 }
540
541 /* Move index to second leftmost pixel of current row. */
542 /* Leftmost pixel is skipped, it has no left neighbor. */
543 i = y*w + 1;
544 for(x=1; x<w; x++, i++)
545 {
546 /* scan right, propagate distance from left */
547 olddist = dist[i];
548 if(olddist <= 0) continue; // Already zero distance
549
550 c = i+offset_l;
551 cdistx = distx[c];
552 cdisty = disty[c];
553 newdistx = cdistx+1;
554 newdisty = cdisty;
555 newdist = DISTAA(c, cdistx, cdisty, newdistx, newdisty);
556 if(newdist < olddist-epsilon)
557 {
558 distx[i]=newdistx;
559 disty[i]=newdisty;
560 dist[i]=newdist;
561 changed = 1;
562 }
563 }
564 }
565 }
566 while(changed); // Sweep until no more updates are made
567
568 /* The transformation is completed. */
569
570 }