freetype-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[freetype2] anuj-distance-field 944711a: * src/sdf/ftbsdf.c (_pass): Use


From: Anuj Verma
Subject: [freetype2] anuj-distance-field 944711a: * src/sdf/ftbsdf.c (_pass): Use function for repetitive task.
Date: Tue, 28 Jul 2020 02:05:05 -0400 (EDT)

branch: anuj-distance-field
commit 944711a6bf455c17f0df38797518d472e2c8ad38
Author: Anuj Verma <anujv@iitbhilai.ac.in>
Commit: Anuj Verma <anujv@iitbhilai.ac.in>

    * 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 85d7f79..0c1b548 100644
--- a/[GSoC]ChangeLog
+++ b/[GSoC]ChangeLog
@@ -1,3 +1,15 @@
+2020-07-28  Anuj Verma  <anujv@iitbhilai.ac.in>
+
+       * 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  <anujv@iitbhilai.ac.in>
 
        * src/sdf/ftbsdf.c (bsdf_is_edge): Use macros to
diff --git a/src/sdf/ftbsdf.c b/src/sdf/ftbsdf.c
index 6950434..2c99d20 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
         }
       }
 
@@ -353,6 +338,59 @@
   /**************************************************************************
    *
    * @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:
    *   first_pass
    *
    * @Description:
@@ -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 );
       }
     }
   }



reply via email to

[Prev in Thread] Current Thread [Next in Thread]