| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 <stddef.h> | 5 #include <stddef.h> |
| 6 #include <stdint.h> | 6 #include <stdint.h> |
| 7 | 7 |
| 8 #include <algorithm> | 8 #include <algorithm> |
| 9 #include <cmath> | 9 #include <cmath> |
| 10 #include <iomanip> | 10 #include <iomanip> |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 155 | 155 |
| 156 // DEBUG_BITMAP_GENERATION (0 or 1) controls whether the routines | 156 // DEBUG_BITMAP_GENERATION (0 or 1) controls whether the routines |
| 157 // to save the test bitmaps are present. By default the test just fails | 157 // to save the test bitmaps are present. By default the test just fails |
| 158 // without reading/writing files but it is then convenient to have | 158 // without reading/writing files but it is then convenient to have |
| 159 // a simple way to make the failing tests write out the input/output images | 159 // a simple way to make the failing tests write out the input/output images |
| 160 // to check them visually. | 160 // to check them visually. |
| 161 #define DEBUG_BITMAP_GENERATION (0) | 161 #define DEBUG_BITMAP_GENERATION (0) |
| 162 | 162 |
| 163 #if DEBUG_BITMAP_GENERATION | 163 #if DEBUG_BITMAP_GENERATION |
| 164 void SaveBitmapToPNG(const SkBitmap& bmp, const char* path) { | 164 void SaveBitmapToPNG(const SkBitmap& bmp, const char* path) { |
| 165 SkAutoLockPixels lock(bmp); | |
| 166 std::vector<unsigned char> png; | 165 std::vector<unsigned char> png; |
| 167 gfx::PNGCodec::ColorFormat color_format = gfx::PNGCodec::FORMAT_RGBA; | 166 gfx::PNGCodec::ColorFormat color_format = gfx::PNGCodec::FORMAT_RGBA; |
| 168 if (!gfx::PNGCodec::Encode( | 167 if (!gfx::PNGCodec::Encode( |
| 169 reinterpret_cast<const unsigned char*>(bmp.getPixels()), | 168 reinterpret_cast<const unsigned char*>(bmp.getPixels()), |
| 170 color_format, gfx::Size(bmp.width(), bmp.height()), | 169 color_format, gfx::Size(bmp.width(), bmp.height()), |
| 171 static_cast<int>(bmp.rowBytes()), | 170 static_cast<int>(bmp.rowBytes()), |
| 172 false, std::vector<gfx::PNGCodec::Comment>(), &png)) { | 171 false, std::vector<gfx::PNGCodec::Comment>(), &png)) { |
| 173 FAIL() << "Failed to encode image"; | 172 FAIL() << "Failed to encode image"; |
| 174 } | 173 } |
| 175 | 174 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 188 const int src_w = 16, src_h = 34; | 187 const int src_w = 16, src_h = 34; |
| 189 SkBitmap src; | 188 SkBitmap src; |
| 190 FillDataToBitmap(src_w, src_h, &src); | 189 FillDataToBitmap(src_w, src_h, &src); |
| 191 | 190 |
| 192 // Do a resize of the full bitmap to the same size. The lanczos filter is good | 191 // Do a resize of the full bitmap to the same size. The lanczos filter is good |
| 193 // enough that we should get exactly the same image for output. | 192 // enough that we should get exactly the same image for output. |
| 194 SkBitmap results = skia::ImageOperations::Resize(src, method, src_w, src_h); | 193 SkBitmap results = skia::ImageOperations::Resize(src, method, src_w, src_h); |
| 195 ASSERT_EQ(src_w, results.width()); | 194 ASSERT_EQ(src_w, results.width()); |
| 196 ASSERT_EQ(src_h, results.height()); | 195 ASSERT_EQ(src_h, results.height()); |
| 197 | 196 |
| 198 SkAutoLockPixels src_lock(src); | |
| 199 SkAutoLockPixels results_lock(results); | |
| 200 for (int y = 0; y < src_h; y++) { | 197 for (int y = 0; y < src_h; y++) { |
| 201 for (int x = 0; x < src_w; x++) { | 198 for (int x = 0; x < src_w; x++) { |
| 202 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); | 199 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); |
| 203 } | 200 } |
| 204 } | 201 } |
| 205 } | 202 } |
| 206 | 203 |
| 207 // Types defined outside of the ResizeShouldAverageColors test to allow | 204 // Types defined outside of the ResizeShouldAverageColors test to allow |
| 208 // use of the arraysize() macro. | 205 // use of the arraysize() macro. |
| 209 // | 206 // |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 const skia::ImageOperations::ResizeMethod method = tested_method.method; | 251 const skia::ImageOperations::ResizeMethod method = tested_method.method; |
| 255 | 252 |
| 256 SkBitmap dest = skia::ImageOperations::Resize(src, method, dest_w, dest_h); | 253 SkBitmap dest = skia::ImageOperations::Resize(src, method, dest_w, dest_h); |
| 257 ASSERT_EQ(dest_w, dest.width()); | 254 ASSERT_EQ(dest_w, dest.width()); |
| 258 ASSERT_EQ(dest_h, dest.height()); | 255 ASSERT_EQ(dest_h, dest.height()); |
| 259 | 256 |
| 260 // Check that pixels match the expected average. | 257 // Check that pixels match the expected average. |
| 261 float max_observed_distance = 0.0f; | 258 float max_observed_distance = 0.0f; |
| 262 bool all_pixels_ok = true; | 259 bool all_pixels_ok = true; |
| 263 | 260 |
| 264 SkAutoLockPixels dest_lock(dest); | |
| 265 | |
| 266 for (size_t pixel_index = 0; | 261 for (size_t pixel_index = 0; |
| 267 pixel_index < arraysize(tested_pixels); | 262 pixel_index < arraysize(tested_pixels); |
| 268 ++pixel_index) { | 263 ++pixel_index) { |
| 269 const TestedPixel& tested_pixel = tested_pixels[pixel_index]; | 264 const TestedPixel& tested_pixel = tested_pixels[pixel_index]; |
| 270 | 265 |
| 271 const int x = tested_pixel.x; | 266 const int x = tested_pixel.x; |
| 272 const int y = tested_pixel.y; | 267 const int y = tested_pixel.y; |
| 273 const float max_allowed_distance = | 268 const float max_allowed_distance = |
| 274 std::max(tested_pixel.max_color_distance, | 269 std::max(tested_pixel.max_color_distance, |
| 275 tested_method.max_color_distance_override); | 270 tested_method.max_color_distance_override); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 360 SkBitmap src; | 355 SkBitmap src; |
| 361 FillDataToBitmap(src_w, src_h, &src); | 356 FillDataToBitmap(src_w, src_h, &src); |
| 362 | 357 |
| 363 // Do a halving of the full bitmap. | 358 // Do a halving of the full bitmap. |
| 364 SkBitmap actual_results = skia::ImageOperations::Resize( | 359 SkBitmap actual_results = skia::ImageOperations::Resize( |
| 365 src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2); | 360 src, skia::ImageOperations::RESIZE_BOX, src_w / 2, src_h / 2); |
| 366 ASSERT_EQ(src_w / 2, actual_results.width()); | 361 ASSERT_EQ(src_w / 2, actual_results.width()); |
| 367 ASSERT_EQ(src_h / 2, actual_results.height()); | 362 ASSERT_EQ(src_h / 2, actual_results.height()); |
| 368 | 363 |
| 369 // Compute the expected values & compare. | 364 // Compute the expected values & compare. |
| 370 SkAutoLockPixels lock(actual_results); | |
| 371 for (int y = 0; y < actual_results.height(); y++) { | 365 for (int y = 0; y < actual_results.height(); y++) { |
| 372 for (int x = 0; x < actual_results.width(); x++) { | 366 for (int x = 0; x < actual_results.width(); x++) { |
| 373 // Note that those expressions take into account the "half-pixel" | 367 // Note that those expressions take into account the "half-pixel" |
| 374 // offset that comes into play due to considering the coordinates | 368 // offset that comes into play due to considering the coordinates |
| 375 // of the center of the pixels. So x * 2 is a simplification | 369 // of the center of the pixels. So x * 2 is a simplification |
| 376 // of ((x+0.5) * 2 - 1) and (x * 2 + 1) is really (x + 0.5) * 2. | 370 // of ((x+0.5) * 2 - 1) and (x * 2 + 1) is really (x + 0.5) * 2. |
| 377 int first_x = x * 2; | 371 int first_x = x * 2; |
| 378 int last_x = std::min(src_w - 1, x * 2 + 1); | 372 int last_x = std::min(src_w - 1, x * 2 + 1); |
| 379 | 373 |
| 380 int first_y = y * 2; | 374 int first_y = y * 2; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 414 // destination coordinate system (max = half of the original image size). | 408 // destination coordinate system (max = half of the original image size). |
| 415 SkIRect subset_rect = { 2, 3, 3, 6 }; | 409 SkIRect subset_rect = { 2, 3, 3, 6 }; |
| 416 SkBitmap subset_results = skia::ImageOperations::Resize( | 410 SkBitmap subset_results = skia::ImageOperations::Resize( |
| 417 src, skia::ImageOperations::RESIZE_BOX, | 411 src, skia::ImageOperations::RESIZE_BOX, |
| 418 src_w / 2, src_h / 2, subset_rect); | 412 src_w / 2, src_h / 2, subset_rect); |
| 419 ASSERT_EQ(subset_rect.width(), subset_results.width()); | 413 ASSERT_EQ(subset_rect.width(), subset_results.width()); |
| 420 ASSERT_EQ(subset_rect.height(), subset_results.height()); | 414 ASSERT_EQ(subset_rect.height(), subset_results.height()); |
| 421 | 415 |
| 422 // The computed subset and the corresponding subset of the original image | 416 // The computed subset and the corresponding subset of the original image |
| 423 // should be the same. | 417 // should be the same. |
| 424 SkAutoLockPixels full_lock(full_results); | |
| 425 SkAutoLockPixels subset_lock(subset_results); | |
| 426 for (int y = 0; y < subset_rect.height(); y++) { | 418 for (int y = 0; y < subset_rect.height(); y++) { |
| 427 for (int x = 0; x < subset_rect.width(); x++) { | 419 for (int x = 0; x < subset_rect.width(); x++) { |
| 428 ASSERT_EQ( | 420 ASSERT_EQ( |
| 429 *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), | 421 *full_results.getAddr32(x + subset_rect.fLeft, y + subset_rect.fTop), |
| 430 *subset_results.getAddr32(x, y)); | 422 *subset_results.getAddr32(x, y)); |
| 431 } | 423 } |
| 432 } | 424 } |
| 433 } | 425 } |
| 434 | 426 |
| 435 TEST(ImageOperations, InvalidParams) { | 427 TEST(ImageOperations, InvalidParams) { |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 10 + src_x * 100, | 528 10 + src_x * 100, |
| 537 10 + src_y * 100, | 529 10 + src_y * 100, |
| 538 0); | 530 0); |
| 539 } | 531 } |
| 540 } | 532 } |
| 541 | 533 |
| 542 SkBitmap dst = skia::ImageOperations::Resize( | 534 SkBitmap dst = skia::ImageOperations::Resize( |
| 543 src, | 535 src, |
| 544 skia::ImageOperations::RESIZE_LANCZOS3, | 536 skia::ImageOperations::RESIZE_LANCZOS3, |
| 545 dst_w, dst_h); | 537 dst_w, dst_h); |
| 546 SkAutoLockPixels dst_lock(dst); | |
| 547 for (int dst_y = 0; dst_y < dst_h; ++dst_y) { | 538 for (int dst_y = 0; dst_y < dst_h; ++dst_y) { |
| 548 for (int dst_x = 0; dst_x < dst_w; ++dst_x) { | 539 for (int dst_x = 0; dst_x < dst_w; ++dst_x) { |
| 549 float dst_x_in_src = (dst_x + 0.5) * src_w / dst_w; | 540 float dst_x_in_src = (dst_x + 0.5) * src_w / dst_w; |
| 550 float dst_y_in_src = (dst_y + 0.5) * src_h / dst_h; | 541 float dst_y_in_src = (dst_y + 0.5) * src_h / dst_h; |
| 551 float a = 0.0f; | 542 float a = 0.0f; |
| 552 float r = 0.0f; | 543 float r = 0.0f; |
| 553 float g = 0.0f; | 544 float g = 0.0f; |
| 554 float b = 0.0f; | 545 float b = 0.0f; |
| 555 float sum = 0.0f; | 546 float sum = 0.0f; |
| 556 for (int src_y = 0; src_y < src_h; ++src_y) { | 547 for (int src_y = 0; src_y < src_h; ++src_y) { |
| (...skipping 25 matching lines...) Expand all Loading... |
| 582 EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); | 573 EXPECT_LE(fabs(SkColorGetA(dst_color) - a), 1.5f); |
| 583 EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); | 574 EXPECT_LE(fabs(SkColorGetR(dst_color) - r), 1.5f); |
| 584 EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); | 575 EXPECT_LE(fabs(SkColorGetG(dst_color) - g), 1.5f); |
| 585 EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); | 576 EXPECT_LE(fabs(SkColorGetB(dst_color) - b), 1.5f); |
| 586 if (HasFailure()) { | 577 if (HasFailure()) { |
| 587 return; | 578 return; |
| 588 } | 579 } |
| 589 } | 580 } |
| 590 } | 581 } |
| 591 } | 582 } |
| OLD | NEW |