diff options
author | hubbe@chromium.org <hubbe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 17:18:50 +0000 |
---|---|---|
committer | hubbe@chromium.org <hubbe@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-03-30 17:18:50 +0000 |
commit | 813432d35b6d559737d6f7995fa8124168f8b553 (patch) | |
tree | 7e58208a3950f3023730798c87ccbc8258da3873 /skia/ext/image_operations_unittest.cc | |
parent | d3c610588caa7a239346930501c02a964cd16bce (diff) | |
download | chromium_src-813432d35b6d559737d6f7995fa8124168f8b553.zip chromium_src-813432d35b6d559737d6f7995fa8124168f8b553.tar.gz chromium_src-813432d35b6d559737d6f7995fa8124168f8b553.tar.bz2 |
Skia upscaling uses filters that are too small. This CL fixes that.
BUG=
Review URL: https://chromiumcodereview.appspot.com/12573012
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@191515 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'skia/ext/image_operations_unittest.cc')
-rw-r--r-- | skia/ext/image_operations_unittest.cc | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/skia/ext/image_operations_unittest.cc b/skia/ext/image_operations_unittest.cc index a0897ad..fead994 100644 --- a/skia/ext/image_operations_unittest.cc +++ b/skia/ext/image_operations_unittest.cc @@ -3,6 +3,7 @@ // found in the LICENSE file. #include <algorithm> +#include <cmath> #include <iomanip> #include <vector> @@ -632,3 +633,88 @@ TEST(ImageOperations, CompareLanczosMethods) { #endif // #if DEBUG_BITMAP_GENERATION } } + +#ifndef M_PI +// No M_PI in math.h on windows? No problem. +#define M_PI 3.14159265358979323846 +#endif + +static double sinc(double x) { + if (x == 0.0) return 1.0; + x *= M_PI; + return sin(x) / x; +} + +static double lanczos3(double offset) { + if (fabs(offset) >= 3) return 0.0; + return sinc(offset) * sinc(offset / 3.0); +} + +TEST(ImageOperations, ScaleUp) { + const int src_w = 3; + const int src_h = 3; + const int dst_w = 9; + const int dst_h = 9; + SkBitmap src; + src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); + src.allocPixels(); + + for (int src_y = 0; src_y < src_h; ++src_y) { + for (int src_x = 0; src_x < src_w; ++src_x) { + *src.getAddr32(src_x, src_y) = SkColorSetARGBInline(255, + 10 + src_x * 100, + 10 + src_y * 100, + 0); + } + } + + SkBitmap dst = skia::ImageOperations::Resize( + src, + skia::ImageOperations::RESIZE_LANCZOS3, + dst_w, dst_h); + SkAutoLockPixels dst_lock(dst); + for (int dst_y = 0; dst_y < dst_h; ++dst_y) { + for (int dst_x = 0; dst_x < dst_w; ++dst_x) { + float dst_x_in_src = (dst_x + 0.5) * src_w / dst_w; + float dst_y_in_src = (dst_y + 0.5) * src_h / dst_h; + float a = 0.0f; + float r = 0.0f; + float g = 0.0f; + float b = 0.0f; + float sum = 0.0f; + for (int src_y = 0; src_y < src_h; ++src_y) { + for (int src_x = 0; src_x < src_w; ++src_x) { + double coeff = + lanczos3(src_x + 0.5 - dst_x_in_src) * + lanczos3(src_y + 0.5 - dst_y_in_src); + sum += coeff; + SkColor tmp = *src.getAddr32(src_x, src_y); + a += coeff * SkColorGetA(tmp); + r += coeff * SkColorGetR(tmp); + g += coeff * SkColorGetG(tmp); + b += coeff * SkColorGetB(tmp); + } + } + a /= sum; + r /= sum; + g /= sum; + b /= sum; + if (a < 0.0f) a = 0.0f; + if (r < 0.0f) r = 0.0f; + if (g < 0.0f) g = 0.0f; + if (b < 0.0f) b = 0.0f; + if (a > 255.0f) a = 255.0f; + if (r > 255.0f) r = 255.0f; + if (g > 255.0f) g = 255.0f; + if (b > 255.0f) b = 255.0f; + SkColor dst_color = *dst.getAddr32(dst_x, dst_y); + EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); + EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); + EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); + EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); + if (HasFailure()) { + return; + } + } + } +} |