1/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <math.h>
18
19#include "Blur.h"
20#include "MathUtils.h"
21
22namespace android {
23namespace uirenderer {
24
25// This constant approximates the scaling done in the software path's
26// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
27static const float BLUR_SIGMA_SCALE = 0.57735f;
28
29float Blur::convertRadiusToSigma(float radius) {
30    return radius > 0 ? BLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
31}
32
33float Blur::convertSigmaToRadius(float sigma) {
34    return sigma > 0.5f ? (sigma - 0.5f) / BLUR_SIGMA_SCALE : 0.0f;
35}
36
37// if the original radius was on an integer boundary and the resulting radius
38// is within the conversion error tolerance then we attempt to snap to the
39// original integer boundary.
40uint32_t Blur::convertRadiusToInt(float radius) {
41    const float radiusCeil  = ceilf(radius);
42    if (MathUtils::areEqual(radiusCeil, radius)) {
43        return radiusCeil;
44    }
45    return radius;
46}
47
48/**
49 * HWUI has used a slightly different equation than Skia to generate the value
50 * for sigma and to preserve compatibility we have kept that logic.
51 *
52 * Based on some experimental radius and sigma values we approximate the
53 * equation sigma = f(radius) as sigma = radius * 0.3  + 0.6.  The larger the
54 * radius gets, the more our gaussian blur will resemble a box blur since with
55 * large sigma the gaussian curve begins to lose its shape.
56 */
57static float legacyConvertRadiusToSigma(float radius) {
58    return radius > 0 ? 0.3f * radius + 0.6f : 0.0f;
59}
60
61void Blur::generateGaussianWeights(float* weights, float radius) {
62    int32_t intRadius = convertRadiusToInt(radius);
63
64    // Compute gaussian weights for the blur
65    // e is the euler's number
66    static float e = 2.718281828459045f;
67    static float pi = 3.1415926535897932f;
68    // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
69    // x is of the form [-radius .. 0 .. radius]
70    // and sigma varies with radius.
71    float sigma = legacyConvertRadiusToSigma(radius);
72
73    // Now compute the coefficints
74    // We will store some redundant values to save some math during
75    // the blur calculations
76    // precompute some values
77    float coeff1 = 1.0f / (sqrt(2.0f * pi) * sigma);
78    float coeff2 = - 1.0f / (2.0f * sigma * sigma);
79
80    float normalizeFactor = 0.0f;
81    for (int32_t r = -intRadius; r <= intRadius; r ++) {
82        float floatR = (float) r;
83        weights[r + intRadius] = coeff1 * pow(e, floatR * floatR * coeff2);
84        normalizeFactor += weights[r + intRadius];
85    }
86
87    //Now we need to normalize the weights because all our coefficients need to add up to one
88    normalizeFactor = 1.0f / normalizeFactor;
89    for (int32_t r = -intRadius; r <= intRadius; r ++) {
90        weights[r + intRadius] *= normalizeFactor;
91    }
92}
93
94void Blur::horizontal(float* weights, int32_t radius,
95        const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
96    float blurredPixel = 0.0f;
97    float currentPixel = 0.0f;
98
99    for (int32_t y = 0; y < height; y ++) {
100
101        const uint8_t* input = source + y * width;
102        uint8_t* output = dest + y * width;
103
104        for (int32_t x = 0; x < width; x ++) {
105            blurredPixel = 0.0f;
106            const float* gPtr = weights;
107            // Optimization for non-border pixels
108            if (x > radius && x < (width - radius)) {
109                const uint8_t *i = input + (x - radius);
110                for (int r = -radius; r <= radius; r ++) {
111                    currentPixel = (float) (*i);
112                    blurredPixel += currentPixel * gPtr[0];
113                    gPtr++;
114                    i++;
115                }
116            } else {
117                for (int32_t r = -radius; r <= radius; r ++) {
118                    // Stepping left and right away from the pixel
119                    int validW = x + r;
120                    if (validW < 0) {
121                        validW = 0;
122                    }
123                    if (validW > width - 1) {
124                        validW = width - 1;
125                    }
126
127                    currentPixel = (float) input[validW];
128                    blurredPixel += currentPixel * gPtr[0];
129                    gPtr++;
130                }
131            }
132            *output = (uint8_t)blurredPixel;
133            output ++;
134        }
135    }
136}
137
138void Blur::vertical(float* weights, int32_t radius,
139        const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
140    float blurredPixel = 0.0f;
141    float currentPixel = 0.0f;
142
143    for (int32_t y = 0; y < height; y ++) {
144        uint8_t* output = dest + y * width;
145
146        for (int32_t x = 0; x < width; x ++) {
147            blurredPixel = 0.0f;
148            const float* gPtr = weights;
149            const uint8_t* input = source + x;
150            // Optimization for non-border pixels
151            if (y > radius && y < (height - radius)) {
152                const uint8_t *i = input + ((y - radius) * width);
153                for (int32_t r = -radius; r <= radius; r ++) {
154                    currentPixel = (float) (*i);
155                    blurredPixel += currentPixel * gPtr[0];
156                    gPtr++;
157                    i += width;
158                }
159            } else {
160                for (int32_t r = -radius; r <= radius; r ++) {
161                    int validH = y + r;
162                    // Clamp to zero and width
163                    if (validH < 0) {
164                        validH = 0;
165                    }
166                    if (validH > height - 1) {
167                        validH = height - 1;
168                    }
169
170                    const uint8_t *i = input + validH * width;
171                    currentPixel = (float) (*i);
172                    blurredPixel += currentPixel * gPtr[0];
173                    gPtr++;
174                }
175            }
176            *output = (uint8_t) blurredPixel;
177            output++;
178        }
179    }
180}
181
182}; // namespace uirenderer
183}; // namespace android
184