VectSharp  2.2.1
A light library for C# vector graphics
CompositeFilter.cs
1 /*
2  VectSharp - A light library for C# vector graphics.
3  Copyright (C) 2020-2022 Giorgio Bianchini
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, version 3.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU Lesser General Public License for more details.
13 
14  You should have received a copy of the GNU Lesser General Public License
15  along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17 
18 using System.Collections.Generic;
19 using System.Collections.Immutable;
20 
21 namespace VectSharp.Filters
22 {
23  /// <summary>
24  /// Represents a filter that corresponds to applying multiple <see cref="ILocationInvariantFilter"/>s one after the other.
25  /// </summary>
27  {
28  /// <inheritdoc/>
29  public Point TopLeftMargin { get; }
30 
31  /// <inheritdoc/>
32  public Point BottomRightMargin { get; }
33 
34  /// <summary>
35  /// The filters that are applied by this filter.
36  /// </summary>
37  public ImmutableList<ILocationInvariantFilter> Filters { get; }
38 
39  /// <summary>
40  /// Creates a new <see cref="CompositeLocationInvariantFilter"/> with the specified filters.
41  /// </summary>
42  /// <param name="filters">The filters that will be applied by the new filter.</param>
43  public CompositeLocationInvariantFilter(IEnumerable<ILocationInvariantFilter> filters)
44  {
45  IEnumerable<ILocationInvariantFilter> flattenedFilters = FlattenFilters(filters);
46  this.Filters = ImmutableList.CreateRange(flattenedFilters);
47 
48  bool initialised = false;
49 
50  foreach (IFilter filter in flattenedFilters)
51  {
52  if (!initialised)
53  {
54  this.TopLeftMargin = filter.TopLeftMargin;
56  initialised = true;
57  }
58  else
59  {
60  this.TopLeftMargin = Point.Max(this.TopLeftMargin, filter.TopLeftMargin);
62  }
63  }
64  }
65 
66  /// <summary>
67  /// Creates a new <see cref="CompositeLocationInvariantFilter"/> with the specified filters.
68  /// </summary>
69  /// <param name="filters">The filters that will be applied by the new filter.</param>
70  public CompositeLocationInvariantFilter(params ILocationInvariantFilter[] filters) : this((IEnumerable<ILocationInvariantFilter>)filters) { }
71 
72  private IEnumerable<ILocationInvariantFilter> FlattenFilters(IEnumerable<ILocationInvariantFilter> filters)
73  {
74  foreach (ILocationInvariantFilter filter in filters)
75  {
76  if (filter is CompositeLocationInvariantFilter composite)
77  {
78  foreach (ILocationInvariantFilter filter2 in FlattenFilters(composite.Filters))
79  {
80  yield return filter2;
81  }
82  }
83  else
84  {
85  yield return filter;
86  }
87  }
88  }
89 
90  /// <inheritdoc/>
91  public RasterImage Filter(RasterImage image, double scale)
92  {
93  RasterImage currImage = image;
94 
95  foreach (ILocationInvariantFilter filter in this.Filters)
96  {
97  RasterImage prevImage = currImage;
98  currImage = filter.Filter(prevImage, scale);
99 
100  if (prevImage != image)
101  {
102  prevImage.Dispose();
103  }
104  }
105 
106  return currImage;
107  }
108  }
109 }
VectSharp.Filters.IFilter.TopLeftMargin
Point TopLeftMargin
Determines how much the area of the filter's subject should be expanded on the top-left to accommodat...
Definition: Filters.cs:30
VectSharp.Filters.CompositeLocationInvariantFilter.CompositeLocationInvariantFilter
CompositeLocationInvariantFilter(IEnumerable< ILocationInvariantFilter > filters)
Creates a new CompositeLocationInvariantFilter with the specified filters.
Definition: CompositeFilter.cs:43
VectSharp.RasterImage
Represents a raster image, created from raw pixel data. Consider using the derived classes included i...
Definition: RasterImage.cs:99
VectSharp.Filters.CompositeLocationInvariantFilter.TopLeftMargin
Point TopLeftMargin
Definition: CompositeFilter.cs:29
VectSharp.Filters.IFilter
Represents a filter. Do not implement this interface directly; instead, implement ILocationInvariantF...
Definition: Filters.cs:26
VectSharp.Filters.CompositeLocationInvariantFilter.CompositeLocationInvariantFilter
CompositeLocationInvariantFilter(params ILocationInvariantFilter[] filters)
Creates a new CompositeLocationInvariantFilter with the specified filters.
Definition: CompositeFilter.cs:70
VectSharp.Filters.CompositeLocationInvariantFilter.Filter
RasterImage Filter(RasterImage image, double scale)
Applies the filter to a RasterImage.
Definition: CompositeFilter.cs:91
VectSharp.Filters.ILocationInvariantFilter.Filter
RasterImage Filter(RasterImage image, double scale)
Applies the filter to a RasterImage.
VectSharp.Filters
Definition: BoxBlurFilter.cs:22
VectSharp.Filters.IFilter.BottomRightMargin
Point BottomRightMargin
Determines how much the area of the filter's subject should be expanded on the bottom-right to accomm...
Definition: Filters.cs:35
VectSharp.Filters.ILocationInvariantFilter
Represents a filter that can be applied to an image regardless of its location on the graphics surfac...
Definition: Filters.cs:42
VectSharp.Filters.CompositeLocationInvariantFilter
Represents a filter that corresponds to applying multiple ILocationInvariantFilters one after the oth...
Definition: CompositeFilter.cs:27
VectSharp.Point.Max
static Point Max(Point p1, Point p2)
Computes the bottom-right corner of the Rectangle identified by two Points.
Definition: Point.cs:97
VectSharp.Point
Represents a point relative to an origin in the top-left corner.
Definition: Point.cs:28
VectSharp.Filters.CompositeLocationInvariantFilter.Filters
ImmutableList< ILocationInvariantFilter > Filters
The filters that are applied by this filter.
Definition: CompositeFilter.cs:37
VectSharp.Filters.CompositeLocationInvariantFilter.BottomRightMargin
Point BottomRightMargin
Definition: CompositeFilter.cs:32