Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017 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/effects/SkHighContrastFilter.h" |
| 11 | #include "tests/Test.h" |
Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 12 | |
| 13 | DEF_TEST(HighContrastFilter_FilterImage, reporter) { |
| 14 | SkHighContrastConfig config; |
| 15 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness; |
| 16 | |
| 17 | int w = 10, h = 10; |
| 18 | SkBitmap filterResult, paintResult; |
| 19 | |
| 20 | filterResult.allocN32Pixels(w, h); |
| 21 | SkCanvas canvasFilter(filterResult); |
| 22 | canvasFilter.clear(0x00000000); |
| 23 | |
| 24 | paintResult.allocN32Pixels(w, h); |
| 25 | SkCanvas canvasPaint(paintResult); |
| 26 | canvasPaint.clear(0x00000000); |
| 27 | |
| 28 | SkPaint paint; |
| 29 | paint.setColor(SK_ColorBLUE); |
| 30 | SkRect r = SkRect::MakeLTRB(SkIntToScalar(2), SkIntToScalar(2), |
| 31 | SkIntToScalar(8), SkIntToScalar(8)); |
| 32 | canvasPaint.drawRect(r, paint); |
| 33 | |
| 34 | paint.setColorFilter(SkHighContrastFilter::Make(config)); |
| 35 | canvasFilter.drawRect(r, paint); |
| 36 | |
Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 37 | for (int y = r.top(); y < r.bottom(); ++y) { |
| 38 | for (int x = r.left(); x < r.right(); ++x) { |
| 39 | SkColor paintColor = paintResult.getColor(x, y); |
| 40 | SkColor filterColor = filterResult.getColor(x, y); |
| 41 | REPORTER_ASSERT( |
| 42 | reporter, filterColor == |
| 43 | paint.getColorFilter()->filterColor(paintColor)); |
| 44 | } |
| 45 | } |
Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | DEF_TEST(HighContrastFilter_SanityCheck, reporter) { |
| 49 | SkHighContrastConfig config; |
| 50 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness; |
| 51 | sk_sp<SkColorFilter> filter = SkHighContrastFilter::Make(config); |
| 52 | |
| 53 | SkColor white_inverted = filter->filterColor(SK_ColorWHITE); |
| 54 | REPORTER_ASSERT(reporter, white_inverted == SK_ColorBLACK); |
| 55 | |
| 56 | SkColor black_inverted = filter->filterColor(SK_ColorBLACK); |
| 57 | REPORTER_ASSERT(reporter, black_inverted == SK_ColorWHITE); |
| 58 | } |
| 59 | |
| 60 | DEF_TEST(HighContrastFilter_InvalidInputs, reporter) { |
| 61 | SkHighContrastConfig config; |
| 62 | REPORTER_ASSERT(reporter, config.isValid()); |
| 63 | |
| 64 | // Valid invert style |
| 65 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertBrightness; |
| 66 | REPORTER_ASSERT(reporter, config.isValid()); |
| 67 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness; |
| 68 | REPORTER_ASSERT(reporter, config.isValid()); |
| 69 | sk_sp<SkColorFilter> filter = SkHighContrastFilter::Make(config); |
| 70 | REPORTER_ASSERT(reporter, filter); |
| 71 | |
| 72 | // Invalid invert style |
| 73 | config.fInvertStyle = static_cast<SkHighContrastConfig::InvertStyle>(999); |
| 74 | REPORTER_ASSERT(reporter, !config.isValid()); |
| 75 | filter = SkHighContrastFilter::Make(config); |
| 76 | REPORTER_ASSERT(reporter, !filter); |
| 77 | |
| 78 | // Valid contrast |
Mike Klein | 2869869 | 2021-02-12 07:59:55 -0600 | [diff] [blame] | 79 | for (float contrast : {0.5f, +1.0f, -1.0f}) { |
| 80 | config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertBrightness; |
| 81 | config.fContrast = contrast; |
| 82 | REPORTER_ASSERT(reporter, config.isValid()); |
| 83 | filter = SkHighContrastFilter::Make(config); |
| 84 | REPORTER_ASSERT(reporter, filter); |
| 85 | } |
Dominic Mazzoni | 394d414 | 2017-02-14 11:15:31 -0800 | [diff] [blame] | 86 | |
| 87 | // Invalid contrast |
| 88 | config.fContrast = 1.1f; |
| 89 | REPORTER_ASSERT(reporter, !config.isValid()); |
| 90 | filter = SkHighContrastFilter::Make(config); |
| 91 | REPORTER_ASSERT(reporter, !filter); |
| 92 | } |