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