Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / ui / gfx / color_analysis_unittest.cc
blob903161fd59e05c007da05a8a38889fae6f06815e
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
3 // found in the LICENSE file.
5 #include "ui/gfx/color_analysis.h"
7 #include <vector>
9 #include "testing/gtest/include/gtest/gtest.h"
10 #include "third_party/skia/include/core/SkBitmap.h"
11 #include "third_party/skia/include/core/SkColor.h"
12 #include "ui/gfx/canvas.h"
13 #include "ui/gfx/color_utils.h"
14 #include "ui/gfx/geometry/rect.h"
15 #include "ui/gfx/image/image.h"
17 namespace color_utils {
19 const unsigned char k1x1White[] = {
20 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
21 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
22 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
23 0x08, 0x02, 0x00, 0x00, 0x00, 0x90, 0x77, 0x53,
24 0xde, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
25 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
26 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
27 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00,
28 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
29 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x11, 0x15,
30 0x16, 0x1b, 0xaa, 0x58, 0x38, 0x76, 0x00, 0x00,
31 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
32 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72,
33 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69,
34 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
35 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x0c, 0x49,
36 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff,
37 0xff, 0x3f, 0x00, 0x05, 0xfe, 0x02, 0xfe, 0xdc,
38 0xcc, 0x59, 0xe7, 0x00, 0x00, 0x00, 0x00, 0x49,
39 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
42 const unsigned char k1x3BlueWhite[] = {
43 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
44 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
45 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
46 0x08, 0x02, 0x00, 0x00, 0x00, 0xdd, 0xbf, 0xf2,
47 0xd5, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
48 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
49 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
50 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00,
51 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
52 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x12, 0x01,
53 0x0a, 0x2c, 0xfd, 0x08, 0x64, 0x66, 0x00, 0x00,
54 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
55 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72,
56 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69,
57 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
58 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49,
59 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xff,
60 0xff, 0x3f, 0x13, 0x03, 0x03, 0x03, 0x03, 0x03,
61 0xc3, 0x7f, 0x00, 0x1e, 0xfd, 0x03, 0xff, 0xde,
62 0x72, 0x58, 0x0e, 0x00, 0x00, 0x00, 0x00, 0x49,
63 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
66 const unsigned char k1x3BlueRed[] = {
67 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a,
68 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52,
69 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
70 0x08, 0x02, 0x00, 0x00, 0x00, 0xdd, 0xbf, 0xf2,
71 0xd5, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47,
72 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00,
73 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00,
74 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, 0x01, 0x00,
75 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74,
76 0x49, 0x4d, 0x45, 0x07, 0xdb, 0x02, 0x12, 0x01,
77 0x07, 0x09, 0x03, 0xa2, 0xce, 0x6c, 0x00, 0x00,
78 0x00, 0x19, 0x74, 0x45, 0x58, 0x74, 0x43, 0x6f,
79 0x6d, 0x6d, 0x65, 0x6e, 0x74, 0x00, 0x43, 0x72,
80 0x65, 0x61, 0x74, 0x65, 0x64, 0x20, 0x77, 0x69,
81 0x74, 0x68, 0x20, 0x47, 0x49, 0x4d, 0x50, 0x57,
82 0x81, 0x0e, 0x17, 0x00, 0x00, 0x00, 0x14, 0x49,
83 0x44, 0x41, 0x54, 0x08, 0xd7, 0x63, 0xf8, 0xcf,
84 0xc0, 0xc0, 0xc4, 0xc0, 0xc0, 0xc0, 0xc0, 0xc0,
85 0xf0, 0x1f, 0x00, 0x0c, 0x10, 0x02, 0x01, 0x2c,
86 0x8f, 0x8b, 0x8c, 0x00, 0x00, 0x00, 0x00, 0x49,
87 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82
90 const HSL kDefaultLowerBound = {-1, -1, 0.15};
91 const HSL kDefaultUpperBound = {-1, -1, 0.85};
93 // Creates a 1-dimensional png of the pixel colors found in |colors|.
94 scoped_refptr<base::RefCountedMemory> CreateTestPNG(
95 const std::vector<SkColor>& colors) {
96 SkBitmap bitmap;
97 bitmap.allocN32Pixels(colors.size(), 1);
99 SkAutoLockPixels lock(bitmap);
100 for (size_t i = 0; i < colors.size(); ++i) {
101 bitmap.eraseArea(SkIRect::MakeXYWH(i, 0, 1, 1), colors[i]);
103 return gfx::Image::CreateFrom1xBitmap(bitmap).As1xPNGBytes();
106 class MockKMeanImageSampler : public KMeanImageSampler {
107 public:
108 MockKMeanImageSampler() : current_result_index_(0) {
111 explicit MockKMeanImageSampler(const std::vector<int>& samples)
112 : prebaked_sample_results_(samples),
113 current_result_index_(0) {
116 ~MockKMeanImageSampler() override {}
118 void AddSample(int sample) {
119 prebaked_sample_results_.push_back(sample);
122 int GetSample(int width, int height) override {
123 if (current_result_index_ >= prebaked_sample_results_.size()) {
124 current_result_index_ = 0;
127 if (prebaked_sample_results_.empty()) {
128 return 0;
131 return prebaked_sample_results_[current_result_index_++];
134 protected:
135 std::vector<int> prebaked_sample_results_;
136 size_t current_result_index_;
139 // Return true if a color channel is approximately equal to an expected value.
140 bool ChannelApproximatelyEqual(int expected, uint8_t channel) {
141 return (abs(expected - static_cast<int>(channel)) <= 1);
144 // Compute minimal and maximal graylevel (or alphalevel) of the input |bitmap|.
145 // |bitmap| has to be allocated and configured to kA8_Config.
146 void Calculate8bitBitmapMinMax(const SkBitmap& bitmap,
147 uint8_t* min_gl,
148 uint8_t* max_gl) {
149 SkAutoLockPixels bitmap_lock(bitmap);
150 DCHECK(bitmap.getPixels());
151 DCHECK_EQ(bitmap.colorType(), kAlpha_8_SkColorType);
152 DCHECK(min_gl);
153 DCHECK(max_gl);
154 *min_gl = std::numeric_limits<uint8_t>::max();
155 *max_gl = std::numeric_limits<uint8_t>::min();
156 for (int y = 0; y < bitmap.height(); ++y) {
157 uint8_t* current_color = bitmap.getAddr8(0, y);
158 for (int x = 0; x < bitmap.width(); ++x, ++current_color) {
159 *min_gl = std::min(*min_gl, *current_color);
160 *max_gl = std::max(*max_gl, *current_color);
165 class ColorAnalysisTest : public testing::Test {
168 TEST_F(ColorAnalysisTest, CalculatePNGKMeanAllWhite) {
169 MockKMeanImageSampler test_sampler;
170 test_sampler.AddSample(0);
172 scoped_refptr<base::RefCountedBytes> png(
173 new base::RefCountedBytes(
174 std::vector<unsigned char>(
175 k1x1White,
176 k1x1White + sizeof(k1x1White) / sizeof(unsigned char))));
178 SkColor color = CalculateKMeanColorOfPNG(
179 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
181 EXPECT_EQ(color, SK_ColorWHITE);
184 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreWhiteLightness) {
185 MockKMeanImageSampler test_sampler;
186 test_sampler.AddSample(0);
187 test_sampler.AddSample(1);
188 test_sampler.AddSample(2);
190 scoped_refptr<base::RefCountedBytes> png(
191 new base::RefCountedBytes(
192 std::vector<unsigned char>(
193 k1x3BlueWhite,
194 k1x3BlueWhite + sizeof(k1x3BlueWhite) / sizeof(unsigned char))));
196 SkColor color = CalculateKMeanColorOfPNG(
197 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
199 EXPECT_EQ(SkColorSetARGB(0xFF, 0x00, 0x00, 0xFF), color);
202 TEST_F(ColorAnalysisTest, CalculatePNGKMeanPickMostCommon) {
203 MockKMeanImageSampler test_sampler;
204 test_sampler.AddSample(0);
205 test_sampler.AddSample(1);
206 test_sampler.AddSample(2);
208 scoped_refptr<base::RefCountedBytes> png(
209 new base::RefCountedBytes(
210 std::vector<unsigned char>(
211 k1x3BlueRed,
212 k1x3BlueRed + sizeof(k1x3BlueRed) / sizeof(unsigned char))));
214 SkColor color = CalculateKMeanColorOfPNG(
215 png, kDefaultLowerBound, kDefaultUpperBound, &test_sampler);
217 EXPECT_EQ(SkColorSetARGB(0xFF, 0xFF, 0x00, 0x00), color);
220 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreRedHue) {
221 MockKMeanImageSampler test_sampler;
222 test_sampler.AddSample(0);
223 test_sampler.AddSample(1);
224 test_sampler.AddSample(2);
226 std::vector<SkColor> colors(4, SK_ColorRED);
227 colors[1] = SK_ColorBLUE;
229 scoped_refptr<base::RefCountedMemory> png = CreateTestPNG(colors);
231 HSL lower = {0.2, -1, 0.15};
232 HSL upper = {0.8, -1, 0.85};
233 SkColor color = CalculateKMeanColorOfPNG(
234 png, lower, upper, &test_sampler);
236 EXPECT_EQ(SK_ColorBLUE, color);
239 TEST_F(ColorAnalysisTest, CalculatePNGKMeanIgnoreGreySaturation) {
240 MockKMeanImageSampler test_sampler;
241 test_sampler.AddSample(0);
242 test_sampler.AddSample(1);
243 test_sampler.AddSample(2);
245 std::vector<SkColor> colors(4, SK_ColorGRAY);
246 colors[1] = SK_ColorBLUE;
248 scoped_refptr<base::RefCountedMemory> png = CreateTestPNG(colors);
249 HSL lower = {-1, 0.3, -1};
250 HSL upper = {-1, 1, -1};
251 SkColor color = CalculateKMeanColorOfPNG(
252 png, lower, upper, &test_sampler);
254 EXPECT_EQ(SK_ColorBLUE, color);
257 TEST_F(ColorAnalysisTest, GridSampler) {
258 GridSampler sampler;
259 const int kWidth = 16;
260 const int kHeight = 16;
261 // Sample starts at 1,1.
262 EXPECT_EQ(1 + 1 * kWidth, sampler.GetSample(kWidth, kHeight));
263 EXPECT_EQ(1 + 4 * kWidth, sampler.GetSample(kWidth, kHeight));
264 EXPECT_EQ(1 + 7 * kWidth, sampler.GetSample(kWidth, kHeight));
265 EXPECT_EQ(1 + 10 * kWidth, sampler.GetSample(kWidth, kHeight));
266 // Step over by 3.
267 EXPECT_EQ(4 + 1 * kWidth, sampler.GetSample(kWidth, kHeight));
268 EXPECT_EQ(4 + 4 * kWidth, sampler.GetSample(kWidth, kHeight));
269 EXPECT_EQ(4 + 7 * kWidth, sampler.GetSample(kWidth, kHeight));
270 EXPECT_EQ(4 + 10 * kWidth, sampler.GetSample(kWidth, kHeight));
273 TEST_F(ColorAnalysisTest, FindClosestColor) {
274 // Empty image returns input color.
275 SkColor color = FindClosestColor(NULL, 0, 0, SK_ColorRED);
276 EXPECT_EQ(SK_ColorRED, color);
278 // Single color image returns that color.
279 SkBitmap bitmap;
280 bitmap.allocN32Pixels(16, 16);
281 bitmap.eraseColor(SK_ColorWHITE);
282 color = FindClosestColor(static_cast<uint8_t*>(bitmap.getPixels()),
283 bitmap.width(),
284 bitmap.height(),
285 SK_ColorRED);
286 EXPECT_EQ(SK_ColorWHITE, color);
288 // Write a black pixel into the image. A dark grey input pixel should match
289 // the black one in the image.
290 uint32_t* pixel = bitmap.getAddr32(0, 0);
291 *pixel = SK_ColorBLACK;
292 color = FindClosestColor(static_cast<uint8_t*>(bitmap.getPixels()),
293 bitmap.width(),
294 bitmap.height(),
295 SK_ColorDKGRAY);
296 EXPECT_EQ(SK_ColorBLACK, color);
299 TEST_F(ColorAnalysisTest, CalculateKMeanColorOfBitmap) {
300 // Create a 16x16 bitmap to represent a favicon.
301 SkBitmap bitmap;
302 bitmap.allocN32Pixels(16, 16);
303 bitmap.eraseARGB(255, 100, 150, 200);
305 SkColor color = CalculateKMeanColorOfBitmap(bitmap);
306 EXPECT_EQ(255u, SkColorGetA(color));
307 // Color values are not exactly equal due to reversal of premultiplied alpha.
308 EXPECT_TRUE(ChannelApproximatelyEqual(100, SkColorGetR(color)));
309 EXPECT_TRUE(ChannelApproximatelyEqual(150, SkColorGetG(color)));
310 EXPECT_TRUE(ChannelApproximatelyEqual(200, SkColorGetB(color)));
312 // Test a bitmap with an alpha channel.
313 bitmap.eraseARGB(128, 100, 150, 200);
314 color = CalculateKMeanColorOfBitmap(bitmap);
316 // Alpha channel should be ignored for dominant color calculation.
317 EXPECT_EQ(255u, SkColorGetA(color));
318 EXPECT_TRUE(ChannelApproximatelyEqual(100, SkColorGetR(color)));
319 EXPECT_TRUE(ChannelApproximatelyEqual(150, SkColorGetG(color)));
320 EXPECT_TRUE(ChannelApproximatelyEqual(200, SkColorGetB(color)));
323 TEST_F(ColorAnalysisTest, ComputeColorCovarianceTrivial) {
324 SkBitmap bitmap;
325 bitmap.setInfo(SkImageInfo::MakeN32Premul(100, 200));
327 EXPECT_EQ(gfx::Matrix3F::Zeros(), ComputeColorCovariance(bitmap));
328 bitmap.allocPixels();
329 bitmap.eraseARGB(255, 50, 150, 200);
330 gfx::Matrix3F covariance = ComputeColorCovariance(bitmap);
331 // The answer should be all zeros.
332 EXPECT_TRUE(covariance == gfx::Matrix3F::Zeros());
335 TEST_F(ColorAnalysisTest, ComputeColorCovarianceWithCanvas) {
336 gfx::Canvas canvas(gfx::Size(250, 200), 1.0f, true);
337 // The image consists of vertical stripes, with color bands set to 100
338 // in overlapping stripes 150 pixels wide.
339 canvas.FillRect(gfx::Rect(0, 0, 50, 200), SkColorSetRGB(100, 0, 0));
340 canvas.FillRect(gfx::Rect(50, 0, 50, 200), SkColorSetRGB(100, 100, 0));
341 canvas.FillRect(gfx::Rect(100, 0, 50, 200), SkColorSetRGB(100, 100, 100));
342 canvas.FillRect(gfx::Rect(150, 0, 50, 200), SkColorSetRGB(0, 100, 100));
343 canvas.FillRect(gfx::Rect(200, 0, 50, 200), SkColorSetRGB(0, 0, 100));
345 SkBitmap bitmap =
346 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
347 gfx::Matrix3F covariance = ComputeColorCovariance(bitmap);
349 gfx::Matrix3F expected_covariance = gfx::Matrix3F::Zeros();
350 expected_covariance.set(2400, 400, -1600,
351 400, 2400, 400,
352 -1600, 400, 2400);
353 EXPECT_EQ(expected_covariance, covariance);
356 TEST_F(ColorAnalysisTest, ApplyColorReductionSingleColor) {
357 // The test runs color reduction on a single-colot image, where results are
358 // bound to be uninteresting. This is an important edge case, though.
359 SkBitmap source, result;
360 source.allocN32Pixels(300, 200);
361 result.allocPixels(SkImageInfo::MakeA8(300, 200));
363 source.eraseARGB(255, 50, 150, 200);
365 gfx::Vector3dF transform(1.0f, .5f, 0.1f);
366 // This transform, if not scaled, should result in GL=145.
367 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
369 uint8_t min_gl = 0;
370 uint8_t max_gl = 0;
371 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
372 EXPECT_EQ(145, min_gl);
373 EXPECT_EQ(145, max_gl);
375 // Now scan requesting rescale. Expect all 0.
376 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
377 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
378 EXPECT_EQ(0, min_gl);
379 EXPECT_EQ(0, max_gl);
381 // Test cliping to upper limit.
382 transform.set_z(1.1f);
383 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
384 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
385 EXPECT_EQ(0xFF, min_gl);
386 EXPECT_EQ(0xFF, max_gl);
388 // Test cliping to upper limit.
389 transform.Scale(-1.0f);
390 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
391 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
392 EXPECT_EQ(0x0, min_gl);
393 EXPECT_EQ(0x0, max_gl);
396 TEST_F(ColorAnalysisTest, ApplyColorReductionBlackAndWhite) {
397 // Check with images with multiple colors. This is really different only when
398 // the result is scaled.
399 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true);
401 // The image consists of vertical non-overlapping stripes 150 pixels wide.
402 canvas.FillRect(gfx::Rect(0, 0, 150, 200), SkColorSetRGB(0, 0, 0));
403 canvas.FillRect(gfx::Rect(150, 0, 150, 200), SkColorSetRGB(255, 255, 255));
404 SkBitmap source =
405 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
406 SkBitmap result;
407 result.allocPixels(SkImageInfo::MakeA8(300, 200));
409 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f);
410 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
411 uint8_t min_gl = 0;
412 uint8_t max_gl = 0;
413 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
415 EXPECT_EQ(0, min_gl);
416 EXPECT_EQ(255, max_gl);
417 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0)));
418 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199)));
420 // Reverse test.
421 transform.Scale(-1.0f);
422 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
423 min_gl = 0;
424 max_gl = 0;
425 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
427 EXPECT_EQ(0, min_gl);
428 EXPECT_EQ(255, max_gl);
429 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(0, 0)));
430 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199)));
433 TEST_F(ColorAnalysisTest, ApplyColorReductionMultiColor) {
434 // Check with images with multiple colors. This is really different only when
435 // the result is scaled.
436 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true);
438 // The image consists of vertical non-overlapping stripes 100 pixels wide.
439 canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(100, 0, 0));
440 canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(0, 255, 0));
441 canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(0, 0, 128));
442 SkBitmap source =
443 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
444 SkBitmap result;
445 result.allocPixels(SkImageInfo::MakeA8(300, 200));
447 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f);
448 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result));
449 uint8_t min_gl = 0;
450 uint8_t max_gl = 0;
451 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
452 EXPECT_EQ(12, min_gl);
453 EXPECT_EQ(127, max_gl);
454 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199)));
455 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(150, 0)));
456 EXPECT_EQ(100U, SkColorGetA(result.getColor(0, 0)));
458 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result));
459 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
460 EXPECT_EQ(0, min_gl);
461 EXPECT_EQ(255, max_gl);
462 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(299, 199)));
463 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(150, 0)));
464 EXPECT_EQ(193U, SkColorGetA(result.getColor(0, 0)));
467 TEST_F(ColorAnalysisTest, ComputePrincipalComponentImageNotComputable) {
468 SkBitmap source, result;
469 source.allocN32Pixels(300, 200);
470 result.allocPixels(SkImageInfo::MakeA8(300, 200));
472 source.eraseARGB(255, 50, 150, 200);
474 // This computation should fail since all colors always vary together.
475 EXPECT_FALSE(ComputePrincipalComponentImage(source, &result));
478 TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) {
479 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true);
481 // The image consists of vertical non-overlapping stripes 100 pixels wide.
482 canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(10, 10, 10));
483 canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(100, 100, 100));
484 canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(255, 255, 255));
485 SkBitmap source =
486 skia::GetTopDevice(*canvas.sk_canvas())->accessBitmap(false);
487 SkBitmap result;
488 result.allocPixels(SkImageInfo::MakeA8(300, 200));
490 // This computation should fail since all colors always vary together.
491 EXPECT_TRUE(ComputePrincipalComponentImage(source, &result));
493 uint8_t min_gl = 0;
494 uint8_t max_gl = 0;
495 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl);
497 EXPECT_EQ(0, min_gl);
498 EXPECT_EQ(255, max_gl);
499 EXPECT_EQ(min_gl, SkColorGetA(result.getColor(0, 0)));
500 EXPECT_EQ(max_gl, SkColorGetA(result.getColor(299, 199)));
501 EXPECT_EQ(93U, SkColorGetA(result.getColor(150, 0)));
504 } // namespace color_utils