60#include "edtaa3func.h"
67void computegradient(
double *img,
int w,
int h,
double *gx,
double *gy )
72#define SQRT2 1.4142136
73 for ( i = 1; i < h - 1;
75 for ( j = 1; j < w - 1; j++ ) {
77 if ( ( img[k] > 0.0 ) &&
79 gx[k] = -img[k - w - 1] - SQRT2 * img[k - 1] - img[k + w - 1] +
80 img[k - w + 1] + SQRT2 * img[k + 1] + img[k + w + 1];
81 gy[k] = -img[k - w - 1] - SQRT2 * img[k - w] - img[k - w + 1] +
82 img[k + w - 1] + SQRT2 * img[k + w] + img[k + w + 1];
83 glength = gx[k] * gx[k] + gy[k] * gy[k];
84 if ( glength > 0.0 ) {
85 glength = sqrt( glength );
86 gx[k] = gx[k] / glength;
87 gy[k] = gy[k] / glength;
106double edgedf(
double gx,
double gy,
double a )
108 double df, glength, temp, a1;
114 glength = sqrt( gx * gx + gy * gy );
132 df = 0.5 * ( gx + gy ) - sqrt( 2.0 * gx * gy * a );
133 }
else if ( a < ( 1.0 - a1 ) ) {
134 df = ( 0.5 - a ) * gx;
136 df = -0.5 * ( gx + gy ) + sqrt( 2.0 * gx * gy * ( 1.0 - a ) );
142double distaa3(
double *img,
double *gximg,
double *gyimg,
int w,
int c,
int xc,
143 int yc,
int xi,
int yi )
145 double di, df, dx, dy, gx, gy, a;
148 closest =
c - xc - yc * w;
167 df = edgedf( gx, gy, a );
170 df = edgedf( dx, dy, a );
177#define DISTAA( c, xc, yc, xi, yi ) \
178 ( distaa3( img, gx, gy, w, c, xc, yc, xi, yi ) )
180void edtaa3(
double *img,
double *gx,
double *gy,
int w,
int h,
short *distx,
181 short *disty,
double *dist )
184 int offset_u, offset_ur, offset_r, offset_rd, offset_d, offset_dl, offset_l,
186 double olddist, newdist;
187 int cdistx, cdisty, newdistx, newdisty;
189 double epsilon = 1e-3;
202 for ( i = 0; i < w * h; i++ ) {
205 if ( img[i] <= 0.0 ) {
207 }
else if ( img[i] < 1.0 ) {
208 dist[i] = edgedf( gx[i], gy[i], img[i] );
219 for ( y = 1; y < h; y++ ) {
234 newdisty = cdisty + 1;
235 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
236 if ( newdist < olddist - epsilon ) {
247 newdistx = cdistx - 1;
248 newdisty = cdisty + 1;
249 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
250 if ( newdist < olddist - epsilon ) {
260 for ( x = 1; x < w - 1; x++, i++ ) {
268 newdistx = cdistx + 1;
270 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
271 if ( newdist < olddist - epsilon ) {
282 newdistx = cdistx + 1;
283 newdisty = cdisty + 1;
284 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
285 if ( newdist < olddist - epsilon ) {
297 newdisty = cdisty + 1;
298 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
299 if ( newdist < olddist - epsilon ) {
310 newdistx = cdistx - 1;
311 newdisty = cdisty + 1;
312 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
313 if ( newdist < olddist - epsilon ) {
328 newdistx = cdistx + 1;
330 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
331 if ( newdist < olddist - epsilon ) {
342 newdistx = cdistx + 1;
343 newdisty = cdisty + 1;
344 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
345 if ( newdist < olddist - epsilon ) {
357 newdisty = cdisty + 1;
358 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
359 if ( newdist < olddist - epsilon ) {
372 for ( x = w - 2; x >= 0; x--, i-- ) {
380 newdistx = cdistx - 1;
382 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
383 if ( newdist < olddist - epsilon ) {
393 for ( y = h - 2; y >= 0; y-- ) {
407 newdisty = cdisty - 1;
408 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
409 if ( newdist < olddist - epsilon ) {
420 newdistx = cdistx + 1;
421 newdisty = cdisty - 1;
422 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
423 if ( newdist < olddist - epsilon ) {
433 for ( x = w - 2; x > 0; x--, i-- ) {
441 newdistx = cdistx - 1;
443 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
444 if ( newdist < olddist - epsilon ) {
455 newdistx = cdistx - 1;
456 newdisty = cdisty - 1;
457 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
458 if ( newdist < olddist - epsilon ) {
470 newdisty = cdisty - 1;
471 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
472 if ( newdist < olddist - epsilon ) {
483 newdistx = cdistx + 1;
484 newdisty = cdisty - 1;
485 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
486 if ( newdist < olddist - epsilon ) {
500 newdistx = cdistx - 1;
502 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
503 if ( newdist < olddist - epsilon ) {
514 newdistx = cdistx - 1;
515 newdisty = cdisty - 1;
516 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
517 if ( newdist < olddist - epsilon ) {
529 newdisty = cdisty - 1;
530 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
531 if ( newdist < olddist - epsilon ) {
542 for ( x = 1; x < w; x++, i++ ) {
551 newdistx = cdistx + 1;
553 newdist = DISTAA(
c, cdistx, cdisty, newdistx, newdisty );
554 if ( newdist < olddist - epsilon ) {