forked from ProgramistycznySwir/GFX-05-Histograms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
143 lines (129 loc) · 4.51 KB
/
MainWindow.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
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.Json;
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 static GFX_05_Histograms.Prelude;
namespace GFX_05_Histograms;
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
MainWindowVM _model;
public MainWindow()
{
InitializeComponent();
base.DataContext = _model = new();
_model.Effects = GFX_05_Histograms.Effect.Effects.Keys.Select(name => new EffectItemVM(name)).ToList();
EffectList.ItemsSource = _model.Effects;
EffectList.UpdateLayout();
}
private void LoadImage_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new(){
CheckFileExists = true,
Title = "Load Image",
};
if (dialog.ShowDialog() == true)
{
_model.Original = new Bitmap(dialog.FileName);
image.Source = BitmapToImageSource(_model.Original);
}
}
BitmapImage BitmapToImageSource(Bitmap bitmap)
{
using (MemoryStream memory = new MemoryStream())
{
bitmap.Save(memory, System.Drawing.Imaging.ImageFormat.Bmp);
memory.Position = 0;
BitmapImage bitmapimage = new BitmapImage();
bitmapimage.BeginInit();
bitmapimage.StreamSource = memory;
bitmapimage.CacheOption = BitmapCacheOption.OnLoad;
bitmapimage.EndInit();
return bitmapimage;
}
}
private void LoadPipeline_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog dialog = new() {
CheckFileExists = true,
Filter = "Pipeline|*.json",
Title = "Load pipeline settings",
};
if (dialog.ShowDialog() == true)
{
_model.Effects = JsonSerializer.Deserialize<List<Effect>>(
File.ReadAllText(dialog.FileName),
new JsonSerializerOptions { AllowTrailingCommas = true, WriteIndented = true }
)!
.Select(e => new EffectItemVM(e))
.ToList();
EffectList.ItemsSource = _model.Effects;
EffectList.UpdateLayout();
}
}
private void SavePipeline_Click(object sender, RoutedEventArgs e)
{
SaveFileDialog dialog = new() {
Filter = "Pipeline|*.json",
Title = "Save pipeline settings",
};
if (dialog.ShowDialog() == true)
{
File.WriteAllText(dialog.FileName,
JsonSerializer.Serialize<List<Effect>>(
_model.Effects.Select(e => e.EffectData).ToList(),
new JsonSerializerOptions { AllowTrailingCommas = true, WriteIndented= true}
));
}
}
public void ApplyPipeline()
{
Bitmap bmp = (Bitmap)_model.Original.Clone();
System.Drawing.Rectangle rect_ = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height);
BitmapData data = bmp.LockBits(rect_, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
int lenght = data.Width * bmp.Height * 3;
byte[] bytes = new byte[lenght];
Marshal.Copy(data.Scan0, bytes, 0, lenght);
foreach(EffectItemVM eff in _model.Effects)
{
string name = eff.EffectData.Name;
if (GFX_05_Histograms.Effect.Effects.ContainsKey(name) is false)
continue;
if (eff.EffectData.IsActive is false)
continue;
var (_, func) = GFX_05_Histograms.Effect.Effects.GetValueOrDefault(name)!;
func(bytes, data.Width, eff.EffectData.Value);
}
Marshal.Copy(bytes, 0, data.Scan0, lenght);
image.Source = BitmapToImageSource(bmp);
}
private void Apply_Click(object sender, RoutedEventArgs e)
{
if (_model.Original is null)
return;
ApplyPipeline();
}
}
public class MainWindowVM
{
public Bitmap Original { get; set; }
public List<EffectItemVM> Effects { get; set; } = new();
}