image_operations_unittest.cc revision 868fa2fe829687343ffae624259930155e16dbd8
1// Copyright (c) 2012 The Chromium Authors. All rights reserved. 2// Use of this source code is governed by a BSD-style license that can be 3// found in the LICENSE file. 4 5#include <algorithm> 6#include <cmath> 7#include <iomanip> 8#include <vector> 9 10#include "base/basictypes.h" 11#include "base/compiler_specific.h" 12#include "base/file_util.h" 13#include "base/strings/string_util.h" 14#include "skia/ext/image_operations.h" 15#include "testing/gtest/include/gtest/gtest.h" 16#include "third_party/skia/include/core/SkBitmap.h" 17#include "third_party/skia/include/core/SkRect.h" 18#include "ui/gfx/codec/png_codec.h" 19#include "ui/gfx/size.h" 20 21namespace { 22 23// Computes the average pixel value for the given range, inclusive. 24uint32_t AveragePixel(const SkBitmap& bmp, 25 int x_min, int x_max, 26 int y_min, int y_max) { 27 float accum[4] = {0, 0, 0, 0}; 28 int count = 0; 29 for (int y = y_min; y <= y_max; y++) { 30 for (int x = x_min; x <= x_max; x++) { 31 uint32_t cur = *bmp.getAddr32(x, y); 32 accum[0] += SkColorGetB(cur); 33 accum[1] += SkColorGetG(cur); 34 accum[2] += SkColorGetR(cur); 35 accum[3] += SkColorGetA(cur); 36 count++; 37 } 38 } 39 40 return SkColorSetARGB(static_cast<unsigned char>(accum[3] / count), 41 static_cast<unsigned char>(accum[2] / count), 42 static_cast<unsigned char>(accum[1] / count), 43 static_cast<unsigned char>(accum[0] / count)); 44} 45 46// Computes the average pixel (/color) value for the given colors. 47SkColor AveragePixel(const SkColor colors[], size_t color_count) { 48 float accum[4] = { 0.0f, 0.0f, 0.0f, 0.0f }; 49 for (size_t i = 0; i < color_count; ++i) { 50 const SkColor cur = colors[i]; 51 accum[0] += static_cast<float>(SkColorGetA(cur)); 52 accum[1] += static_cast<float>(SkColorGetR(cur)); 53 accum[2] += static_cast<float>(SkColorGetG(cur)); 54 accum[3] += static_cast<float>(SkColorGetB(cur)); 55 } 56 const SkColor average_color = 57 SkColorSetARGB(static_cast<uint8_t>(accum[0] / color_count), 58 static_cast<uint8_t>(accum[1] / color_count), 59 static_cast<uint8_t>(accum[2] / color_count), 60 static_cast<uint8_t>(accum[3] / color_count)); 61 return average_color; 62} 63 64void PrintPixel(const SkBitmap& bmp, 65 int x_min, int x_max, 66 int y_min, int y_max) { 67 char str[128]; 68 69 for (int y = y_min; y <= y_max; ++y) { 70 for (int x = x_min; x <= x_max; ++x) { 71 const uint32_t cur = *bmp.getAddr32(x, y); 72 base::snprintf(str, sizeof(str), "bmp[%d,%d] = %08X", x, y, cur); 73 ADD_FAILURE() << str; 74 } 75 } 76} 77 78// Returns the euclidian distance between two RGBA colors interpreted 79// as 4-components vectors. 80// 81// Notes: 82// - This is a really poor definition of color distance. Yet it 83// is "good enough" for our uses here. 84// - More realistic measures like the various Delta E formulas defined 85// by CIE are way more complex and themselves require the RGBA to 86// to transformed into CIELAB (typically via sRGB first). 87// - The static_cast<int> below are needed to avoid interpreting "negative" 88// differences as huge positive values. 89float ColorsEuclidianDistance(const SkColor a, const SkColor b) { 90 int b_int_diff = static_cast<int>(SkColorGetB(a) - SkColorGetB(b)); 91 int g_int_diff = static_cast<int>(SkColorGetG(a) - SkColorGetG(b)); 92 int r_int_diff = static_cast<int>(SkColorGetR(a) - SkColorGetR(b)); 93 int a_int_diff = static_cast<int>(SkColorGetA(a) - SkColorGetA(b)); 94 95 float b_float_diff = static_cast<float>(b_int_diff); 96 float g_float_diff = static_cast<float>(g_int_diff); 97 float r_float_diff = static_cast<float>(r_int_diff); 98 float a_float_diff = static_cast<float>(a_int_diff); 99 100 return sqrtf((b_float_diff * b_float_diff) + (g_float_diff * g_float_diff) + 101 (r_float_diff * r_float_diff) + (a_float_diff * a_float_diff)); 102} 103 104// Returns true if each channel of the given two colors are "close." This is 105// used for comparing colors where rounding errors may cause off-by-one. 106bool ColorsClose(uint32_t a, uint32_t b) { 107 return abs(static_cast<int>(SkColorGetB(a) - SkColorGetB(b))) < 2 && 108 abs(static_cast<int>(SkColorGetG(a) - SkColorGetG(b))) < 2 && 109 abs(static_cast<int>(SkColorGetR(a) - SkColorGetR(b))) < 2 && 110 abs(static_cast<int>(SkColorGetA(a) - SkColorGetA(b))) < 2; 111} 112 113void FillDataToBitmap(int w, int h, SkBitmap* bmp) { 114 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); 115 bmp->allocPixels(); 116 117 for (int y = 0; y < h; ++y) { 118 for (int x = 0; x < w; ++x) { 119 const uint8_t component = static_cast<uint8_t>(y * w + x); 120 const SkColor pixel = SkColorSetARGB(component, component, 121 component, component); 122 *bmp->getAddr32(x, y) = pixel; 123 } 124 } 125} 126 127// Draws a horizontal and vertical grid into the w x h bitmap passed in. 128// Each line in the grid is drawn with a width of "grid_width" pixels, 129// and those lines repeat every "grid_pitch" pixels. The top left pixel (0, 0) 130// is considered to be part of a grid line. 131// The pixels that fall on a line are colored with "grid_color", while those 132// outside of the lines are colored in "background_color". 133// Note that grid_with can be greather than or equal to grid_pitch, in which 134// case the resulting bitmap will be a solid color "grid_color". 135void DrawGridToBitmap(int w, int h, 136 SkColor background_color, SkColor grid_color, 137 int grid_pitch, int grid_width, 138 SkBitmap* bmp) { 139 ASSERT_GT(grid_pitch, 0); 140 ASSERT_GT(grid_width, 0); 141 ASSERT_NE(background_color, grid_color); 142 143 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); 144 bmp->allocPixels(); 145 146 for (int y = 0; y < h; ++y) { 147 bool y_on_grid = ((y % grid_pitch) < grid_width); 148 149 for (int x = 0; x < w; ++x) { 150 bool on_grid = (y_on_grid || ((x % grid_pitch) < grid_width)); 151 152 *bmp->getAddr32(x, y) = (on_grid ? grid_color : background_color); 153 } 154 } 155} 156 157// Draws a checkerboard pattern into the w x h bitmap passed in. 158// Each rectangle is rect_w in width, rect_h in height. 159// The colors alternate between color1 and color2, color1 being used 160// in the rectangle at the top left corner. 161void DrawCheckerToBitmap(int w, int h, 162 SkColor color1, SkColor color2, 163 int rect_w, int rect_h, 164 SkBitmap* bmp) { 165 ASSERT_GT(rect_w, 0); 166 ASSERT_GT(rect_h, 0); 167 ASSERT_NE(color1, color2); 168 169 bmp->setConfig(SkBitmap::kARGB_8888_Config, w, h); 170 bmp->allocPixels(); 171 172 for (int y = 0; y < h; ++y) { 173 bool y_bit = (((y / rect_h) & 0x1) == 0); 174 175 for (int x = 0; x < w; ++x) { 176 bool x_bit = (((x / rect_w) & 0x1) == 0); 177 178 bool use_color2 = (x_bit != y_bit); // xor 179 180 *bmp->getAddr32(x, y) = (use_color2 ? color2 : color1); 181 } 182 } 183} 184 185// DEBUG_BITMAP_GENERATION (0 or 1) controls whether the routines 186// to save the test bitmaps are present. By default the test just fails 187// without reading/writing files but it is then convenient to have 188// a simple way to make the failing tests write out the input/output images 189// to check them visually. 190#define DEBUG_BITMAP_GENERATION (0) 191 192#if DEBUG_BITMAP_GENERATION 193void SaveBitmapToPNG(const SkBitmap& bmp, const char* path) { 194 SkAutoLockPixels lock(bmp); 195 std::vector<unsigned char> png; 196 gfx::PNGCodec::ColorFormat color_format = gfx::PNGCodec::FORMAT_RGBA; 197 if (!gfx::PNGCodec::Encode( 198 reinterpret_cast<const unsigned char*>(bmp.getPixels()), 199 color_format, gfx::Size(bmp.width(), bmp.height()), 200 static_cast<int>(bmp.rowBytes()), 201 false, std::vector<gfx::PNGCodec::Comment>(), &png)) { 202 FAIL() << "Failed to encode image"; 203 } 204 205 const base::FilePath fpath(path); 206 const int num_written = 207 file_util::WriteFile(fpath, reinterpret_cast<const char*>(&png[0]), 208 png.size()); 209 if (num_written != static_cast<int>(png.size())) { 210 FAIL() << "Failed to write dest \"" << path << '"'; 211 } 212} 213#endif // #if DEBUG_BITMAP_GENERATION 214 215void CheckResampleToSame(skia::ImageOperations::ResizeMethod method) { 216 // Make our source bitmap. 217 const int src_w = 16, src_h = 34; 218 SkBitmap src; 219 FillDataToBitmap(src_w, src_h, &src); 220 221 // Do a resize of the full bitmap to the same size. The lanczos filter is good 222 // enough that we should get exactly the same image for output. 223 SkBitmap results = skia::ImageOperations::Resize(src, method, src_w, src_h); 224 ASSERT_EQ(src_w, results.width()); 225 ASSERT_EQ(src_h, results.height()); 226 227 SkAutoLockPixels src_lock(src); 228 SkAutoLockPixels results_lock(results); 229 for (int y = 0; y < src_h; y++) { 230 for (int x = 0; x < src_w; x++) { 231 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); 232 } 233 } 234} 235 236// Types defined outside of the ResizeShouldAverageColors test to allow 237// use of the arraysize() macro. 238// 239// 'max_color_distance_override' is used in a max() call together with 240// the value of 'max_color_distance' defined in a TestedPixel instance. 241// Hence a value of 0.0 in 'max_color_distance_override' means 242// "use the pixel-specific value" and larger values can be used to allow 243// worse computation errors than provided in a TestedPixel instance. 244struct TestedResizeMethod { 245 skia::ImageOperations::ResizeMethod method; 246 const char* name; 247 float max_color_distance_override; 248}; 249 250struct TestedPixel { 251 int x; 252 int y; 253 float max_color_distance; 254 const char* name; 255}; 256 257// Helper function used by the test "ResizeShouldAverageColors" below. 258// Note that ASSERT_EQ does a "return;" on failure, hence we can't have 259// a "bool" return value to reflect success. Hence "all_pixels_pass" 260void CheckResizeMethodShouldAverageGrid( 261 const SkBitmap& src, 262 const TestedResizeMethod& tested_method, 263 int dest_w, int dest_h, SkColor average_color, 264 bool* method_passed) { 265 *method_passed = false; 266 267 const TestedPixel tested_pixels[] = { 268 // Corners 269 { 0, 0, 2.3f, "Top left corner" }, 270 { 0, dest_h - 1, 2.3f, "Bottom left corner" }, 271 { dest_w - 1, 0, 2.3f, "Top right corner" }, 272 { dest_w - 1, dest_h - 1, 2.3f, "Bottom right corner" }, 273 // Middle points of each side 274 { dest_w / 2, 0, 1.0f, "Top middle" }, 275 { dest_w / 2, dest_h - 1, 1.0f, "Bottom middle" }, 276 { 0, dest_h / 2, 1.0f, "Left middle" }, 277 { dest_w - 1, dest_h / 2, 1.0f, "Right middle" }, 278 // Center 279 { dest_w / 2, dest_h / 2, 1.0f, "Center" } 280 }; 281 282 // Resize the src 283 const skia::ImageOperations::ResizeMethod method = tested_method.method; 284 285 SkBitmap dest = skia::ImageOperations::Resize(src, method, dest_w, dest_h); 286 ASSERT_EQ(dest_w, dest.width()); 287 ASSERT_EQ(dest_h, dest.height()); 288 289 // Check that pixels match the expected average. 290 float max_observed_distance = 0.0f; 291 bool all_pixels_ok = true; 292 293 SkAutoLockPixels dest_lock(dest); 294 295 for (size_t pixel_index = 0; 296 pixel_index < arraysize(tested_pixels); 297 ++pixel_index) { 298 const TestedPixel& tested_pixel = tested_pixels[pixel_index]; 299 300 const int x = tested_pixel.x; 301 const int y = tested_pixel.y; 302 const float max_allowed_distance = 303 std::max(tested_pixel.max_color_distance, 304 tested_method.max_color_distance_override); 305 306 const SkColor actual_color = *dest.getAddr32(x, y); 307 308 // Check that the pixels away from the border region are very close 309 // to the expected average color 310 float distance = ColorsEuclidianDistance(average_color, actual_color); 311 312 EXPECT_LE(distance, max_allowed_distance) 313 << "Resizing method: " << tested_method.name 314 << ", pixel tested: " << tested_pixel.name 315 << "(" << x << ", " << y << ")" 316 << std::hex << std::showbase 317 << ", expected (avg) hex: " << average_color 318 << ", actual hex: " << actual_color; 319 320 if (distance > max_allowed_distance) { 321 all_pixels_ok = false; 322 } 323 if (distance > max_observed_distance) { 324 max_observed_distance = distance; 325 } 326 } 327 328 if (!all_pixels_ok) { 329 ADD_FAILURE() << "Maximum observed color distance for method " 330 << tested_method.name << ": " << max_observed_distance; 331 332#if DEBUG_BITMAP_GENERATION 333 char path[128]; 334 base::snprintf(path, sizeof(path), 335 "/tmp/ResizeShouldAverageColors_%s_dest.png", 336 tested_method.name); 337 SaveBitmapToPNG(dest, path); 338#endif // #if DEBUG_BITMAP_GENERATION 339 } 340 341 *method_passed = all_pixels_ok; 342} 343 344 345} // namespace 346 347// Helper tests that saves bitmaps to PNGs in /tmp/ to visually check 348// that the bitmap generation functions work as expected. 349// Those tests are not enabled by default as verification is done 350// manually/visually, however it is convenient to leave the functions 351// in place. 352#if 0 && DEBUG_BITMAP_GENERATION 353TEST(ImageOperations, GenerateGradientBitmap) { 354 // Make our source bitmap. 355 const int src_w = 640, src_h = 480; 356 SkBitmap src; 357 FillDataToBitmap(src_w, src_h, &src); 358 359 SaveBitmapToPNG(src, "/tmp/gradient_640x480.png"); 360} 361 362TEST(ImageOperations, GenerateGridBitmap) { 363 const int src_w = 640, src_h = 480, src_grid_pitch = 10, src_grid_width = 4; 364 const SkColor grid_color = SK_ColorRED, background_color = SK_ColorBLUE; 365 SkBitmap src; 366 DrawGridToBitmap(src_w, src_h, 367 background_color, grid_color, 368 src_grid_pitch, src_grid_width, 369 &src); 370 371 SaveBitmapToPNG(src, "/tmp/grid_640x408_10_4_red_blue.png"); 372} 373 374TEST(ImageOperations, GenerateCheckerBitmap) { 375 const int src_w = 640, src_h = 480, rect_w = 10, rect_h = 4; 376 const SkColor color1 = SK_ColorRED, color2 = SK_ColorBLUE; 377 SkBitmap src; 378 DrawCheckerToBitmap(src_w, src_h, color1, color2, rect_w, rect_h, &src); 379 380 SaveBitmapToPNG(src, "/tmp/checker_640x408_10_4_red_blue.png"); 381} 382#endif // #if ... && DEBUG_BITMAP_GENERATION 383 384// Makes the bitmap 50% the size as the original using a box filter. This is 385// an easy operation that we can check the results for manually. 386TEST(ImageOperations, Halve) { 387 // Make our source bitmap. 388 int src_w = 30, src_h = 38; 389 SkBitmap src; 390 FillDataToBitmap(src_w, src_h, &src); 391 392 // Do a halving of the full bitmap. 393 SkBitmap actual_results = skia::ImageOperations::Resize( 394 src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2); 395 ASSERT_EQ(src_w / 2, actual_results.width()); 396 ASSERT_EQ(src_h / 2, actual_results.height()); 397 398 // Compute the expected values & compare. 399 SkAutoLockPixels lock(actual_results); 400 for (int y = 0; y < actual_results.height(); y++) { 401 for (int x = 0; x < actual_results.width(); x++) { 402 // Note that those expressions take into account the "half-pixel" 403 // offset that comes into play due to considering the coordinates 404 // of the center of the pixels. So x * 2 is a simplification 405 // of ((x+0.5) * 2 - 1) and (x * 2 + 1) is really (x + 0.5) * 2. 406 int first_x = x * 2; 407 int last_x = std::min(src_w - 1, x * 2 + 1); 408 409 int first_y = y * 2; 410 int last_y = std::min(src_h - 1, y * 2 + 1); 411 412 const uint32_t expected_color = AveragePixel(src, 413 first_x, last_x, 414 first_y, last_y); 415 const uint32_t actual_color = *actual_results.getAddr32(x, y); 416 const bool close = ColorsClose(expected_color, actual_color); 417 EXPECT_TRUE(close); 418 if (!close) { 419 char str[128]; 420 base::snprintf(str, sizeof(str), 421 "exp[%d,%d] = %08X, actual[%d,%d] = %08X", 422 x, y, expected_color, x, y, actual_color); 423 ADD_FAILURE() << str; 424 PrintPixel(src, first_x, last_x, first_y, last_y); 425 } 426 } 427 } 428} 429 430TEST(ImageOperations, HalveSubset) { 431 // Make our source bitmap. 432 int src_w = 16, src_h = 34; 433 SkBitmap src; 434 FillDataToBitmap(src_w, src_h, &src); 435 436 // Do a halving of the full bitmap. 437 SkBitmap full_results = skia::ImageOperations::Resize( 438 src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2); 439 ASSERT_EQ(src_w / 2, full_results.width()); 440 ASSERT_EQ(src_h / 2, full_results.height()); 441 442 // Now do a halving of a a subset, recall the destination subset is in the 443 // destination coordinate system (max = half of the original image size). 444 SkIRect subset_rect = { 2, 3, 3, 6 }; 445 SkBitmap subset_results = skia::ImageOperations::Resize( 446 src, skia::ImageOperations::RESIZE_BOX, 447 src_w / 2, src_h / 2, subset_rect); 448 ASSERT_EQ(subset_rect.width(), subset_results.width()); 449 ASSERT_EQ(subset_rect.height(), subset_results.height()); 450 451 // The computed subset and the corresponding subset of the original image 452 // should be the same. 453 SkAutoLockPixels full_lock(full_results); 454 SkAutoLockPixels subset_lock(subset_results); 455 for (int y = 0; y < subset_rect.height(); y++) { 456 for (int x = 0; x < subset_rect.width(); x++) { 457 ASSERT_EQ( 458 *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), 459 *subset_results.getAddr32(x, y)); 460 } 461 } 462} 463 464// Resamples an image to the same image, it should give the same result. 465TEST(ImageOperations, ResampleToSameHamming1) { 466 CheckResampleToSame(skia::ImageOperations::RESIZE_HAMMING1); 467} 468 469TEST(ImageOperations, ResampleToSameLanczos2) { 470 CheckResampleToSame(skia::ImageOperations::RESIZE_LANCZOS2); 471} 472 473TEST(ImageOperations, ResampleToSameLanczos3) { 474 CheckResampleToSame(skia::ImageOperations::RESIZE_LANCZOS3); 475} 476 477// Check that all Good/Better/Best, Box, Lanczos2 and Lanczos3 generate purple 478// when resizing a 4x8 red/blue checker pattern by 1/16x1/16. 479TEST(ImageOperations, ResizeShouldAverageColors) { 480 // Make our source bitmap. 481 const int src_w = 640, src_h = 480, checker_rect_w = 4, checker_rect_h = 8; 482 const SkColor checker_color1 = SK_ColorRED, checker_color2 = SK_ColorBLUE; 483 484 const int dest_w = src_w / (4 * checker_rect_w); 485 const int dest_h = src_h / (2 * checker_rect_h); 486 487 // Compute the expected (average) color 488 const SkColor colors[] = { checker_color1, checker_color2 }; 489 const SkColor average_color = AveragePixel(colors, arraysize(colors)); 490 491 // RESIZE_SUBPIXEL is only supported on Linux/non-GTV platforms. 492 static const TestedResizeMethod tested_methods[] = { 493 { skia::ImageOperations::RESIZE_GOOD, "GOOD", 0.0f }, 494 { skia::ImageOperations::RESIZE_BETTER, "BETTER", 0.0f }, 495 { skia::ImageOperations::RESIZE_BEST, "BEST", 0.0f }, 496 { skia::ImageOperations::RESIZE_BOX, "BOX", 0.0f }, 497 { skia::ImageOperations::RESIZE_HAMMING1, "HAMMING1", 0.0f }, 498 { skia::ImageOperations::RESIZE_LANCZOS2, "LANCZOS2", 0.0f }, 499 { skia::ImageOperations::RESIZE_LANCZOS3, "LANCZOS3", 0.0f }, 500#if defined(OS_LINUX) && !defined(GTV) 501 // SUBPIXEL has slightly worse performance than the other filters: 502 // 6.324 Bottom left/right corners 503 // 5.099 Top left/right corners 504 // 2.828 Bottom middle 505 // 1.414 Top/Left/Right middle, center 506 // 507 // This is expected since, in order to judge RESIZE_SUBPIXEL accurately, 508 // we'd need to compute distances for each sub-pixel, and potentially 509 // tweak the test parameters so that expectations were realistic when 510 // looking at sub-pixels in isolation. 511 // 512 // Rather than going to these lengths, we added the "max_distance_override" 513 // field in TestedResizeMethod, intended for RESIZE_SUBPIXEL. It allows 514 // us to to enable its testing without having to lower the success criteria 515 // for the other methods. This procedure is distateful but defining 516 // a distance limit for each tested pixel for each method was judged to add 517 // unneeded complexity. 518 { skia::ImageOperations::RESIZE_SUBPIXEL, "SUBPIXEL", 6.4f }, 519#endif 520 }; 521 522 // Create our source bitmap. 523 SkBitmap src; 524 DrawCheckerToBitmap(src_w, src_h, 525 checker_color1, checker_color2, 526 checker_rect_w, checker_rect_h, 527 &src); 528 529 // For each method, downscale by 16 in each dimension, 530 // and check each tested pixel against the expected average color. 531 bool all_methods_ok ALLOW_UNUSED = true; 532 533 for (size_t method_index = 0; 534 method_index < arraysize(tested_methods); 535 ++method_index) { 536 bool pass = true; 537 CheckResizeMethodShouldAverageGrid(src, 538 tested_methods[method_index], 539 dest_w, dest_h, average_color, 540 &pass); 541 if (!pass) { 542 all_methods_ok = false; 543 } 544 } 545 546#if DEBUG_BITMAP_GENERATION 547 if (!all_methods_ok) { 548 SaveBitmapToPNG(src, "/tmp/ResizeShouldAverageColors_src.png"); 549 } 550#endif // #if DEBUG_BITMAP_GENERATION 551} 552 553 554// Check that Lanczos2 and Lanczos3 thumbnails produce similar results 555TEST(ImageOperations, CompareLanczosMethods) { 556 const int src_w = 640, src_h = 480, src_grid_pitch = 8, src_grid_width = 4; 557 558 const int dest_w = src_w / 4; 559 const int dest_h = src_h / 4; 560 561 // 5.0f is the maximum distance we see in this test given the current 562 // parameters. The value is very ad-hoc and the parameters of the scaling 563 // were picked to produce a small value. So this test is very much about 564 // revealing egregious regression rather than doing a good job at checking 565 // the math behind the filters. 566 // TODO(evannier): because of the half pixel error mentioned inside 567 // image_operations.cc, this distance is much larger than it should be. 568 // This should read: 569 // const float max_color_distance = 5.0f; 570 const float max_color_distance = 12.1f; 571 572 // Make our source bitmap. 573 SkColor grid_color = SK_ColorRED, background_color = SK_ColorBLUE; 574 SkBitmap src; 575 DrawGridToBitmap(src_w, src_h, 576 background_color, grid_color, 577 src_grid_pitch, src_grid_width, 578 &src); 579 580 // Resize the src using both methods. 581 SkBitmap dest_l2 = skia::ImageOperations::Resize( 582 src, 583 skia::ImageOperations::RESIZE_LANCZOS2, 584 dest_w, dest_h); 585 ASSERT_EQ(dest_w, dest_l2.width()); 586 ASSERT_EQ(dest_h, dest_l2.height()); 587 588 SkBitmap dest_l3 = skia::ImageOperations::Resize( 589 src, 590 skia::ImageOperations::RESIZE_LANCZOS3, 591 dest_w, dest_h); 592 ASSERT_EQ(dest_w, dest_l3.width()); 593 ASSERT_EQ(dest_h, dest_l3.height()); 594 595 // Compare the pixels produced by both methods. 596 float max_observed_distance = 0.0f; 597 bool all_pixels_ok = true; 598 599 SkAutoLockPixels l2_lock(dest_l2); 600 SkAutoLockPixels l3_lock(dest_l3); 601 for (int y = 0; y < dest_h; ++y) { 602 for (int x = 0; x < dest_w; ++x) { 603 const SkColor color_lanczos2 = *dest_l2.getAddr32(x, y); 604 const SkColor color_lanczos3 = *dest_l3.getAddr32(x, y); 605 606 float distance = ColorsEuclidianDistance(color_lanczos2, color_lanczos3); 607 608 EXPECT_LE(distance, max_color_distance) 609 << "pixel tested: (" << x << ", " << y 610 << std::hex << std::showbase 611 << "), lanczos2 hex: " << color_lanczos2 612 << ", lanczos3 hex: " << color_lanczos3 613 << std::setprecision(2) 614 << ", distance: " << distance; 615 616 if (distance > max_color_distance) { 617 all_pixels_ok = false; 618 } 619 if (distance > max_observed_distance) { 620 max_observed_distance = distance; 621 } 622 } 623 } 624 625 if (!all_pixels_ok) { 626 ADD_FAILURE() << "Maximum observed color distance: " 627 << max_observed_distance; 628 629#if DEBUG_BITMAP_GENERATION 630 SaveBitmapToPNG(src, "/tmp/CompareLanczosMethods_source.png"); 631 SaveBitmapToPNG(dest_l2, "/tmp/CompareLanczosMethods_lanczos2.png"); 632 SaveBitmapToPNG(dest_l3, "/tmp/CompareLanczosMethods_lanczos3.png"); 633#endif // #if DEBUG_BITMAP_GENERATION 634 } 635} 636 637#ifndef M_PI 638// No M_PI in math.h on windows? No problem. 639#define M_PI 3.14159265358979323846 640#endif 641 642static double sinc(double x) { 643 if (x == 0.0) return 1.0; 644 x *= M_PI; 645 return sin(x) / x; 646} 647 648static double lanczos3(double offset) { 649 if (fabs(offset) >= 3) return 0.0; 650 return sinc(offset) * sinc(offset / 3.0); 651} 652 653TEST(ImageOperations, ScaleUp) { 654 const int src_w = 3; 655 const int src_h = 3; 656 const int dst_w = 9; 657 const int dst_h = 9; 658 SkBitmap src; 659 src.setConfig(SkBitmap::kARGB_8888_Config, src_w, src_h); 660 src.allocPixels(); 661 662 for (int src_y = 0; src_y < src_h; ++src_y) { 663 for (int src_x = 0; src_x < src_w; ++src_x) { 664 *src.getAddr32(src_x, src_y) = SkColorSetARGBInline(255, 665 10 + src_x * 100, 666 10 + src_y * 100, 667 0); 668 } 669 } 670 671 SkBitmap dst = skia::ImageOperations::Resize( 672 src, 673 skia::ImageOperations::RESIZE_LANCZOS3, 674 dst_w, dst_h); 675 SkAutoLockPixels dst_lock(dst); 676 for (int dst_y = 0; dst_y < dst_h; ++dst_y) { 677 for (int dst_x = 0; dst_x < dst_w; ++dst_x) { 678 float dst_x_in_src = (dst_x + 0.5) * src_w / dst_w; 679 float dst_y_in_src = (dst_y + 0.5) * src_h / dst_h; 680 float a = 0.0f; 681 float r = 0.0f; 682 float g = 0.0f; 683 float b = 0.0f; 684 float sum = 0.0f; 685 for (int src_y = 0; src_y < src_h; ++src_y) { 686 for (int src_x = 0; src_x < src_w; ++src_x) { 687 double coeff = 688 lanczos3(src_x + 0.5 - dst_x_in_src) * 689 lanczos3(src_y + 0.5 - dst_y_in_src); 690 sum += coeff; 691 SkColor tmp = *src.getAddr32(src_x, src_y); 692 a += coeff * SkColorGetA(tmp); 693 r += coeff * SkColorGetR(tmp); 694 g += coeff * SkColorGetG(tmp); 695 b += coeff * SkColorGetB(tmp); 696 } 697 } 698 a /= sum; 699 r /= sum; 700 g /= sum; 701 b /= sum; 702 if (a < 0.0f) a = 0.0f; 703 if (r < 0.0f) r = 0.0f; 704 if (g < 0.0f) g = 0.0f; 705 if (b < 0.0f) b = 0.0f; 706 if (a > 255.0f) a = 255.0f; 707 if (r > 255.0f) r = 255.0f; 708 if (g > 255.0f) g = 255.0f; 709 if (b > 255.0f) b = 255.0f; 710 SkColor dst_color = *dst.getAddr32(dst_x, dst_y); 711 EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); 712 EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); 713 EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); 714 EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); 715 if (HasFailure()) { 716 return; 717 } 718 } 719 } 720} 721