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