-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMainForm.cs
207 lines (170 loc) · 7.08 KB
/
MainForm.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Threading.Tasks;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.IO;
using ImageQuantization;
using System.Drawing.Imaging;
using System.Windows.Threading;
namespace ImageQuantization
{
public partial class MainForm : Form
{
public static Dictionary<string, string> requiredK; //makes testing easier
public static string currImageName;
public static Stopwatch stopWatch;
void populateRequiredK()
{
string samplePath = Paths.testcasesPath + @"Sample\Sample Test\";
string completePath = Paths.testcasesPath + @"Complete\Complete Test\";
requiredK = new Dictionary<string, string>
{
////Sample
{ samplePath + @"Case1\Sample.Case1.bmp", "3" },
{ samplePath + @"Case2\Sample.Case2.bmp", "2" },
{ samplePath + @"Case3\Sample.Case3.bmp", "500" },
{ samplePath + @"Case4\Sample.Case4.bmp", "10" },
{ samplePath + @"Case5\Sample.Case5.bmp", "32" },
//Complete
//Small
{ completePath + @"Small\Small.Case1.bmp", "192" },
{ completePath + @"Small\Small.Case2.bmp", "2160" },
////Meduim
//{ completePath + @"Medium\Medium.Case1.bmp", "1737" },
//{ completePath + @"Medium\Medium.Case2.bmp", "2284" },
////Large
//{ completePath + @"Large\Large.Case1.bmp", "3829" },
//{ completePath + @"Large\Large.Case2.bmp", "25666" },
};
}
public MainForm()
{
populateRequiredK();
InitializeComponent();
progressBar1.Visible = false;
Config.VerifyOutputDirExistEmpty();
}
private async Task AutoTest()
{
File.WriteAllText(Paths.outputPath + "Test_Results.txt", Config.dashes);
foreach (var path in requiredK.Keys)
{
OriginalImageMatrix = ImageOperations.OpenImage(path);
ImageOperations.DisplayImage(OriginalImageMatrix, pictureBox1);
txtWidth.Text = ImageOperations.GetWidth(OriginalImageMatrix).ToString();
txtHeight.Text = ImageOperations.GetHeight(OriginalImageMatrix).ToString();
currImageName = path;
currImageName = currImageName.Substring(currImageName.LastIndexOf("\\") + 1);
txtClusters.Text = requiredK[path];
await RunColorQuantize();
}
File.AppendAllText(Paths.outputPath + "Test_Results.txt", Config.dashes + Environment.NewLine);
}
RGBPixel[,] OriginalImageMatrix;
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
//Open the browsed image and display it
string OpenedFilePath = openFileDialog1.FileName;
currImageName = OpenedFilePath;
currImageName = currImageName.Substring(currImageName.LastIndexOf("\\"));
OriginalImageMatrix = ImageOperations.OpenImage(OpenedFilePath);
ImageOperations.DisplayImage(OriginalImageMatrix, pictureBox1);
if (requiredK.ContainsKey(OpenedFilePath)) //makes testing easier
{
txtClusters.Text = requiredK[OpenedFilePath];
}
else
{
txtClusters.Text = "";
}
}
txtWidth.Text = ImageOperations.GetWidth(OriginalImageMatrix).ToString();
txtHeight.Text = ImageOperations.GetHeight(OriginalImageMatrix).ToString();
}
private async void btnQuantize_Click(object sender, EventArgs e)
{
await RunColorQuantize();
}
private async Task RunColorQuantize()
{
ColorQuantization.Reset();
int clusters = int.Parse(txtClusters.Text);
RGBPixel[,] QuantizedImageMatrix = (RGBPixel[,])OriginalImageMatrix.Clone();
stopWatch = new Stopwatch();
// Get the elapsed time as a TimeSpan value.
SetAllInputEnabled(false);
quantizedLabel.Hide();
progressBar1.Show();
stopWatch.Start();
await Task.Run(() =>
{
ColorQuantization.ColorQuantize(QuantizedImageMatrix, clusters);
});
stopWatch.Stop();
progressBar1.Hide();
quantizedLabel.Show();
SetAllInputEnabled(true);
TimeSpan ts = stopWatch.Elapsed;
distinctColorsTextBox.Text = ColorQuantization.distinctColorsList.Count.ToString();
mst_sum_text_box.Text = String.Format("{0:0.00}", MST.sum);
String tsString = ts.ToString();
tsString = tsString.Substring(tsString.IndexOf(':') + 1, 8);
totalTimeTextBox.Text = tsString;
numOfClustersTextBox.Text = ColorQuantization.k.ToString();
string testCaseResult = Config.GetTestCaseResultString(currImageName, distinctColorsTextBox.Text, mst_sum_text_box.Text, totalTimeTextBox.Text, numOfClustersTextBox.Text);
File.AppendAllText(Paths.outputPath + "Test_Results.txt", testCaseResult + Environment.NewLine);
if (pictureBox2.Image != null)
{
pictureBox2.Image = null;
pictureBox2.Update();
if (!Config.AUTOTEST)
MessageBox.Show("Cleared!");
}
ImageOperations.DisplayImage(QuantizedImageMatrix, pictureBox2);
if (Config.AUTOTEST)
pictureBox2.Image.Save(Paths.outputPath + "Result_" + currImageName, ImageFormat.Bmp);
if (!Config.AUTOTEST)
MessageBox.Show(ts.ToString());
}
private void SetAllInputEnabled(bool enabled)
{
btnQuantize.Enabled = enabled;
btnOpen.Enabled = enabled;
txtClusters.Enabled = enabled;
autoTestButton.Enabled = enabled;
}
private void label3_Click(object sender, EventArgs e)
{
}
private void label3_Click_1(object sender, EventArgs e)
{
}
private void label3_Click_2(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void distinctColorsTextBox_TextChanged(object sender, EventArgs e)
{
}
private void totalTimeTextBox_TextChanged(object sender, EventArgs e)
{
}
private async void autoTestButton_Click(object sender, EventArgs e)
{
Config.AUTOTEST = true;
await AutoTest();
Config.AUTOTEST = false;
}
}
}