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
134
135
136
137
138
139
140
141
142
143
|
/*
* GNU AFFERO GENERAL PUBLIC LICENSE
* Version 3, 19 November 2007
* Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/>
* Everyone is permitted to copy and distribute verbatim copies
* of this license document, but changing it is not allowed.
*/
using System;
using System.Text;
using System.Xml.Serialization;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationBlur : Operation
{
private BlurAlgorithm _blurOperation = BlurAlgorithm.Blur;
private uint _size = 1;
#region Overrides
public override string Title => "Blur";
public override string Description =>
$"Blur layer images by applying a low pass filter\n\n" +
"NOTE: Target printer must support AntiAliasing in order to use this function.\n\n" +
"See https://docs.opencv.org/master/d4/d13/tutorial_py_filtering.html";
public override string ConfirmationText =>
$"blur model with {BlurOperation} from layers {LayerIndexStart} through {LayerIndexEnd}?";
public override string ProgressTitle =>
$"Bluring model with {BlurOperation} from layers {LayerIndexStart} through {LayerIndexEnd}";
public override string ProgressAction => "Blured layers";
public override StringTag Validate(params object[] parameters)
{
var sb = new StringBuilder();
if (BlurOperation == BlurAlgorithm.GaussianBlur ||
BlurOperation == BlurAlgorithm.MedianBlur)
{
if (Size % 2 != 1)
{
sb.AppendLine("Size must be a odd number.");
}
}
if (BlurOperation == BlurAlgorithm.Filter2D)
{
if (Kernel is null)
{
sb.AppendLine("Kernel can not be empty.");
}
}
return new StringTag(sb.ToString());
}
#endregion
#region Enums
public enum BlurAlgorithm
{
Blur,
Pyramid,
MedianBlur,
GaussianBlur,
Filter2D
}
#endregion
#region Properties
public static StringTag[] BlurTypes => new[]
{
new StringTag("Blur: Normalized box filter", BlurAlgorithm.Blur),
new StringTag("Pyramid: Down/up-sampling step of Gaussian pyramid decomposition", BlurAlgorithm.Pyramid),
new StringTag("Median Blur: Each pixel becomes the median of its surrounding pixels", BlurAlgorithm.MedianBlur),
new StringTag("Gaussian Blur: Each pixel is a sum of fractions of each pixel in its neighborhood", BlurAlgorithm.GaussianBlur),
new StringTag("Filter 2D: Applies an arbitrary linear filter to an image", BlurAlgorithm.Filter2D),
};
public byte BlurTypeIndex
{
get
{
for (byte i = 0; i < BlurTypes.Length; i++)
{
if ((BlurAlgorithm)BlurTypes[i].Tag == BlurOperation) return i;
}
return 0;
}
}
public BlurAlgorithm BlurOperation
{
get => _blurOperation;
set => RaiseAndSetIfChanged(ref _blurOperation, value);
}
public uint Size
{
get => _size;
set => RaiseAndSetIfChanged(ref _size, value);
}
[XmlIgnore]
public Kernel Kernel { get; set; } = new Kernel();
public override string ToString()
{
var result = $"[{_blurOperation}] [Size: {_size}]" + LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
#endregion
#region Equality
private bool Equals(OperationBlur other)
{
return _blurOperation == other._blurOperation && _size == other._size;
}
public override bool Equals(object obj)
{
return ReferenceEquals(this, obj) || obj is OperationBlur other && Equals(other);
}
public override int GetHashCode()
{
unchecked
{
return ((int) _blurOperation * 397) ^ (int) _size;
}
}
#endregion
}
}
|