aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/core/SkDistanceFieldGen.h
blob: 5e7af52a540895e05ae521b18cb2f82984311772 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
 * Copyright 2014 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#ifndef SkDistanceFieldGen_DEFINED
#define SkDistanceFieldGen_DEFINED

#include "SkTypes.h"

// the max magnitude for the distance field
// distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
#define SK_DistanceFieldMagnitude   4
// we need to pad around the original glyph to allow our maximum distance of
// SK_DistanceFieldMagnitude texels away from any edge
#define SK_DistanceFieldPad         4
// the rect we render with is inset from the distance field glyph size to allow for bilerp
#define SK_DistanceFieldInset       2

// For the fragment shader:
//   The distance field is constructed as unsigned char values,
//   so that the zero value is at 128, and the supported range of distances is [-4 * 127/128, 4].
//   Hence our multiplier (width of the range) is 4 * 255/128 and zero threshold is 128/255.
#define SK_DistanceFieldMultiplier   "7.96875"
#define SK_DistanceFieldThreshold    "0.50196078431"

/** Given 8-bit mask data, generate the associated distance field

 *  @param distanceField     The distance field to be generated. Should already be allocated
 *                           by the client with the padding above.
 *  @param image             8-bit mask we're using to generate the distance field.
 *  @param w                 Width of the original image.
 *  @param h                 Height of the original image.
 *  @param rowBytes          Size of each row in the image, in bytes
 */
bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
                                        const unsigned char* image,
                                        int w, int h, size_t rowBytes);

/** Given 1-bit mask data, generate the associated distance field

 *  @param distanceField     The distance field to be generated. Should already be allocated
 *                           by the client with the padding above.
 *  @param image             1-bit mask we're using to generate the distance field.
 *  @param w                 Width of the original image.
 *  @param h                 Height of the original image.
 *  @param rowBytes          Size of each row in the image, in bytes
 */
bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
                                        const unsigned char* image,
                                        int w, int h, size_t rowBytes);

/** Given width and height of original image, return size (in bytes) of distance field
 *  @param w                 Width of the original image.
 *  @param h                 Height of the original image.
 */
inline size_t SkComputeDistanceFieldSize(int w, int h) {
    return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
}

#endif