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

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

namespace UVtools.Core.Operations
{
    [Serializable]
    public class OperationPixelArithmetic : Operation
    {
        #region Members
        private PixelArithmeticOperators _operator = PixelArithmeticOperators.Set;
        private byte _value = byte.MaxValue;
        private ThresholdType _thresholdType = ThresholdType.Binary;
        private byte _thresholdMaxValue = 255;
        private bool _affectBackPixels;

        #endregion

        #region Enums
        public enum PixelArithmeticOperators : byte
        {
            [Description("Set: to a brightness")]
            Set,
            [Description("Add: with a brightness")]
            Add,
            [Description("Subtract: with a brightness")]
            Subtract,
            [Description("Multiply: with a brightness")]
            Multiply,
            [Description("Divide: with a brightness")]
            Divide,
            //[Description("Exponential: pixels by a brightness")]
            //Exponential,
            [Description("Minimum: set to a brightness if is lower than the current pixel")]
            Minimum,
            [Description("Maximum: set to a brightness if is higher than the current pixel")]
            Maximum,
            [Description("Bitwise Not: invert pixels")]
            BitwiseNot,
            [Description("Bitwise And: with a brightness")]
            BitwiseAnd,
            [Description("Bitwise Or: with a brightness")]
            BitwiseOr,
            [Description("Bitwise Xor: with a brightness")]
            BitwiseXor,
            [Description("AbsDiff: perform a absolute difference between pixel and brightness")]
            AbsDiff,
            [Description("Threshold: between a minimum/maximum brightness")]
            Threshold,
            [Description("Keep Region: in the selected ROI or masks")]
            KeepRegion,
            [Description("Discard Region: in the selected ROI or masks")]
            DiscardRegion
        }
        #endregion

        #region Overrides
        public override string Title => "Pixel arithmetic";

        public override string Description =>
            "Perform arithmetic operations over the pixels.";

        public override string ConfirmationText =>
            $"arithmetic {_operator}" +
            (ValueEnabled ? $"={_value}" : string.Empty) +
            (_operator is PixelArithmeticOperators.Threshold ? $"/{_thresholdMaxValue}" : string.Empty)
            + $" layers from {LayerIndexStart} through {LayerIndexEnd}";

        public override string ProgressTitle =>
            $"Arithmetic {_operator}"+
            (ValueEnabled ? $"={_value}" : string.Empty)
            +$" layers from {LayerIndexStart} through {LayerIndexEnd}";

        public override string ProgressAction => "Calculated layers";

        public override string ValidateInternally()
        {
            var sb = new StringBuilder();
            if (_operator == PixelArithmeticOperators.KeepRegion && !HaveROI && !HaveMask)
            {
                sb.AppendLine("The 'Keep' operator requires selected ROI/masks.");
            }
            else if (_operator == PixelArithmeticOperators.DiscardRegion && !HaveROI && !HaveMask)
            {
                sb.AppendLine("The 'Discard' operator requires selected ROI/masks.");
            }
            else if (_operator 
                is PixelArithmeticOperators.Add 
                or PixelArithmeticOperators.Subtract
                or PixelArithmeticOperators.Maximum
                or PixelArithmeticOperators.BitwiseOr
                or PixelArithmeticOperators.BitwiseXor
                or PixelArithmeticOperators.AbsDiff
                     && _value == 0) 
                /*||
                     (_operator is PixelArithmeticOperators.Exponential && _value == 1)
                     )*/
            {
                sb.AppendLine($"{_operator} by {_value} will have no effect.");
            }
            else if (_operator == PixelArithmeticOperators.Divide && _value == 0)
            {
                sb.AppendLine("Can't divide by 0.");
            }

            return sb.ToString();
        }

        public override string ToString()
        {
            var result = $"[{_operator}: {_value}] [ABP: {_affectBackPixels}]"
                         + LayerRangeString;
            if (!string.IsNullOrEmpty(ProfileName)) result = $"{ProfileName}: {result}";
            return result;
        }
        #endregion

        #region Properties

        public PixelArithmeticOperators Operator
        {
            get => _operator;
            set
            {
                if(!RaiseAndSetIfChanged(ref _operator, value)) return;
                RaisePropertyChanged(nameof(ValueEnabled));
                RaisePropertyChanged(nameof(ThresholdEnabled));
                RaisePropertyChanged(nameof(AffectBackPixelsEnabled));
            }
        }

        public byte Value
        {
            get => _value;
            set
            {
                if(!RaiseAndSetIfChanged(ref _value, value)) return;
                RaisePropertyChanged(nameof(ValuePercent));
            }
        }

        // 255  - 100
        //value -  x
        public float ValuePercent => (float) Math.Round(_value * 100f / byte.MaxValue, 2);

        public bool ValueEnabled => _operator
            is not PixelArithmeticOperators.BitwiseNot
            and not PixelArithmeticOperators.KeepRegion
            and not PixelArithmeticOperators.DiscardRegion
            ;

        public ThresholdType ThresholdType
        {
            get => _thresholdType;
            set => RaiseAndSetIfChanged(ref _thresholdType, value);
        }

        public byte ThresholdMaxValue
        {
            get => _thresholdMaxValue;
            set => RaiseAndSetIfChanged(ref _thresholdMaxValue, value);
        }

        public bool ThresholdEnabled => _operator is PixelArithmeticOperators.Threshold;

        public bool AffectBackPixels
        {
            get => _affectBackPixels;
            set => RaiseAndSetIfChanged(ref _affectBackPixels, value);
        }

        public bool AffectBackPixelsEnabled => _operator
            is not PixelArithmeticOperators.Subtract
            and not PixelArithmeticOperators.Multiply
            and not PixelArithmeticOperators.Divide
            and not PixelArithmeticOperators.BitwiseNot
            and not PixelArithmeticOperators.BitwiseAnd
            and not PixelArithmeticOperators.KeepRegion
            and not PixelArithmeticOperators.DiscardRegion
            and not PixelArithmeticOperators.Threshold
            ;

        #endregion

        #region Constructor

        public OperationPixelArithmetic() { }

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

        #endregion

        #region Methods

        protected override bool ExecuteInternally(OperationProgress progress)
        {
            var tempMat = GetTempMat();

            Parallel.For(LayerIndexStart, LayerIndexEnd + 1, layerIndex =>
            {
                if (progress.Token.IsCancellationRequested) return;
                using (var mat = SlicerFile[layerIndex].LayerMat)
                {
                    Execute(mat, tempMat);
                    SlicerFile[layerIndex].LayerMat = mat;
                }

                progress.LockAndIncrement();
            });

            tempMat?.Dispose();

            return !progress.Token.IsCancellationRequested;
        }

        public override bool Execute(Mat mat, params object[] arguments)
        {
            using var original = mat.Clone();
            var target = GetRoiOrDefault(mat);

            Mat tempMat;
            bool needDispose = false;
            if (arguments is not null && arguments.Length > 0)
            {
                tempMat = arguments[0] as Mat;
            }
            else
            {
                tempMat = GetTempMat();
                needDispose = true;
            }

            switch (_operator)
            {
                case PixelArithmeticOperators.Set:
                    tempMat.CopyTo(target, _affectBackPixels ? null : target);
                    break;
                case PixelArithmeticOperators.Add:
                    CvInvoke.Add(target, tempMat, target, _affectBackPixels ? null : target);
                    break;
                case PixelArithmeticOperators.Subtract:
                    CvInvoke.Subtract(target, tempMat, target, _affectBackPixels ? null : target);
                    break;
                case PixelArithmeticOperators.Multiply:
                    CvInvoke.Multiply(target, tempMat, target);
                    break;
                case PixelArithmeticOperators.Divide:
                    CvInvoke.Divide(target, tempMat, target);
                    break;
                /*case PixelArithmeticOperators.Exponential:
                    CvInvoke.Pow(target, _value, tempMat);
                    if(!_affectBackPixels) ApplyMask(original, mat, original);
                    break;*/
                case PixelArithmeticOperators.Minimum:
                    CvInvoke.Min(target, tempMat, target);
                    if (!_affectBackPixels) ApplyMask(original, target, original);
                    break;
                case PixelArithmeticOperators.Maximum:
                    CvInvoke.Max(target, tempMat, target);
                    if (!_affectBackPixels) ApplyMask(original, target, original);
                    break;
                case PixelArithmeticOperators.BitwiseNot:
                    CvInvoke.BitwiseNot(target, target);
                    break;
                case PixelArithmeticOperators.BitwiseAnd:
                    CvInvoke.BitwiseAnd(target, tempMat, target);
                    break;
                case PixelArithmeticOperators.BitwiseOr:
                    CvInvoke.BitwiseOr(target, tempMat, target, _affectBackPixels ? null : target);
                    break;
                case PixelArithmeticOperators.BitwiseXor:
                    CvInvoke.BitwiseXor(target, tempMat, target, _affectBackPixels ? null : target);
                    break;
                case PixelArithmeticOperators.Threshold:
                    CvInvoke.Threshold(target, target, _value, _thresholdMaxValue, _thresholdType);
                    break;
                case PixelArithmeticOperators.AbsDiff:
                    CvInvoke.AbsDiff(target, tempMat, target);
                    if (!_affectBackPixels) ApplyMask(original, target, original);
                    break;
                case PixelArithmeticOperators.KeepRegion:
                {
                    using var targetClone = target.Clone();
                    original.SetTo(EmguExtensions.BlackByte);
                    mat.SetTo(EmguExtensions.BlackByte);
                    targetClone.CopyTo(target);
                    break;
                }
                case PixelArithmeticOperators.DiscardRegion:
                    target.SetTo(EmguExtensions.BlackByte);
                    break;
                default:
                    throw new NotImplementedException();
            }

            ApplyMask(original, target);

            if (needDispose)
            {
                tempMat?.Dispose();
            }

            return true;
        }

        public Mat GetTempMat() => _operator
                is not PixelArithmeticOperators.BitwiseNot
                and not PixelArithmeticOperators.KeepRegion
                and not PixelArithmeticOperators.DiscardRegion ? EmguExtensions.InitMat(HaveROI ? ROI.Size : SlicerFile.Resolution, new MCvScalar(_value)) : null;

        public void PresetStripAntiAliasing()
        {
            Operator = PixelArithmeticOperators.Threshold;
            Value = 127;
            ThresholdMaxValue = 255;
            ThresholdType = ThresholdType.Binary;
        }

        public void PresetHalfBrightness()
        {
            Value = 128;
        }

        #endregion

        #region Equality

        protected bool Equals(OperationPixelArithmetic other)
        {
            return _operator == other._operator && _value == other._value && _thresholdType == other._thresholdType && _thresholdMaxValue == other._thresholdMaxValue && _affectBackPixels == other._affectBackPixels;
        }

        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((OperationPixelArithmetic) obj);
        }

        public override int GetHashCode()
        {
            return HashCode.Combine((int) _operator, _value, (int) _thresholdType, _thresholdMaxValue, _affectBackPixels);
        }

        #endregion
    }
}