commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 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 | |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 10 | #include "SkTypes.h" |
| 11 | |
| 12 | // the max magnitude for the distance field |
jvanverth | 970fd1f | 2016-02-24 08:33:10 -0800 | [diff] [blame] | 13 | // distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude] |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | 64b08a1 | 2014-04-15 17:53:21 +0000 | [diff] [blame] | 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 |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 20 | |
jvanverth | 970fd1f | 2016-02-24 08:33:10 -0800 | [diff] [blame] | 21 | // 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.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 25 | #define SK_DistanceFieldMultiplier "7.96875" |
| 26 | #define SK_DistanceFieldThreshold "0.50196078431" |
| 27 | |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 28 | /** 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.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 31 | * by the client with the padding above. |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 32 | * @param image 8-bit mask we're using to generate the distance field. |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 33 | * @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.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 36 | */ |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 37 | bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField, |
| 38 | const unsigned char* image, |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 39 | int w, int h, size_t rowBytes); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 40 | |
| 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 | */ |
| 50 | bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField, |
| 51 | const unsigned char* image, |
bsalomon | ef3fcd8 | 2014-12-12 08:51:38 -0800 | [diff] [blame] | 52 | int w, int h, size_t rowBytes); |
commit-bot@chromium.org | 762cd80 | 2014-04-14 22:05:07 +0000 | [diff] [blame] | 53 | |
| 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 | */ |
| 58 | inline size_t SkComputeDistanceFieldSize(int w, int h) { |
| 59 | return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char); |
| 60 | } |
commit-bot@chromium.org | 8065ec5 | 2014-03-11 15:57:40 +0000 | [diff] [blame] | 61 | |
| 62 | #endif |