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

Unified Diff: ui/gfx/color_utils_unittest.cc

Issue 12335009: Implementation of principal-component color reduction. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Addressed (some) remarks from the review. Created 7 years, 10 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 side-by-side diff with in-line comments
Download patch
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..f07062fb8a5b7505305521daa8d94dc67dff1a61 100644
--- a/ui/gfx/color_utils_unittest.cc
+++ b/ui/gfx/color_utils_unittest.cc
@@ -4,11 +4,41 @@
#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(const SkBitmap& bitmap,
+ uint8_t* min_gl,
+ uint8_t* max_gl) {
+ // Compute minimal and maximal graylevel (or alphalevel) of the input
+ // |bitmap|. |bitmap| has to be allocated and configured to kA8_Config.
Alexei Svitkine (slow) 2013/02/21 19:49:33 Nit: move comment above function
motek. 2013/02/21 22:45:03 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);
+ }
+ }
+}
+
+} // namespace
+
TEST(ColorUtils, SkColorToHSLRed) {
color_utils::HSL hsl = { 0, 0, 0 };
color_utils::SkColorToHSL(SK_ColorRED, &hsl);
@@ -36,7 +66,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 +127,168 @@ 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.
+ EXPECT_TRUE(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(145, min_gl);
+ EXPECT_EQ(145, max_gl);
+
+ // Now scan requesting rescale. Expect all 0.
+ EXPECT_TRUE(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);
+ EXPECT_TRUE(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);
+ EXPECT_TRUE(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);
+ EXPECT_TRUE(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(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);
+ EXPECT_TRUE(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);
+ EXPECT_TRUE(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(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)));
+
+ EXPECT_TRUE(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)));
+}
« ui/gfx/color_utils.cc ('K') | « ui/gfx/color_utils.cc ('k') | ui/gfx/vector3d_f.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698