-
Notifications
You must be signed in to change notification settings - Fork 1
/
MainWindow.xaml.cs
165 lines (149 loc) · 5.05 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
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;
namespace imgdiff
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
int tolerance = 0;
string leftFile = null;
string rightFile = null;
public MainWindow()
{
InitializeComponent();
int tol;
if (int.TryParse(Environment.GetEnvironmentVariable("IMAGE_DIFF_DEFAULT_TOLERANCE"), out tol))
{
this.tolerance = Math.Max(0, Math.Min(255, tol));
}
this.Loaded += (s, e) => { Startup(); };
}
public void ErrorLogLine(string message)
{
this.imageDiff.Visibility = Visibility.Collapsed;
this.logText.Visibility = Visibility.Visible;
this.logText.Text = this.logText.Text + message + "\r\n";
}
void Startup()
{
if (!ParseCommandLine(Environment.GetCommandLineArgs()))
{
return;
}
// Load image files
var leftTask = Task.Factory.StartNew<BitmapImage>(() => OpenImageFile(this.leftFile));
var rightTask = Task.Factory.StartNew<BitmapImage>(() => OpenImageFile(this.rightFile));
leftTask.Wait();
rightTask.Wait();
var leftImage = leftTask.Result;
var rightImage = rightTask.Result;
if (null == leftImage) return;
if (null == rightImage) return;
this.imageDiff.SetImages(leftImage, rightImage, this.tolerance);
}
BitmapImage OpenImageFile(string path)
{
if (!File.Exists(path))
{
ErrorLogLine(string.Format("File not found: {0}", path));
return null;
}
try
{
var image = new BitmapImage();
image.BeginInit();
image.CacheOption = BitmapCacheOption.OnLoad;
image.UriSource = new Uri(Path.GetFullPath(path));
image.EndInit();
image.Freeze(); // freeze it. So it can be passes safely across threads.
return image;
}
catch(Exception ex)
{
ErrorLogLine(string.Format("Fail to open file {0}: {1}", path, ex.ToString()));
return null;
}
}
bool ParseCommandLine(string[] args)
{
if (args.Length < 2)
{
ShowUsage();
return false;
}
for (int i = 1; i < args.Length; ++i)
{
var a = args[i];
if (a[0] == '-' || a[0] == '/')
{
string o = a.Substring(1).ToLower();
if ("h" == o || "?" == o)
{
ShowUsage();
return false;
}
else if ("t" == o)
{
if ((i+1) < args.Length)
{
++i;
if (!int.TryParse(args[i], out this.tolerance))
{
ErrorLogLine("Tolerance value must be an integer.");
return false;
}
}
else
{
ErrorLogLine("Missing tolerance number.");
return false;
}
}
}
else if (string.IsNullOrEmpty(this.leftFile))
{
this.leftFile = a;
}
else if (string.IsNullOrEmpty(this.rightFile))
{
this.rightFile = a;
}
}
if (string.IsNullOrEmpty(leftFile))
{
ErrorLogLine("Left file is not defined.");
return false;
}
if (string.IsNullOrEmpty(rightFile))
{
ErrorLogLine("Right file is not defined.");
return false;
}
return true;
}
void ShowUsage()
{
string usage = @"
Usage: [options] <left> <right>
Options:
-t [0-255] Set image diff tolerance. Default is 0, or whatever defined
by environment variable IMAGE_DIFF_DEFAULT_TOLERANCE.
";
ErrorLogLine(usage);
}
}
}