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
22 namespace android {
23 namespace uirenderer {
24
25 // This constant approximates the scaling done in the software path's
26 // "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
27 static const float BLUR_SIGMA_SCALE = 0.57735f;
28
convertRadiusToSigma(float radius)29 float Blur::convertRadiusToSigma(float radius) {
30 return radius > 0 ? BLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
31 }
32
convertSigmaToRadius(float sigma)33 float 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.
convertRadiusToInt(float radius)40 uint32_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 */
legacyConvertRadiusToSigma(float radius)57 static float legacyConvertRadiusToSigma(float radius) {
58 return radius > 0 ? 0.3f * radius + 0.6f : 0.0f;
59 }
60
generateGaussianWeights(float * weights,float radius)61 void 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
horizontal(float * weights,int32_t radius,const uint8_t * source,uint8_t * dest,int32_t width,int32_t height)94 void Blur::horizontal(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
95 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 const uint8_t* input = source + y * width;
101 uint8_t* output = dest + y * width;
102
103 for (int32_t x = 0; x < width; x++) {
104 blurredPixel = 0.0f;
105 const float* gPtr = weights;
106 // Optimization for non-border pixels
107 if (x > radius && x < (width - radius)) {
108 const uint8_t* i = input + (x - radius);
109 for (int r = -radius; r <= radius; r++) {
110 currentPixel = (float)(*i);
111 blurredPixel += currentPixel * gPtr[0];
112 gPtr++;
113 i++;
114 }
115 } else {
116 for (int32_t r = -radius; r <= radius; r++) {
117 // Stepping left and right away from the pixel
118 int validW = x + r;
119 if (validW < 0) {
120 validW = 0;
121 }
122 if (validW > width - 1) {
123 validW = width - 1;
124 }
125
126 currentPixel = (float)input[validW];
127 blurredPixel += currentPixel * gPtr[0];
128 gPtr++;
129 }
130 }
131 *output = (uint8_t)blurredPixel;
132 output++;
133 }
134 }
135 }
136
vertical(float * weights,int32_t radius,const uint8_t * source,uint8_t * dest,int32_t width,int32_t height)137 void Blur::vertical(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
138 int32_t width, int32_t height) {
139 float blurredPixel = 0.0f;
140 float currentPixel = 0.0f;
141
142 for (int32_t y = 0; y < height; y++) {
143 uint8_t* output = dest + y * width;
144
145 for (int32_t x = 0; x < width; x++) {
146 blurredPixel = 0.0f;
147 const float* gPtr = weights;
148 const uint8_t* input = source + x;
149 // Optimization for non-border pixels
150 if (y > radius && y < (height - radius)) {
151 const uint8_t* i = input + ((y - radius) * width);
152 for (int32_t r = -radius; r <= radius; r++) {
153 currentPixel = (float)(*i);
154 blurredPixel += currentPixel * gPtr[0];
155 gPtr++;
156 i += width;
157 }
158 } else {
159 for (int32_t r = -radius; r <= radius; r++) {
160 int validH = y + r;
161 // Clamp to zero and width
162 if (validH < 0) {
163 validH = 0;
164 }
165 if (validH > height - 1) {
166 validH = height - 1;
167 }
168
169 const uint8_t* i = input + validH * width;
170 currentPixel = (float)(*i);
171 blurredPixel += currentPixel * gPtr[0];
172 gPtr++;
173 }
174 }
175 *output = (uint8_t)blurredPixel;
176 output++;
177 }
178 }
179 }
180
181 }; // namespace uirenderer
182 }; // namespace android
183