Home | History | Annotate | Download | only in effects
      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 
     16 class SkBlurMask {
     17 public:
     18     static bool SK_WARN_UNUSED_RESULT BlurRect(SkScalar sigma, SkMask *dst, const SkRect &src,
     19                                                SkBlurStyle, SkIPoint *margin = nullptr,
     20                                                SkMask::CreateMode createMode =
     21                                                   SkMask::kComputeBoundsAndRenderImage_CreateMode);
     22     static bool SK_WARN_UNUSED_RESULT BlurRRect(SkScalar sigma, SkMask *dst, const SkRRect &src,
     23                                                 SkBlurStyle, SkIPoint *margin = nullptr,
     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     // Calling details:
     34     // * calculate margin - if src.fImage is null, then this call only calculates the border.
     35     // * failure          - if src.fImage is not null, failure is signal with dst->fImage being
     36     //                      null.
     37 
     38     static bool SK_WARN_UNUSED_RESULT BoxBlur(SkMask* dst, const SkMask& src,
     39                                               SkScalar sigma, SkBlurStyle style, SkBlurQuality,
     40                                               SkIPoint* margin = nullptr,
     41                                               bool forceQuality = false);
     42 
     43     // the "ground truth" blur does a gaussian convolution; it's slow
     44     // but useful for comparison purposes.
     45     static bool SK_WARN_UNUSED_RESULT BlurGroundTruth(SkScalar sigma, SkMask* dst,
     46                                                       const SkMask& src,
     47                                                       SkBlurStyle, SkIPoint* margin = nullptr);
     48 
     49     // If radius > 0, return the corresponding sigma, else return 0
     50     static SkScalar SK_API ConvertRadiusToSigma(SkScalar radius);
     51     // If sigma > 0.5, return the corresponding radius, else return 0
     52     static SkScalar SK_API ConvertSigmaToRadius(SkScalar sigma);
     53 
     54     /* Helper functions for analytic rectangle blurs */
     55 
     56     /** Look up the intensity of the (one dimnensional) blurred half-plane.
     57         @param profile The precomputed 1D blur profile; initialized by ComputeBlurProfile below.
     58         @param loc the location to look up; The lookup will clamp invalid inputs, but
     59                    meaningful data are available between 0 and blurred_width
     60         @param blurred_width The width of the final, blurred rectangle
     61         @param sharp_width The width of the original, unblurred rectangle.
     62     */
     63     static uint8_t ProfileLookup(const uint8_t* profile, int loc, int blurredWidth, int sharpWidth);
     64 
     65     /** Populate the profile of a 1D blurred halfplane.
     66         @param profile The 1D table to fill in
     67         @param size    Should be 6*sigma bytes
     68         @param sigma   The standard deviation of the gaussian blur kernel
     69     */
     70     static void ComputeBlurProfile(uint8_t* profile, int size, SkScalar sigma);
     71 
     72     /** Compute an entire scanline of a blurred step function.  This is a 1D helper that
     73         will produce both the horizontal and vertical profiles of the blurry rectangle.
     74         @param pixels Location to store the resulting pixel data; allocated and managed by caller
     75         @param profile Precomputed blur profile computed by ComputeBlurProfile above.
     76         @param width Size of the pixels array.
     77         @param sigma Standard deviation of the gaussian blur kernel used to compute the profile;
     78                      this implicitly gives the size of the pixels array.
     79     */
     80 
     81     static void ComputeBlurredScanline(uint8_t* pixels, const uint8_t* profile,
     82                                        unsigned int width, SkScalar sigma);
     83 
     84 
     85 
     86 };
     87 
     88 #endif
     89