blob: 763d1aa177b75469bc57dd8b8bda75a552e94b2d [file] [log] [blame]
Romain Guy6e200402013-03-08 11:28:22 -08001/*
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
Romain Guy6e200402013-03-08 11:28:22 -080017#include <math.h>
18
19#include "Blur.h"
Derek Sollenbergere392c812014-05-21 11:25:22 -040020#include "MathUtils.h"
Romain Guy6e200402013-03-08 11:28:22 -080021
22namespace android {
23namespace uirenderer {
24
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040025// 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
Derek Sollenbergere392c812014-05-21 11:25:22 -040037// 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) {
John Reck1bcacfd2017-11-03 10:12:19 -070041 const float radiusCeil = ceilf(radius);
Derek Sollenbergere392c812014-05-21 11:25:22 -040042 if (MathUtils::areEqual(radiusCeil, radius)) {
43 return radiusCeil;
44 }
45 return radius;
46}
47
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040048/**
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
huanhuan.x.wanga46ca5e2015-04-14 16:23:15 +020061void Blur::generateGaussianWeights(float* weights, float radius) {
62 int32_t intRadius = convertRadiusToInt(radius);
63
Romain Guy6e200402013-03-08 11:28:22 -080064 // 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.
huanhuan.x.wanga46ca5e2015-04-14 16:23:15 +020071 float sigma = legacyConvertRadiusToSigma(radius);
Romain Guy6e200402013-03-08 11:28:22 -080072
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);
John Reck1bcacfd2017-11-03 10:12:19 -070078 float coeff2 = -1.0f / (2.0f * sigma * sigma);
Romain Guy6e200402013-03-08 11:28:22 -080079
80 float normalizeFactor = 0.0f;
John Reck1bcacfd2017-11-03 10:12:19 -070081 for (int32_t r = -intRadius; r <= intRadius; r++) {
82 float floatR = (float)r;
huanhuan.x.wanga46ca5e2015-04-14 16:23:15 +020083 weights[r + intRadius] = coeff1 * pow(e, floatR * floatR * coeff2);
84 normalizeFactor += weights[r + intRadius];
Romain Guy6e200402013-03-08 11:28:22 -080085 }
86
John Reck1bcacfd2017-11-03 10:12:19 -070087 // Now we need to normalize the weights because all our coefficients need to add up to one
Romain Guy6e200402013-03-08 11:28:22 -080088 normalizeFactor = 1.0f / normalizeFactor;
John Reck1bcacfd2017-11-03 10:12:19 -070089 for (int32_t r = -intRadius; r <= intRadius; r++) {
huanhuan.x.wanga46ca5e2015-04-14 16:23:15 +020090 weights[r + intRadius] *= normalizeFactor;
Romain Guy6e200402013-03-08 11:28:22 -080091 }
92}
93
John Reck1bcacfd2017-11-03 10:12:19 -070094void Blur::horizontal(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
95 int32_t width, int32_t height) {
Romain Guy6e200402013-03-08 11:28:22 -080096 float blurredPixel = 0.0f;
97 float currentPixel = 0.0f;
98
John Reck1bcacfd2017-11-03 10:12:19 -070099 for (int32_t y = 0; y < height; y++) {
Romain Guy6e200402013-03-08 11:28:22 -0800100 const uint8_t* input = source + y * width;
101 uint8_t* output = dest + y * width;
102
John Reck1bcacfd2017-11-03 10:12:19 -0700103 for (int32_t x = 0; x < width; x++) {
Romain Guy6e200402013-03-08 11:28:22 -0800104 blurredPixel = 0.0f;
105 const float* gPtr = weights;
106 // Optimization for non-border pixels
107 if (x > radius && x < (width - radius)) {
John Reck1bcacfd2017-11-03 10:12:19 -0700108 const uint8_t* i = input + (x - radius);
109 for (int r = -radius; r <= radius; r++) {
110 currentPixel = (float)(*i);
Romain Guy6e200402013-03-08 11:28:22 -0800111 blurredPixel += currentPixel * gPtr[0];
112 gPtr++;
113 i++;
114 }
115 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700116 for (int32_t r = -radius; r <= radius; r++) {
Romain Guy6e200402013-03-08 11:28:22 -0800117 // 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
John Reck1bcacfd2017-11-03 10:12:19 -0700126 currentPixel = (float)input[validW];
Romain Guy6e200402013-03-08 11:28:22 -0800127 blurredPixel += currentPixel * gPtr[0];
128 gPtr++;
129 }
130 }
131 *output = (uint8_t)blurredPixel;
John Reck1bcacfd2017-11-03 10:12:19 -0700132 output++;
Romain Guy6e200402013-03-08 11:28:22 -0800133 }
134 }
135}
136
John Reck1bcacfd2017-11-03 10:12:19 -0700137void Blur::vertical(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest,
138 int32_t width, int32_t height) {
Romain Guy6e200402013-03-08 11:28:22 -0800139 float blurredPixel = 0.0f;
140 float currentPixel = 0.0f;
141
John Reck1bcacfd2017-11-03 10:12:19 -0700142 for (int32_t y = 0; y < height; y++) {
Romain Guy6e200402013-03-08 11:28:22 -0800143 uint8_t* output = dest + y * width;
144
John Reck1bcacfd2017-11-03 10:12:19 -0700145 for (int32_t x = 0; x < width; x++) {
Romain Guy6e200402013-03-08 11:28:22 -0800146 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)) {
John Reck1bcacfd2017-11-03 10:12:19 -0700151 const uint8_t* i = input + ((y - radius) * width);
152 for (int32_t r = -radius; r <= radius; r++) {
153 currentPixel = (float)(*i);
Romain Guy6e200402013-03-08 11:28:22 -0800154 blurredPixel += currentPixel * gPtr[0];
155 gPtr++;
156 i += width;
157 }
158 } else {
John Reck1bcacfd2017-11-03 10:12:19 -0700159 for (int32_t r = -radius; r <= radius; r++) {
Romain Guy6e200402013-03-08 11:28:22 -0800160 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
John Reck1bcacfd2017-11-03 10:12:19 -0700169 const uint8_t* i = input + validH * width;
170 currentPixel = (float)(*i);
Romain Guy6e200402013-03-08 11:28:22 -0800171 blurredPixel += currentPixel * gPtr[0];
172 gPtr++;
173 }
174 }
John Reck1bcacfd2017-11-03 10:12:19 -0700175 *output = (uint8_t)blurredPixel;
Romain Guy6e200402013-03-08 11:28:22 -0800176 output++;
177 }
178 }
179}
180
Chris Blume7b8a8082018-11-30 15:51:58 -0800181} // namespace uirenderer
182} // namespace android