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, 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. 25 #define SK_DistanceFieldMultiplier "7.96875" 26 #define SK_DistanceFieldThreshold "0.50196078431" 27 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 31 * by the client with the padding above. 32 * @param image 8-bit mask we're using to generate the distance field. 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 36 */ 37 bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField, 38 const unsigned char* image, 39 int w, int h, size_t rowBytes); 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, 52 int w, int h, size_t rowBytes); 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 } 61 62 #endif 63