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