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: d7a75babe709daa37ff662092aa44c1dd4efdbe2 (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
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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
/*
 *                     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 Emgu.CV;
using Emgu.CV.CvEnum;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;
using UVtools.Core.Layers;
using UVtools.Core.Objects;

namespace UVtools.Core.Operations;

[Serializable]
public sealed class OperationLayerImport : Operation
{
    #region Enums
    public enum ImportTypes : byte
    {
        [Description("Insert: Inserts the new imported layers")]
        Insert,
        [Description("Replace: Replaces layers with the imported layers")]
        Replace,
        [Description("Stack: Stacks and combine imported layers in the current layers")]
        Stack,
        [Description("MergeSum: Merges current layers with the imported content by summing value of layer pixels")]
        MergeSum,
        [Description("MergeMax: Merges current layers with the imported content by using the maximum value of layer pixels")]
        MergeMax,
        [Description("Subtract: Subtracts current layers with the imported content")]
        Subtract,
        [Description("AbsDiff: Absolute difference between current layers and the imported content")]
        AbsDiff,
        [Description("BitwiseAnd: Perform a 'bitwise AND' operation over layers and the imported pixels")]
        BitwiseAnd,
        [Description("BitwiseOr: Perform a 'bitwise OR' operation over layers and the imported pixels")]
        BitwiseOr,
        [Description("BitwiseXOr: Perform a 'bitwise XOR' operation over layers and the imported pixels")]
        BitwiseXOr,
    }
    #endregion

    #region Members
    private ImportTypes _importType = ImportTypes.Stack;
    private uint _startLayerIndex;
    private bool _extendBeyondLayerCount = true;
    private bool _discardUnmodifiedLayers;
    private ushort _stackMargin = 50;
    private RangeObservableCollection<GenericFileRepresentation> _files = new();
    #endregion

    #region Overrides

    public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.None;
    public override bool CanROI => false;

    public override string IconClass => "mdi-database-import";

    public override string Title => "Import layers";

    public override string Description =>
        "Import layers from local files into the model at a selected layer height.\n" +
        "NOTE: Imported images must be greyscale and have the same resolution as the model.";

    public override string ConfirmationText => $"{_importType} import {Count} file{(Count>=1?"s":"")}?";

    public override string ProgressTitle =>
        $"{_importType} importing {Count} file{(Count>=1 ? "s" : "")}";

    public override string ProgressAction => "Imported layers";

    public override bool CanCancel => true;

    public override uint LayerIndexEnd => _startLayerIndex + Count - 1;

    public override string? ValidateInternally()
    {
        /*var result = new ConcurrentBag<StringTag>();
        Parallel.ForEach(Files,  CoreSettings.ParallelOptions, 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);*/

        StringBuilder sb = new();

        if (_files.Count == 0)
        {
            sb.AppendLine("No files to import.");
        }
        else
        {
            foreach (var keyValue in _files)
            {
                if (!keyValue.Exists)
                {
                    sb.AppendLine($"The file '{keyValue.FilePath}' does not exists.");
                }
            }
        }

        return sb.ToString();
    }
    #endregion

    #region Properties

    public static string[] ValidImageExtensions => new[]
    {
        "png",
        "bmp",
        "jpeg",
        "jpg",
        "gif",
    };

    public ImportTypes ImportType
    {
        get => _importType;
        set
        {
            if(!RaiseAndSetIfChanged(ref _importType, value)) return;
            RaisePropertyChanged(nameof(IsImportStackType));
            RaisePropertyChanged(nameof(IsExtendBeyondLayerCountVisible));
        }
    }

        
    public static Array ImportTypesItems => Enum.GetValues(typeof(ImportTypes));

    public bool IsImportStackType => _importType == ImportTypes.Stack;

    public uint StartLayerIndex
    {
        get => _startLayerIndex;
        set => RaiseAndSetIfChanged(ref _startLayerIndex, value);
    }

    public bool ExtendBeyondLayerCount
    {
        get => _extendBeyondLayerCount;
        set => RaiseAndSetIfChanged(ref _extendBeyondLayerCount, value);
    }

    public bool IsExtendBeyondLayerCountVisible => _importType is ImportTypes.Replace or ImportTypes.Stack or ImportTypes.MergeSum or ImportTypes.MergeMax;

    public bool DiscardUnmodifiedLayers
    {
        get => _discardUnmodifiedLayers;
        set => RaiseAndSetIfChanged(ref _discardUnmodifiedLayers, value);
    }

    public ushort StackMargin
    {
        get => _stackMargin;
        set => RaiseAndSetIfChanged(ref _stackMargin, value);
    }

    public RangeObservableCollection<GenericFileRepresentation> Files
    {
        get => _files;
        set => RaiseAndSetIfChanged(ref _files, value);
    }

    public uint Count => (uint)_files.Count;
    #endregion

    #region Constructor

    public OperationLayerImport() { }

    public OperationLayerImport(FileFormat slicerFile) : base(slicerFile) { }


    #endregion

    #region Methods

    public void AddFile(string file)
    {
        _files.Add(new GenericFileRepresentation(file));
    }

    public void Sort()
    {
        _files.Sort();
    }
        

    /*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 = $"[{_importType}] [Start at: {_startLayerIndex}] [Files: {Count}]";
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        progress.ItemCount = 0;
        var result = SlicerFile.SuppressRebuildPropertiesWork(() => { 
            List<FileFormat> fileFormats = new();
            List<KeyValuePair<uint, string>> keyImage = new();
            int lastProcessedLayerIndex = -1;

            // Order raw images
            for (int i = 0; i < Count; i++)
            {
                if(!ValidImageExtensions.Any(extension => _files[i].IsExtension(extension))) continue;
                keyImage.Add(new KeyValuePair<uint, string>((uint)keyImage.Count, _files[i].FilePath));
            }

            // Create virtual file format with images
            if (keyImage.Count > 0)
            {
                progress.Reset("Packing images", (uint)keyImage.Count);
                SL1File format = new();
                format.Init((uint)keyImage.Count);

                Parallel.ForEach(keyImage, CoreSettings.GetParallelOptions(progress), pair =>
                {
                    using var mat = CvInvoke.Imread(pair.Value, ImreadModes.Grayscale);
                    if (pair.Key == 0) format.Resolution = mat.Size;
                    format[pair.Key] = new Layer(pair.Key, mat, format);

                    progress.LockAndIncrement();
                });

                fileFormats.Add(format);
            }

            // Order remaining possible file formats that are not images
            for (int i = 0; i < Count; i++)
            {
                if (ValidImageExtensions.Any(extension => _files[i].IsExtension(extension))) continue;

                var fileFormat = FileFormat.FindByExtensionOrFilePath(_files[i].FilePath, true);
                if (fileFormat is null) continue;
                fileFormat.FileFullPath = _files[i].FilePath;
                fileFormats.Add(fileFormat);
            }

            progress.ThrowIfCancellationRequested();

            if (fileFormats.Count == 0) return false;

            if (_importType == ImportTypes.Stack)
            {
                new OperationMove(SlicerFile, Anchor.TopLeft).Execute(progress);
            }

            foreach (var fileFormat in fileFormats)
            {
                if (!string.IsNullOrEmpty(fileFormat.FileFullPath))
                {
                    fileFormat.Decode(fileFormat.FileFullPath, progress);
                }

                var boundingRectangle = SlicerFile.GetBoundingRectangle(progress);
                var fileFormatBoundingRectangle = fileFormat.GetBoundingRectangle(progress);
                var roiRectangle = Rectangle.Empty;

                // Check if is possible to process this file
                switch (_importType)
                {
                    case ImportTypes.Insert:
                        if (SlicerFile.Resolution != fileFormat.Resolution &&
                            (SlicerFile.Resolution.Width < fileFormatBoundingRectangle.Width ||
                             SlicerFile.Resolution.Height < fileFormatBoundingRectangle.Height)) continue;
                        SlicerFile.ReallocateInsert(_startLayerIndex, fileFormat.LayerCount);
                        break;
                    case ImportTypes.Replace:
                    case ImportTypes.Stack:
                        if (SlicerFile.Resolution != fileFormat.Resolution &&
                            (SlicerFile.Resolution.Width < fileFormatBoundingRectangle.Width ||
                             SlicerFile.Resolution.Height < fileFormatBoundingRectangle.Height)) continue;


                        //if(fileFormatBoundingRectangle.Width >= SlicerFile.ResolutionX || fileFormatBoundingRectangle.Height >= SlicerFile.ResolutionY) 
                        //    continue;

                        if (_importType == ImportTypes.Stack)
                        {
                            int x = 0;
                            int y = 0;

                            if (boundingRectangle.Right + _stackMargin + fileFormatBoundingRectangle.Width <
                                SlicerFile.ResolutionX)
                            {
                                x = boundingRectangle.Right + _stackMargin;
                            }
                            else
                            {
                                y = boundingRectangle.Bottom + _stackMargin;
                            }

                            if (x + fileFormatBoundingRectangle.Width >= SlicerFile.ResolutionX)
                                continue;
                            if (y + fileFormatBoundingRectangle.Height >= SlicerFile.ResolutionY)
                                continue;

                            roiRectangle = new Rectangle(x, y, fileFormatBoundingRectangle.Width,
                                fileFormatBoundingRectangle.Height);
                        }

                        if (_extendBeyondLayerCount)
                        {
                            int layerCountDifference = (int)(_startLayerIndex + fileFormat.LayerCount - SlicerFile.LayerCount);
                            if (layerCountDifference > 0)
                            {
                                SlicerFile.ReallocateEnd((uint)layerCountDifference, _importType == ImportTypes.Stack);
                            }
                        }

                        break;
                    case ImportTypes.MergeSum:
                    case ImportTypes.MergeMax:
                        if (SlicerFile.Resolution != fileFormat.Resolution) continue;
                        if (_extendBeyondLayerCount)
                        {
                            int layerCountDifference = (int)(_startLayerIndex + fileFormat.LayerCount - SlicerFile.LayerCount);
                            if (layerCountDifference > 0)
                            {
                                SlicerFile.ReallocateEnd((uint)layerCountDifference, true);
                            }
                        }
                        break;
                    case ImportTypes.Subtract:
                    case ImportTypes.AbsDiff:
                    case ImportTypes.BitwiseAnd:
                    case ImportTypes.BitwiseOr:
                    case ImportTypes.BitwiseXOr:
                        if (SlicerFile.Resolution != fileFormat.Resolution) continue;
                        break;
                }

                progress.Reset(ProgressAction, fileFormat.LayerCount);
                Parallel.For(0, fileFormat.LayerCount, CoreSettings.GetParallelOptions(progress), i =>
                {
                    uint layerIndex = (uint)(_startLayerIndex + i);

                    switch (_importType)
                    {
                        case ImportTypes.Insert:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            if (SlicerFile.Resolution == fileFormat.Resolution)
                            {
                                SlicerFile[layerIndex] = fileFormat[i];
                                break;
                            }

                            using var layer = fileFormat[i].LayerMat;
                            using var layerRoi = layer.NewMatFromCenterRoi(SlicerFile.Resolution, fileFormatBoundingRectangle);
                            SlicerFile[layerIndex] = new Layer(layerIndex, layerRoi, SlicerFile);

                            break;
                        }
                        case ImportTypes.Replace:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            if (SlicerFile.Resolution == fileFormat.Resolution)
                            {
                                SlicerFile[layerIndex] = fileFormat[i];
                                break;
                            }

                            using var layer = fileFormat[i].LayerMat;
                            using var layerRoi = layer.NewMatFromCenterRoi(SlicerFile.Resolution, fileFormatBoundingRectangle);
                            SlicerFile[layerIndex] = new Layer(layerIndex, layerRoi, SlicerFile);
                            break;
                        }
                        case ImportTypes.Stack:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var mat = SlicerFile[layerIndex].LayerMat;
                            using var importMat = fileFormat[i].LayerMat;
                            var matRoi = new Mat(mat, roiRectangle);
                            var importMatRoi = new Mat(importMat, fileFormatBoundingRectangle);
                            importMatRoi.CopyTo(matRoi);
                            SlicerFile[layerIndex].LayerMat = mat;

                            break;
                        }
                        case ImportTypes.MergeSum:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.Add(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.MergeMax:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.Max(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.Subtract:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.Subtract(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.AbsDiff:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.AbsDiff(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.BitwiseAnd:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.BitwiseAnd(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.BitwiseOr:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.BitwiseOr(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        case ImportTypes.BitwiseXOr:
                        {
                            if (layerIndex >= SlicerFile.LayerCount) return;
                            using var originalMat = SlicerFile[layerIndex].LayerMat;
                            using var newMat = fileFormat[i].LayerMat;
                            CvInvoke.BitwiseXor(originalMat, newMat, newMat);
                            SlicerFile[layerIndex].LayerMat = newMat;
                            break;
                        }
                        default:
                            throw new ArgumentOutOfRangeException();
                    }


                    lock (progress.Mutex)
                    {
                        lastProcessedLayerIndex = Math.Max(lastProcessedLayerIndex, (int)layerIndex);
                        progress++;
                    }
                });

                fileFormat.Dispose();
            }

            if (_importType == ImportTypes.Stack)
            {
                new OperationMove(SlicerFile).Execute(progress);
            }

            if (lastProcessedLayerIndex <= -1) return false;

            if (lastProcessedLayerIndex + 1 < SlicerFile.LayerCount && _discardUnmodifiedLayers)
            {
                SlicerFile.Reallocate((uint)lastProcessedLayerIndex + 1);
            }

            return true;
        }, true);
            
        return !progress.Token.IsCancellationRequested && result;
    }

    #endregion
}