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_SmokeTest,reporter)48 DEF_TEST(HighContrastFilter_SmokeTest, reporter) {
49 SkHighContrastConfig config;
50 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
51 sk_sp<SkColorFilter> filter = SkHighContrastFilter::Make(config);
52 REPORTER_ASSERT(reporter, filter->isAlphaUnchanged());
53
54 SkColor white_inverted = filter->filterColor(SK_ColorWHITE);
55 REPORTER_ASSERT(reporter, white_inverted == SK_ColorBLACK);
56
57 SkColor black_inverted = filter->filterColor(SK_ColorBLACK);
58 REPORTER_ASSERT(reporter, black_inverted == SK_ColorWHITE);
59 }
60
DEF_TEST(HighContrastFilter_InvalidInputs,reporter)61 DEF_TEST(HighContrastFilter_InvalidInputs, reporter) {
62 SkHighContrastConfig config;
63 REPORTER_ASSERT(reporter, config.isValid());
64
65 // Valid invert style
66 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertBrightness;
67 REPORTER_ASSERT(reporter, config.isValid());
68 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertLightness;
69 REPORTER_ASSERT(reporter, config.isValid());
70 sk_sp<SkColorFilter> filter = SkHighContrastFilter::Make(config);
71 REPORTER_ASSERT(reporter, filter);
72
73 // Invalid invert style
74 config.fInvertStyle = static_cast<SkHighContrastConfig::InvertStyle>(999);
75 REPORTER_ASSERT(reporter, !config.isValid());
76 filter = SkHighContrastFilter::Make(config);
77 REPORTER_ASSERT(reporter, !filter);
78
79 // Valid contrast
80 for (float contrast : {0.5f, +1.0f, -1.0f}) {
81 config.fInvertStyle = SkHighContrastConfig::InvertStyle::kInvertBrightness;
82 config.fContrast = contrast;
83 REPORTER_ASSERT(reporter, config.isValid());
84 filter = SkHighContrastFilter::Make(config);
85 REPORTER_ASSERT(reporter, filter);
86 }
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 }
94