forked from ProgramistycznySwir/GFX-05-Histograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEffectItem.xaml.cs
369 lines (315 loc) · 14.8 KB
/
EffectItem.xaml.cs
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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using GFX_05_Histograms.Effects;
using static GFX_05_Histograms.Prelude;
namespace GFX_05_Histograms;
/// <summary>
/// Interaction logic for EffectItem.xaml
/// </summary>
public partial class EffectItem : UserControl
{
EffectItemVM _model;
public EffectItem()
{
InitializeComponent();
//base.DataContext = _model;
}
}
public class EffectItemVM : INotifyPropertyChanged
{
public EffectItemVM(string name)
{
if(Effect.Effects.ContainsKey(name) is false)
{
EffectData.Name = "INVALID EFFECT";
return;
}
EffectData.Name = name;
(Type, _) = Effect.Effects.GetValueOrDefault(EffectData.Name)!;
//PropertyChanged(this, new PropertyChangedEventArgs(nameof(TextBox_IsVisible)));
//PropertyChanged(this, new PropertyChangedEventArgs(nameof(Slider_IsVisible)));
}
public EffectItemVM(Effect eff)
{
if (Effect.Effects.ContainsKey(eff.Name) is false)
{
EffectData.Name = "INVALID EFFECT";
return;
}
EffectData = eff;
(Type, _) = Effect.Effects.GetValueOrDefault(EffectData.Name)!;
//PropertyChanged(this, new PropertyChangedEventArgs(nameof(TextBox_IsVisible)));
//PropertyChanged(this, new PropertyChangedEventArgs(nameof(Slider_IsVisible)));
}
public Effect EffectData { get; set; } = new();
public Effect.Type Type { get; set; }
public Visibility TextBox_IsVisible => Type is not Effect.Type.Switch ? Visibility.Visible : Visibility.Collapsed;
public Visibility Slider_IsVisible => Type is Effect.Type.Percent ? Visibility.Visible : Visibility.Collapsed;
public event PropertyChangedEventHandler PropertyChanged;
}
public class Effect
{
public string Name { get; set; }
public bool IsActive { get; set; } = false;
public float Value { get; set; } = 1f;
public enum Type { Switch, Value, Percent }
/// <summary>
/// All effects work by mautating input byte-array, creating copy at the beginning of pipeline is advised.<br/>
/// Intensity should be in range 0-1.
/// </summary>
public static Dictionary<string, (Type, Action<byte[], int, float>)> Effects { get; } = new()
{
{ "Add_R", (Type.Percent, (bmp, _, value) => Add(bmp, ChaR, invLerp(value))) },
{ "Add_G", (Type.Percent, (bmp, _, value) => Add(bmp, ChaG, invLerp(value))) },
{ "Add_B", (Type.Percent, (bmp, _, value) => Add(bmp, ChaB, invLerp(value))) },
{ "Sub_R", (Type.Percent, (bmp, _, value) => Add(bmp, ChaR, - invLerp(value))) },
{ "Sub_G", (Type.Percent, (bmp, _, value) => Add(bmp, ChaG, - invLerp(value))) },
{ "Sub_B", (Type.Percent, (bmp, _, value) => Add(bmp, ChaB, - invLerp(value))) },
{ "Mul_R", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaR, value)) },
{ "Mul_G", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaG, value)) },
{ "Mul_B", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaB, value)) },
{ "Div_R", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaR, 1/value)) },
{ "Div_G", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaG, 1/value)) },
{ "Div_B", (Type.Percent, (bmp, _, value) => Mul(bmp, ChaB, 1/value)) },
{ "Lum", (Type.Value, (bmp, _, value) => Lum(bmp, value)) },
{ "Gray_R", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.R)) },
{ "Gray_G", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.G)) },
{ "Gray_B", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.B)) },
{ "Gray_RG", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.RG)) },
{ "Gray_RB", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.RB)) },
{ "Gray_GB", (Type.Switch, (bmp, _, _) => Gray_Cha(bmp, Cha.GB)) },
{ "Gray_RGB", (Type.Switch, (bmp, _, _) => GrayScale(bmp)) },
{ "Filter_Avg", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_Avg)) },
{ "Filter_Avg2", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_Avg2)) },
{ "Filter_Median", (Type.Switch, (bmp, width, _) => Filter_Median(bmp, width, Mask_Avg)) },
{ "Filter_Sobel", (Type.Switch, (bmp, width, _) => Filter_Sobel(bmp, width, Mask_Sobel_X, Mask_Sobel_Y)) },
{ "Filter_Sobel_Grayscale", (Type.Switch, (bmp, width, _) => Filter_Sobel(bmp, width, Mask_Sobel_X, Mask_Sobel_Y, true)) },
{ "Filter_HighPass1", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_HighPass1)) },
{ "Filter_HighPass2", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_HighPass2)) },
{ "Filter_HighPass3", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_HighPass3)) },
{ "Filter_GaussBlur3", (Type.Percent, (bmp, width, value) => GaussianBlurEffect.Apply(bmp, width, GaussianBlurEffect.GenerateMatrix(3, value))) },
{ "Filter_GaussBlur5", (Type.Percent, (bmp, width, value) => GaussianBlurEffect.Apply(bmp, width, GaussianBlurEffect.GenerateMatrix(5, value))) },
{ "Filter_GaussBlur7", (Type.Percent, (bmp, width, value) => GaussianBlurEffect.Apply(bmp, width, GaussianBlurEffect.GenerateMatrix(7, value))) },
{ "Filter_GaussBlur9", (Type.Percent, (bmp, width, value) => GaussianBlurEffect.Apply(bmp, width, GaussianBlurEffect.GenerateMatrix(9, value))) },
// Below filter is for my fun ONLY.
{ "Funky_Looooooooong", (Type.Switch, (bmp, width, _) => Filter_Mask(bmp, width, Mask_Funky_Long)) },
{ "Hist_Expand", (Type.Switch, (bmp, _, _) => Hist_Expand.Apply(bmp)) },
{ "Hist_Equalize", (Type.Switch, (bmp, _, _) => Hist_Equalize.Apply(bmp)) },
{ "Hist_Bin_Threshold", (Type.Percent, (bmp, _, value) => Hist_Binarize_Threshold.Apply(bmp, value)) },
{ "Hist_Bin_Mean", (Type.Switch, (bmp, _, _) => Hist_Binarize_Mean.Apply(bmp)) },
{ "Hist_Bin_Median", (Type.Switch, (bmp, _, _) => Hist_Binarize_Median.Apply(bmp)) },
{ "Hist_Bin_MinError", (Type.Switch, (bmp, _, _) => Hist_Binarize_MinError.Apply(bmp)) },
{ "Hist_Otsu", (Type.Switch, (bmp, _, _) => Hist_Otsu.Apply(bmp)) },
{ "Hist_Savuola", (Type.Switch, (bmp, width, _) => Hist_Savuola.Apply(bmp, width)) },
{ "Hist_Niblack", (Type.Switch, (bmp, width, _) => Hist_Niblack.Apply(bmp, width)) },
{ "Hist_Bernsen", (Type.Switch, (bmp, width, _) => Hist_Bernsen.Apply(bmp, width)) }
};
#region >>> Point Transforms <<<
public static void Add(Span<byte> bmp, int channel, int amount)
{
for (int i = channel; i < bmp.Length; i += 3)
bmp[i] = clampByte(bmp[i] + amount);
}
public static void Mul(Span<byte> bmp, int channel, float amount)
{
for (int i = channel; i < bmp.Length; i += 3)
bmp[i] = clampByte((int)(bmp[i] * amount));
}
public static void Lum(Span<byte> bmp, float amount)
{
Add(bmp, ChaR, invLerp(amount));
Add(bmp, ChaG, invLerp(amount));
Add(bmp, ChaB, invLerp(amount));
}
public static void Gray_Cha(Span<byte> bmp, Cha cha)
{
for (int i = 0; i < bmp.Length; i += 3)
{
int sum =
(((cha & Cha.R) is not 0) ? bmp[R(i)] : 0)
+ (((cha & Cha.G) is not 0) ? bmp[G(i)] : 0)
+ (((cha & Cha.B) is not 0) ? bmp[B(i)] : 0);
int chaCount =
(((cha & Cha.R) is not 0) ? 1 : 0)
+ (((cha & Cha.G) is not 0) ? 1 : 0)
+ (((cha & Cha.B) is not 0) ? 1 : 0);
byte avg = (byte)(sum / chaCount);
bmp[R(i)] = avg;
bmp[G(i)] = avg;
bmp[B(i)] = avg;
}
}
#endregion >>> Point Transforms <<<
public static readonly (int X, int Y, int Ratio)[] Mask_Avg = {
(-1, -1, 1), ( 0, -1, 1), ( 1, -1, 1),
(-1, 0, 1), ( 0, 0, 1), ( 1, 0, 1),
(-1, 1, 1), ( 0, 1, 1), ( 1, 1, 1),
};
public static readonly (int X, int Y, int Ratio)[] Mask_Avg2 = {
(-1, -1, 1), ( 0, -1, 2), ( 1, -1, 1),
(-1, 0, 2), ( 0, 0, 4), ( 1, 0, 2),
(-1, 1, 1), ( 0, 1, 2), ( 1, 1, 1),
};
public static void Filter_Mask(Span<byte> bmp, int width, (int X, int Y, int Ratio)[] mask)
{
Span<byte> result = new byte[bmp.Length];
bmp.CopyTo(result);
int height = (bmp.Length / 3) / width;
Func<int, int, int> xy = (x, y) => (x + y * width) * 3;
// Loop over all pixels:
for (int x = 0; x < width; x++)
for(int y = 0; y < height; y++)
// Loop over channels:
for(int cha = 0; cha < 3; cha++)
{
int sum = 0, sumRatio = 0;
// Loop over mask pixels:
for(int i = 0; i < mask.Length; i++)
{
var(maskX, maskY) = (x + mask[i].X, y + mask[i].Y);
if (maskX < 0 || maskX >= width || maskY < 0 || maskY >= height)
continue;
sum += bmp[xy(maskX, maskY) + cha] * mask[i].Ratio;
sumRatio += mask[i].Ratio;
}
result[xy(x, y) + cha] = clampByte(sum / sumRatio);
}
result.CopyTo(bmp);
}
public static void Filter_Median(Span<byte> bmp, int width, (int X, int Y, int)[] selector)
{
Span<byte> result = new byte[bmp.Length];
bmp.CopyTo(result);
int height = (bmp.Length / 3) / width;
Func<int, int, int> xy = (x, y) => (x + y * width) * 3;
Span<byte> buffer = stackalloc byte[selector.Length];
// Loop over all pixels:
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
// Loop over channels:
for (int cha = 0; cha < 3; cha++)
{
int buffer_idx = 0;
// Loop over mask pixels:
for (int i = 0; i < selector.Length; i++)
{
var (maskX, maskY) = (x + selector[i].X, y + selector[i].Y);
if (maskX < 0 || maskX >= width || maskY < 0 || maskY >= height)
continue;
buffer[buffer_idx++] = bmp[xy(maskX, maskY) + cha];
}
result[xy(x, y) + cha] = Median(buffer.Slice(0, buffer_idx));
}
result.CopyTo(bmp);
}
private static byte Median(Span<byte> arr)
{
arr.Sort();
int half = arr.Length / 2;
return
arr.Length % 2 == 0
? clampByte((arr[half - 1] + arr[half]) / 2)
: arr[half];
}
public static readonly (int X, int Y, int Ratio)[] Mask_Sobel_X = {
(-1, -1, -1), ( 0, -1, -2), ( 1, -1, -1),
(-1, 0, 0), ( 0, 0, 0), ( 1, 0, 0),
(-1, 1, 1), ( 0, 1, 2), ( 1, 1, 1),
};
public static readonly (int X, int Y, int Ratio)[] Mask_Sobel_Y = {
(-1, -1, -1), ( 0, -1, 0), ( 1, -1, 1),
(-1, 0, -2), ( 0, 0, 0), ( 1, 0, 2),
(-1, 1, -1), ( 0, 1, 0), ( 1, 1, 1),
};
public static void GrayScale(Span<byte> bmp)
{
for (int i = 0; i < bmp.Length; i += 3)
{
byte gray = (byte)(bmp[i] * .21f + bmp[i + 1] * .71f + bmp[i + 2] * .071f);
bmp[i] = bmp[i + 1] = bmp[i + 2] = gray;
}
}
public static void Filter_Sobel(Span<byte> bmp, int width,
(int X, int Y, int Ratio)[] maskX, (int X, int Y, int Ratio)[] maskY,
bool grayscale = false)
{
if (grayscale)
GrayScale(bmp);
Span<byte> result = new byte[bmp.Length];
bmp.CopyTo(result);
int height = (bmp.Length / 3) / width;
Func<int, int, int> xy = (x, y) => (x + y * width) * 3;
// Loop over all pixels:
for (int x = 0; x < width; x++)
for (int y = 0; y < height; y++)
// Loop over channels:
for (int cha = 0; cha < 3; cha++)
{
float valueX = 0;
// Loop over mask pixels:
for (int i = 0; i < maskX.Length; i++)
{
var (maskX_x, maskX_y) = (x + maskX[i].X, y + maskX[i].Y);
// Check if position is in image boundaries.
if (maskX_x < 0 || maskX_x >= width || maskX_y < 0 || maskX_y >= height)
continue;
valueX += bmp[xy(maskX_x, maskX_y) + cha] * maskX[i].Ratio;
}
float valueY = 0;
// Loop over mask pixels:
for (int i = 0; i < maskY.Length; i++)
{
var (maskY_x, maskY_y) = (x + maskY[i].X, y + maskY[i].Y);
// Check if position is in image boundaries.
if (maskY_x < 0 || maskY_x >= width || maskY_y < 0 || maskY_y >= height)
continue;
valueY += bmp[xy(maskY_x, maskY_y) + cha] * maskY[i].Ratio;
}
float value_ = MathF.Sqrt((valueX * valueX) + (valueY * valueY));
result[xy(x, y) + cha] = clampByte((int)value_);
}
result.CopyTo(bmp);
}
public static readonly (int X, int Y, int Ratio)[] Mask_HighPass1 = {
(-1, -1, -1), ( 0, -1, -1), ( 1, -1, -1),
(-1, 0, -1), ( 0, 0, 9), ( 1, 0, -1),
(-1, 1, -1), ( 0, 1, -1), ( 1, 1, -1),
};
public static readonly (int X, int Y, int Ratio)[] Mask_HighPass2 = {
(-1, -1, 1), ( 0, -1, -2), ( 1, -1, 1),
(-1, 0, -2), ( 0, 0, 5), ( 1, 0, -2),
(-1, 1, 1), ( 0, 1, -2), ( 1, 1, 1),
};
public static readonly (int X, int Y, int Ratio)[] Mask_HighPass3 = {
(-1, -1, 0), ( 0, -1, -1), ( 1, -1, 0),
(-1, 0, -1), ( 0, 0, 5), ( 1, 0, -1),
(-1, 1, 0), ( 0, 1, -1), ( 1, 1, 0),
};
public static readonly (int X, int Y, int Ratio)[] Mask_Funky_Long = {
( 0, 0, 1),
( 1, 0, 1),
( 2, 0, 1),
( 3, 0, 1),
( 4, 0, 1),
( 5, 0, 1),
( 6, 0, 1),
( 7, 0, 1),
( 8, 0, 1),
( 9, 0, 1),
(10, 0, 1),
(11, 0, 1),
};
}