SkDistanceFieldGen.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. #include "include/core/SkTypes.h"
  10. // the max magnitude for the distance field
  11. // distance values are limited to the range (-SK_DistanceFieldMagnitude, SK_DistanceFieldMagnitude]
  12. #define SK_DistanceFieldMagnitude 4
  13. // we need to pad around the original glyph to allow our maximum distance of
  14. // SK_DistanceFieldMagnitude texels away from any edge
  15. #define SK_DistanceFieldPad 4
  16. // the rect we render with is inset from the distance field glyph size to allow for bilerp
  17. #define SK_DistanceFieldInset 2
  18. // For the fragment shader:
  19. // The distance field is constructed as unsigned char values,
  20. // so that the zero value is at 128, and the supported range of distances is [-4 * 127/128, 4].
  21. // Hence our multiplier (width of the range) is 4 * 255/128 and zero threshold is 128/255.
  22. #define SK_DistanceFieldMultiplier "7.96875"
  23. #define SK_DistanceFieldThreshold "0.50196078431"
  24. /** Given 8-bit mask data, generate the associated distance field
  25. * @param distanceField The distance field to be generated. Should already be allocated
  26. * by the client with the padding above.
  27. * @param image 8-bit mask we're using to generate the distance field.
  28. * @param w Width of the original image.
  29. * @param h Height of the original image.
  30. * @param rowBytes Size of each row in the image, in bytes
  31. */
  32. bool SkGenerateDistanceFieldFromA8Image(unsigned char* distanceField,
  33. const unsigned char* image,
  34. int w, int h, size_t rowBytes);
  35. /** Given LCD16 mask data (not a 16-bit image), generate the associated distance field
  36. * @param distanceField The distance field to be generated. Should already be allocated
  37. * by the client with the padding above.
  38. * @param image 16-bit LCD data we're using to generate the distance field.
  39. * @param w Width of the original image.
  40. * @param h Height of the original image.
  41. * @param rowBytes Size of each row in the image, in bytes
  42. */
  43. bool SkGenerateDistanceFieldFromLCD16Mask(unsigned char* distanceField,
  44. const unsigned char* image,
  45. int w, int h, size_t rowBytes);
  46. /** Given 1-bit mask data, generate the associated distance field
  47. * @param distanceField The distance field to be generated. Should already be allocated
  48. * by the client with the padding above.
  49. * @param image 1-bit mask we're using to generate the distance field.
  50. * @param w Width of the original image.
  51. * @param h Height of the original image.
  52. * @param rowBytes Size of each row in the image, in bytes
  53. */
  54. bool SkGenerateDistanceFieldFromBWImage(unsigned char* distanceField,
  55. const unsigned char* image,
  56. int w, int h, size_t rowBytes);
  57. /** Given width and height of original image, return size (in bytes) of distance field
  58. * @param w Width of the original image.
  59. * @param h Height of the original image.
  60. */
  61. inline size_t SkComputeDistanceFieldSize(int w, int h) {
  62. return (w + 2*SK_DistanceFieldPad) * (h + 2*SK_DistanceFieldPad) * sizeof(unsigned char);
  63. }
  64. #endif