summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/filters/fe_morphology.cc
blob: 40842ec1d32538df0bdaeb6e23cbd2c8c23d2669 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * Copyright (C) 2004, 2005, 2006, 2007 Nikolas Zimmermann <zimmermann@kde.org>
 * Copyright (C) 2004, 2005 Rob Buis <buis@kde.org>
 * Copyright (C) 2005 Eric Seidel <eric@webkit.org>
 * Copyright (C) 2009 Dirk Schulze <krit@webkit.org>
 * Copyright (C) Research In Motion Limited 2010. All rights reserved.
 * Copyright (C) 2013 Google Inc. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this library; see the file COPYING.LIB.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#include "third_party/blink/renderer/platform/graphics/filters/fe_morphology.h"

#include "SkMorphologyImageFilter.h"
#include "third_party/blink/renderer/platform/graphics/filters/filter.h"
#include "third_party/blink/renderer/platform/graphics/filters/paint_filter_builder.h"
#include "third_party/blink/renderer/platform/wtf/text/text_stream.h"

namespace blink {

FEMorphology::FEMorphology(Filter* filter,
                           MorphologyOperatorType type,
                           float radius_x,
                           float radius_y)
    : FilterEffect(filter),
      type_(type),
      radius_x_(std::max(0.0f, radius_x)),
      radius_y_(std::max(0.0f, radius_y)) {}

FEMorphology* FEMorphology::Create(Filter* filter,
                                   MorphologyOperatorType type,
                                   float radius_x,
                                   float radius_y) {
  return new FEMorphology(filter, type, radius_x, radius_y);
}

MorphologyOperatorType FEMorphology::MorphologyOperator() const {
  return type_;
}

bool FEMorphology::SetMorphologyOperator(MorphologyOperatorType type) {
  if (type_ == type)
    return false;
  type_ = type;
  return true;
}

float FEMorphology::RadiusX() const {
  return radius_x_;
}

bool FEMorphology::SetRadiusX(float radius_x) {
  radius_x = std::max(0.0f, radius_x);
  if (radius_x_ == radius_x)
    return false;
  radius_x_ = radius_x;
  return true;
}

float FEMorphology::RadiusY() const {
  return radius_y_;
}

bool FEMorphology::SetRadiusY(float radius_y) {
  radius_y = std::max(0.0f, radius_y);
  if (radius_y_ == radius_y)
    return false;
  radius_y_ = radius_y;
  return true;
}

FloatRect FEMorphology::MapEffect(const FloatRect& rect) const {
  FloatRect result = rect;
  result.InflateX(GetFilter()->ApplyHorizontalScale(radius_x_));
  result.InflateY(GetFilter()->ApplyVerticalScale(radius_y_));
  return result;
}

sk_sp<PaintFilter> FEMorphology::CreateImageFilter() {
  sk_sp<PaintFilter> input(
      PaintFilterBuilder::Build(InputEffect(0), OperatingInterpolationSpace()));
  int radius_x = clampTo<int>(GetFilter()->ApplyHorizontalScale(radius_x_));
  int radius_y = clampTo<int>(GetFilter()->ApplyVerticalScale(radius_y_));
  PaintFilter::CropRect rect = GetCropRect();
  MorphologyPaintFilter::MorphType morph_type =
      type_ == FEMORPHOLOGY_OPERATOR_DILATE
          ? MorphologyPaintFilter::MorphType::kDilate
          : MorphologyPaintFilter::MorphType::kErode;
  return sk_make_sp<MorphologyPaintFilter>(morph_type, radius_x, radius_y,
                                           std::move(input), &rect);
}

static WTF::TextStream& operator<<(WTF::TextStream& ts,
                                   const MorphologyOperatorType& type) {
  switch (type) {
    case FEMORPHOLOGY_OPERATOR_UNKNOWN:
      ts << "UNKNOWN";
      break;
    case FEMORPHOLOGY_OPERATOR_ERODE:
      ts << "ERODE";
      break;
    case FEMORPHOLOGY_OPERATOR_DILATE:
      ts << "DILATE";
      break;
  }
  return ts;
}

WTF::TextStream& FEMorphology::ExternalRepresentation(WTF::TextStream& ts,
                                                      int indent) const {
  WriteIndent(ts, indent);
  ts << "[feMorphology";
  FilterEffect::ExternalRepresentation(ts);
  ts << " operator=\"" << MorphologyOperator() << "\" "
     << "radius=\"" << RadiusX() << ", " << RadiusY() << "\"]\n";
  InputEffect(0)->ExternalRepresentation(ts, indent + 1);
  return ts;
}

}  // namespace blink