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