Chromium Code Reviews| Index: ui/gfx/color_utils_unittest.cc |
| diff --git a/ui/gfx/color_utils_unittest.cc b/ui/gfx/color_utils_unittest.cc |
| index 59eaeba1e3e8c779b3a475cc768003e54113a1f7..e2f53731c44ff8992615bd8aa66ac16ec8a6a452 100644 |
| --- a/ui/gfx/color_utils_unittest.cc |
| +++ b/ui/gfx/color_utils_unittest.cc |
| @@ -4,11 +4,38 @@ |
| #include <stdlib.h> |
| +#include <algorithm> |
| +#include <limits> |
| + |
| +#include "base/logging.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| #include "third_party/skia/include/core/SkBitmap.h" |
| #include "third_party/skia/include/core/SkColorPriv.h" |
| +#include "ui/gfx/canvas.h" |
| #include "ui/gfx/color_utils.h" |
| +namespace { |
| + |
| +void Calculate8bitBitmapMinMax( |
|
Alexei Svitkine (slow)
2013/02/21 16:49:59
ADD comment explaining what this does.
motek.
2013/02/21 18:38:43
Done.
|
| + const SkBitmap& bitmap, uint8_t* min_gl, uint8_t* max_gl) { |
|
Alexei Svitkine (slow)
2013/02/21 16:49:59
Each param on a separate line.
motek.
2013/02/21 18:38:43
Done.
|
| + SkAutoLockPixels bitmap_lock(bitmap); |
| + DCHECK(bitmap.getPixels()); |
| + DCHECK(bitmap.config() == SkBitmap::kA8_Config); |
| + DCHECK(min_gl); |
| + DCHECK(max_gl); |
| + *min_gl = std::numeric_limits<uint8_t>::max(); |
| + *max_gl = std::numeric_limits<uint8_t>::min(); |
| + for (int y = 0; y < bitmap.height(); ++y) { |
| + uint8_t* current_color = bitmap.getAddr8(0, y); |
| + for (int x = 0; x < bitmap.width(); ++x, ++current_color) { |
| + *min_gl = std::min(*min_gl, *current_color); |
| + *max_gl = std::max(*max_gl, *current_color); |
| + } |
| + } |
| +} |
| + |
| +} |
|
Alexei Svitkine (slow)
2013/02/21 16:49:59
// namespace
motek.
2013/02/21 18:38:43
Done.
|
| + |
| TEST(ColorUtils, SkColorToHSLRed) { |
| color_utils::HSL hsl = { 0, 0, 0 }; |
| color_utils::SkColorToHSL(SK_ColorRED, &hsl); |
| @@ -36,7 +63,6 @@ TEST(ColorUtils, HSLToSkColorWithAlpha) { |
| EXPECT_EQ(SkColorGetB(red), SkColorGetB(result)); |
| } |
| - |
| TEST(ColorUtils, RGBtoHSLRoundTrip) { |
| // Just spot check values near the edges. |
| for (int r = 0; r < 10; ++r) { |
| @@ -98,3 +124,175 @@ TEST(ColorUtils, AlphaBlend) { |
| fore = SkColorSetA(fore, 0); |
| EXPECT_EQ(0U, SkColorGetA(color_utils::AlphaBlend(fore, back, 255))); |
| } |
| + |
| +TEST(ColorUtils, ApplyColorReductionSingleColor) { |
| + // The test runs color reduction on a single-colot image, where results are |
| + // bound to be uninteresting. This is an important edge case, though. |
| + SkBitmap source, result; |
| + source.setConfig(SkBitmap::kARGB_8888_Config, 300, 200); |
| + result.setConfig(SkBitmap::kA8_Config, 300, 200); |
| + |
| + source.allocPixels(); |
| + result.allocPixels(); |
| + source.eraseRGB(50, 150, 200); |
| + |
| + gfx::Vector3dF transform(1.0f, .5f, 0.1f); |
| + // This transform, if not scaled, should result in GL=145. |
| + gfx::Rect result_rect = color_utils::ApplyColorReduction( |
| + source, transform, false, &result); |
| + EXPECT_EQ(source.width(), result_rect.width()); |
| + EXPECT_EQ(source.height(), result_rect.height()); |
| + |
| + uint8_t min_gl = 0; |
| + uint8_t max_gl = 0; |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + EXPECT_EQ(145, min_gl); |
| + EXPECT_EQ(145, max_gl); |
| + |
| + // Now scan requesting rescale. Expect all 0. |
| + result_rect = color_utils::ApplyColorReduction( |
| + source, transform, true, &result); |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + EXPECT_EQ(0, min_gl); |
| + EXPECT_EQ(0, max_gl); |
| + |
| + // Test cliping to upper limit. |
| + transform.set_z(1.1f); |
| + result_rect = color_utils::ApplyColorReduction( |
| + source, transform, false, &result); |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + EXPECT_EQ(0xFF, min_gl); |
| + EXPECT_EQ(0xFF, max_gl); |
| + |
| + // Test cliping to upper limit. |
| + transform.Scale(-1.0f); |
| + result_rect = color_utils::ApplyColorReduction( |
| + source, transform, false, &result); |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + EXPECT_EQ(0x0, min_gl); |
| + EXPECT_EQ(0x0, max_gl); |
| +} |
| + |
| +TEST(ColorUtils, ApplyColorReductionBlackAndWhite) { |
| + // Check with images with multiple colors. This is really different only when |
| + // the result is scaled. |
| + gfx::Canvas canvas(gfx::Size(300, 200), ui::SCALE_FACTOR_100P, true); |
| + |
| + // The image consists of vertical non-overlapping stripes 150 pixels wide. |
| + canvas.FillRect(gfx::Rect(0, 0, 150, 200), SkColorSetRGB(0, 0, 0)); |
| + canvas.FillRect(gfx::Rect(150, 0, 150, 200), SkColorSetRGB(255, 255, 255)); |
| + SkBitmap source = |
| + skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| + SkBitmap result; |
| + result.setConfig(SkBitmap::kA8_Config, 300, 200); |
| + result.allocPixels(); |
| + |
| + |
| + gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); |
| + gfx::Rect result_rect = color_utils::ApplyColorReduction( |
| + source, transform, true, &result); |
| + uint8_t min_gl = 0; |
| + uint8_t max_gl = 0; |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + |
| + EXPECT_EQ(source.width(), result_rect.width()); |
| + EXPECT_EQ(source.height(), result_rect.height()); |
| + EXPECT_EQ(0, min_gl); |
| + EXPECT_EQ(255, max_gl); |
| + EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0))); |
| + EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199))); |
| + |
| + // Reverse test. |
| + transform.Scale(-1.0f); |
| + result_rect = color_utils::ApplyColorReduction( |
| + source, transform, true, &result); |
| + min_gl = 0; |
| + max_gl = 0; |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + |
| + EXPECT_EQ(0, min_gl); |
| + EXPECT_EQ(255, max_gl); |
| + EXPECT_EQ(max_gl, SkColorGetA(result.getColor(0, 0))); |
| + EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199))); |
| +} |
| + |
| +TEST(ColorUtils, ApplyColorReductionMultiColor) { |
| + // Check with images with multiple colors. This is really different only when |
| + // the result is scaled. |
| + gfx::Canvas canvas(gfx::Size(300, 200), ui::SCALE_FACTOR_100P, true); |
| + |
| + // The image consists of vertical non-overlapping stripes 100 pixels wide. |
| + canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(100, 0, 0)); |
| + canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(0, 255, 0)); |
| + canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(0, 0, 128)); |
| + SkBitmap source = |
| + skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| + SkBitmap result; |
| + result.setConfig(SkBitmap::kA8_Config, 300, 200); |
| + result.allocPixels(); |
| + |
| + |
| + gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); |
| + gfx::Rect result_rect = color_utils::ApplyColorReduction( |
| + source, transform, false, &result); |
| + uint8_t min_gl = 0; |
| + uint8_t max_gl = 0; |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + |
| + EXPECT_EQ(source.width(), result_rect.width()); |
| + EXPECT_EQ(source.height(), result_rect.height()); |
| + EXPECT_EQ(12, min_gl); |
| + EXPECT_EQ(127, max_gl); |
| + EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199))); |
| + EXPECT_EQ(max_gl, SkColorGetA(result.getColor(150, 0))); |
| + EXPECT_EQ(100U, SkColorGetA(result.getColor(0, 0))); |
| + |
| + result_rect = color_utils::ApplyColorReduction( |
| + source, transform, true, &result); |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + EXPECT_EQ(0, min_gl); |
| + EXPECT_EQ(255, max_gl); |
| + EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199))); |
| + EXPECT_EQ(max_gl, SkColorGetA(result.getColor(150, 0))); |
| + EXPECT_EQ(193U, SkColorGetA(result.getColor(0, 0))); |
| +} |
| + |
| +TEST(ColorUtils, ComputePrincipalComponentImageNotComputable) { |
| + SkBitmap source, result; |
| + source.setConfig(SkBitmap::kARGB_8888_Config, 300, 200); |
| + result.setConfig(SkBitmap::kA8_Config, 300, 200); |
| + |
| + source.allocPixels(); |
| + result.allocPixels(); |
| + source.eraseRGB(50, 150, 200); |
| + |
| + // This computation should fail since all colors always vary together. |
| + EXPECT_FALSE(color_utils::ComputePrincipalComponentImage(source, &result)); |
| +} |
| + |
| +TEST(ColorUtils, ComputePrincipalComponentImage) { |
| + gfx::Canvas canvas(gfx::Size(300, 200), ui::SCALE_FACTOR_100P, true); |
| + |
| + // The image consists of vertical non-overlapping stripes 100 pixels wide. |
| + canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(10, 10, 10)); |
| + canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(100, 100, 100)); |
| + canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(255, 255, 255)); |
| + SkBitmap source = |
| + skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false); |
| + SkBitmap result; |
| + result.setConfig(SkBitmap::kA8_Config, 300, 200); |
| + result.allocPixels(); |
| + |
| + // This computation should fail since all colors always vary together. |
| + EXPECT_TRUE(color_utils::ComputePrincipalComponentImage(source, &result)); |
| + |
| + uint8_t min_gl = 0; |
| + uint8_t max_gl = 0; |
| + Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
| + |
| + EXPECT_EQ(0, min_gl); |
| + EXPECT_EQ(255, max_gl); |
| + EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0))); |
| + EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199))); |
| + EXPECT_EQ(93U, SkColorGetA(result.getColor(150, 0))); |
| +} |