aboutsummaryrefslogtreecommitdiffhomepage
path: root/tools/sk_tool_utils.cpp
diff options
context:
space:
mode:
authorGravatar robertphillips <robertphillips@google.com>2015-10-19 06:39:17 -0700
committerGravatar Commit bot <commit-bot@chromium.org>2015-10-19 06:39:17 -0700
commit9c4909b50ff9d0fdf9bce2a67cd459aeb28cdc3c (patch)
treee5ca55588af4b047c73ea0df40fd9557b57b7a54 /tools/sk_tool_utils.cpp
parentb3f1636ec80e66188ddc01306672a9a79a954dfe (diff)
Add imgblur tool to assist BlurMaskFilter debugging
imgblur is intended to establish a ground truth for debugging mask blur issues. It performs a brute force (non-separable) Gaussian blur of the provided image. The blur code itself is in sk_tools_utils so it can be more easily used programmatically in other places (e.g., blur unit tests). Review URL: https://codereview.chromium.org/1384203002
Diffstat (limited to 'tools/sk_tool_utils.cpp')
-rw-r--r--tools/sk_tool_utils.cpp94
1 files changed, 94 insertions, 0 deletions
diff --git a/tools/sk_tool_utils.cpp b/tools/sk_tool_utils.cpp
index 710c204271..9a667b4a45 100644
--- a/tools/sk_tool_utils.cpp
+++ b/tools/sk_tool_utils.cpp
@@ -349,4 +349,98 @@ void make_big_path(SkPath& path) {
#include "BigPathBench.inc"
}
+static float gaussian2d_value(int x, int y, float sigma) {
+ // don't bother with the scale term since we're just going to normalize the
+ // kernel anyways
+ float temp = exp(-(x*x + y*y)/(2*sigma*sigma));
+ return temp;
+}
+
+static float* create_2d_kernel(float sigma, int* filterSize) {
+ // We will actually take 2*halfFilterSize+1 samples (i.e., our filter kernel
+ // sizes are always odd)
+ int halfFilterSize = SkScalarCeilToInt(6*sigma)/2;
+ int wh = *filterSize = 2*halfFilterSize + 1;
+
+ float* temp = new float[wh*wh];
+
+ float filterTot = 0.0f;
+ for (int yOff = 0; yOff < wh; ++yOff) {
+ for (int xOff = 0; xOff < wh; ++xOff) {
+ temp[yOff*wh+xOff] = gaussian2d_value(xOff-halfFilterSize, yOff-halfFilterSize, sigma);
+
+ filterTot += temp[yOff*wh+xOff];
+ }
+ }
+
+ // normalize the kernel
+ for (int yOff = 0; yOff < wh; ++yOff) {
+ for (int xOff = 0; xOff < wh; ++xOff) {
+ temp[yOff*wh+xOff] /= filterTot;
+ }
+ }
+
+ return temp;
+}
+
+static SkPMColor blur_pixel(const SkBitmap& bm, int x, int y, float* kernel, int wh) {
+ SkASSERT(wh & 0x1);
+
+ int halfFilterSize = (wh-1)/2;
+
+ float r = 0.0f, g = 0.0f, b = 0.0f;
+ for (int yOff = 0; yOff < wh; ++yOff) {
+ int ySamp = y + yOff - halfFilterSize;
+
+ if (ySamp < 0) {
+ ySamp = 0;
+ } else if (ySamp > bm.height()-1) {
+ ySamp = bm.height()-1;
+ }
+
+ for (int xOff = 0; xOff < wh; ++xOff) {
+ int xSamp = x + xOff - halfFilterSize;
+
+ if (xSamp < 0) {
+ xSamp = 0;
+ } else if (xSamp > bm.width()-1) {
+ xSamp = bm.width()-1;
+ }
+
+ float filter = kernel[yOff*wh + xOff];
+
+ SkPMColor c = *bm.getAddr32(xSamp, ySamp);
+
+ r += SkGetPackedR32(c) * filter;
+ g += SkGetPackedG32(c) * filter;
+ b += SkGetPackedB32(c) * filter;
+ }
+ }
+
+ U8CPU r8, g8, b8;
+
+ r8 = (U8CPU) (r+0.5f);
+ g8 = (U8CPU) (g+0.5f);
+ b8 = (U8CPU) (b+0.5f);
+
+ return SkPackARGB32(255, r8, g8, b8);
+}
+
+SkBitmap slow_blur(const SkBitmap& src, float sigma) {
+ SkBitmap dst;
+
+ dst.allocN32Pixels(src.width(), src.height(), true);
+
+ int wh;
+ SkAutoTDeleteArray<float> kernel(create_2d_kernel(sigma, &wh));
+
+ for (int y = 0; y < src.height(); ++y) {
+ for (int x = 0; x < src.width(); ++x) {
+ *dst.getAddr32(x, y) = blur_pixel(src, x, y, kernel.get(), wh);
+ }
+ }
+
+ return dst;
+}
+
} // namespace sk_tool_utils