HighContrastFilterTest.cpp 3.1 KB

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