-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
544 lines (467 loc) · 18.7 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
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Navigation;
using System.Windows.Shapes;
using tools;
namespace photoCuter
{
using Brushes = System.Windows.Media.Brushes;
/// <summary>
/// Logika interakcji dla klasy MainWindow.xaml
/// </summary>
///
using OperationsOnImageList = List<tools.OperationOnImage>;
public partial class MainWindow : Window
{
string[] files;
string openedFile;
OpenedImageObject openedImageObject;
byte quantityOfOperationOnImage = 3;
CutRectangle CutRectangleWithCorner;
MadeOperationsList madeOperationsList;
Stopwatch sw1 = new Stopwatch();
double stopWatchTimeInSec(Stopwatch sw)
{
return Math.Round((double)sw1.ElapsedTicks / (double)Stopwatch.Frequency, 3);
}
Bitmap tempBitmap;
bool timeOfOperationsDebug = false;
public byte thread = 4;
byte previewResolution = 4;
MenuItem[] threadingOptions;
MenuItem[] previewResolutionOptions;
public MainWindow()
{
InitializeComponent();
threadingOptions = new MenuItem[]{
MT1,MT2,MT4,MT6,MT8, MT16
};
previewResolutionOptions = new MenuItem[]
{
PS1,PS2,PS4
};
}
void restartParameters()
{
if (openedImageObject != null)
{
//brightness
brightnessSlider.Value = 0;
settingBrightnessTextBox.Text = 0.ToString();
//contrast
contrastSlider.Value = 0;
settingConstrastTextBox.Text = 0.ToString();
//cutRectangle
CutRectangleWithCorner.putRectangle();
}
}
private void brightnessSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
settingBrightnessTextBox.Text = Math.Round(brightnessSlider.Value, 1).ToString();
}
private void contrastSlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
settingConstrastTextBox.Text = Math.Round(contrastSlider.Value, 1).ToString();
}
void setSliderWithTextBox(float min, float max, TextBox textBox, Slider slider)
{
float value;
if (float.TryParse(textBox.Text, out value))
if (value >= min && value <= max)
slider.Value = value;
else
MessageBox.Show("Out of the range");
else
MessageBox.Show("Wrong format");
}
private Bitmap setContrast(float cont, Bitmap bitmap)
{
if (cont == 0)
{
cont = 1;
}
else
{
if (cont < 0)
{
cont = 1.0f + cont * 0.1f;
}
else
{
cont = cont + 1;
}
}
if (madeOperationsList.TempListLength >= quantityOfOperationOnImage - 1)
{
madeOperationsList.ClearTempOperations();
}
madeOperationsList.AddTempOpearation(new OperationOnImage
{
OperationType = OperationOnImage.Contrast,
Value = cont,
});
return tools.Contrast.setContrast(bitmap, cont, thread);
}
void confirmSetContrast()
{
if (openedImageObject != null)
{
sw1.Restart();
sw1.Start();
Bitmap bitmap = setBrightness((float)brightnessSlider.Value, openedImageObject.OpenedBitmap);
sw1.Stop();
double time = stopWatchTimeInSec(sw1);
sw1.Restart();
sw1.Start();
bitmap = setContrast((float)contrastSlider.Value, bitmap);
sw1.Stop();
if (timeOfOperationsDebug)
MessageBox.Show(stopWatchTimeInSec(sw1).ToString(), "Contrast");
statusBarLabel.Content = "Contrast: " + stopWatchTimeInSec(sw1)+"s";
sw1.Start();
tempBitmap = bitmap;
photoCanvas.Background = OpenedImageObject.convertToBrush(OpenedImageObject.convertToBitmapImage(bitmap));
sw1.Stop();
statusBarLabel.Content += "\t total: "+(stopWatchTimeInSec(sw1)+time).ToString()+"s";
}
}
private void settingConstrastTextBox_ClickEnter(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
setSliderWithTextBox(-10, 10, settingConstrastTextBox, contrastSlider);
confirmSetContrast();
}
}
private Bitmap setBrightness(float bright, Bitmap bitmap)
{
Bitmap image;
if (bright != 0)
{
image = tools.Brightness.SetBrightness(bitmap, bright, 10, thread);
}
else
{
image = bitmap;
}
if (madeOperationsList.TempListLength >= quantityOfOperationOnImage - 1)
{
madeOperationsList.ClearTempOperations();
}
madeOperationsList.AddTempOpearation(new OperationOnImage
{
OperationType = OperationOnImage.Brightness,
Value = bright,
});
return image;
}
void confirmSetBrightness()
{
if (openedImageObject != null)
{
sw1.Restart();
sw1.Start();
Bitmap bitmap = setContrast((float)contrastSlider.Value, openedImageObject.OpenedBitmap);
sw1.Stop();
double time = sw1.ElapsedMilliseconds / 1000;
sw1.Restart();
sw1.Start();
bitmap = setBrightness((float)brightnessSlider.Value, bitmap);
sw1.Stop();
if (timeOfOperationsDebug)
MessageBox.Show(stopWatchTimeInSec(sw1).ToString() + "s", "Brightness");
statusBarLabel.Content = "Brightness: " + stopWatchTimeInSec(sw1) + "s";
tempBitmap = bitmap;
photoCanvas.Background = OpenedImageObject.convertToBrush(OpenedImageObject.convertToBitmapImage(bitmap));
statusBarLabel.Content += "\ttotal: " + (stopWatchTimeInSec(sw1)+time).ToString() + "s";
}
}
private void settinBrightnessTextBox_ClickEnter(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
setSliderWithTextBox(-10, 10, settingBrightnessTextBox, brightnessSlider);
confirmSetBrightness();
}
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
}
//Open click
void loadPhoto(Bitmap bitmap)
{
openedImageObject = new OpenedImageObject();
BitmapImage bI = OpenedImageObject.convertToBitmapImage(OpenedImageObject.changeResolution(bitmap, previewResolution));
ImageBrush iB = openedImageObject.putImage(bI, photoCanvas.ActualWidth, photoCanvas.ActualHeight);
photoCanvas.Background = iB;
CutRectangleWithCorner = new CutRectangle(openedImageObject, photoCanvas, cutRectangle, cutRectangleThumbLeft, cutRectangleThumbRight);
CutRectangleWithCorner.putRectangle();
tempBitmap = openedImageObject.OpenedBitmap;
}
private async void MenuItem_Click(object sender, RoutedEventArgs e)
{
var dialogForm = new Microsoft.Win32.OpenFileDialog();
dialogForm.Filter = "Image files (*.bmp, *.jpg, *.png)|*.bmp;*.jpg; *.png|All files (*.*)|*.*";
dialogForm.Title = "Choose photos";
dialogForm.DefaultExt = ".png";
dialogForm.Multiselect = true;
bool? result = dialogForm.ShowDialog();
if(result == true)
{
sw1.Restart();
sw1.Start();
files = dialogForm.FileNames;
openedFile = files[0];
madeOperationsList = new MadeOperationsList(quantityOfOperationOnImage, operationsList, tempOpearationsList, files.Length);
loadPhoto(new Bitmap(openedFile));
madeOperationsList.ClearOperations();
statusBarLabel.Content = "Opened: " + files[0];
restartParameters();
sw1.Stop();
statusBarLabel.Content += "\tOpening time: " + stopWatchTimeInSec(sw1) + "s";
}
}
private void photoCanvas_Loaded(object sender, RoutedEventArgs e)
{
}
private void Main_SizeChanged(object sender, SizeChangedEventArgs e)
{
if(openedImageObject != null)
{
CutRectangleWithCorner.updateRectangle();
}
}
private void cutRectangle_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
}
private void cutRectangleThumbLeft_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
CutRectangleWithCorner.SetLeftUpCorner(e.HorizontalChange, e.VerticalChange);
}
private void cutRectangleThumbLeft_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
cutRectangleThumbLeft.Background = Brushes.Black;
}
private void cutRectangleThumbLeft_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
cutRectangleThumbLeft.Background = Brushes.Gray;
}
private void cutRectangleThumbRight_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
CutRectangleWithCorner.SetRightDownCorner(e.HorizontalChange, e.VerticalChange);
}
private void cutRectangleThumbRight_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
cutRectangleThumbRight.Background = Brushes.Black;
}
private void cutRectangleThumbRight_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
cutRectangleThumbRight.Background = Brushes.Gray;
}
private void confirmButton_Click(object sender, RoutedEventArgs e)
{
if (openedImageObject != null)
{
openedImageObject.updateImageSize(photoCanvas.ActualWidth, photoCanvas.ActualHeight);
Bitmap bitmap = tempBitmap;
if ((bool)cutPhotCheckBox.IsChecked)
{
tools.OperationOnImage cutImage = new tools.OperationOnImage();
cutImage.OperationType = tools.OperationOnImage.CutImage;
cutImage.X = CutRectangleWithCorner.XWithoutScale*previewResolution;
cutImage.Y = CutRectangleWithCorner.YWithoutScale*previewResolution;
cutImage.Width = CutRectangleWithCorner.WidthWithoutScale * previewResolution;
cutImage.Height = CutRectangleWithCorner.HeightWithoutScale * previewResolution;
madeOperationsList.AddTempOpearation(cutImage);
openedImageObject.updateImageSource(bitmap);
sw1.Restart();
sw1.Start();
bitmap = CutRectangle.CutImage(bitmap, cutImage.X/previewResolution, cutImage.Y / previewResolution, cutImage.Width / previewResolution, cutImage.Height / previewResolution);
sw1.Stop();
if (timeOfOperationsDebug)
MessageBox.Show(((double)sw1.ElapsedTicks / (double)Stopwatch.Frequency).ToString() + "s", "Cut");
statusBarLabel.Content = "Cut Photo: " + stopWatchTimeInSec(sw1) + "s";
tempBitmap = bitmap;
photoCanvas.Background = openedImageObject.putImage(OpenedImageObject.convertToBitmapImage(bitmap), photoCanvas.ActualWidth, photoCanvas.ActualHeight);
}
madeOperationsList.MigrateToMainImageOperation();
madeOperationsList.RemoveTempOperations();
restartParameters();
}
}
void saveFiles(String dir)
{
}
private void MenuItem_Click_1(object sender, RoutedEventArgs en)
{
void updateStatusBar(string text)
{
statusBarLabel.Content = text;
statusBarProgressBar.Value++;
}
void saveFiles(Microsoft.Win32.SaveFileDialog dialog)
{
for (int i = 0; i < files.Length; i++)
{
Bitmap b = new Bitmap(files[i]);
String[] fileSplit = files[i].Split('\\');
String fileName = fileSplit[fileSplit.Length - 1];
String path = dialog.FileName + "\\" + fileName;
Application.Current.Dispatcher.Invoke( () => updateStatusBar("Saved: " + path));
foreach (var o in madeOperationsList[i])
{
b = OperationOnImage.MakeOperation(b, o, thread);
}
b.Save(path, ImageFormat.Png);
}
}
if (files != null)
{
sw1.Restart();
sw1.Start();
var dialog = new Microsoft.Win32.SaveFileDialog();
dialog.FileName = "Folder"; // Default file name
// Show save file dialog box
bool? result = dialog.ShowDialog();
Directory.CreateDirectory(dialog.FileName);
statusBarProgressBar.Visibility = Visibility.Visible;
statusBarProgressBar.Minimum = 0;
statusBarProgressBar.Maximum = files.Length-1;
saveFiles(dialog);
statusBarProgressBar.Visibility = Visibility.Hidden;
sw1.Stop();
statusBarLabel.Content += "\tSaving time: " + stopWatchTimeInSec(sw1) + "s";
}
}
private void brightnessSlider_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
{
}
private void brightnessSlider_DragOver(object sender, DragEventArgs e)
{
MessageBox.Show("napis");
}
private void MenuItem_Click_2(object sender, RoutedEventArgs e)
{
timeOfOperationsMenuItem.IsChecked = !timeOfOperationsMenuItem.IsChecked;
timeOfOperationsDebug = !timeOfOperationsDebug;
}
private void brightnessSlider_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
confirmSetBrightness();
}
}
private void contrastSlider_KeyDown(object sender, KeyEventArgs e)
{
if(e.Key == Key.Enter)
{
confirmSetContrast();
}
}
void uncheckMultiThreading()
{
foreach(MenuItem m in threadingOptions)
{
m.IsChecked = false;
}
}
void checkMultiThreadOption(byte number)
{
uncheckMultiThreading();
threadingOptions[number].IsChecked = !threadingOptions[number].IsChecked;
}
private void MenuItem_Click_MT4(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(2);
thread = 4;
}
private void MT8_Click(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(4);
thread = 8;
}
private void MenuItem_Click_MT1(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(0);
thread = 1;
}
private void MenuItem_Click_MT2(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(1);
thread = 2;
}
private void MenuItem_Click_MT6(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(3);
thread = 6;
}
private void MenuItem_Click_MT16(object sender, RoutedEventArgs e)
{
checkMultiThreadOption(5);
thread = 16;
}
void uncheckPreviewResolutionOptions()
{
foreach(MenuItem m in previewResolutionOptions)
{
m.IsChecked = false;
}
}
private void PS1_Click(object sender, RoutedEventArgs e)
{
uncheckPreviewResolutionOptions();
PS1.IsChecked = true;
previewResolution = 1;
if (openedFile != null)
{
loadPhoto(OpenedImageObject.changeResolution(new Bitmap(openedFile), previewResolution));
madeOperationsList.ClearOperations();
restartParameters();
}
}
private void PS2_Click(object sender, RoutedEventArgs e)
{
uncheckPreviewResolutionOptions();
PS2.IsChecked = true;
previewResolution = 2;
if (openedFile != null)
{
loadPhoto(OpenedImageObject.changeResolution(new Bitmap(openedFile), previewResolution));
madeOperationsList.ClearOperations();
restartParameters();
}
}
private void PS4_Click(object sender, RoutedEventArgs e)
{
uncheckPreviewResolutionOptions();
PS4.IsChecked = true;
previewResolution = 4;
if (openedFile != null)
{
loadPhoto(OpenedImageObject.changeResolution(new Bitmap(openedFile), previewResolution));
madeOperationsList.ClearOperations();
restartParameters();
}
}
}
}