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