Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(155)

Side by Side Diff: skia/ext/image_operations_unittest.cc

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

Powered by Google App Engine
This is Rietveld 408576698