SkBlurMask.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * Copyright 2006 The Android Open Source Project
  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 SkBlurMask_DEFINED
  8. #define SkBlurMask_DEFINED
  9. #include "include/core/SkBlurTypes.h"
  10. #include "include/core/SkRRect.h"
  11. #include "include/core/SkShader.h"
  12. #include "src/core/SkMask.h"
  13. class SkBlurMask {
  14. public:
  15. static bool SK_WARN_UNUSED_RESULT BlurRect(SkScalar sigma, SkMask *dst, const SkRect &src,
  16. SkBlurStyle, SkIPoint *margin = nullptr,
  17. SkMask::CreateMode createMode =
  18. SkMask::kComputeBoundsAndRenderImage_CreateMode);
  19. static bool SK_WARN_UNUSED_RESULT BlurRRect(SkScalar sigma, SkMask *dst, const SkRRect &src,
  20. SkBlurStyle, SkIPoint *margin = nullptr,
  21. SkMask::CreateMode createMode =
  22. SkMask::kComputeBoundsAndRenderImage_CreateMode);
  23. // forceQuality will prevent BoxBlur from falling back to the low quality approach when sigma
  24. // is very small -- this can be used predict the margin bump ahead of time without completely
  25. // replicating the internal logic. This permits not only simpler caching of blurred results,
  26. // but also being able to predict precisely at what pixels the blurred profile of e.g. a
  27. // rectangle will lie.
  28. //
  29. // Calling details:
  30. // * calculate margin - if src.fImage is null, then this call only calculates the border.
  31. // * failure - if src.fImage is not null, failure is signal with dst->fImage being
  32. // null.
  33. static bool SK_WARN_UNUSED_RESULT BoxBlur(SkMask* dst, const SkMask& src,
  34. SkScalar sigma, SkBlurStyle style,
  35. SkIPoint* margin = nullptr);
  36. // the "ground truth" blur does a gaussian convolution; it's slow
  37. // but useful for comparison purposes.
  38. static bool SK_WARN_UNUSED_RESULT BlurGroundTruth(SkScalar sigma, SkMask* dst,
  39. const SkMask& src,
  40. SkBlurStyle, SkIPoint* margin = nullptr);
  41. // If radius > 0, return the corresponding sigma, else return 0
  42. static SkScalar SK_API ConvertRadiusToSigma(SkScalar radius);
  43. // If sigma > 0.5, return the corresponding radius, else return 0
  44. static SkScalar SK_API ConvertSigmaToRadius(SkScalar sigma);
  45. /* Helper functions for analytic rectangle blurs */
  46. /** Look up the intensity of the (one dimnensional) blurred half-plane.
  47. @param profile The precomputed 1D blur profile; initialized by ComputeBlurProfile below.
  48. @param loc the location to look up; The lookup will clamp invalid inputs, but
  49. meaningful data are available between 0 and blurred_width
  50. @param blurred_width The width of the final, blurred rectangle
  51. @param sharp_width The width of the original, unblurred rectangle.
  52. */
  53. static uint8_t ProfileLookup(const uint8_t* profile, int loc, int blurredWidth, int sharpWidth);
  54. /** Populate the profile of a 1D blurred halfplane.
  55. @param profile The 1D table to fill in
  56. @param size Should be 6*sigma bytes
  57. @param sigma The standard deviation of the gaussian blur kernel
  58. */
  59. static void ComputeBlurProfile(uint8_t* profile, int size, SkScalar sigma);
  60. /** Compute an entire scanline of a blurred step function. This is a 1D helper that
  61. will produce both the horizontal and vertical profiles of the blurry rectangle.
  62. @param pixels Location to store the resulting pixel data; allocated and managed by caller
  63. @param profile Precomputed blur profile computed by ComputeBlurProfile above.
  64. @param width Size of the pixels array.
  65. @param sigma Standard deviation of the gaussian blur kernel used to compute the profile;
  66. this implicitly gives the size of the pixels array.
  67. */
  68. static void ComputeBlurredScanline(uint8_t* pixels, const uint8_t* profile,
  69. unsigned int width, SkScalar sigma);
  70. };
  71. #endif