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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
using System;
using System.Drawing;
using Avalonia.Controls;
using Avalonia.Markup.Xaml;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using UVtools.Core.Extensions;
using UVtools.Core.Operations;
using UVtools.WPF.Extensions;
using UVtools.WPF.Windows;
using Bitmap = Avalonia.Media.Imaging.Bitmap;
namespace UVtools.WPF.Controls.Tools
{
public class ToolMaskControl : ToolControl
{
private bool _isMaskInverted;
private byte _genMinimumBrightness = 200;
private byte _genMaximumBrightness = byte.MaxValue;
private uint _genDiameter;
private Bitmap _maskImage;
public OperationMask Operation => BaseOperation as OperationMask;
public bool IsMaskInverted
{
get => _isMaskInverted;
set
{
if(!RaiseAndSetIfChanged(ref _isMaskInverted, value)) return;
if (!Operation.HaveMask) return;
Operation.InvertMask();
MaskImage = Operation.Mask.ToBitmap();
}
}
public byte GenMinimumBrightness
{
get => _genMinimumBrightness;
set => RaiseAndSetIfChanged(ref _genMinimumBrightness, value);
}
public byte GenMaximumBrightness
{
get => _genMaximumBrightness;
set => RaiseAndSetIfChanged(ref _genMaximumBrightness, value);
}
public uint GenDiameter
{
get => _genDiameter;
set => RaiseAndSetIfChanged(ref _genDiameter, value);
}
public string InfoPrinterResolutionStr => $"Printer resolution: {App.SlicerFile.Resolution}";
public string InfoMaskResolutionStr => $"Mask resolution: "+ (Operation.HaveMask ? Operation.Mask.Size.ToString() : "(Unloaded)");
public Bitmap MaskImage
{
get => _maskImage;
set
{
if(!RaiseAndSetIfChanged(ref _maskImage, value)) return;
RaisePropertyChanged(nameof(InfoMaskResolutionStr));
ParentWindow.ButtonOkEnabled = Operation.HaveMask;
}
}
public ToolMaskControl()
{
InitializeComponent();
BaseOperation = new OperationMask(SlicerFile);
}
private void InitializeComponent()
{
AvaloniaXamlLoader.Load(this);
}
public override void Callback(ToolWindow.Callbacks callback)
{
switch (callback)
{
case ToolWindow.Callbacks.Init:
ParentWindow.ButtonOkEnabled = false;
break;
case ToolWindow.Callbacks.Loaded:
case ToolWindow.Callbacks.ClearROI:
Operation.Mask = null;
MaskImage = null;
break;
}
}
public async void ImportImageMask()
{
var dialog = new OpenFileDialog
{
Filters = Helpers.ImagesFileFilter,
AllowMultiple = false
};
var result = await dialog.ShowAsync(ParentWindow);
if (result is null || result.Length == 0) return;
try
{
Operation.Mask = CvInvoke.Imread(result[0], ImreadModes.Grayscale);
var roi = App.MainWindow.ROI;
if (roi.IsEmpty)
{
if (Operation.Mask.Size != App.SlicerFile.Resolution)
{
CvInvoke.Resize(Operation.Mask, Operation.Mask, App.SlicerFile.Resolution);
}
}
else
{
if (Operation.Mask.Size != roi.Size)
{
CvInvoke.Resize(Operation.Mask, Operation.Mask, roi.Size);
}
}
if (_isMaskInverted)
{
Operation.InvertMask();
}
MaskImage = Operation.Mask.ToBitmap();
}
catch (Exception e)
{
await ParentWindow.MessageBoxError(e.ToString(), "Error while trying to read the image");
}
}
public void GenerateMask()
{
var roi = App.MainWindow.ROI;
Operation.Mask = roi.IsEmpty ? App.MainWindow.LayerCache.Image.CloneBlank() : new Mat(roi.Size, DepthType.Cv8U, 1);
int radius = (int)_genDiameter;
if (radius == 0)
{
radius = Math.Min(Operation.Mask.Width, Operation.Mask.Height) / 2;
}
else
{
radius = radius.Clamp(2, Math.Min(Operation.Mask.Width, Operation.Mask.Height)) / 2;
}
var maxScalar = new MCvScalar(_genMaximumBrightness);
Operation.Mask.SetTo(maxScalar);
var center = new Point(Operation.Mask.Width / 2, Operation.Mask.Height / 2);
var colorDifference = _genMinimumBrightness - _genMaximumBrightness;
//CvInvoke.Circle(Mask, center, radius, minScalar, -1);
for (decimal i = 1; i < radius; i++)
{
int color = (int)(_genMinimumBrightness - i / radius * colorDifference); //or some another color calculation
CvInvoke.Circle(Operation.Mask, center, (int)i, new MCvScalar(color), 2);
}
if (_isMaskInverted)
Operation.InvertMask();
MaskImage = Operation.Mask.ToBitmap();
}
}
}
|