| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 <stdlib.h> | 5 #include <stdlib.h> |
| 6 | 6 |
| 7 #include "base/gfx/image_operations.h" | 7 #include "skia/ext/image_operations.h" |
| 8 |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "SkBitmap.h" | 10 #include "SkBitmap.h" |
| 10 | 11 |
| 11 namespace { | 12 namespace { |
| 12 | 13 |
| 13 // Computes the average pixel value for the given range, inclusive. | 14 // Computes the average pixel value for the given range, inclusive. |
| 14 uint32_t AveragePixel(const SkBitmap& bmp, | 15 uint32_t AveragePixel(const SkBitmap& bmp, |
| 15 int x_min, int x_max, | 16 int x_min, int x_max, |
| 16 int y_min, int y_max) { | 17 int y_min, int y_max) { |
| 17 float accum[4] = {0, 0, 0, 0}; | 18 float accum[4] = {0, 0, 0, 0}; |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 | 61 |
| 61 // Makes the bitmap 50% the size as the original using a box filter. This is | 62 // Makes the bitmap 50% the size as the original using a box filter. This is |
| 62 // an easy operation that we can check the results for manually. | 63 // an easy operation that we can check the results for manually. |
| 63 TEST(ImageOperations, Halve) { | 64 TEST(ImageOperations, Halve) { |
| 64 // Make our source bitmap. | 65 // Make our source bitmap. |
| 65 int src_w = 30, src_h = 38; | 66 int src_w = 30, src_h = 38; |
| 66 SkBitmap src; | 67 SkBitmap src; |
| 67 FillDataToBitmap(src_w, src_h, &src); | 68 FillDataToBitmap(src_w, src_h, &src); |
| 68 | 69 |
| 69 // Do a halving of the full bitmap. | 70 // Do a halving of the full bitmap. |
| 70 SkBitmap actual_results = gfx::ImageOperations::Resize( | 71 SkBitmap actual_results = skia::ImageOperations::Resize( |
| 71 src, gfx::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); | 72 src, skia::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); |
| 72 ASSERT_EQ(src_w / 2, actual_results.width()); | 73 ASSERT_EQ(src_w / 2, actual_results.width()); |
| 73 ASSERT_EQ(src_h / 2, actual_results.height()); | 74 ASSERT_EQ(src_h / 2, actual_results.height()); |
| 74 | 75 |
| 75 // Compute the expected values & compare. | 76 // Compute the expected values & compare. |
| 76 SkAutoLockPixels lock(actual_results); | 77 SkAutoLockPixels lock(actual_results); |
| 77 for (int y = 0; y < actual_results.height(); y++) { | 78 for (int y = 0; y < actual_results.height(); y++) { |
| 78 for (int x = 0; x < actual_results.width(); x++) { | 79 for (int x = 0; x < actual_results.width(); x++) { |
| 79 int first_x = std::max(0, x * 2 - 1); | 80 int first_x = std::max(0, x * 2 - 1); |
| 80 int last_x = std::min(src_w - 1, x * 2); | 81 int last_x = std::min(src_w - 1, x * 2); |
| 81 | 82 |
| 82 int first_y = std::max(0, y * 2 - 1); | 83 int first_y = std::max(0, y * 2 - 1); |
| 83 int last_y = std::min(src_h - 1, y * 2); | 84 int last_y = std::min(src_h - 1, y * 2); |
| 84 | 85 |
| 85 uint32_t expected_color = AveragePixel(src, | 86 uint32_t expected_color = AveragePixel(src, |
| 86 first_x, last_x, first_y, last_y); | 87 first_x, last_x, first_y, last_y); |
| 87 EXPECT_TRUE(ColorsClose(expected_color, *actual_results.getAddr32(x, y))); | 88 EXPECT_TRUE(ColorsClose(expected_color, *actual_results.getAddr32(x, y))); |
| 88 } | 89 } |
| 89 } | 90 } |
| 90 } | 91 } |
| 91 | 92 |
| 92 TEST(ImageOperations, HalveSubset) { | 93 TEST(ImageOperations, HalveSubset) { |
| 93 // Make our source bitmap. | 94 // Make our source bitmap. |
| 94 int src_w = 16, src_h = 34; | 95 int src_w = 16, src_h = 34; |
| 95 SkBitmap src; | 96 SkBitmap src; |
| 96 FillDataToBitmap(src_w, src_h, &src); | 97 FillDataToBitmap(src_w, src_h, &src); |
| 97 | 98 |
| 98 // Do a halving of the full bitmap. | 99 // Do a halving of the full bitmap. |
| 99 SkBitmap full_results = gfx::ImageOperations::Resize( | 100 SkBitmap full_results = skia::ImageOperations::Resize( |
| 100 src, gfx::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); | 101 src, skia::ImageOperations::RESIZE_BOX, gfx::Size(src_w / 2, src_h / 2)); |
| 101 ASSERT_EQ(src_w / 2, full_results.width()); | 102 ASSERT_EQ(src_w / 2, full_results.width()); |
| 102 ASSERT_EQ(src_h / 2, full_results.height()); | 103 ASSERT_EQ(src_h / 2, full_results.height()); |
| 103 | 104 |
| 104 // Now do a halving of a a subset, recall the destination subset is in the | 105 // Now do a halving of a a subset, recall the destination subset is in the |
| 105 // destination coordinate system (max = half of the original image size). | 106 // destination coordinate system (max = half of the original image size). |
| 106 gfx::Rect subset_rect(2, 3, 3, 6); | 107 gfx::Rect subset_rect(2, 3, 3, 6); |
| 107 SkBitmap subset_results = gfx::ImageOperations::Resize( | 108 SkBitmap subset_results = skia::ImageOperations::Resize( |
| 108 src, gfx::ImageOperations::RESIZE_BOX, | 109 src, skia::ImageOperations::RESIZE_BOX, |
| 109 gfx::Size(src_w / 2, src_h / 2), subset_rect); | 110 gfx::Size(src_w / 2, src_h / 2), subset_rect); |
| 110 ASSERT_EQ(subset_rect.width(), subset_results.width()); | 111 ASSERT_EQ(subset_rect.width(), subset_results.width()); |
| 111 ASSERT_EQ(subset_rect.height(), subset_results.height()); | 112 ASSERT_EQ(subset_rect.height(), subset_results.height()); |
| 112 | 113 |
| 113 // The computed subset and the corresponding subset of the original image | 114 // The computed subset and the corresponding subset of the original image |
| 114 // should be the same. | 115 // should be the same. |
| 115 SkAutoLockPixels full_lock(full_results); | 116 SkAutoLockPixels full_lock(full_results); |
| 116 SkAutoLockPixels subset_lock(subset_results); | 117 SkAutoLockPixels subset_lock(subset_results); |
| 117 for (int y = 0; y < subset_rect.height(); y++) { | 118 for (int y = 0; y < subset_rect.height(); y++) { |
| 118 for (int x = 0; x < subset_rect.width(); x++) { | 119 for (int x = 0; x < subset_rect.width(); x++) { |
| 119 ASSERT_EQ( | 120 ASSERT_EQ( |
| 120 *full_results.getAddr32(x + subset_rect.x(), y + subset_rect.y()), | 121 *full_results.getAddr32(x + subset_rect.x(), y + subset_rect.y()), |
| 121 *subset_results.getAddr32(x, y)); | 122 *subset_results.getAddr32(x, y)); |
| 122 } | 123 } |
| 123 } | 124 } |
| 124 } | 125 } |
| 125 | 126 |
| 126 // Resamples an iamge to the same image, it should give almost the same result. | 127 // Resamples an iamge to the same image, it should give almost the same result. |
| 127 TEST(ImageOperations, ResampleToSame) { | 128 TEST(ImageOperations, ResampleToSame) { |
| 128 // Make our source bitmap. | 129 // Make our source bitmap. |
| 129 int src_w = 16, src_h = 34; | 130 int src_w = 16, src_h = 34; |
| 130 SkBitmap src; | 131 SkBitmap src; |
| 131 FillDataToBitmap(src_w, src_h, &src); | 132 FillDataToBitmap(src_w, src_h, &src); |
| 132 | 133 |
| 133 // Do a resize of the full bitmap to the same size. The lanczos filter is good | 134 // Do a resize of the full bitmap to the same size. The lanczos filter is good |
| 134 // enough that we should get exactly the same image for output. | 135 // enough that we should get exactly the same image for output. |
| 135 SkBitmap results = gfx::ImageOperations::Resize( | 136 SkBitmap results = skia::ImageOperations::Resize( |
| 136 src, gfx::ImageOperations::RESIZE_LANCZOS3, gfx::Size(src_w, src_h)); | 137 src, skia::ImageOperations::RESIZE_LANCZOS3, gfx::Size(src_w, src_h)); |
| 137 ASSERT_EQ(src_w, results.width()); | 138 ASSERT_EQ(src_w, results.width()); |
| 138 ASSERT_EQ(src_h, results.height()); | 139 ASSERT_EQ(src_h, results.height()); |
| 139 | 140 |
| 140 SkAutoLockPixels src_lock(src); | 141 SkAutoLockPixels src_lock(src); |
| 141 SkAutoLockPixels results_lock(results); | 142 SkAutoLockPixels results_lock(results); |
| 142 for (int y = 0; y < src_h; y++) { | 143 for (int y = 0; y < src_h; y++) { |
| 143 for (int x = 0; x < src_w; x++) { | 144 for (int x = 0; x < src_w; x++) { |
| 144 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); | 145 EXPECT_EQ(*src.getAddr32(x, y), *results.getAddr32(x, y)); |
| 145 } | 146 } |
| 146 } | 147 } |
| 147 } | 148 } |
| 148 | 149 |
| OLD | NEW |