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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
|
/*
* 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.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.FileFormats;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
[Serializable]
public sealed class OperationLayerImport : Operation
{
private uint _insertAfterLayerIndex;
private bool _replaceStartLayer;
private bool _replaceSubsequentLayers;
private bool _discardRemainingLayers;
private bool _mergeImages;
private ObservableCollection<StringTag> _files = new ObservableCollection<StringTag>();
#region Overrides
public override Enumerations.LayerRangeSelection StartLayerRangeSelection => Enumerations.LayerRangeSelection.None;
public override bool CanROI => false;
public override string Title => "Import Layers";
public override string Description =>
"Import layers from local files into the model at a selected layer height.\n\n" +
"NOTE: Imported images must be greyscale and have the same resolution as the model.";
public override string ConfirmationText => $"import {Count} layer{(Count!=1?"s":"")}?";
public override string ProgressTitle =>
$"Importing {Count} layer{(Count!=1 ? "s" : "")}";
public override string ProgressAction => "Imported layers";
public override bool CanCancel => true;
public override uint LayerIndexStart => InsertAfterLayerIndex + (ReplaceStartLayer ? 0u : 1);
public override uint LayerIndexEnd => (uint)(LayerIndexStart + Count - 1);
public override bool CanHaveProfiles => false;
public override StringTag Validate(params object[] parameters)
{
var result = new ConcurrentBag<StringTag>();
Parallel.ForEach(Files, file =>
{
using (Mat mat = CvInvoke.Imread(file.TagString, ImreadModes.AnyColor))
{
if (mat.Size != FileResolution)
{
result.Add(file);
}
}
});
if (result.IsEmpty) return null;
var message = new StringBuilder();
message.AppendLine($"The following {result.Count} files mismatched the target resolution of {FileResolution.Width}x{FileResolution.Height}:");
message.AppendLine();
uint count = 0;
foreach (var file in result)
{
count++;
if (count == 20)
{
message.AppendLine("... Too many to show ...");
break;
}
message.AppendLine(file.Content);
}
return new StringTag(message.ToString(), result);
}
#endregion
#region Properties
public Size FileResolution { get; }
public uint InsertAfterLayerIndex
{
get => _insertAfterLayerIndex;
set => RaiseAndSetIfChanged(ref _insertAfterLayerIndex, value);
}
public bool ReplaceStartLayer
{
get => _replaceStartLayer;
set => RaiseAndSetIfChanged(ref _replaceStartLayer, value);
}
public bool ReplaceSubsequentLayers
{
get => _replaceSubsequentLayers;
set => RaiseAndSetIfChanged(ref _replaceSubsequentLayers, value);
}
public bool DiscardRemainingLayers
{
get => _discardRemainingLayers;
set => RaiseAndSetIfChanged(ref _discardRemainingLayers, value);
}
public bool MergeImages
{
get => _mergeImages;
set => RaiseAndSetIfChanged(ref _mergeImages, value);
}
public ObservableCollection<StringTag> Files
{
get => _files;
set => RaiseAndSetIfChanged(ref _files, value);
}
public int Count => Files.Count;
#endregion
#region Constructor
public OperationLayerImport()
{
}
public OperationLayerImport(Size fileResolution)
{
FileResolution = fileResolution;
}
#endregion
#region Methods
public void AddFile(string file)
{
Files.Add(new StringTag(Path.GetFileNameWithoutExtension(file), file));
}
public void Sort()
{
var sortedFiles = Files.ToList();
sortedFiles.Sort((file1, file2) => string.Compare(Path.GetFileNameWithoutExtension(file1.TagString), Path.GetFileNameWithoutExtension(file2.TagString), StringComparison.Ordinal));
Files.Clear();
foreach (var file in sortedFiles)
{
Files.Add(file);
}
//Files.Sort((file1, file2) => string.Compare(Path.GetFileNameWithoutExtension(file1), Path.GetFileNameWithoutExtension(file2), StringComparison.Ordinal));
}
public uint CalculateTotalLayers(uint totalLayers)
{
if (DiscardRemainingLayers)
{
return (uint) (1 + InsertAfterLayerIndex + Files.Count - (ReplaceStartLayer ? 1 : 0));
}
if (ReplaceSubsequentLayers)
{
uint result = (uint) (1 + InsertAfterLayerIndex + Files.Count - (ReplaceStartLayer ? 1 : 0));
return result <= totalLayers ? totalLayers : result;
}
return (uint)(totalLayers + Files.Count - (ReplaceStartLayer ? 1 : 0));
}
public override string ToString()
{
var result = $"[Files: {Count}]" + LayerRangeString;
if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
return result;
}
public override bool Execute(FileFormat slicerFile, OperationProgress progress = null)
{
progress ??= new OperationProgress();
progress.Reset(ProgressAction, (uint)Count);
var oldLayers = slicerFile.LayerManager.Layers;
uint newLayerCount = CalculateTotalLayers((uint)oldLayers.Length);
uint startIndex = LayerIndexStart;
var layers = new Layer[newLayerCount];
// Keep same layers up to InsertAfterLayerIndex
for (uint i = 0; i <= InsertAfterLayerIndex; i++)
{
layers[i] = oldLayers[i];
}
// Keep all old layers if not discarding them
if (ReplaceSubsequentLayers)
{
if (!DiscardRemainingLayers)
{
for (uint i = InsertAfterLayerIndex + 1; i < oldLayers.Length; i++)
{
layers[i] = oldLayers[i];
}
}
}
else // Push remaining layers to the end of imported layers
{
uint oldLayerIndex = InsertAfterLayerIndex;
for (uint i = LayerIndexEnd + 1; i < newLayerCount; i++)
{
oldLayerIndex++;
layers[i] = oldLayers[oldLayerIndex];
}
}
Parallel.For(0, Count,
//new ParallelOptions{MaxDegreeOfParallelism = 1},
i =>
{
var mat = CvInvoke.Imread(Files[i].TagString, ImreadModes.Grayscale);
uint layerIndex = (uint)(startIndex + i);
if (MergeImages)
{
if (layers[layerIndex] is not null)
{
using var oldMat = layers[layerIndex].LayerMat;
CvInvoke.Add(oldMat, mat, mat);
}
}
layers[layerIndex] = new Layer(layerIndex, mat, slicerFile.LayerManager);
lock (progress.Mutex)
{
progress++;
}
});
slicerFile.LayerManager.Layers = layers;
slicerFile.RequireFullEncode = true;
progress.Token.ThrowIfCancellationRequested();
return true;
}
#endregion
}
}
|