forked from Rob-Kachmar/real-time-filter-demo
/
MirrorEffect.cs
86 lines (79 loc) · 3.39 KB
/
MirrorEffect.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
// ============================================================================
// DATE AUTHOR DESCRIPTION
// ---------- ----------------------- ---------------------------------------
// 2014.01.15 Rob.Kachmar Initial creation
// ============================================================================
using Nokia.Graphics.Imaging;
using System;
namespace NISDKExtendedEffects.ImageEffects
{
public class MirrorEffect : CustomEffectBase
{
public enum MirrorType
{
Horizontal = 0,
Vertical = 1
}
MirrorType m_MirrorType = MirrorType.Horizontal;
public MirrorEffect(IImageProvider source, MirrorType mirrorType = MirrorType.Horizontal) : base(source)
{
m_MirrorType = mirrorType;
}
protected override void OnProcess(PixelRegion sourcePixelRegion, PixelRegion targetPixelRegion)
{
var sourcePixels = sourcePixelRegion.ImagePixels;
var targetPixels = targetPixelRegion.ImagePixels;
int rowIndex = 0;
sourcePixelRegion.ForEachRow((index, width, position) =>
{
// get the vertical midpoint >>> L = (P / W) >>> V = (L / 2)
int verticalMidPoint = (sourcePixels.Length / width) / 2;
// get the horizontal midpoint >>> M = (W / 2)
int horizontalMidPoint = width / 2;
if (m_MirrorType.Equals(MirrorType.Vertical))
{
for (int x = 0; x < width; ++x, ++index)
{
if (rowIndex < verticalMidPoint)
{
// Just keep the first half of the column as is
targetPixels[index] = sourcePixels[index];
}
else
{
// Now we start repeating the mirror image from the first half of the column
// index - (((i - V) * 2 * W) - 1)
int sourceIndex = index - ((rowIndex - verticalMidPoint) * 2 * width) - 1;
if (sourceIndex > 0)
{
targetPixels[index] = sourcePixels[sourceIndex];
}
}
}
}
else
{
for (int x = 0; x < width; ++x, ++index)
{
if (x < horizontalMidPoint)
{
// Just keep the first half of the row as is
targetPixels[index] = sourcePixels[index];
}
else
{
// Now we start repeating the mirror image from the first half of the row
// index - (((x - H) * 2) - 1)
int sourceIndex = index - ((x - horizontalMidPoint) * 2) - 1;
if (sourceIndex > 0)
{
targetPixels[index] = sourcePixels[sourceIndex];
}
}
}
}
rowIndex++;
});
}
}
}