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