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 "include/core/SkBitmap.h"
9 #include "include/core/SkCanvas.h"
10 #include "include/effects/SkHighContrastFilter.h"
11 #include "tests/Test.h"
12
DEF_TEST(HighContrastFilter_FilterImage,reporter)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
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 }
46 }
47
DEF_TEST(HighContrastFilter_SanityCheck,reporter)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
DEF_TEST(HighContrastFilter_InvalidInputs,reporter)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
79 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertBrightness;
80 config.fContrast = 0.5f;
81 REPORTER_ASSERT(reporter, config.isValid());
82 filter = SkHighContrastFilter::Make(config);
83 REPORTER_ASSERT(reporter, filter);
84
85 // Invalid contrast
86 config.fContrast = 1.1f;
87 REPORTER_ASSERT(reporter, !config.isValid());
88 filter = SkHighContrastFilter::Make(config);
89 REPORTER_ASSERT(reporter, !filter);
90 }
91