forked from PedroQ/PictureQuiz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageProcessor.cs
144 lines (126 loc) · 5.22 KB
/
ImageProcessor.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
using Nokia.Graphics.Imaging;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Media.Imaging;
using Nokia.Graphics;
using Nokia.InteropServices.WindowsRuntime;
using System.Windows;
namespace PictureQuiz
{
public class ImageProcessor
{
public enum Difficulty
{
Easy = 1,
Moderate,
Hard,
VeryHard,
ReallyHard
}
// Filter groups for each difficulty
private FilterGroup _easyFilterGroup;
private FilterGroup _moderateFilterGroup;
private FilterGroup _hardFilterGroup;
private FilterGroup _veryHardFilterGroup;
private FilterGroup _impossibleFilterGroup;
// Masks to be used with the ImageFusion Filter
private Bitmap _mask1;
private Bitmap _mask2;
private Bitmap _mask3;
private Bitmap _maskBackground;
public ImageProcessor()
{
//Initialize the filter groups for each difficulty level
SetupDifficultyFilterGroups();
}
private void SetupDifficultyFilterGroups()
{
// Alpha Masks
_mask1 = LoadBitmap("mask1.jpg");
_mask2 = LoadBitmap("mask2.jpg");
_mask3 = LoadBitmap("mask3.jpg");
// Alpha mask background
_maskBackground = LoadBitmap("maskBg.jpg");
// Easy Difficulty: Antique filter and 23 degree rotation
_easyFilterGroup = new FilterGroup(new IFilter[]
{
FilterFactory.CreateAntiqueFilter(),
FilterFactory.CreateFreeRotationFilter(23f, RotationResizeMode.FitInside)
});
// Moderate Difficulty: Milky and Blur filters and alpha mask 1
_moderateFilterGroup = new FilterGroup(new IFilter[]
{
FilterFactory.CreateMilkyFilter(),
FilterFactory.CreateBlurFilter(BlurLevel.Blur3),
FilterFactory.CreateImageFusionFilter(_maskBackground, _mask1, false)
});
// Hard Difficulty: Warp, Magic Pen and Gray Scale Negative filters
_hardFilterGroup = new FilterGroup(new IFilter[]
{
FilterFactory.CreateWarpFilter(WarpEffect.SmallNose, 0.8f),
FilterFactory.CreateMagicPenFilter(),
FilterFactory.CreateGrayscaleNegativeFilter()
});
// Very Hard Difficulty: Cartoon filter and apply alpha mask 2
_veryHardFilterGroup = new FilterGroup(new IFilter[]
{
FilterFactory.CreateCartoonFilter(true),
FilterFactory.CreateImageFusionFilter(_maskBackground, _mask2, false)
});
// Impossible Difficulty: 45 degree rotation, watercolor filer and alpha mask 3
_impossibleFilterGroup = new FilterGroup(new IFilter[]
{
FilterFactory.CreateFreeRotationFilter(45f, RotationResizeMode.FitInside),
FilterFactory.CreateWatercolorFilter(0.8f, 1),
FilterFactory.CreateImageFusionFilter(_maskBackground, _mask3, false)
});
}
private static Bitmap LoadBitmap(string fileName)
{
Stream maskStream = Application.GetResourceStream(new Uri("Assets/Masks/" + fileName, UriKind.Relative)).Stream;
BitmapImage maskImage = new BitmapImage();
maskImage.SetSource(maskStream);
return new WriteableBitmap(maskImage).AsBitmap();
}
public async Task ApplyDifficultyToImage(Stream image, Difficulty level, Image resultImage)
{
try
{
using (EditingSession _session = await EditingSessionFactory.CreateEditingSessionAsync(image))
{
switch (level)
{
case Difficulty.Easy:
_session.AddFilter(_easyFilterGroup);
break;
case Difficulty.Moderate:
_session.AddFilter(_moderateFilterGroup);
break;
case Difficulty.Hard:
_session.AddFilter(_hardFilterGroup);
break;
case Difficulty.VeryHard:
_session.AddFilter(_veryHardFilterGroup);
break;
case Difficulty.ReallyHard:
_session.AddFilter(_impossibleFilterGroup);
break;
default:
break;
}
//Render the image to the Image control in the Quiz page
await _session.RenderToImageAsync(resultImage);
}
}
catch (Exception e)
{
MessageBox.Show("An error occurred while processing the image: " + e.Message);
}
}
}
}