1 /*
2  * Copyright 2014 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/private/SkColorData.h"
9 #include "include/private/SkTPin.h"
10 #include "include/private/SkTemplates.h"
11 #include "src/core/SkAutoMalloc.h"
12 #include "src/core/SkDistanceFieldGen.h"
13 #include "src/core/SkMask.h"
14 #include "src/core/SkPointPriv.h"
15 
16 #include <utility>
17 
18 struct DFData {
19     float   fAlpha;      // alpha value of source texel
20     float   fDistSq;     // distance squared to nearest (so far) edge texel
21     SkPoint fDistVector; // distance vector to nearest (so far) edge texel
22 };
23 
24 enum NeighborFlags {
25     kLeft_NeighborFlag        = 0x01,
26     kRight_NeighborFlag       = 0x02,
27     kTopLeft_NeighborFlag     = 0x04,
28     kTop_NeighborFlag         = 0x08,
29     kTopRight_NeighborFlag    = 0x10,
30     kBottomLeft_NeighborFlag  = 0x20,
31     kBottom_NeighborFlag      = 0x40,
32     kBottomRight_NeighborFlag = 0x80,
33     kAll_NeighborFlags        = 0xff,
34 
35     kNeighborFlagCount        = 8
36 };
37 
38 // We treat an "edge" as a place where we cross from >=128 to <128, or vice versa, or
39 // where we have two non-zero pixels that are <128.
40 // 'neighborFlags' is used to limit the directions in which we test to avoid indexing
41 // outside of the image
found_edge(const unsigned char * imagePtr,int width,int neighborFlags)42 static bool found_edge(const unsigned char* imagePtr, int width, int neighborFlags) {
43     // the order of these should match the neighbor flags above
44     const int kNum8ConnectedNeighbors = 8;
45     const int offsets[8] = {-1, 1, -width-1, -width, -width+1, width-1, width, width+1 };
46     SkASSERT(kNum8ConnectedNeighbors == kNeighborFlagCount);
47 
48     // search for an edge
49     unsigned char currVal = *imagePtr;
50     unsigned char currCheck = (currVal >> 7);
51     for (int i = 0; i < kNum8ConnectedNeighbors; ++i) {
52         unsigned char neighborVal;
53         if ((1 << i) & neighborFlags) {
54             const unsigned char* checkPtr = imagePtr + offsets[i];
55             neighborVal = *checkPtr;
56         } else {
57             neighborVal = 0;
58         }
59         unsigned char neighborCheck = (neighborVal >> 7);
60         SkASSERT(currCheck == 0 || currCheck == 1);
61         SkASSERT(neighborCheck == 0 || neighborCheck == 1);
62         // if sharp transition
63         if (currCheck != neighborCheck ||
64             // or both <128 and >0
65             (!currCheck && !neighborCheck && currVal && neighborVal)) {
66             return true;
67         }
68     }
69 
70     return false;
71 }
72 
init_glyph_data(DFData * data,unsigned char * edges,const unsigned char * image,int dataWidth,int dataHeight,int imageWidth,int imageHeight,int pad)73 static void init_glyph_data(DFData* data, unsigned char* edges, const unsigned char* image,
74                             int dataWidth, int dataHeight,
75                             int imageWidth, int imageHeight,
76                             int pad) {
77     data += pad*dataWidth;
78     data += pad;
79     edges += (pad*dataWidth + pad);
80 
81     for (int j = 0; j < imageHeight; ++j) {
82         for (int i = 0; i < imageWidth; ++i) {
83             if (255 == *image) {
84                 data->fAlpha = 1.0f;
85             } else {
86                 data->fAlpha = (*image)*0.00392156862f;  // 1/255
87             }
88             int checkMask = kAll_NeighborFlags;
89             if (i == 0) {
90                 checkMask &= ~(kLeft_NeighborFlag|kTopLeft_NeighborFlag|kBottomLeft_NeighborFlag);
91             }
92             if (i == imageWidth-1) {
93                 checkMask &= ~(kRight_NeighborFlag|kTopRight_NeighborFlag|kBottomRight_NeighborFlag);
94             }
95             if (j == 0) {
96                 checkMask &= ~(kTopLeft_NeighborFlag|kTop_NeighborFlag|kTopRight_NeighborFlag);
97             }
98             if (j == imageHeight-1) {
99                 checkMask &= ~(kBottomLeft_NeighborFlag|kBottom_NeighborFlag|kBottomRight_NeighborFlag);
100             }
101             if (found_edge(image, imageWidth, checkMask)) {
102                 *edges = 255;  // using 255 makes for convenient debug rendering
103             }
104             ++data;
105             ++image;
106             ++edges;
107         }
108         data += 2*pad;
109         edges += 2*pad;
110     }
111 }
112 
113 // from Gustavson (2011)
114 // computes the distance to an edge given an edge normal vector and a pixel's alpha value
115 // assumes that direction has been pre-normalized
edge_distance(const SkPoint & direction,float alpha)116 static float edge_distance(const SkPoint& direction, float alpha) {
117     float dx = direction.fX;
118     float dy = direction.fY;
119     float distance;
120     if (SkScalarNearlyZero(dx) || SkScalarNearlyZero(dy)) {
121         distance = 0.5f - alpha;
122     } else {
123         // this is easier if we treat the direction as being in the first octant
124         // (other octants are symmetrical)
125         dx = SkScalarAbs(dx);
126         dy = SkScalarAbs(dy);
127         if (dx < dy) {
128             using std::swap;
129             swap(dx, dy);
130         }
131 
132         // a1 = 0.5*dy/dx is the smaller fractional area chopped off by the edge
133         // to avoid the divide, we just consider the numerator
134         float a1num = 0.5f*dy;
135 
136         // we now compute the approximate distance, depending where the alpha falls
137         // relative to the edge fractional area
138 
139         // if 0 <= alpha < a1
140         if (alpha*dx < a1num) {
141             // TODO: find a way to do this without square roots?
142             distance = 0.5f*(dx + dy) - SkScalarSqrt(2.0f*dx*dy*alpha);
143         // if a1 <= alpha <= 1 - a1
144         } else if (alpha*dx < (dx - a1num)) {
145             distance = (0.5f - alpha)*dx;
146         // if 1 - a1 < alpha <= 1
147         } else {
148             // TODO: find a way to do this without square roots?
149             distance = -0.5f*(dx + dy) + SkScalarSqrt(2.0f*dx*dy*(1.0f - alpha));
150         }
151     }
152 
153     return distance;
154 }
155 
init_distances(DFData * data,unsigned char * edges,int width,int height)156 static void init_distances(DFData* data, unsigned char* edges, int width, int height) {
157     // skip one pixel border
158     DFData* currData = data;
159     DFData* prevData = data - width;
160     DFData* nextData = data + width;
161 
162     for (int j = 0; j < height; ++j) {
163         for (int i = 0; i < width; ++i) {
164             if (*edges) {
165                 // we should not be in the one-pixel outside band
166                 SkASSERT(i > 0 && i < width-1 && j > 0 && j < height-1);
167                 // gradient will point from low to high
168                 // +y is down in this case
169                 // i.e., if you're outside, gradient points towards edge
170                 // if you're inside, gradient points away from edge
171                 SkPoint currGrad;
172                 currGrad.fX = (prevData+1)->fAlpha - (prevData-1)->fAlpha
173                              + SK_ScalarSqrt2*(currData+1)->fAlpha
174                              - SK_ScalarSqrt2*(currData-1)->fAlpha
175                              + (nextData+1)->fAlpha - (nextData-1)->fAlpha;
176                 currGrad.fY = (nextData-1)->fAlpha - (prevData-1)->fAlpha
177                              + SK_ScalarSqrt2*nextData->fAlpha
178                              - SK_ScalarSqrt2*prevData->fAlpha
179                              + (nextData+1)->fAlpha - (prevData+1)->fAlpha;
180                 SkPointPriv::SetLengthFast(&currGrad, 1.0f);
181 
182                 // init squared distance to edge and distance vector
183                 float dist = edge_distance(currGrad, currData->fAlpha);
184                 currGrad.scale(dist, &currData->fDistVector);
185                 currData->fDistSq = dist*dist;
186             } else {
187                 // init distance to "far away"
188                 currData->fDistSq = 2000000.f;
189                 currData->fDistVector.fX = 1000.f;
190                 currData->fDistVector.fY = 1000.f;
191             }
192             ++currData;
193             ++prevData;
194             ++nextData;
195             ++edges;
196         }
197     }
198 }
199 
200 // Danielsson's 8SSEDT
201 
202 // first stage forward pass
203 // (forward in Y, forward in X)
F1(DFData * curr,int width)204 static void F1(DFData* curr, int width) {
205     // upper left
206     DFData* check = curr - width-1;
207     SkPoint distVec = check->fDistVector;
208     float distSq = check->fDistSq - 2.0f*(distVec.fX + distVec.fY - 1.0f);
209     if (distSq < curr->fDistSq) {
210         distVec.fX -= 1.0f;
211         distVec.fY -= 1.0f;
212         curr->fDistSq = distSq;
213         curr->fDistVector = distVec;
214     }
215 
216     // up
217     check = curr - width;
218     distVec = check->fDistVector;
219     distSq = check->fDistSq - 2.0f*distVec.fY + 1.0f;
220     if (distSq < curr->fDistSq) {
221         distVec.fY -= 1.0f;
222         curr->fDistSq = distSq;
223         curr->fDistVector = distVec;
224     }
225 
226     // upper right
227     check = curr - width+1;
228     distVec = check->fDistVector;
229     distSq = check->fDistSq + 2.0f*(distVec.fX - distVec.fY + 1.0f);
230     if (distSq < curr->fDistSq) {
231         distVec.fX += 1.0f;
232         distVec.fY -= 1.0f;
233         curr->fDistSq = distSq;
234         curr->fDistVector = distVec;
235     }
236 
237     // left
238     check = curr - 1;
239     distVec = check->fDistVector;
240     distSq = check->fDistSq - 2.0f*distVec.fX + 1.0f;
241     if (distSq < curr->fDistSq) {
242         distVec.fX -= 1.0f;
243         curr->fDistSq = distSq;
244         curr->fDistVector = distVec;
245     }
246 }
247 
248 // second stage forward pass
249 // (forward in Y, backward in X)
F2(DFData * curr,int width)250 static void F2(DFData* curr, int width) {
251     // right
252     DFData* check = curr + 1;
253     SkPoint distVec = check->fDistVector;
254     float distSq = check->fDistSq + 2.0f*distVec.fX + 1.0f;
255     if (distSq < curr->fDistSq) {
256         distVec.fX += 1.0f;
257         curr->fDistSq = distSq;
258         curr->fDistVector = distVec;
259     }
260 }
261 
262 // first stage backward pass
263 // (backward in Y, forward in X)
B1(DFData * curr,int width)264 static void B1(DFData* curr, int width) {
265     // left
266     DFData* check = curr - 1;
267     SkPoint distVec = check->fDistVector;
268     float distSq = check->fDistSq - 2.0f*distVec.fX + 1.0f;
269     if (distSq < curr->fDistSq) {
270         distVec.fX -= 1.0f;
271         curr->fDistSq = distSq;
272         curr->fDistVector = distVec;
273     }
274 }
275 
276 // second stage backward pass
277 // (backward in Y, backwards in X)
B2(DFData * curr,int width)278 static void B2(DFData* curr, int width) {
279     // right
280     DFData* check = curr + 1;
281     SkPoint distVec = check->fDistVector;
282     float distSq = check->fDistSq + 2.0f*distVec.fX + 1.0f;
283     if (distSq < curr->fDistSq) {
284         distVec.fX += 1.0f;
285         curr->fDistSq = distSq;
286         curr->fDistVector = distVec;
287     }
288 
289     // bottom left
290     check = curr + width-1;
291     distVec = check->fDistVector;
292     distSq = check->fDistSq - 2.0f*(distVec.fX - distVec.fY - 1.0f);
293     if (distSq < curr->fDistSq) {
294         distVec.fX -= 1.0f;
295         distVec.fY += 1.0f;
296         curr->fDistSq = distSq;
297         curr->fDistVector = distVec;
298     }
299 
300     // bottom
301     check = curr + width;
302     distVec = check->fDistVector;
303     distSq = check->fDistSq + 2.0f*distVec.fY + 1.0f;
304     if (distSq < curr->fDistSq) {
305         distVec.fY += 1.0f;
306         curr->fDistSq = distSq;
307         curr->fDistVector = distVec;
308     }
309 
310     // bottom right
311     check = curr + width+1;
312     distVec = check->fDistVector;
313     distSq = check->fDistSq + 2.0f*(distVec.fX + distVec.fY + 1.0f);
314     if (distSq < curr->fDistSq) {
315         distVec.fX += 1.0f;
316         distVec.fY += 1.0f;
317         curr->fDistSq = distSq;
318         curr->fDistVector = distVec;
319     }
320 }
321 
322 // enable this to output edge data rather than the distance field
323 #define DUMP_EDGE 0
324 
325 #if !DUMP_EDGE
326 template <int distanceMagnitude>
pack_distance_field_val(float dist)327 static unsigned char pack_distance_field_val(float dist) {
328     // The distance field is constructed as unsigned char values, so that the zero value is at 128,
329     // Beside 128, we have 128 values in range [0, 128), but only 127 values in range (128, 255].
330     // So we multiply distanceMagnitude by 127/128 at the latter range to avoid overflow.
331     dist = SkTPin<float>(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 128.0f);
332 
333     // Scale into the positive range for unsigned distance.
334     dist += distanceMagnitude;
335 
336     // Scale into unsigned char range.
337     // Round to place negative and positive values as equally as possible around 128
338     // (which represents zero).
339     return (unsigned char)SkScalarRoundToInt(dist / (2 * distanceMagnitude) * 256.0f);
340 }
341 #endif
342 
343 // assumes a padded 8-bit image and distance field
344 // width and height are the original width and height of the image
generate_distance_field_from_image(unsigned char * distanceField,const unsigned char * copyPtr,int width,int height)345 static bool generate_distance_field_from_image(unsigned char* distanceField,
346                                                const unsigned char* copyPtr,
347                                                int width, int height) {
348     SkASSERT(distanceField);
349     SkASSERT(copyPtr);
350 
351     // we expand our temp data by one more on each side to simplify
352     // the scanning code -- will always be treated as infinitely far away
353     int pad = SK_DistanceFieldPad + 1;
354 
355     // set params for distance field data
356     int dataWidth = width + 2*pad;
357     int dataHeight = height + 2*pad;
358 
359     // create zeroed temp DFData+edge storage
360     SkAutoFree storage(sk_calloc_throw(dataWidth*dataHeight*(sizeof(DFData) + 1)));
361     DFData*        dataPtr = (DFData*)storage.get();
362     unsigned char* edgePtr = (unsigned char*)storage.get() + dataWidth*dataHeight*sizeof(DFData);
363 
364     // copy glyph into distance field storage
365     init_glyph_data(dataPtr, edgePtr, copyPtr,
366                     dataWidth, dataHeight,
367                     width+2, height+2, SK_DistanceFieldPad);
368 
369     // create initial distance data, particularly at edges
370     init_distances(dataPtr, edgePtr, dataWidth, dataHeight);
371 
372     // now perform Euclidean distance transform to propagate distances
373 
374     // forwards in y
375     DFData* currData = dataPtr+dataWidth+1; // skip outer buffer
376     unsigned char* currEdge = edgePtr+dataWidth+1;
377     for (int j = 1; j < dataHeight-1; ++j) {
378         // forwards in x
379         for (int i = 1; i < dataWidth-1; ++i) {
380             // don't need to calculate distance for edge pixels
381             if (!*currEdge) {
382                 F1(currData, dataWidth);
383             }
384             ++currData;
385             ++currEdge;
386         }
387 
388         // backwards in x
389         --currData; // reset to end
390         --currEdge;
391         for (int i = 1; i < dataWidth-1; ++i) {
392             // don't need to calculate distance for edge pixels
393             if (!*currEdge) {
394                 F2(currData, dataWidth);
395             }
396             --currData;
397             --currEdge;
398         }
399 
400         currData += dataWidth+1;
401         currEdge += dataWidth+1;
402     }
403 
404     // backwards in y
405     currData = dataPtr+dataWidth*(dataHeight-2) - 1; // skip outer buffer
406     currEdge = edgePtr+dataWidth*(dataHeight-2) - 1;
407     for (int j = 1; j < dataHeight-1; ++j) {
408         // forwards in x
409         for (int i = 1; i < dataWidth-1; ++i) {
410             // don't need to calculate distance for edge pixels
411             if (!*currEdge) {
412                 B1(currData, dataWidth);
413             }
414             ++currData;
415             ++currEdge;
416         }
417 
418         // backwards in x
419         --currData; // reset to end
420         --currEdge;
421         for (int i = 1; i < dataWidth-1; ++i) {
422             // don't need to calculate distance for edge pixels
423             if (!*currEdge) {
424                 B2(currData, dataWidth);
425             }
426             --currData;
427             --currEdge;
428         }
429 
430         currData -= dataWidth-1;
431         currEdge -= dataWidth-1;
432     }
433 
434     // copy results to final distance field data
435     currData = dataPtr + dataWidth+1;
436     currEdge = edgePtr + dataWidth+1;
437     unsigned char *dfPtr = distanceField;
438     for (int j = 1; j < dataHeight-1; ++j) {
439         for (int i = 1; i < dataWidth-1; ++i) {
440 #if DUMP_EDGE
441             float alpha = currData->fAlpha;
442             float edge = 0.0f;
443             if (*currEdge) {
444                 edge = 0.25f;
445             }
446             // blend with original image
447             float result = alpha + (1.0f-alpha)*edge;
448             unsigned char val = sk_float_round2int(255*result);
449             *dfPtr++ = val;
450 #else
451             float dist;
452             if (currData->fAlpha > 0.5f) {
453                 dist = -SkScalarSqrt(currData->fDistSq);
454             } else {
455                 dist = SkScalarSqrt(currData->fDistSq);
456             }
457             *dfPtr++ = pack_distance_field_val<SK_DistanceFieldMagnitude>(dist);
458 #endif
459             ++currData;
460             ++currEdge;
461         }
462         currData += 2;
463         currEdge += 2;
464     }
465 
466     return true;
467 }
468 
469 // assumes an 8-bit image and distance field
SkGenerateDistanceFieldFromA8Image(unsigned char * distanceField,const unsigned char * image,int width,int height,size_t rowBytes)470 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
471                                         const unsigned char* image,
472                                         int width, int height, size_t rowBytes) {
473     SkASSERT(distanceField);
474     SkASSERT(image);
475 
476     // create temp data
477     SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char));
478     unsigned char* copyPtr = (unsigned char*) copyStorage.get();
479 
480     // we copy our source image into a padded copy to ensure we catch edge transitions
481     // around the outside
482     const unsigned char* currSrcScanLine = image;
483     sk_bzero(copyPtr, (width+2)*sizeof(char));
484     unsigned char* currDestPtr = copyPtr + width + 2;
485     for (int i = 0; i < height; ++i) {
486         *currDestPtr++ = 0;
487         memcpy(currDestPtr, currSrcScanLine, width);
488         currSrcScanLine += rowBytes;
489         currDestPtr += width;
490         *currDestPtr++ = 0;
491     }
492     sk_bzero(currDestPtr, (width+2)*sizeof(char));
493 
494     return generate_distance_field_from_image(distanceField, copyPtr, width, height);
495 }
496 
497 // assumes a 16-bit lcd mask and 8-bit distance field
SkGenerateDistanceFieldFromLCD16Mask(unsigned char * distanceField,const unsigned char * image,int w,int h,size_t rowBytes)498 bool SkGenerateDistanceFieldFromLCD16Mask(unsigned char* distanceField,
499                                            const unsigned char* image,
500                                            int w, int h, size_t rowBytes) {
501     SkASSERT(distanceField);
502     SkASSERT(image);
503 
504     // create temp data
505     SkAutoSMalloc<1024> copyStorage((w+2)*(h+2)*sizeof(char));
506     unsigned char* copyPtr = (unsigned char*) copyStorage.get();
507 
508     // we copy our source image into a padded copy to ensure we catch edge transitions
509     // around the outside
510     const uint16_t* start = reinterpret_cast<const uint16_t*>(image);
511     auto currSrcScanline = SkMask::AlphaIter<SkMask::kLCD16_Format>(start);
512     auto endSrcScanline = SkMask::AlphaIter<SkMask::kLCD16_Format>(start + w);
513     sk_bzero(copyPtr, (w+2)*sizeof(char));
514     unsigned char* currDestPtr = copyPtr + w + 2;
515     for (int i = 0; i < h; ++i, currSrcScanline >>= rowBytes, endSrcScanline >>= rowBytes) {
516         *currDestPtr++ = 0;
517         for (auto src = currSrcScanline; src < endSrcScanline; ++src) {
518             *currDestPtr++ = *src;
519         }
520         *currDestPtr++ = 0;
521     }
522     sk_bzero(currDestPtr, (w+2)*sizeof(char));
523 
524     return generate_distance_field_from_image(distanceField, copyPtr, w, h);
525 }
526 
527 // assumes a 1-bit image and 8-bit distance field
SkGenerateDistanceFieldFromBWImage(unsigned char * distanceField,const unsigned char * image,int width,int height,size_t rowBytes)528 bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
529                                         const unsigned char* image,
530                                         int width, int height, size_t rowBytes) {
531     SkASSERT(distanceField);
532     SkASSERT(image);
533 
534     // create temp data
535     SkAutoSMalloc<1024> copyStorage((width+2)*(height+2)*sizeof(char));
536     unsigned char* copyPtr = (unsigned char*) copyStorage.get();
537 
538     // we copy our source image into a padded copy to ensure we catch edge transitions
539     // around the outside
540     const unsigned char* currSrcScanLine = image;
541     sk_bzero(copyPtr, (width+2)*sizeof(char));
542     unsigned char* currDestPtr = copyPtr + width + 2;
543     for (int i = 0; i < height; ++i) {
544         *currDestPtr++ = 0;
545 
546 
547         int rowWritesLeft = width;
548         const unsigned char *maskPtr = currSrcScanLine;
549         while (rowWritesLeft > 0) {
550             unsigned mask = *maskPtr++;
551             for (int i = 7; i >= 0 && rowWritesLeft; --i, --rowWritesLeft) {
552                 *currDestPtr++ = (mask & (1 << i)) ? 0xff : 0;
553             }
554         }
555         currSrcScanLine += rowBytes;
556 
557 
558         *currDestPtr++ = 0;
559     }
560     sk_bzero(currDestPtr, (width+2)*sizeof(char));
561 
562     return generate_distance_field_from_image(distanceField, copyPtr, width, height);
563 }
564