Home | History | Annotate | Download | only in utils
      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 #define LOG_TAG "OpenGLRenderer"
     18 
     19 #include <math.h>
     20 
     21 #include "Blur.h"
     22 
     23 namespace android {
     24 namespace uirenderer {
     25 
     26 void Blur::generateGaussianWeights(float* weights, int32_t radius) {
     27     // Compute gaussian weights for the blur
     28     // e is the euler's number
     29     static float e = 2.718281828459045f;
     30     static float pi = 3.1415926535897932f;
     31     // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
     32     // x is of the form [-radius .. 0 .. radius]
     33     // and sigma varies with radius.
     34     // Based on some experimental radius values and sigma's
     35     // we approximately fit sigma = f(radius) as
     36     // sigma = radius * 0.3  + 0.6
     37     // The larger the radius gets, the more our gaussian blur
     38     // will resemble a box blur since with large sigma
     39     // the gaussian curve begins to lose its shape
     40     float sigma = 0.3f * (float) radius + 0.6f;
     41 
     42     // Now compute the coefficints
     43     // We will store some redundant values to save some math during
     44     // the blur calculations
     45     // precompute some values
     46     float coeff1 = 1.0f / (sqrt(2.0f * pi) * sigma);
     47     float coeff2 = - 1.0f / (2.0f * sigma * sigma);
     48 
     49     float normalizeFactor = 0.0f;
     50     for (int32_t r = -radius; r <= radius; r ++) {
     51         float floatR = (float) r;
     52         weights[r + radius] = coeff1 * pow(e, floatR * floatR * coeff2);
     53         normalizeFactor += weights[r + radius];
     54     }
     55 
     56     //Now we need to normalize the weights because all our coefficients need to add up to one
     57     normalizeFactor = 1.0f / normalizeFactor;
     58     for (int32_t r = -radius; r <= radius; r ++) {
     59         weights[r + radius] *= normalizeFactor;
     60     }
     61 }
     62 
     63 void Blur::horizontal(float* weights, int32_t radius,
     64         const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
     65     float blurredPixel = 0.0f;
     66     float currentPixel = 0.0f;
     67 
     68     for (int32_t y = 0; y < height; y ++) {
     69 
     70         const uint8_t* input = source + y * width;
     71         uint8_t* output = dest + y * width;
     72 
     73         for (int32_t x = 0; x < width; x ++) {
     74             blurredPixel = 0.0f;
     75             const float* gPtr = weights;
     76             // Optimization for non-border pixels
     77             if (x > radius && x < (width - radius)) {
     78                 const uint8_t *i = input + (x - radius);
     79                 for (int r = -radius; r <= radius; r ++) {
     80                     currentPixel = (float) (*i);
     81                     blurredPixel += currentPixel * gPtr[0];
     82                     gPtr++;
     83                     i++;
     84                 }
     85             } else {
     86                 for (int32_t r = -radius; r <= radius; r ++) {
     87                     // Stepping left and right away from the pixel
     88                     int validW = x + r;
     89                     if (validW < 0) {
     90                         validW = 0;
     91                     }
     92                     if (validW > width - 1) {
     93                         validW = width - 1;
     94                     }
     95 
     96                     currentPixel = (float) input[validW];
     97                     blurredPixel += currentPixel * gPtr[0];
     98                     gPtr++;
     99                 }
    100             }
    101             *output = (uint8_t)blurredPixel;
    102             output ++;
    103         }
    104     }
    105 }
    106 
    107 void Blur::vertical(float* weights, int32_t radius,
    108         const uint8_t* source, uint8_t* dest, int32_t width, int32_t height) {
    109     float blurredPixel = 0.0f;
    110     float currentPixel = 0.0f;
    111 
    112     for (int32_t y = 0; y < height; y ++) {
    113         uint8_t* output = dest + y * width;
    114 
    115         for (int32_t x = 0; x < width; x ++) {
    116             blurredPixel = 0.0f;
    117             const float* gPtr = weights;
    118             const uint8_t* input = source + x;
    119             // Optimization for non-border pixels
    120             if (y > radius && y < (height - radius)) {
    121                 const uint8_t *i = input + ((y - radius) * width);
    122                 for (int32_t r = -radius; r <= radius; r ++) {
    123                     currentPixel = (float) (*i);
    124                     blurredPixel += currentPixel * gPtr[0];
    125                     gPtr++;
    126                     i += width;
    127                 }
    128             } else {
    129                 for (int32_t r = -radius; r <= radius; r ++) {
    130                     int validH = y + r;
    131                     // Clamp to zero and width
    132                     if (validH < 0) {
    133                         validH = 0;
    134                     }
    135                     if (validH > height - 1) {
    136                         validH = height - 1;
    137                     }
    138 
    139                     const uint8_t *i = input + validH * width;
    140                     currentPixel = (float) (*i);
    141                     blurredPixel += currentPixel * gPtr[0];
    142                     gPtr++;
    143                 }
    144             }
    145             *output = (uint8_t) blurredPixel;
    146             output++;
    147         }
    148     }
    149 }
    150 
    151 }; // namespace uirenderer
    152 }; // namespace android
    153