ColorMatrixTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright 2015 Google Inc.
  3. *
  4. * Use of this source code is governed by a BSD-style license that can be
  5. * found in the LICENSE file.
  6. */
  7. #include "include/core/SkBitmap.h"
  8. #include "include/core/SkCanvas.h"
  9. #include "include/core/SkColor.h"
  10. #include "include/core/SkColorFilter.h"
  11. #include "include/core/SkPaint.h"
  12. #include "include/core/SkRefCnt.h"
  13. #include "tests/Test.h"
  14. #include <cmath>
  15. #include <cstdlib>
  16. static inline void assert_color(skiatest::Reporter* reporter,
  17. SkColor expected, SkColor actual, int tolerance) {
  18. REPORTER_ASSERT(reporter, abs((int)(SkColorGetA(expected) - SkColorGetA(actual))) <= tolerance);
  19. REPORTER_ASSERT(reporter, abs((int)(SkColorGetR(expected) - SkColorGetR(actual))) <= tolerance);
  20. REPORTER_ASSERT(reporter, abs((int)(SkColorGetG(expected) - SkColorGetG(actual))) <= tolerance);
  21. REPORTER_ASSERT(reporter, abs((int)(SkColorGetB(expected) - SkColorGetB(actual))) <= tolerance);
  22. }
  23. static inline void assert_color(skiatest::Reporter* reporter, SkColor expected, SkColor actual) {
  24. const int TOLERANCE = 1;
  25. assert_color(reporter, expected, actual, TOLERANCE);
  26. }
  27. /**
  28. * This test case is a mirror of the Android CTS tests for MatrixColorFilter
  29. * found in the android.graphics.ColorMatrixColorFilterTest class.
  30. */
  31. static inline void test_colorMatrixCTS(skiatest::Reporter* reporter) {
  32. SkBitmap bitmap;
  33. bitmap.allocN32Pixels(1,1);
  34. SkCanvas canvas(bitmap);
  35. SkPaint paint;
  36. float blueToCyan[20] = {
  37. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  38. 0.0f, 1.0f, 1.0f, 0.0f, 0.0f,
  39. 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
  40. 0.0f, 0.0f, 0.0f, 1.0f, 0.0f };
  41. paint.setColorFilter(SkColorFilters::Matrix(blueToCyan));
  42. paint.setColor(SK_ColorBLUE);
  43. canvas.drawPoint(0, 0, paint);
  44. assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
  45. paint.setColor(SK_ColorGREEN);
  46. canvas.drawPoint(0, 0, paint);
  47. assert_color(reporter, SK_ColorGREEN, bitmap.getColor(0, 0));
  48. paint.setColor(SK_ColorRED);
  49. canvas.drawPoint(0, 0, paint);
  50. assert_color(reporter, SK_ColorRED, bitmap.getColor(0, 0));
  51. // color components are clipped, not scaled
  52. paint.setColor(SK_ColorMAGENTA);
  53. canvas.drawPoint(0, 0, paint);
  54. assert_color(reporter, SK_ColorWHITE, bitmap.getColor(0, 0));
  55. float transparentRedAddBlue[20] = {
  56. 1.0f, 0.0f, 0.0f, 0.0f, 0.0f,
  57. 0.0f, 1.0f, 0.0f, 0.0f, 0.0f,
  58. 0.0f, 0.0f, 1.0f, 0.0f, 64.0f/255,
  59. -0.5f, 0.0f, 0.0f, 1.0f, 0.0f
  60. };
  61. paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
  62. bitmap.eraseColor(SK_ColorTRANSPARENT);
  63. paint.setColor(SK_ColorRED);
  64. canvas.drawPoint(0, 0, paint);
  65. assert_color(reporter, SkColorSetARGB(128, 255, 0, 64), bitmap.getColor(0, 0), 2);
  66. paint.setColor(SK_ColorCYAN);
  67. canvas.drawPoint(0, 0, paint);
  68. // blue gets clipped
  69. assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
  70. // change array to filter out green
  71. REPORTER_ASSERT(reporter, 1.0f == transparentRedAddBlue[6]);
  72. transparentRedAddBlue[6] = 0.0f;
  73. // check that changing the array has no effect
  74. canvas.drawPoint(0, 0, paint);
  75. assert_color(reporter, SK_ColorCYAN, bitmap.getColor(0, 0));
  76. // create a new filter with the changed matrix
  77. paint.setColorFilter(SkColorFilters::Matrix(transparentRedAddBlue));
  78. canvas.drawPoint(0, 0, paint);
  79. assert_color(reporter, SK_ColorBLUE, bitmap.getColor(0, 0));
  80. }
  81. DEF_TEST(ColorMatrix, reporter) {
  82. test_colorMatrixCTS(reporter);
  83. }
  84. DEF_TEST(ColorMatrix_clamp_while_unpremul, r) {
  85. // This matrix does green += 255/255 and alpha += 32/255. We want to test
  86. // that if we pass it opaque alpha and small red and blue values, red and
  87. // blue stay unchanged, not pumped up by that ~1.12 intermediate alpha.
  88. float m[] = {
  89. 1, 0, 0, 0, 0,
  90. 0, 1, 0, 0, 1,
  91. 0, 0, 1, 0, 0,
  92. 0, 0, 0, 1, 32.0f/255,
  93. };
  94. auto filter = SkColorFilters::Matrix(m);
  95. SkColor filtered = filter->filterColor(0xff0a0b0c);
  96. REPORTER_ASSERT(r, SkColorGetA(filtered) == 0xff);
  97. REPORTER_ASSERT(r, SkColorGetR(filtered) == 0x0a);
  98. REPORTER_ASSERT(r, SkColorGetG(filtered) == 0xff);
  99. REPORTER_ASSERT(r, SkColorGetB(filtered) == 0x0c);
  100. }