blob: c020b40a31ee0211c2b18f162a691fafba76818b [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
17#define LOG_TAG "OpenGLRenderer"
18
19#include <math.h>
20
21#include "Blur.h"
22
23namespace android {
24namespace uirenderer {
25
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040026// This constant approximates the scaling done in the software path's
27// "high quality" mode, in SkBlurMask::Blur() (1 / sqrt(3)).
28static const float BLUR_SIGMA_SCALE = 0.57735f;
29
30float Blur::convertRadiusToSigma(float radius) {
31 return radius > 0 ? BLUR_SIGMA_SCALE * radius + 0.5f : 0.0f;
32}
33
34float Blur::convertSigmaToRadius(float sigma) {
35 return sigma > 0.5f ? (sigma - 0.5f) / BLUR_SIGMA_SCALE : 0.0f;
36}
37
38/**
39 * HWUI has used a slightly different equation than Skia to generate the value
40 * for sigma and to preserve compatibility we have kept that logic.
41 *
42 * Based on some experimental radius and sigma values we approximate the
43 * equation sigma = f(radius) as sigma = radius * 0.3 + 0.6. The larger the
44 * radius gets, the more our gaussian blur will resemble a box blur since with
45 * large sigma the gaussian curve begins to lose its shape.
46 */
47static float legacyConvertRadiusToSigma(float radius) {
48 return radius > 0 ? 0.3f * radius + 0.6f : 0.0f;
49}
50
Romain Guy6e200402013-03-08 11:28:22 -080051void Blur::generateGaussianWeights(float* weights, int32_t radius) {
52 // Compute gaussian weights for the blur
53 // e is the euler's number
54 static float e = 2.718281828459045f;
55 static float pi = 3.1415926535897932f;
56 // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
57 // x is of the form [-radius .. 0 .. radius]
58 // and sigma varies with radius.
Derek Sollenbergerc29a0a42014-03-31 13:52:39 -040059 float sigma = legacyConvertRadiusToSigma((float) radius);
Romain Guy6e200402013-03-08 11:28:22 -080060
61 // Now compute the coefficints
62 // We will store some redundant values to save some math during
63 // the blur calculations
64 // precompute some values
65 float coeff1 = 1.0f / (sqrt(2.0f * pi) * sigma);
66 float coeff2 = - 1.0f / (2.0f * sigma * sigma);
67
68 float normalizeFactor = 0.0f;
69 for (int32_t r = -radius; r <= radius; r ++) {
70 float floatR = (float) r;
71 weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2);
72 normalizeFactor += weights[r + radius];
73 }
74
75 //Now we need to normalize the weights because all our coefficients need to add up to one
76 normalizeFactor = 1.0f / normalizeFactor;
77 for (int32_t r = -radius; r <= radius; r ++) {
78 weights[r + radius] *= normalizeFactor;
79 }
80}
81
82void Blur::horizontal(float* weights, int32_t radius,
83 const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
84 float blurredPixel = 0.0f;
85 float currentPixel = 0.0f;
86
87 for (int32_t y = 0; y < height; y ++) {
88
89 const uint8_t* input = source + y * width;
90 uint8_t* output = dest + y * width;
91
92 for (int32_t x = 0; x < width; x ++) {
93 blurredPixel = 0.0f;
94 const float* gPtr = weights;
95 // Optimization for non-border pixels
96 if (x > radius && x < (width - radius)) {
97 const uint8_t *i = input + (x - radius);
98 for (int r = -radius; r <= radius; r ++) {
99 currentPixel = (float) (*i);
100 blurredPixel += currentPixel * gPtr[0];
101 gPtr++;
102 i++;
103 }
104 } else {
105 for (int32_t r = -radius; r <= radius; r ++) {
106 // Stepping left and right away from the pixel
107 int validW = x + r;
108 if (validW < 0) {
109 validW = 0;
110 }
111 if (validW > width - 1) {
112 validW = width - 1;
113 }
114
115 currentPixel = (float) input[validW];
116 blurredPixel += currentPixel * gPtr[0];
117 gPtr++;
118 }
119 }
120 *output = (uint8_t)blurredPixel;
121 output ++;
122 }
123 }
124}
125
126void Blur::vertical(float* weights, int32_t radius,
127 const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
128 float blurredPixel = 0.0f;
129 float currentPixel = 0.0f;
130
131 for (int32_t y = 0; y < height; y ++) {
132 uint8_t* output = dest + y * width;
133
134 for (int32_t x = 0; x < width; x ++) {
135 blurredPixel = 0.0f;
136 const float* gPtr = weights;
137 const uint8_t* input = source + x;
138 // Optimization for non-border pixels
139 if (y > radius && y < (height - radius)) {
140 const uint8_t *i = input + ((y - radius) * width);
141 for (int32_t r = -radius; r <= radius; r ++) {
142 currentPixel = (float) (*i);
143 blurredPixel += currentPixel * gPtr[0];
144 gPtr++;
145 i += width;
146 }
147 } else {
148 for (int32_t r = -radius; r <= radius; r ++) {
149 int validH = y + r;
150 // Clamp to zero and width
151 if (validH < 0) {
152 validH = 0;
153 }
154 if (validH > height - 1) {
155 validH = height - 1;
156 }
157
158 const uint8_t *i = input + validH * width;
159 currentPixel = (float) (*i);
160 blurredPixel += currentPixel * gPtr[0];
161 gPtr++;
162 }
163 }
164 *output = (uint8_t) blurredPixel;
165 output++;
166 }
167 }
168}
169
170}; // namespace uirenderer
171}; // namespace android