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