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

KernelControl.axaml.cs « Controls « UVtools.WPF - github.com/sn4k3/UVtools.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e5248011f6df8f0f4fbb38013a65cbd821263307 (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
using System;
using System.Collections.Generic;
using Avalonia.Markup.Xaml;
using Emgu.CV;
using Emgu.CV.CvEnum;
using UVtools.Core.Extensions;
using UVtools.Core.Objects;
using UVtools.WPF.Extensions;
using Point = System.Drawing.Point;
using Size = System.Drawing.Size;

namespace UVtools.WPF.Controls
{
    public class KernelControl : UserControlEx
    {

        private ElementShape _selectedKernelShape = ElementShape.Rectangle;
        private uint _matrixWidth = 3;
        private uint _matrixHeight = 3;
        private int _anchorX = -1;
        private int _anchorY = -1;
        private string _matrixText;
        public List<ElementShape> KernelShapes { get; } = new();

        public string MatrixText
        {
            get => _matrixText;
            set => RaiseAndSetIfChanged(ref _matrixText, value);
        }

        public ElementShape SelectedKernelShape
        {
            get => _selectedKernelShape;
            set => RaiseAndSetIfChanged(ref _selectedKernelShape, value);
        }

        public uint MatrixWidth
        {
            get => _matrixWidth;
            set => RaiseAndSetIfChanged(ref _matrixWidth, value);
        }

        public uint MatrixHeight
        {
            get => _matrixHeight;
            set => RaiseAndSetIfChanged(ref _matrixHeight, value);
        }

        public Size MatrixSize => new((int)_matrixWidth, (int)_matrixHeight);

        public int AnchorX
        {
            get => _anchorX;
            set => RaiseAndSetIfChanged(ref _anchorX, value);
        }

        public int AnchorY
        {
            get => _anchorY;
            set => RaiseAndSetIfChanged(ref _anchorY, value);
        }

        
        public Point Anchor => new(_anchorX, _anchorY);


        public KernelControl()
        {
            InitializeComponent();
            foreach (var element in (ElementShape[])Enum.GetValues(
                typeof(ElementShape)))
            {
                if (element == ElementShape.Custom) continue;
                KernelShapes.Add(element);
            }
            ResetKernel();
            DataContext = this;
        }

        private void InitializeComponent()
        {
            AvaloniaXamlLoader.Load(this);
        }

        public void GenerateKernel()
        {
            if (MatrixWidth <= AnchorX || MatrixHeight <= AnchorY)
            {
                App.MainWindow.MessageBoxError("Anchor position X/Y can't be higher or equal than size X/Y\nPlease fix the values.", "Invalid anchor position");
                return;
            }

            using var kernel = CvInvoke.GetStructuringElement(SelectedKernelShape, MatrixSize, Anchor);
            string text = string.Empty;
            for (int y = 0; y < kernel.Height; y++)
            {
                var span = kernel.GetRowSpan<byte>(y);
                var line = string.Empty;
                for (int x = 0; x < span.Length; x++)
                {
                    line += $" {span[x]}";
                }

                line = line.Remove(0, 1);
                text += line;
                if (y < kernel.Height - 1)
                {
                    text += '\n';
                }
            }

            MatrixText = text;
        }

        public void ResetKernel()
        {
            SelectedKernelShape = ElementShape.Rectangle;
            MatrixWidth = 3;
            MatrixHeight = 3;
            AnchorX = -1;
            AnchorY = -1;
            GenerateKernel();
        }

        public Matrix<byte> GetMatrix()
        {
            if (string.IsNullOrEmpty(MatrixText))
            {
                App.MainWindow.MessageBoxError("Kernel can't be empty.", "Empty Kernel");
                return null;
            }
            Matrix<byte> matrix = null;
            var lines = MatrixText.Split('\n');
            for (var row = 0; row < lines.Length; row++)
            {
                var bytes = lines[row].Trim().Split(' ');
                if (row == 0)
                {
                    matrix = new Matrix<byte>(lines.Length, bytes.Length);
                }
                else
                {
                    if (matrix.Cols != bytes.Length)
                    {
                        App.MainWindow.MessageBoxError($"Row {row + 1} have invalid number of columns, the matrix must have equal columns count per line, per defined on line 1", "Invalid kernel");
                        matrix.Dispose();
                        return null;
                    }
                }

                for (int col = 0; col < bytes.Length; col++)
                {
                    if (byte.TryParse(bytes[col], out var value))
                    {
                        matrix[row, col] = value;
                    }
                    else
                    {
                        App.MainWindow.MessageBoxError($"{bytes[col]} is a invalid number, use values from 0 to 255");
                        matrix.Dispose();
                        return null;
                    }
                }
            }
            if (matrix.Cols <= AnchorX || matrix.Rows <= AnchorY)
            {
                App.MainWindow.MessageBoxError("Anchor position X/Y can't be higher or equal than kernel columns/rows\nPlease fix the values.", "Invalid anchor position");
                matrix.Dispose();
                return null;
            }
            return matrix;
        }

        public Kernel GetKernel()
        {
            var matrix = GetMatrix();
            return matrix is null ? null : new Kernel(matrix, Anchor);
        }
    }
}