From ffb6890089ebe1bd5dad6bb76f696a8a5ef4a596 Mon Sep 17 00:00:00 2001 From: Anuj Verma Date: Tue, 28 Jul 2020 11:34:25 +0530 Subject: [PATCH] * src/sdf/ftbsdf.c (_pass): Use function for repetitive task. Use function `compare_neighbor' for comparing the neighbors. Makes it easier to tweak the algorithm and looks better. * src/sdf/ftbsdf.c (compare_neighbor): Added function to compare the neighbors and assign values if the new distance is the shortest. --- [GSoC]ChangeLog | 12 +++ src/sdf/ftbsdf.c | 230 +++++++++++++---------------------------------- 2 files changed, 75 insertions(+), 167 deletions(-) diff --git a/[GSoC]ChangeLog b/[GSoC]ChangeLog index 85d7f794a..0c1b54840 100644 --- a/[GSoC]ChangeLog +++ b/[GSoC]ChangeLog @@ -1,3 +1,15 @@ +2020-07-28 Anuj Verma + + * src/sdf/ftbsdf.c (_pass): Use function for repetitive task. + + Use function `compare_neighbor' for comparing the + neighbors. Makes it easier to tweak the algorithm + and looks better. + + * src/sdf/ftbsdf.c (compare_neighbor): Added function + to compare the neighbors and assign values if the + new distance is the shortest. + 2020-07-27 Anuj Verma * src/sdf/ftbsdf.c (bsdf_is_edge): Use macros to diff --git a/src/sdf/ftbsdf.c b/src/sdf/ftbsdf.c index 69504342a..2c99d200f 100644 --- a/src/sdf/ftbsdf.c +++ b/src/sdf/ftbsdf.c @@ -304,19 +304,6 @@ pixel_value = s[s_index]; - #if 0 - - /* to make the fractional value 1 */ - /* for completely filled pixels */ - if ( pixel_value == 255 ) - pixel_value = 256; - - /* Assume that 256 is fractional value with */ - /* 0.8 representation, to make it 16.16 left */ - /* shift the value by 8. */ - pixel_value <<= 8; - - #else if ( bsdf_is_edge( s + s_index , s_i, s_j, s_width, s_rows ) ) { t[t_index].dist = 0; @@ -332,8 +319,6 @@ t[t_index].sign = 1; else t[t_index].sign = -1; - - #endif } } @@ -350,6 +335,59 @@ return error; } + /************************************************************************** + * + * @Function: + * compare_neighbor + * + * @Description: + * [TODO] + * + * @Input: + * [TODO] + * + * @Return: + * [TODO] + */ + static void + compare_neighbor( ED* current, + FT_Int x_offset, + FT_Int y_offset, + FT_Int width ) + { + ED* to_check; + FT_16D16 dist; + FT_16D16_Vec dist_vec; + + + to_check = current + ( y_offset * width ) + x_offset; + + /* While checking for nearest point we first */ + /* approximate the dist of the `current' point */ + /* by adding the deviation ( which will be root */ + /* 2 max ). And if the new value is lesser than */ + /* the current value then only we calculate the */ + /* actual distances using `FT_Vector_Length'. */ + /* Of course this will be eliminated while using */ + /* squared distances. */ + + /* approximate the distance */ + dist = to_check->dist + ( FT_ABS( x_offset ) + FT_ABS( y_offset ) ) * ONE; + + if ( dist < current->dist ) + { + dist_vec = to_check->near; + dist_vec.x += x_offset * ONE; + dist_vec.y += y_offset * ONE; + dist = FT_Vector_Length( &dist_vec ); + if ( dist < current->dist ) + { + current->dist = dist; + current->near = dist_vec; + } + } + } + /************************************************************************** * * @Function: @@ -397,76 +435,17 @@ index = j * w + i; current = dm + index; - /* While checking for nearest point we first */ - /* approximate the dist of the `current' point */ - /* by adding the deviation ( which will be root */ - /* 2 max ). And if the new value is lesser than */ - /* the current value then only we calculate the */ - /* actual distances using `FT_Vector_Length'. */ - /* Of course this will be eliminated while using */ - /* squared distances. */ - /* left-up */ - to_check = current - w - 1; - dist = to_check->dist + 2 * ONE; /* will be root(2) but 2 is fine */ - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x -= ONE; - dist_vec.y -= ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, -1, -1, w ); /* up */ - to_check = current - w; - dist = to_check->dist + 1 * ONE; /* can only be 1 */ - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.y -= ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 0, -1, w ); /* up-right */ - to_check = current - w + 1; - dist = to_check->dist + 2 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x += ONE; - dist_vec.y -= ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 1, -1, w ); /* left */ - to_check = current - 1; - dist = to_check->dist + 1 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x -= ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, -1, 0, w ); } /* Backward pass of rows (right -> left), leave, the */ @@ -477,19 +456,7 @@ current = dm + index; /* right */ - to_check = current + 1; - dist = to_check->dist + 1 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x += ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 1, 0, w ); } } } @@ -541,76 +508,17 @@ index = j * w + i; current = dm + index; - /* While checking for nearest point we first */ - /* approximate the dist of the `current' point */ - /* by adding the deviation ( which will be root */ - /* 2 max ). And if the new value is lesser than */ - /* the current value then only we calculate the */ - /* actual distances using `FT_Vector_Length'. */ - /* Of course this will be eliminated while using */ - /* squared distances. */ - /* left-up */ - to_check = current + w - 1; - dist = to_check->dist + 2 * ONE; /* will be root(2) but 2 is fine */ - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x -= ONE; - dist_vec.y += ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, -1, 1, w ); /* up */ - to_check = current + w; - dist = to_check->dist + 1 * ONE; /* can only be 1 */ - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.y += ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 0, 1, w ); /* up-right */ - to_check = current + w + 1; - dist = to_check->dist + 2 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x += ONE; - dist_vec.y += ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 1, 1, w ); /* left */ - to_check = current - 1; - dist = to_check->dist + 1 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x -= ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, -1, 0, w ); } /* Backward pass of rows (right -> left), leave, the */ @@ -621,19 +529,7 @@ current = dm + index; /* right */ - to_check = current + 1; - dist = to_check->dist + 1 * ONE; - if ( dist < current->dist ) - { - dist_vec = to_check->near; - dist_vec.x += ONE; - dist = FT_Vector_Length( &dist_vec ); - if ( dist < current->dist ) - { - current->dist = dist; - current->near = dist_vec; - } - } + compare_neighbor( current, 1, 0, w ); } } }