c10c577552a17c1e9b9fc1d1d63ac0e99f6f01c2
[platform/core/uifw/dali-adaptor.git] / dali / internal / imaging / common / gaussian-blur.cpp
1 /*
2  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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 // EXTERNAL INCLUDES
18 #include <memory.h>
19 #include <cmath>
20
21 // INTERNAL INCLUDES
22 #include <dali/internal/imaging/common/gaussian-blur.h>
23 #include <dali/internal/imaging/common/pixel-buffer-impl.h>
24
25 namespace Dali
26 {
27 namespace Internal
28 {
29 namespace Adaptor
30 {
31 void ConvoluteAndTranspose(unsigned char*     inBuffer,
32                            unsigned char*     outBuffer,
33                            const unsigned int bufferWidth,
34                            const unsigned int bufferHeight,
35                            const float        blurRadius)
36 {
37   // Calculate the weights for gaussian blur
38   int radius = static_cast<int>(std::ceil(blurRadius));
39   int rows   = radius * 2 + 1;
40
41   float sigma           = (blurRadius < Math::MACHINE_EPSILON_1) ? 0.0f : blurRadius * 0.4f + 0.6f; // The same equation used by Android
42   float sigma22         = 2.0f * sigma * sigma;
43   float sqrtSigmaPi2    = std::sqrt(2.0f * Math::PI) * sigma;
44   float radius2         = radius * radius;
45   float normalizeFactor = 0.0f;
46
47   float* weightMatrix = new float[rows];
48   int    index        = 0;
49
50   for(int row = -radius; row <= radius; row++)
51   {
52     float distance = row * row;
53     if(distance > radius2)
54     {
55       weightMatrix[index] = 0.0f;
56     }
57     else
58     {
59       weightMatrix[index] = static_cast<float>(std::exp(-(distance) / sigma22) / sqrtSigmaPi2);
60     }
61     normalizeFactor += weightMatrix[index];
62     index++;
63   }
64
65   for(int i = 0; i < rows; i++)
66   {
67     weightMatrix[i] /= normalizeFactor;
68   }
69
70   // Perform the convolution and transposition using the weights
71   int columns  = rows;
72   int columns2 = columns / 2;
73   for(unsigned int y = 0; y < bufferHeight; y++)
74   {
75     unsigned int targetPixelIndex = y;
76     unsigned int ioffset          = y * bufferWidth;
77     for(unsigned int x = 0; x < bufferWidth; x++)
78     {
79       float r = 0.0f, g = 0.0f, b = 0.0f, a = 0.0f;
80       int   weightColumnOffset = columns2;
81       for(int column = -columns2; column <= columns2; column++)
82       {
83         float weight = weightMatrix[weightColumnOffset + column];
84         if(fabsf(weight) > Math::MACHINE_EPSILON_1)
85         {
86           int ix                        = x + column;
87           ix                            = std::max(0, std::min(ix, static_cast<int>(bufferWidth - 1)));
88           unsigned int sourcePixelIndex = ioffset + ix;
89           r += weight * inBuffer[sourcePixelIndex * 4];
90           g += weight * inBuffer[sourcePixelIndex * 4 + 1];
91           b += weight * inBuffer[sourcePixelIndex * 4 + 2];
92           a += weight * inBuffer[sourcePixelIndex * 4 + 3];
93         }
94       }
95
96       outBuffer[targetPixelIndex * 4]     = std::max(0, std::min(static_cast<int>(r + 0.5f), 255));
97       outBuffer[targetPixelIndex * 4 + 1] = std::max(0, std::min(static_cast<int>(g + 0.5f), 255));
98       outBuffer[targetPixelIndex * 4 + 2] = std::max(0, std::min(static_cast<int>(b + 0.5f), 255));
99       outBuffer[targetPixelIndex * 4 + 3] = std::max(0, std::min(static_cast<int>(a + 0.5f), 255));
100
101       targetPixelIndex += bufferHeight;
102     }
103   }
104
105   delete[] weightMatrix;
106 }
107
108 void PerformGaussianBlurRGBA(PixelBuffer& buffer, const float blurRadius)
109 {
110   unsigned int bufferWidth  = buffer.GetWidth();
111   unsigned int bufferHeight = buffer.GetHeight();
112
113   // Create a temporary buffer for the two-pass blur
114   PixelBufferPtr softShadowImageBuffer = PixelBuffer::New(bufferWidth, bufferHeight, Pixel::RGBA8888);
115   memcpy(softShadowImageBuffer->GetBuffer(), buffer.GetBuffer(), 4u * bufferWidth * bufferHeight);
116
117   // We perform the blur first but write its output image buffer transposed, so that we
118   // can just do it in two passes. The first pass blurs horizontally and transposes, the
119   // second pass does the same, but as the image is now transposed, it's really doing a
120   // vertical blur. The second transposition makes the image the right way up again. This
121   // is much faster than doing a 2D convolution.
122   ConvoluteAndTranspose(buffer.GetBuffer(), softShadowImageBuffer->GetBuffer(), bufferWidth, bufferHeight, blurRadius);
123   ConvoluteAndTranspose(softShadowImageBuffer->GetBuffer(), buffer.GetBuffer(), bufferHeight, bufferWidth, blurRadius);
124
125   // On leaving scope, softShadowImageBuffer will get destroyed.
126 }
127
128 } //namespace Adaptor
129
130 } // namespace Internal
131
132 } // namespace Dali