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, so that the zero value is at 128,
23// and the range is [-4, 4 - 1/255). Hence our multiplier is 8 - 1/32 and zero threshold is 128/255.
24#define SK_DistanceFieldMultiplier   "7.96875"
25#define SK_DistanceFieldThreshold    "0.50196078431"
26
27/** Given 8-bit mask data, generate the associated distance field
28
29 *  @param distanceField     The distance field to be generated. Should already be allocated
30 *                           by the client with the padding above.
31 *  @param image             8-bit mask we're using to generate the distance field.
32 *  @param w                 Width of the original image.
33 *  @param h                 Height of the original image.
34 *  @param rowBytes          Size of each row in the image, in bytes
35 */
36bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
37                                        const unsigned char* image,
38                                        int w, int h, int rowBytes);
39
40/** Given 1-bit mask data, generate the associated distance field
41
42 *  @param distanceField     The distance field to be generated. Should already be allocated
43 *                           by the client with the padding above.
44 *  @param image             1-bit mask we're using to generate the distance field.
45 *  @param w                 Width of the original image.
46 *  @param h                 Height of the original image.
47 *  @param rowBytes          Size of each row in the image, in bytes
48 */
49bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
50                                        const unsigned char* image,
51                                        int w, int h, int rowBytes);
52
53/** Given width and height of original image, return size (in bytes) of distance field
54 *  @param w                 Width of the original image.
55 *  @param h                 Height of the original image.
56 */
57inline size_t SkComputeDistanceFieldSize(int w, int h) {
58    return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
59}
60
61#endif
62