blob: b7c8502f3759c7a068cb7511f66644c68af4ea89 (
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
|
/*
* 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.Generic;
using System.Drawing;
using System.IO;
using System.Text;
using System.Threading.Tasks;
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.Objects;
namespace UVtools.Core.Operations
{
public sealed class OperationLayerImport : Operation
{
#region Overrides
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 => false;
public override uint LayerIndexStart => InsertAfterLayerIndex + (ReplaceStartLayer ? 0u : 1);
public override uint LayerIndexEnd => (uint)(LayerIndexStart + Count - 1);
public override StringTag Validate(params object[] parameters)
{
var result = new ConcurrentBag<string>();
Parallel.ForEach(Files, file =>
{
using (Mat mat = CvInvoke.Imread(file, 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(Path.GetFileNameWithoutExtension(file));
}
return new StringTag(message.ToString(), result);
}
#endregion
#region Properties
public Size FileResolution { get; }
public uint InsertAfterLayerIndex { get; set; }
public bool ReplaceStartLayer { get; set; }
public bool ReplaceSubsequentLayers { get; set; }
public bool DiscardRemainingLayers { get; set; }
public bool MergeImages { get; set; }
public List<string> Files { get; } = new List<string>();
public int Count => Files.Count;
#endregion
#region Constructor
public OperationLayerImport()
{
}
public OperationLayerImport(Size fileResolution)
{
FileResolution = fileResolution;
}
#endregion
#region Methods
public void Sort()
{
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));
}
#endregion
}
}
|