Welcome to mirror list, hosted at ThFree Co, Russian Federation.

OperationLayerImport.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 07099f0279505af805fa5952d6f05da45553f8fb (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
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
/*
 *                     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.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.Objects;

namespace UVtools.Core.Operations
{
    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 LayerRangeSelection => Enumerations.LayerRangeSelection.None;
        public override bool CanROI { get; set; } = 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 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));
        }

        

        #endregion
    }
}