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

OperationLightBleedCompensation.cs « Operations « UVtools.Core - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d307a71c52ed82c37f5d98c407cfea9f8194b411 (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
/*
 *                     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.Structure;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UVtools.Core.Extensions;
using UVtools.Core.FileFormats;

namespace UVtools.Core.Operations;

[Serializable]
public class OperationLightBleedCompensation : Operation
{
    #region Enums

    public enum LightBleedCompensationLookupMode : byte
    {
        [Description("Previous: Look for sequential pixels relative to the previous layers")]
        Previous,

        [Description("Next: Look for sequential pixels relative to the next layers")]
        Next,

        [Description("Both: Look for sequential pixels relative to the previous and next layers")]
        Both
    }

    #endregion

    #region Members

    private LightBleedCompensationLookupMode _lookupMode = LightBleedCompensationLookupMode.Next;
    private string _dimBy = "25,15,10,5";

    #endregion

    #region Overrides

    public override LayerRangeSelection StartLayerRangeSelection => LayerRangeSelection.Normal;
    public override string IconClass => "mdi-lightbulb-on";
    public override string Title => "Light bleed compensation";
    public override string Description =>
        "Compensate the over-curing and light bleed from clear resins by dimming the sequential pixels.\n" +
        "Note: You need to find the optimal minimum pixel brightness that such resin can print in order to optimize this process.\n" +
        "With more translucent resins you can go with lower brightness but stick to a limit that can form the layer without loss." +
        " Tiny details can be lost when using low brightness level.\n" +
        "After apply a light bleed compensation, do not apply or re-run this tool over.";

    public override string ConfirmationText =>
        $"compensate layers {LayerIndexStart} through {LayerIndexEnd}?";

    public override string ProgressTitle =>
        $"Compensate layers {LayerIndexStart} through {LayerIndexEnd}";

    public override string ProgressAction => "Compensated layers";

    public override string? ValidateInternally()
    {
        StringBuilder sb = new();

        if (DimByArray.Length == 0)
        {
            sb.AppendLine($"The dim levels are invalid or not set.");
        }

        if (MaximumSubtraction >= byte.MaxValue)
        {
            sb.AppendLine($"The sum of dim levels are producing black pixels.");
        }

        return sb.ToString();
    }

    public override string ToString()
    {
        var result = $"[Lookup: {_lookupMode}]" +
                     $" [Dim by: {_dimBy}]" + LayerRangeString;
        if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
        return result;
    }
    #endregion

    #region Constructor

    public OperationLightBleedCompensation() { }

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

    #endregion

    #region Properties

    public LightBleedCompensationLookupMode LookupMode
    {
        get => _lookupMode;
        set => RaiseAndSetIfChanged(ref _lookupMode, value);
    }

    public string DimBy
    {
        get => _dimBy;
        set
        {
            if(!RaiseAndSetIfChanged(ref _dimBy, value)) return;
            RaisePropertyChanged(nameof(MinimumBrightness));
            RaisePropertyChanged(nameof(MaximumSubtraction));
        }
    }

    public int MinimumBrightness => 255 - MaximumSubtraction;
    public int MaximumSubtraction => DimByArray.Aggregate(0, (current, dim) => current + dim);

    public byte[] DimByArray
    {
        get
        {
            List<byte> levels = new();
            var split = _dimBy.Split(',', StringSplitOptions.TrimEntries);
            foreach (var str in split)
            {
                if (!byte.TryParse(str, out var brightness)) continue;
                if (brightness is byte.MinValue or byte.MaxValue) continue;
                levels.Add(brightness);
            }

            return levels.ToArray();
        }
    }

    public MCvScalar[] DimByMCvScalar
    {
        get
        {
            List<MCvScalar> levels = new();
            var split = _dimBy.Split(',', StringSplitOptions.TrimEntries);
            foreach (var str in split)
            {
                if (!byte.TryParse(str, out var brightness)) continue;
                if (brightness is byte.MinValue or byte.MaxValue) continue;
                levels.Add(new MCvScalar(brightness));
            }

            return levels.ToArray();
        }
    }

    #endregion

    #region Equality

    protected bool Equals(OperationLightBleedCompensation other)
    {
        return _lookupMode == other._lookupMode && _dimBy == other._dimBy;
    }

    public override bool Equals(object? obj)
    {
        if (ReferenceEquals(null, obj)) return false;
        if (ReferenceEquals(this, obj)) return true;
        if (obj.GetType() != this.GetType()) return false;
        return Equals((OperationLightBleedCompensation) obj);
    }

    public override int GetHashCode()
    {
        return HashCode.Combine((int) _lookupMode, _dimBy);
    }

    #endregion

    #region Methods

    /// <summary>
    /// Get the cached dim mat's
    /// </summary>
    /// <returns></returns>
    public Mat[] GetDimMats()
    {
        var dimLevels = DimByMCvScalar;
        if (dimLevels.Length == 0) return Array.Empty<Mat>();
        var mats = new Mat[dimLevels.Length];
        var matSize = GetRoiSizeOrDefault();
        for (var i = 0; i < mats.Length; i++)
        {
            mats[i] = EmguExtensions.InitMat(matSize, dimLevels[i]);
        }

        return mats;
    }

    protected override bool ExecuteInternally(OperationProgress progress)
    {
        var dimMats = GetDimMats();
        if (dimMats.Length == 0) return false;
        Parallel.For(LayerIndexStart, LayerIndexEnd + 1, CoreSettings.GetParallelOptions(progress), layerIndex =>
        {
            var layer = SlicerFile[layerIndex];
            using var mat = layer.LayerMat;    
            using var original = mat.Clone();    
            var target = GetRoiOrDefault(mat);

            for (byte i = 0; i < dimMats.Length; i++)
            {
                Mat? mask = null;
                Mat? previousMat = null;
                Mat? previousMatRoi = null;
                Mat? nextMat = null;
                Mat? nextMatRoi = null;


                if (_lookupMode is LightBleedCompensationLookupMode.Previous or LightBleedCompensationLookupMode.Both)
                {
                    int layerPreviousIndex = (int)layerIndex - i - 1;
                    if (layerPreviousIndex >= LayerIndexStart)
                    {
                        previousMat = SlicerFile[layerPreviousIndex].LayerMat;
                        mask = previousMatRoi = GetRoiOrDefault(previousMat);
                    }
                }
                if (_lookupMode is LightBleedCompensationLookupMode.Next or LightBleedCompensationLookupMode.Both)
                {
                    uint layerIndexNext = (uint) (layerIndex + i + 1);
                    if (layerIndexNext <= LayerIndexEnd)
                    {
                        nextMat = SlicerFile[layerIndexNext].LayerMat;
                        mask = nextMatRoi = GetRoiOrDefault(nextMat);
                    }
                }
                    
                if (previousMat is null && nextMat is null) break; // Nothing more to do
                if (previousMat is not null && nextMat is not null) // both, need to merge previous with next layer
                {
                    CvInvoke.Add(previousMatRoi, nextMatRoi, previousMatRoi);
                    mask = previousMatRoi;
                }

                CvInvoke.Subtract(target, dimMats[i], target, mask);

                previousMat?.Dispose();
                nextMat?.Dispose();
            }

            // Apply the results only to the selected masked area, if user selected one
            ApplyMask(original, target);

            // Set current layer image with the modified mat we just manipulated
            layer.LayerMat = mat;

            // Increment progress bar by 1
            progress.LockAndIncrement();
        });

        foreach (var dimMat in dimMats)
        {
            dimMat.Dispose();
        }

        // return true if not cancelled by user
        return !progress.Token.IsCancellationRequested;
    }

    #endregion
}