blob: 6884aedc2bff7a8017f85a5a75e260d188dda92d [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) {
41 const float radiusCeil = ceilf(radius);
42 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
Romain Guy6e200402013-03-08 11:28:22 -080061void Blur::generateGaussianWeights(float* weights, int32_t radius) {
62 // Compute gaussian weights for the blur
63 // e is the euler's number
64 static float e = 2.718281828459045f;
65 static float pi = 3.1415926535897932f;
66 // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
67 // x is of the form [-radius .. 0 .. radius]
68 // and sigma varies with radius.
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040069 float sigma = legacyConvertRadiusToSigma((float) radius);
Romain Guy6e200402013-03-08 11:28:22 -080070
71 // Now compute the coefficints
72 // We will store some redundant values to save some math during
73 // the blur calculations
74 // precompute some values
75 float coeff1 = 1.0f / (sqrt(2.0f * pi) * sigma);
76 float coeff2 = - 1.0f / (2.0f * sigma * sigma);
77
78 float normalizeFactor = 0.0f;
79 for (int32_t r = -radius; r <= radius; r ++) {
80 float floatR = (float) r;
81 weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2);
82 normalizeFactor += weights[r + radius];
83 }
84
85 //Now we need to normalize the weights because all our coefficients need to add up to one
86 normalizeFactor = 1.0f / normalizeFactor;
87 for (int32_t r = -radius; r <= radius; r ++) {
88 weights[r + radius] *= normalizeFactor;
89 }
90}
91
92void Blur::horizontal(float* weights, int32_t radius,
93 const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
94 float blurredPixel = 0.0f;
95 float currentPixel = 0.0f;
96
97 for (int32_t y = 0; y < height; y ++) {
98
99 const uint8_t* input = source + y * width;
100 uint8_t* output = dest + y * width;
101
102 for (int32_t x = 0; x < width; x ++) {
103 blurredPixel = 0.0f;
104 const float* gPtr = weights;
105 // Optimization for non-border pixels
106 if (x > radius && x < (width - radius)) {
107 const uint8_t *i = input + (x - radius);
108 for (int r = -radius; r <= radius; r ++) {
109 currentPixel = (float) (*i);
110 blurredPixel += currentPixel * gPtr[0];
111 gPtr++;
112 i++;
113 }
114 } else {
115 for (int32_t r = -radius; r <= radius; r ++) {
116 // Stepping left and right away from the pixel
117 int validW = x + r;
118 if (validW < 0) {
119 validW = 0;
120 }
121 if (validW > width - 1) {
122 validW = width - 1;
123 }
124
125 currentPixel = (float) input[validW];
126 blurredPixel += currentPixel * gPtr[0];
127 gPtr++;
128 }
129 }
130 *output = (uint8_t)blurredPixel;
131 output ++;
132 }
133 }
134}
135
136void Blur::vertical(float* weights, int32_t radius,
137 const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
138 float blurredPixel = 0.0f;
139 float currentPixel = 0.0f;
140
141 for (int32_t y = 0; y < height; y ++) {
142 uint8_t* output = dest + y * width;
143
144 for (int32_t x = 0; x < width; x ++) {
145 blurredPixel = 0.0f;
146 const float* gPtr = weights;
147 const uint8_t* input = source + x;
148 // Optimization for non-border pixels
149 if (y > radius && y < (height - radius)) {
150 const uint8_t *i = input + ((y - radius) * width);
151 for (int32_t r = -radius; r <= radius; r ++) {
152 currentPixel = (float) (*i);
153 blurredPixel += currentPixel * gPtr[0];
154 gPtr++;
155 i += width;
156 }
157 } else {
158 for (int32_t r = -radius; r <= radius; r ++) {
159 int validH = y + r;
160 // Clamp to zero and width
161 if (validH < 0) {
162 validH = 0;
163 }
164 if (validH > height - 1) {
165 validH = height - 1;
166 }
167
168 const uint8_t *i = input + validH * width;
169 currentPixel = (float) (*i);
170 blurredPixel += currentPixel * gPtr[0];
171 gPtr++;
172 }
173 }
174 *output = (uint8_t) blurredPixel;
175 output++;
176 }
177 }
178}
179
180}; // namespace uirenderer
181}; // namespace android