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