forked from microsoft/simple-filter-mixer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Imaging.cs
316 lines (274 loc) · 12.1 KB
/
Imaging.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Threading.Tasks;
using Windows.Foundation;
using Windows.Storage;
using Windows.UI;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
using Nokia.Graphics.Imaging;
using simple_filter_mixer.DataModel;
namespace simple_filter_mixer
{
public class Imaging
{
public static readonly string ImagingTypeStringStub = "Nokia.Graphics.Imaging.{0}, Nokia.Graphics.Imaging, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime";
public static readonly string ImagingLibraryNamespace = "Nokia.Graphics.Imaging";
private const string DebugTag = "Imaging: ";
public EventHandler<bool> IsRenderingChanged;
private bool _rendering; // Do not start rendering if we haven't finished previous rendering yet
/// <summary>
/// Load mask image
/// </summary>
/// <returns></returns>
public static async Task<StorageFileImageSource> InitializeFile()
{
const string imageFile = @"Assets\mask.jpg";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
return new StorageFileImageSource(file);
}
/// <summary>
/// Using reflection we create dynamically at runtime all the chosen filters and
/// apply the property values if the user has chosen any
/// </summary>
/// <param name="filters"></param>
public static void GetFilters(List<IFilter> filters)
{
if (App.ChosenFilters == null)
{
return;
}
// Get the name of the filter
foreach (FilterItem selectedFilterItem in App.ChosenFilters)
{
if (selectedFilterItem == null)
{
continue;
}
if (selectedFilterItem.Filter == null)
{
CreateFilter(selectedFilterItem);
}
else if (selectedFilterItem.Parameters != null)
{
string temp = "";
SetFilterParameters(selectedFilterItem.Filter, selectedFilterItem.Parameters, out temp);
}
filters.Add(selectedFilterItem.Filter);
}
}
/// <summary>
/// Constructs the IFilter instance of the given FilterItem based on
/// its properties. Note: No sanity checks!
/// </summary>
/// <param name="filterItem"></param>
public static void CreateFilter(FilterItem filterItem)
{
filterItem.Filter = CreateFilter(filterItem.Name, filterItem.Constructor, filterItem.Parameters);
}
/// <summary>
/// Returns a new filter instance, which is created using reflection
/// dynamically at runtime.
/// </summary>
/// <param name="filterClassName">Filter class name.</param>
/// <param name="constructorArguments"></param>
/// <param name="filterParameters">Filter parameters. Can be null.</param>
/// <returns>A new filter instance or null in case of a failure.</returns>
public static IFilter CreateFilter(string filterClassName, object[] constructorArguments, Dictionary<string, object> filterParameters = null)
{
if (filterClassName == null || filterClassName.Length == 0)
{
Debug.WriteLine(DebugTag + "CreateFilter(): Invalid class name!");
return null;
}
// Format the fully qualified name of the class
var type = string.Format(ImagingTypeStringStub, filterClassName);
// Use reflection to create the filter class
var filterType = Type.GetType(type);
if (filterType == null)
{
Debug.WriteLine(DebugTag + "CreateFilter(): Failed to get the filter type!");
return null;
}
var filter = (IFilter)Activator.CreateInstance(filterType, constructorArguments);
// Apply changed parameter values if any
if (filterParameters != null)
{
string temp = "";
SetFilterParameters(filter, filterParameters, out temp);
}
return filter;
}
/// <summary>
/// For convenience.
/// </summary>
/// <param name="filterItem"></param>
/// <param name="errorMessage"></param>
/// <returns></returns>
public static bool SetFilterParameters(FilterItem filterItem, out string errorMessage)
{
return SetFilterParameters(filterItem.Filter, filterItem.Parameters, out errorMessage);
}
/// <summary>
/// Sets the given parameter values for the given filter.
/// </summary>
/// <param name="filter"></param>
/// <param name="filterParameters"></param>
/// <param name="errorMessage"></param>
/// <returns>True if successful, false otherwise.</returns>
public static bool SetFilterParameters(IFilter filter,
Dictionary<string, object> filterParameters,
out string errorMessage)
{
errorMessage = "";
if (filter == null || filterParameters == null || filterParameters.Count == 0)
{
Debug.WriteLine(DebugTag + "SetFilterParameters(): No filter or parameters given!");
errorMessage = "Invalid arguments: No filter or parameters given!";
return false;
}
bool success = true;
PropertyInfo propertyInfo = null;
var filterType = filter.GetType();
string propertyTypeName = "";
string propertyTypeNameToLower = "";
try
{
foreach (var parameter in filterParameters)
{
propertyInfo = filterType.GetRuntimeProperty(parameter.Key);
if (propertyInfo == null || propertyInfo.Name != parameter.Key)
{
continue;
}
propertyTypeName = propertyInfo.PropertyType.ToString();
Debug.WriteLine(DebugTag + "SetFilterParameters(): Setting property: " + parameter.Key + " (" + propertyTypeName + ") == " + parameter.Value);
propertyTypeNameToLower = propertyTypeName.ToLower();
switch (propertyTypeNameToLower)
{
case "system.double":
propertyInfo.SetValue(filter, Convert.ToDouble(parameter.Value));
break;
case "system.string":
propertyInfo.SetValue(filter, parameter.Value.ToString());
break;
case "system.boolean":
propertyInfo.SetValue(filter, parameter.Value);
break;
case "system.int32":
propertyInfo.SetValue(filter, Convert.ToInt32(parameter.Value));
break;
case "windows.foundation.rect":
propertyInfo.SetValue(filter, (Windows.Foundation.Rect)parameter.Value);
break;
case "windows.ui.color":
propertyInfo.SetValue(filter, (Windows.UI.Color)parameter.Value);
break;
case "windows.foundation.point":
propertyInfo.SetValue(filter, (Windows.Foundation.Point)parameter.Value);
break;
default:
if (propertyTypeName.StartsWith(ImagingLibraryNamespace))
{
// The value is probably Imaging SDK specific enumeration
try
{
propertyInfo.SetValue(filter, Convert.ToInt32(parameter.Value));
}
catch (Exception e)
{
success = false;
errorMessage = "Failed to set value " + parameter.Value
+ " for type " + propertyTypeName + ": " + e.ToString();
}
}
else
{
success = false;
errorMessage = "No implementation for handling type " + propertyTypeName + ".";
}
break;
}
}
}
catch (Exception e)
{
success = false;
errorMessage = "Setting property value: " + propertyInfo.Name
+ " (" + propertyTypeName + ") failed with message: " + e.Message;
}
if (!success)
{
Debug.WriteLine(DebugTag + "SetFilterParameters(): " + errorMessage);
}
return success;
}
/// <summary>
/// Apply the chosen filter(s)
/// </summary>
/// <param name="sampleEffect"></param>
public async Task<WriteableBitmap> ApplyBasicFilter(List<IFilter> sampleEffect)
{
if (App.ChosenPhoto == null || _rendering)
{
return null;
}
_rendering = true;
if (IsRenderingChanged != null)
{
IsRenderingChanged(this, true);
}
var props = await App.ChosenPhoto.Properties.GetImagePropertiesAsync();
var target = new WriteableBitmap((int)props.Width, (int)props.Height);
try
{
// Create a source to read the image from PhotoResult stream
using (var source = new StorageFileImageSource(App.ChosenPhoto))
using (var filters = new FilterEffect(source))
{
filters.Filters = sampleEffect.ToArray();
// Create a new renderer which outputs WriteableBitmaps
using (var renderer = new WriteableBitmapRenderer(filters, target))
{
// Render the image with the filter
await renderer.RenderAsync();
}
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
_rendering = false;
if (IsRenderingChanged != null)
{
IsRenderingChanged(this, false);
}
return target;
}
/// <summary>
/// Render a photo without any filters
/// </summary>
/// <param name="imageControl"></param>
/// <returns></returns>
public async Task RenderPlainPhoto(Image imageControl)
{
using (var source = new StorageFileImageSource(App.ChosenPhoto))
{
var props = await App.ChosenPhoto.Properties.GetImagePropertiesAsync();
// Create a target where the filtered image will be rendered to
var target = new WriteableBitmap((int)props.Width, (int)props.Height);
// Create a new renderer which outputs WriteableBitmaps
using (var renderer = new WriteableBitmapRenderer(source, target))
{
// Render the image with the filter(s)
await renderer.RenderAsync();
// Set the output image to Image control as a source
imageControl.Source = target;
}
}
}
}
}