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 #ifndef SkDistanceFieldGen_DEFINED
8 #define SkDistanceFieldGen_DEFINED
9 
10 #include "SkTypes.h"
11 
12 // the max magnitude for the distance field
13 // distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
14 #define SK_DistanceFieldMagnitude   4
15 // we need to pad around the original glyph to allow our maximum distance of
16 // SK_DistanceFieldMagnitude texels away from any edge
17 #define SK_DistanceFieldPad         4
18 // the rect we render with is inset from the distance field glyph size to allow for bilerp
19 #define SK_DistanceFieldInset       2
20 
21 // For the fragment shader:
22 //   The distance field is constructed as unsigned char values,
23 //   so that the zero value is at 128, and the supported range of distances is [-4 * 127/128, 4].
24 //   Hence our multiplier (width of the range) is 4 * 255/128 and zero threshold is 128/255.
25 #define SK_DistanceFieldMultiplier   "7.96875"
26 #define SK_DistanceFieldThreshold    "0.50196078431"
27 
28 /** Given 8-bit mask data, generate the associated distance field
29 
30  *  @param distanceField     The distance field to be generated. Should already be allocated
31  *                           by the client with the padding above.
32  *  @param image             8-bit mask we're using to generate the distance field.
33  *  @param w                 Width of the original image.
34  *  @param h                 Height of the original image.
35  *  @param rowBytes          Size of each row in the image, in bytes
36  */
37 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
38                                         const unsigned char* image,
39                                         int w, int h, size_t rowBytes);
40 
41 /** Given LCD16 mask data (not a 16-bit image), generate the associated distance field
42 
43  *  @param distanceField     The distance field to be generated. Should already be allocated
44  *                           by the client with the padding above.
45  *  @param image             16-bit LCD data we're using to generate the distance field.
46  *  @param w                 Width of the original image.
47  *  @param h                 Height of the original image.
48  *  @param rowBytes          Size of each row in the image, in bytes
49  */
50 bool SkGenerateDistanceFieldFromLCD16Mask(unsigned char* distanceField,
51                                           const unsigned char* image,
52                                           int w, int h, size_t rowBytes);
53 
54 /** Given 1-bit mask data, generate the associated distance field
55 
56  *  @param distanceField     The distance field to be generated. Should already be allocated
57  *                           by the client with the padding above.
58  *  @param image             1-bit mask we're using to generate the distance field.
59  *  @param w                 Width of the original image.
60  *  @param h                 Height of the original image.
61  *  @param rowBytes          Size of each row in the image, in bytes
62  */
63 bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
64                                         const unsigned char* image,
65                                         int w, int h, size_t rowBytes);
66 
67 /** Given width and height of original image, return size (in bytes) of distance field
68  *  @param w                 Width of the original image.
69  *  @param h                 Height of the original image.
70  */
SkComputeDistanceFieldSize(int w,int h)71 inline size_t SkComputeDistanceFieldSize(int w, int h) {
72     return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
73 }
74 
75 #endif
76