forked from CjangCjengh/AutoCGAligner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchWin.cs
411 lines (377 loc) · 14.9 KB
/
BatchWin.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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace AutoCGAligner
{
public partial class BatchWin : Form
{
public BatchWin()
{
InitializeComponent();
baseFullPath = new Dictionary<string, string>();
diffFullPath = new Dictionary<string, Dictionary<string, string>>();
baseToDiff = new Dictionary<string, ArrayList>();
}
public int initStep = 20;
public int stepDivisor = 10;
public int extScale = 2;
private readonly Dictionary<string, string> baseFullPath;
private readonly Dictionary<string, Dictionary<string, string>> diffFullPath;
private readonly Dictionary<string, ArrayList> baseToDiff;
private Bitmap baseImage;
private Bitmap diffImage;
private Color[,] baseColors;
private Bitmap resultImage;
private void ClearLabel_CheckedChanged(object sender, EventArgs e)
{
degreeLabel.Enabled = !degreeLabel.Enabled;
degreeValue.Enabled = !degreeValue.Enabled;
}
private void AlignAdvanced_Click(object sender, EventArgs e)
{
AdvancedWin aw = new AdvancedWin((initStep, stepDivisor, extScale) =>
{
this.initStep = initStep;
this.stepDivisor = stepDivisor;
this.extScale = extScale;
if (speedMode.Checked)
speedMode.Checked = false;
else if (precisionMode.Checked)
precisionMode.Checked = false;
});
aw.SetMaximum(50, 30);
aw.SetValue(initStep, stepDivisor, extScale);
aw.ShowDialog(this);
aw.Dispose();
}
private void OpenFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
if (fbd.ShowDialog(this) == DialogResult.OK)
savePath.Text = fbd.SelectedPath;
fbd.Dispose();
}
private bool CreateFolder(string path)
{
if (string.IsNullOrEmpty(path))
{
MessageBox.Show("请指定保存路径!", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
if (!Directory.Exists(path))
{
if (MessageBox.Show("当前文件夹不存在,是否新建文件夹?", "",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information) == DialogResult.OK)
{
string[] folders = path.Split('\\');
string fpath = "";
foreach (string folder in folders)
{
fpath += folder + "/";
if (Directory.Exists(fpath))
continue;
try
{
Directory.CreateDirectory(fpath);
}
catch
{
MessageBox.Show("路径名称不合法!", "",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
}
if (!Directory.Exists(path))
{
MessageBox.Show("新建文件夹失败!", "",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
return true;
}
return true;
}
private void SavePath_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == '\r')
CreateFolder(savePath.Text);
}
private void ConfirmButton_Click(object sender, EventArgs e)
{
if (!CreateFolder(savePath.Text))
return;
BatchAlignWin baw = new BatchAlignWin();
baw.baseBar.Maximum = baseFullPath.Count;
baw.baseProgress.Text = $"0 / {baw.baseBar.Maximum}";
Task t1 = Task.Run(() =>
{
foreach (string bpath in baseFullPath.Values)
{
string baseName = Path.GetFileName(bpath);
try
{
baseImage = new Bitmap(bpath);
baseColors = Program.BitmapToColors(baseImage);
}
catch
{
if (MessageBox.Show($"{baseName}读取失败,是否跳过该文件?", "",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
continue;
else
{
baw.Close();
return;
}
}
Invoke(new Action(() =>
{
baw.diffBar.Value = 0;
baw.diffBar.Maximum = diffFullPath[baseName].Count;
baw.diffProgress.Text = $"0 / {baw.diffBar.Maximum}";
}));
foreach (string dpath in diffFullPath[baseName].Values)
{
string diffName = Path.GetFileName(dpath);
try
{
diffImage = new Bitmap(dpath);
}
catch
{
if (MessageBox.Show($"{diffName}读取失败,是否跳过该文件?", "",
MessageBoxButtons.YesNo,
MessageBoxIcon.Warning) == DialogResult.Yes)
continue;
else
{
baw.Close();
return;
}
}
if (clearLabel.Checked)
{
Dictionary<Color, int> numOfColor = Program.CountColors(diffImage);
Color bgColor = numOfColor.Aggregate((a, b) => a.Value > b.Value ? a : b).Key;
double ratio = (double)degreeValue.Value / (double)degreeValue.Maximum;
Program.ClearColor(ref diffImage, bgColor,
(int)(ratio * ratio * Program.MaxPixelDis));
}
Color[,] border = Program.GetBorder(diffImage, out int npixels);
if (!Program.AlignImage(baseColors, border,
out int x, out int y, out long dis,
initStep, stepDivisor, extScale,
baw.alignBar, baw.cts, this, null))
return;
resultImage = new Bitmap(baseImage);
Graphics g = Graphics.FromImage(resultImage);
g.DrawImage(diffImage, x, y);
resultImage.Save(Path.Combine(savePath.Text,
Path.GetFileNameWithoutExtension(baseName) + "+" +
Path.GetFileNameWithoutExtension(diffName) + ".png"));
Invoke(new Action(() =>
{
baw.diffProgress.Text = $"{++baw.diffBar.Value} / {baw.diffBar.Maximum}";
}));
}
Invoke(new Action(() =>
{
baw.baseProgress.Text = $"{++baw.baseBar.Value} / {baw.baseBar.Maximum}";
}));
}
Invoke(new Action(() =>
{
baw.Close();
}));
MessageBox.Show("合成结束!", "",
MessageBoxButtons.OK, MessageBoxIcon.Information);
});
Task t2 = Task.Run(() =>
{
while (!baw.cts.IsCancellationRequested)
{
Thread.Sleep(100);
Invoke(new Action(() =>
{
baw.alignProgress.Text = ((double)baw.alignBar.Value /
baw.alignBar.Maximum * 100).ToString("f2") + "%";
}));
}
});
baw.ShowDialog(this);
baw.Dispose();
}
private void SpeedMode_CheckedChanged(object sender, EventArgs e)
{
if (speedMode.Checked)
{
initStep = 20;
stepDivisor = 10;
extScale = 2;
}
}
private void PrecisionMode_CheckedChanged(object sender, EventArgs e)
{
if (precisionMode.Checked)
{
initStep = 1;
stepDivisor = 3;
extScale = 1;
}
}
private void BaseFiles_SelectedIndexChanged(object sender, EventArgs e)
{
diffFiles.Items.Clear();
if (!string.IsNullOrEmpty(baseFiles.Text))
foreach (string file in baseToDiff[baseFiles.Text])
diffFiles.Items.Add(file);
}
private void BaseFiles_DoubleClick(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog
{
Multiselect = true,
Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.tif;*.tiff"
};
if (ofd.ShowDialog() == DialogResult.OK)
LoadFiles(ofd.FileNames, true);
ofd.Dispose();
}
private void DiffFiles_DoubleClick(object sender, EventArgs e)
{
if (!CheckBaseSelected())
return;
OpenFileDialog ofd = new OpenFileDialog
{
Multiselect = true,
Filter = "图片文件|*.png;*.bmp;*.jpg;*.jpeg;*.tif;*.tiff"
};
if (ofd.ShowDialog() == DialogResult.OK)
LoadFiles(ofd.FileNames, false);
ofd.Dispose();
}
private void FilesDragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}
private void BaseFiles_DragDrop(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
LoadFiles((string[])e.Data.GetData(DataFormats.FileDrop), true);
}
private void DiffFiles_DragDrop(object sender, DragEventArgs e)
{
if (!CheckBaseSelected())
return;
if (e.Data.GetDataPresent(DataFormats.FileDrop))
LoadFiles((string[])e.Data.GetData(DataFormats.FileDrop), false);
}
private void LoadFiles(string[] paths, bool isBaseFiles)
{
foreach (string path in paths)
if (File.Exists(path))
if (isBaseFiles)
LoadBaseFile(path);
else
LoadDiffFile(path);
else if (Directory.Exists(path))
if (MessageBox.Show($"是否读取文件夹{path}中的所有文件?", "",
MessageBoxButtons.OKCancel,
MessageBoxIcon.Information) == DialogResult.OK)
{
DirectoryInfo info = new DirectoryInfo(path);
foreach (FileInfo file in info.GetFiles())
if (isBaseFiles)
LoadBaseFile(file.FullName);
else
LoadDiffFile(file.FullName);
}
CheckFiles();
}
private void LoadBaseFile(string path)
{
string fileName = Path.GetFileName(path);
if (!baseFiles.Items.Contains(fileName))
{
baseFullPath.Add(fileName, path);
diffFullPath.Add(fileName, new Dictionary<string, string>());
baseToDiff.Add(fileName, new ArrayList());
baseFiles.Items.Add(fileName);
}
}
private void LoadDiffFile(string path)
{
string fileName = Path.GetFileName(path);
if (!diffFiles.Items.Contains(fileName))
{
string baseName = baseFiles.Text;
diffFullPath[baseName].Add(fileName, path);
baseToDiff[baseName].Add(fileName);
diffFiles.Items.Add(fileName);
}
}
private bool CheckBaseSelected()
{
if (string.IsNullOrEmpty(baseFiles.Text))
{
MessageBox.Show("请先选择对应的CG背景!", "",
MessageBoxButtons.OK, MessageBoxIcon.Warning);
return false;
}
return true;
}
private void BaseFiles_KeyPress(object sender, KeyPressEventArgs e)
{
string baseName = baseFiles.Text;
if (!string.IsNullOrEmpty(baseName) && e.KeyChar == '\b')
{
baseFullPath.Remove(baseName);
diffFullPath.Remove(baseName);
baseToDiff.Remove(baseName);
baseFiles.Items.Remove(baseName);
CheckFiles();
}
}
private void DiffFiles_KeyPress(object sender, KeyPressEventArgs e)
{
string diffName = diffFiles.Text;
if (!string.IsNullOrEmpty(diffName) && e.KeyChar == '\b')
{
string baseName = baseFiles.Text;
diffFullPath[baseName].Remove(diffName);
baseToDiff[baseName].Remove(diffName);
diffFiles.Items.Remove(diffName);
CheckFiles();
}
}
private void CheckFiles()
{
confirmButton.Enabled = false;
if (baseToDiff.Count > 0)
{
foreach (string baseFile in baseToDiff.Keys)
if (baseToDiff[baseFile].Count == 0)
return;
confirmButton.Enabled = true;
}
}
private void BatchWin_HelpButtonClicked(object sender, CancelEventArgs e)
{
e.Cancel = true;
MessageBox.Show("添加图片:双击白框或直接拖动\n" +
"移除图片:选中后按backspace", "帮助", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}