Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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 | |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 17 | #include <math.h> |
| 18 | |
| 19 | #include "Blur.h" |
Derek Sollenberger | e392c81 | 2014-05-21 11:25:22 -0400 | [diff] [blame] | 20 | #include "MathUtils.h" |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 21 | |
| 22 | namespace android { |
| 23 | namespace uirenderer { |
| 24 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 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 | |
| 29 | float Blur::convertRadiusToSigma(float radius) { |
| 30 | return radius > 0 ? BLUR_SIGMA_SCALE * radius + 0.5f : 0.0f; |
| 31 | } |
| 32 | |
| 33 | float Blur::convertSigmaToRadius(float sigma) { |
| 34 | return sigma > 0.5f ? (sigma - 0.5f) / BLUR_SIGMA_SCALE : 0.0f; |
| 35 | } |
| 36 | |
Derek Sollenberger | e392c81 | 2014-05-21 11:25:22 -0400 | [diff] [blame] | 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. |
| 40 | uint32_t Blur::convertRadiusToInt(float radius) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 41 | const float radiusCeil = ceilf(radius); |
Derek Sollenberger | e392c81 | 2014-05-21 11:25:22 -0400 | [diff] [blame] | 42 | if (MathUtils::areEqual(radiusCeil, radius)) { |
| 43 | return radiusCeil; |
| 44 | } |
| 45 | return radius; |
| 46 | } |
| 47 | |
Derek Sollenberger | c29a0a4 | 2014-03-31 13:52:39 -0400 | [diff] [blame] | 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 | */ |
| 57 | static float legacyConvertRadiusToSigma(float radius) { |
| 58 | return radius > 0 ? 0.3f * radius + 0.6f : 0.0f; |
| 59 | } |
| 60 | |
huanhuan.x.wang | a46ca5e | 2015-04-14 16:23:15 +0200 | [diff] [blame] | 61 | void Blur::generateGaussianWeights(float* weights, float radius) { |
| 62 | int32_t intRadius = convertRadiusToInt(radius); |
| 63 | |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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. |
huanhuan.x.wang | a46ca5e | 2015-04-14 16:23:15 +0200 | [diff] [blame] | 71 | float sigma = legacyConvertRadiusToSigma(radius); |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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); |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 78 | float coeff2 = -1.0f / (2.0f * sigma * sigma); |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 79 | |
| 80 | float normalizeFactor = 0.0f; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 81 | for (int32_t r = -intRadius; r <= intRadius; r++) { |
| 82 | float floatR = (float)r; |
huanhuan.x.wang | a46ca5e | 2015-04-14 16:23:15 +0200 | [diff] [blame] | 83 | weights[r + intRadius] = coeff1 * pow(e, floatR * floatR * coeff2); |
| 84 | normalizeFactor += weights[r + intRadius]; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 85 | } |
| 86 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 87 | // Now we need to normalize the weights because all our coefficients need to add up to one |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 88 | normalizeFactor = 1.0f / normalizeFactor; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 89 | for (int32_t r = -intRadius; r <= intRadius; r++) { |
huanhuan.x.wang | a46ca5e | 2015-04-14 16:23:15 +0200 | [diff] [blame] | 90 | weights[r + intRadius] *= normalizeFactor; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 94 | void Blur::horizontal(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest, |
| 95 | int32_t width, int32_t height) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 96 | float blurredPixel = 0.0f; |
| 97 | float currentPixel = 0.0f; |
| 98 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 99 | for (int32_t y = 0; y < height; y++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 100 | const uint8_t* input = source + y * width; |
| 101 | uint8_t* output = dest + y * width; |
| 102 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 103 | for (int32_t x = 0; x < width; x++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 104 | blurredPixel = 0.0f; |
| 105 | const float* gPtr = weights; |
| 106 | // Optimization for non-border pixels |
| 107 | if (x > radius && x < (width - radius)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 108 | const uint8_t* i = input + (x - radius); |
| 109 | for (int r = -radius; r <= radius; r++) { |
| 110 | currentPixel = (float)(*i); |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 111 | blurredPixel += currentPixel * gPtr[0]; |
| 112 | gPtr++; |
| 113 | i++; |
| 114 | } |
| 115 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 116 | for (int32_t r = -radius; r <= radius; r++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 126 | currentPixel = (float)input[validW]; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 127 | blurredPixel += currentPixel * gPtr[0]; |
| 128 | gPtr++; |
| 129 | } |
| 130 | } |
| 131 | *output = (uint8_t)blurredPixel; |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 132 | output++; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | } |
| 136 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 137 | void Blur::vertical(float* weights, int32_t radius, const uint8_t* source, uint8_t* dest, |
| 138 | int32_t width, int32_t height) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 139 | float blurredPixel = 0.0f; |
| 140 | float currentPixel = 0.0f; |
| 141 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 142 | for (int32_t y = 0; y < height; y++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 143 | uint8_t* output = dest + y * width; |
| 144 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 145 | for (int32_t x = 0; x < width; x++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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)) { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 151 | const uint8_t* i = input + ((y - radius) * width); |
| 152 | for (int32_t r = -radius; r <= radius; r++) { |
| 153 | currentPixel = (float)(*i); |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 154 | blurredPixel += currentPixel * gPtr[0]; |
| 155 | gPtr++; |
| 156 | i += width; |
| 157 | } |
| 158 | } else { |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 159 | for (int32_t r = -radius; r <= radius; r++) { |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 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 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 169 | const uint8_t* i = input + validH * width; |
| 170 | currentPixel = (float)(*i); |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 171 | blurredPixel += currentPixel * gPtr[0]; |
| 172 | gPtr++; |
| 173 | } |
| 174 | } |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 175 | *output = (uint8_t)blurredPixel; |
Romain Guy | 6e20040 | 2013-03-08 11:28:22 -0800 | [diff] [blame] | 176 | output++; |
| 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
John Reck | 1bcacfd | 2017-11-03 10:12:19 -0700 | [diff] [blame] | 181 | }; // namespace uirenderer |
| 182 | }; // namespace android |