-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMainWindow.xaml.cs
77 lines (70 loc) · 2.76 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
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
namespace perspectivePlayground
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
private WriteableBitmapWrapper _bitmap;
private WriteableBitmapWrapper _sourceImage;
public MainWindow()
{
InitializeComponent();
}
private Float2 GetThumbPosition(Thumb thumb)
{
var x = Canvas.GetLeft(thumb);
var y = Canvas.GetTop(thumb);
return new Float2((float)x, (float)y);
}
private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
_sourceImage = new WriteableBitmapWrapper(@"..\..\Sydney_New_Year's_Eve_2.jpg");
SetThumbPosition(ThumbTopLeft, new Float2(0, 0));
SetThumbPosition(ThumbTopRight, new Float2(_sourceImage.Width, 0));
SetThumbPosition(ThumbBottomLeft, new Float2(0, _sourceImage.Height));
SetThumbPosition(ThumbBottomRight, new Float2(_sourceImage.Width, _sourceImage.Height));
_bitmap = new WriteableBitmapWrapper(_sourceImage.Width, _sourceImage.Height);
Image.Source = _bitmap.Bitmap;
}
private void SetThumbPosition(Thumb thumb, Float2 pos)
{
Canvas.SetLeft(thumb, pos.X);
Canvas.SetTop(thumb, pos.Y);
}
private void ThumbTopLeft_OnDragDelta(object sender, DragDeltaEventArgs e)
{
var thumb = (Thumb)sender;
Canvas.SetLeft(thumb, Canvas.GetLeft(thumb) + e.HorizontalChange);
Canvas.SetTop(thumb, Canvas.GetTop(thumb) + e.VerticalChange);
Update();
}
private void Update()
{
var t1 = GetThumbPosition(ThumbTopLeft);
var t2 = GetThumbPosition(ThumbTopRight);
var t3 = GetThumbPosition(ThumbBottomLeft);
var t4 = GetThumbPosition(ThumbBottomRight);
var transform = Float3x3.Perspective(0, 0, _sourceImage.Width, _sourceImage.Height, t1, t2, t3, t4);
transform = transform.Invert();
for (var i = 0; i < 1; i++)
{
using (_bitmap.Edit())
{
for (var x = 0; x < _sourceImage.Width; x++)
{
for (var y = 0; y < _sourceImage.Height; y++)
{
var tileCoord = transform.TransformPoint(new Float2(x, y));
var c = _sourceImage.GetPixelOrDefault(tileCoord);
_bitmap.SetPixel(x, y, c);
}
}
}
}
}
}
}