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 "ui/gfx/color_analysis.h" | 5 #include "ui/gfx/color_analysis.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <vector> | 10 #include <vector> |
(...skipping 327 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 TEST_F(ColorAnalysisTest, ComputeColorCovarianceWithCanvas) { | 338 TEST_F(ColorAnalysisTest, ComputeColorCovarianceWithCanvas) { |
339 gfx::Canvas canvas(gfx::Size(250, 200), 1.0f, true); | 339 gfx::Canvas canvas(gfx::Size(250, 200), 1.0f, true); |
340 // The image consists of vertical stripes, with color bands set to 100 | 340 // The image consists of vertical stripes, with color bands set to 100 |
341 // in overlapping stripes 150 pixels wide. | 341 // in overlapping stripes 150 pixels wide. |
342 canvas.FillRect(gfx::Rect(0, 0, 50, 200), SkColorSetRGB(100, 0, 0)); | 342 canvas.FillRect(gfx::Rect(0, 0, 50, 200), SkColorSetRGB(100, 0, 0)); |
343 canvas.FillRect(gfx::Rect(50, 0, 50, 200), SkColorSetRGB(100, 100, 0)); | 343 canvas.FillRect(gfx::Rect(50, 0, 50, 200), SkColorSetRGB(100, 100, 0)); |
344 canvas.FillRect(gfx::Rect(100, 0, 50, 200), SkColorSetRGB(100, 100, 100)); | 344 canvas.FillRect(gfx::Rect(100, 0, 50, 200), SkColorSetRGB(100, 100, 100)); |
345 canvas.FillRect(gfx::Rect(150, 0, 50, 200), SkColorSetRGB(0, 100, 100)); | 345 canvas.FillRect(gfx::Rect(150, 0, 50, 200), SkColorSetRGB(0, 100, 100)); |
346 canvas.FillRect(gfx::Rect(200, 0, 50, 200), SkColorSetRGB(0, 0, 100)); | 346 canvas.FillRect(gfx::Rect(200, 0, 50, 200), SkColorSetRGB(0, 0, 100)); |
347 | 347 |
348 gfx::Matrix3F covariance = ComputeColorCovariance(canvas.ToBitmap()); | 348 gfx::Matrix3F covariance = ComputeColorCovariance(canvas.GetBitmap()); |
349 | 349 |
350 gfx::Matrix3F expected_covariance = gfx::Matrix3F::Zeros(); | 350 gfx::Matrix3F expected_covariance = gfx::Matrix3F::Zeros(); |
351 expected_covariance.set(2400, 400, -1600, | 351 expected_covariance.set(2400, 400, -1600, |
352 400, 2400, 400, | 352 400, 2400, 400, |
353 -1600, 400, 2400); | 353 -1600, 400, 2400); |
354 EXPECT_EQ(expected_covariance, covariance); | 354 EXPECT_EQ(expected_covariance, covariance); |
355 } | 355 } |
356 | 356 |
357 TEST_F(ColorAnalysisTest, ApplyColorReductionSingleColor) { | 357 TEST_F(ColorAnalysisTest, ApplyColorReductionSingleColor) { |
358 // The test runs color reduction on a single-colot image, where results are | 358 // The test runs color reduction on a single-colot image, where results are |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
395 } | 395 } |
396 | 396 |
397 TEST_F(ColorAnalysisTest, ApplyColorReductionBlackAndWhite) { | 397 TEST_F(ColorAnalysisTest, ApplyColorReductionBlackAndWhite) { |
398 // Check with images with multiple colors. This is really different only when | 398 // Check with images with multiple colors. This is really different only when |
399 // the result is scaled. | 399 // the result is scaled. |
400 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); | 400 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); |
401 | 401 |
402 // The image consists of vertical non-overlapping stripes 150 pixels wide. | 402 // The image consists of vertical non-overlapping stripes 150 pixels wide. |
403 canvas.FillRect(gfx::Rect(0, 0, 150, 200), SkColorSetRGB(0, 0, 0)); | 403 canvas.FillRect(gfx::Rect(0, 0, 150, 200), SkColorSetRGB(0, 0, 0)); |
404 canvas.FillRect(gfx::Rect(150, 0, 150, 200), SkColorSetRGB(255, 255, 255)); | 404 canvas.FillRect(gfx::Rect(150, 0, 150, 200), SkColorSetRGB(255, 255, 255)); |
405 SkBitmap source = canvas.ToBitmap(); | 405 SkBitmap source = canvas.GetBitmap(); |
406 SkBitmap result; | 406 SkBitmap result; |
407 result.allocPixels(SkImageInfo::MakeA8(300, 200)); | 407 result.allocPixels(SkImageInfo::MakeA8(300, 200)); |
408 | 408 |
409 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); | 409 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); |
410 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result)); | 410 EXPECT_TRUE(ApplyColorReduction(source, transform, true, &result)); |
411 uint8_t min_gl = 0; | 411 uint8_t min_gl = 0; |
412 uint8_t max_gl = 0; | 412 uint8_t max_gl = 0; |
413 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); | 413 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
414 | 414 |
415 EXPECT_EQ(0, min_gl); | 415 EXPECT_EQ(0, min_gl); |
(...skipping 16 matching lines...) Expand all Loading... |
432 | 432 |
433 TEST_F(ColorAnalysisTest, ApplyColorReductionMultiColor) { | 433 TEST_F(ColorAnalysisTest, ApplyColorReductionMultiColor) { |
434 // Check with images with multiple colors. This is really different only when | 434 // Check with images with multiple colors. This is really different only when |
435 // the result is scaled. | 435 // the result is scaled. |
436 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); | 436 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); |
437 | 437 |
438 // The image consists of vertical non-overlapping stripes 100 pixels wide. | 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)); | 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)); | 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)); | 441 canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(0, 0, 128)); |
442 SkBitmap source = canvas.ToBitmap(); | 442 SkBitmap source = canvas.GetBitmap(); |
443 SkBitmap result; | 443 SkBitmap result; |
444 result.allocPixels(SkImageInfo::MakeA8(300, 200)); | 444 result.allocPixels(SkImageInfo::MakeA8(300, 200)); |
445 | 445 |
446 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); | 446 gfx::Vector3dF transform(1.0f, 0.5f, 0.1f); |
447 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result)); | 447 EXPECT_TRUE(ApplyColorReduction(source, transform, false, &result)); |
448 uint8_t min_gl = 0; | 448 uint8_t min_gl = 0; |
449 uint8_t max_gl = 0; | 449 uint8_t max_gl = 0; |
450 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); | 450 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
451 EXPECT_EQ(12, min_gl); | 451 EXPECT_EQ(12, min_gl); |
452 EXPECT_EQ(127, max_gl); | 452 EXPECT_EQ(127, max_gl); |
(...skipping 21 matching lines...) Expand all Loading... |
474 EXPECT_FALSE(ComputePrincipalComponentImage(source, &result)); | 474 EXPECT_FALSE(ComputePrincipalComponentImage(source, &result)); |
475 } | 475 } |
476 | 476 |
477 TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) { | 477 TEST_F(ColorAnalysisTest, ComputePrincipalComponentImage) { |
478 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); | 478 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); |
479 | 479 |
480 // The image consists of vertical non-overlapping stripes 100 pixels wide. | 480 // The image consists of vertical non-overlapping stripes 100 pixels wide. |
481 canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(10, 10, 10)); | 481 canvas.FillRect(gfx::Rect(0, 0, 100, 200), SkColorSetRGB(10, 10, 10)); |
482 canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(100, 100, 100)); | 482 canvas.FillRect(gfx::Rect(100, 0, 100, 200), SkColorSetRGB(100, 100, 100)); |
483 canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(255, 255, 255)); | 483 canvas.FillRect(gfx::Rect(200, 0, 100, 200), SkColorSetRGB(255, 255, 255)); |
484 SkBitmap source = canvas.ToBitmap(); | 484 SkBitmap source = canvas.GetBitmap(); |
485 SkBitmap result; | 485 SkBitmap result; |
486 result.allocPixels(SkImageInfo::MakeA8(300, 200)); | 486 result.allocPixels(SkImageInfo::MakeA8(300, 200)); |
487 | 487 |
488 // This computation should fail since all colors always vary together. | 488 // This computation should fail since all colors always vary together. |
489 EXPECT_TRUE(ComputePrincipalComponentImage(source, &result)); | 489 EXPECT_TRUE(ComputePrincipalComponentImage(source, &result)); |
490 | 490 |
491 uint8_t min_gl = 0; | 491 uint8_t min_gl = 0; |
492 uint8_t max_gl = 0; | 492 uint8_t max_gl = 0; |
493 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); | 493 Calculate8bitBitmapMinMax(result, &min_gl, &max_gl); |
494 | 494 |
(...skipping 12 matching lines...) Expand all Loading... |
507 {LumaRange::NORMAL, SaturationRange::VIBRANT}, | 507 {LumaRange::NORMAL, SaturationRange::VIBRANT}, |
508 {LumaRange::LIGHT, SaturationRange::VIBRANT}, | 508 {LumaRange::LIGHT, SaturationRange::VIBRANT}, |
509 {LumaRange::DARK, SaturationRange::MUTED}, | 509 {LumaRange::DARK, SaturationRange::MUTED}, |
510 {LumaRange::NORMAL, SaturationRange::MUTED}, | 510 {LumaRange::NORMAL, SaturationRange::MUTED}, |
511 {LumaRange::LIGHT, SaturationRange::MUTED}}; | 511 {LumaRange::LIGHT, SaturationRange::MUTED}}; |
512 | 512 |
513 // A totally dark gray image, which yields no prominent color as it's too | 513 // A totally dark gray image, which yields no prominent color as it's too |
514 // close to black. | 514 // close to black. |
515 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); | 515 gfx::Canvas canvas(gfx::Size(300, 200), 1.0f, true); |
516 canvas.FillRect(gfx::Rect(0, 0, 300, 200), SkColorSetRGB(10, 10, 10)); | 516 canvas.FillRect(gfx::Rect(0, 0, 300, 200), SkColorSetRGB(10, 10, 10)); |
517 SkBitmap bitmap = canvas.ToBitmap(); | 517 SkBitmap bitmap = canvas.GetBitmap(); |
518 | 518 |
519 // All expectations start at SK_ColorTRANSPARENT (i.e. 0). | 519 // All expectations start at SK_ColorTRANSPARENT (i.e. 0). |
520 SkColor expectations[arraysize(color_profiles)] = {}; | 520 SkColor expectations[arraysize(color_profiles)] = {}; |
521 for (size_t i = 0; i < arraysize(color_profiles); ++i) { | 521 for (size_t i = 0; i < arraysize(color_profiles); ++i) { |
522 EXPECT_EQ(expectations[i], | 522 EXPECT_EQ(expectations[i], |
523 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, | 523 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, |
524 color_profiles[i].saturation)); | 524 color_profiles[i].saturation)); |
525 } | 525 } |
526 | 526 |
527 // Add a green that could hit a couple values. | 527 // Add a green that could hit a couple values. |
528 const SkColor kVibrantGreen = SkColorSetRGB(25, 200, 25); | 528 const SkColor kVibrantGreen = SkColorSetRGB(25, 200, 25); |
529 canvas.FillRect(gfx::Rect(0, 1, 300, 1), kVibrantGreen); | 529 canvas.FillRect(gfx::Rect(0, 1, 300, 1), kVibrantGreen); |
530 bitmap = canvas.ToBitmap(); | 530 bitmap = canvas.GetBitmap(); |
531 expectations[0] = kVibrantGreen; | 531 expectations[0] = kVibrantGreen; |
532 expectations[1] = kVibrantGreen; | 532 expectations[1] = kVibrantGreen; |
533 for (size_t i = 0; i < arraysize(color_profiles); ++i) { | 533 for (size_t i = 0; i < arraysize(color_profiles); ++i) { |
534 EXPECT_EQ(expectations[i], | 534 EXPECT_EQ(expectations[i], |
535 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, | 535 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, |
536 color_profiles[i].saturation)); | 536 color_profiles[i].saturation)); |
537 } | 537 } |
538 | 538 |
539 // Add a stripe of a dark, muted green (saturation .33, luma .29). | 539 // Add a stripe of a dark, muted green (saturation .33, luma .29). |
540 const SkColor kDarkGreen = SkColorSetRGB(50, 100, 50); | 540 const SkColor kDarkGreen = SkColorSetRGB(50, 100, 50); |
541 canvas.FillRect(gfx::Rect(0, 2, 300, 1), kDarkGreen); | 541 canvas.FillRect(gfx::Rect(0, 2, 300, 1), kDarkGreen); |
542 bitmap = canvas.ToBitmap(); | 542 bitmap = canvas.GetBitmap(); |
543 expectations[3] = kDarkGreen; | 543 expectations[3] = kDarkGreen; |
544 for (size_t i = 0; i < arraysize(color_profiles); ++i) { | 544 for (size_t i = 0; i < arraysize(color_profiles); ++i) { |
545 EXPECT_EQ(expectations[i], | 545 EXPECT_EQ(expectations[i], |
546 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, | 546 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, |
547 color_profiles[i].saturation)); | 547 color_profiles[i].saturation)); |
548 } | 548 } |
549 | 549 |
550 // Now draw a little bit of pure green. That should be closer to the goal for | 550 // Now draw a little bit of pure green. That should be closer to the goal for |
551 // normal vibrant, but is out of range for other color profiles. | 551 // normal vibrant, but is out of range for other color profiles. |
552 const SkColor kPureGreen = SkColorSetRGB(0, 255, 0); | 552 const SkColor kPureGreen = SkColorSetRGB(0, 255, 0); |
553 canvas.FillRect(gfx::Rect(0, 3, 300, 1), kPureGreen); | 553 canvas.FillRect(gfx::Rect(0, 3, 300, 1), kPureGreen); |
554 bitmap = canvas.ToBitmap(); | 554 bitmap = canvas.GetBitmap(); |
555 expectations[1] = kPureGreen; | 555 expectations[1] = kPureGreen; |
556 for (size_t i = 0; i < arraysize(color_profiles); ++i) { | 556 for (size_t i = 0; i < arraysize(color_profiles); ++i) { |
557 EXPECT_EQ(expectations[i], | 557 EXPECT_EQ(expectations[i], |
558 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, | 558 CalculateProminentColorOfBitmap(bitmap, color_profiles[i].luma, |
559 color_profiles[i].saturation)); | 559 color_profiles[i].saturation)); |
560 } | 560 } |
561 } | 561 } |
562 | 562 |
563 } // namespace color_utils | 563 } // namespace color_utils |
OLD | NEW |