djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 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 | |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 8 | #include "SkBitmap.h" |
| 9 | #include "SkCanvas.h" |
| 10 | #include "SkColor.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 11 | #include "SkColorFilter.h" |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 12 | #include "SkPaint.h" |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 13 | #include "SkRefCnt.h" |
| 14 | #include "SkScalar.h" |
| 15 | #include "Test.h" |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 16 | |
Ben Wagner | eed6128 | 2018-04-17 14:14:51 -0400 | [diff] [blame] | 17 | #include <cmath> |
| 18 | #include <cstdlib> |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 19 | |
| 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 | |
| 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 | */ |
| 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 }; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 50 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(blueToCyan)); |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 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 | }; |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 75 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(transparentRedAddBlue)); |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 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 |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 96 | paint.setColorFilter(SkColorFilter::MakeMatrixFilterRowMajor255(transparentRedAddBlue)); |
djsollen | b500ffa | 2015-06-05 09:41:18 -0700 | [diff] [blame] | 97 | canvas.drawPoint(0, 0, paint); |
| 98 | assert_color(reporter, SK_ColorBLUE, bitmap.getColor(0, 0)); |
| 99 | } |
| 100 | |
| 101 | DEF_TEST(ColorMatrix, reporter) { |
| 102 | test_colorMatrixCTS(reporter); |
| 103 | } |
Mike Klein | d53f9a1 | 2017-11-29 16:31:37 -0500 | [diff] [blame] | 104 | |
| 105 | |
| 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 | } |