18065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org/*
28065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org * Copyright 2014 Google Inc.
38065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org *
48065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org * Use of this source code is governed by a BSD-style license that can be
58065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org * found in the LICENSE file.
68065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org */
78065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org#ifndef SkDistanceFieldGen_DEFINED
88065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org#define SkDistanceFieldGen_DEFINED
98065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org
10762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org#include "SkTypes.h"
11762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
12762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org// the max magnitude for the distance field
13970fd1fbea16da013d5b103182d78499ed85f95fjvanverth// distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
14762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org#define SK_DistanceFieldMagnitude   4
15762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org// we need to pad around the original glyph to allow our maximum distance of
16762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org// SK_DistanceFieldMagnitude texels away from any edge
1764b08a1026851a84031713f0e12a3e59d55ce808commit-bot@chromium.org#define SK_DistanceFieldPad         4
1864b08a1026851a84031713f0e12a3e59d55ce808commit-bot@chromium.org// the rect we render with is inset from the distance field glyph size to allow for bilerp
1964b08a1026851a84031713f0e12a3e59d55ce808commit-bot@chromium.org#define SK_DistanceFieldInset       2
20762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
21970fd1fbea16da013d5b103182d78499ed85f95fjvanverth// For the fragment shader:
22970fd1fbea16da013d5b103182d78499ed85f95fjvanverth//   The distance field is constructed as unsigned char values,
23970fd1fbea16da013d5b103182d78499ed85f95fjvanverth//   so that the zero value is at 128, and the supported range of distances is [-4 * 127/128, 4].
24970fd1fbea16da013d5b103182d78499ed85f95fjvanverth//   Hence our multiplier (width of the range) is 4 * 255/128 and zero threshold is 128/255.
25762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org#define SK_DistanceFieldMultiplier   "7.96875"
26762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org#define SK_DistanceFieldThreshold    "0.50196078431"
27762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
288065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org/** Given 8-bit mask data, generate the associated distance field
298065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org
308065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org *  @param distanceField     The distance field to be generated. Should already be allocated
31762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *                           by the client with the padding above.
328065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org *  @param image             8-bit mask we're using to generate the distance field.
33762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param w                 Width of the original image.
34762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param h                 Height of the original image.
35762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param rowBytes          Size of each row in the image, in bytes
36762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org */
37762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.orgbool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
38762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org                                        const unsigned char* image,
39ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon                                        int w, int h, size_t rowBytes);
40762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
41762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org/** Given 1-bit mask data, generate the associated distance field
42762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
43762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param distanceField     The distance field to be generated. Should already be allocated
44762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *                           by the client with the padding above.
45762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param image             1-bit mask we're using to generate the distance field.
46762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param w                 Width of the original image.
47762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param h                 Height of the original image.
48762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param rowBytes          Size of each row in the image, in bytes
49762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org */
50762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.orgbool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
51762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org                                        const unsigned char* image,
52ef3fcd877aa78c1d0ac802043cd8785180304c12bsalomon                                        int w, int h, size_t rowBytes);
53762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org
54762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org/** Given width and height of original image, return size (in bytes) of distance field
55762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param w                 Width of the original image.
56762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org *  @param h                 Height of the original image.
578065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org */
58762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.orginline size_t SkComputeDistanceFieldSize(int w, int h) {
59762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org    return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
60762cd804375ff8478427dba4fe29379562b8fb60commit-bot@chromium.org}
618065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org
628065ec50f1937c1562810bfe2b216abfb98362b3commit-bot@chromium.org#endif
63