From 1fb6f37cfa76c47b65331b6fe8217f8973f35885 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 4 May 2018 22:15:07 +0300 Subject: [PATCH] Improve distance field accuracy A better initial guess allows the square root function to deal with larger numbers. --- ttf2png.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/ttf2png.c b/ttf2png.c index 5e6e351..bf9f8d2 100644 --- a/ttf2png.c +++ b/ttf2png.c @@ -694,15 +694,9 @@ int copy_bitmap(const FT_Bitmap *bmp, Image *image) unsigned sqrti(unsigned num) { - unsigned result = num; - while(result*result>num) - { - unsigned diff = result*result-num; - if(diff0xFFFF ? 0xFFFF : 0x100); + while(result && result*result>=result+num) + result -= (result*result+result-num)/(result*2); return result; } @@ -740,7 +734,7 @@ unsigned find_distance_to_edge(const Image *image, int origin_x, int origin_y, u } } - return sqrti(closest)*0x7F/range; + return sqrti(closest*0x3F01)/range; } int create_distance_field(const FT_Bitmap *bmp, Image *image, unsigned scale, unsigned margin) -- 2.43.0