Index: src/core/SkDistanceFieldGen.cpp |
diff --git a/src/core/SkDistanceFieldGen.cpp b/src/core/SkDistanceFieldGen.cpp |
index 147aefad79052428453494e33fb98ea4e21e9d43..85ad5b95f7d4e2642916741e3dca1166912aa8bc 100755 |
--- a/src/core/SkDistanceFieldGen.cpp |
+++ b/src/core/SkDistanceFieldGen.cpp |
@@ -317,13 +317,16 @@ static void B2(DFData* curr, int width) { |
#if !DUMP_EDGE |
static unsigned char pack_distance_field_val(float dist, float distanceMagnitude) { |
- if (dist <= -distanceMagnitude) { |
- return 255; |
- } else if (dist > distanceMagnitude) { |
- return 0; |
- } else { |
- return (unsigned char)((distanceMagnitude-dist)*128.0f/distanceMagnitude); |
- } |
+ // The distance field is constructed as unsigned char values, so that the zero value is at 128, |
jvanverth1
2016/02/19 23:36:46
I was looking at this to understand it a little be
Joel.Liang
2016/02/22 07:34:00
Yes, to do that we could add SkScalarRoundToInt to
|
+ // Beside 128, we have 128 values in range [0, 128), but only 127 values in range (128, 255]. |
+ // So we multiply distanceMagnitude by 127/128 at the latter range to avoid overflow. |
+ dist = SkScalarPin(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 128.0f); |
+ |
+ // Scale into the positive range for unsigned distance |
+ dist += distanceMagnitude; |
+ |
+ // Scale into unsigned char range |
+ return (unsigned char)(dist / (2 * distanceMagnitude) * 256.0f); |
} |
#endif |