blob: 5e7af52a540895e05ae521b18cb2f82984311772 [file] [log] [blame]
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +00001/*
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
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000010#include "SkTypes.h"
11
12// the max magnitude for the distance field
jvanverth970fd1f2016-02-24 08:33:10 -080013// distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000014#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
commit-bot@chromium.org64b08a12014-04-15 17:53:21 +000017#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
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000020
jvanverth970fd1f2016-02-24 08:33:10 -080021// 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.
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000025#define SK_DistanceFieldMultiplier "7.96875"
26#define SK_DistanceFieldThreshold "0.50196078431"
27
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000028/** Given 8-bit mask data, generate the associated distance field
29
30 * @param distanceField The distance field to be generated. Should already be allocated
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000031 * by the client with the padding above.
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000032 * @param image 8-bit mask we're using to generate the distance field.
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000033 * @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
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000036 */
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000037bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
38 const unsigned char* image,
bsalomonef3fcd82014-12-12 08:51:38 -080039 int w, int h, size_t rowBytes);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000040
41/** Given 1-bit mask data, 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 1-bit mask 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 */
50bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
51 const unsigned char* image,
bsalomonef3fcd82014-12-12 08:51:38 -080052 int w, int h, size_t rowBytes);
commit-bot@chromium.org762cd802014-04-14 22:05:07 +000053
54/** Given width and height of original image, return size (in bytes) of distance field
55 * @param w Width of the original image.
56 * @param h Height of the original image.
57 */
58inline size_t SkComputeDistanceFieldSize(int w, int h) {
59 return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
60}
commit-bot@chromium.org8065ec52014-03-11 15:57:40 +000061
62#endif