-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
1462 lines (1302 loc) · 61.5 KB
/
Form1.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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//#define _ENABLE_LAB3_MULTISELECT_EDGESELECT_CHANGEANYSHAPECOLORTHICKNESS
/*
* To avoid defining this symbol in every file, refer to: https://stackoverflow.com/questions/436369/how-to-define-a-constant-globally-in-c-sharp-like-debug
* Also, learn about Conditional Attribute and the like here: https://stackoverflow.com/a/975370
*/
//#define Lab3
using Computer_Graphics_1.HelperClasses;
using Computer_Graphics_1.HelperClasses.Extensions;
using Computer_Graphics_1.Lab1;
using Computer_Graphics_1.Lab1.LabPart;
using Computer_Graphics_1.Lab2;
using Computer_Graphics_1.Lab3;
using Computer_Graphics_1.Lab4;
using Computer_Graphics_1.Lab5;
using Computer_Graphics_1.Lab5.LabPart;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Media.Imaging;
namespace Computer_Graphics_1
{
public partial class MainForm : Form
{
private Color selectedFillColor = Color.MediumVioletRed;
private Bitmap selectedFillPattern = Computer_Graphics_1.Properties.Resources.chessPatternImperfectCrop;
private Bitmap ogBitmap = null;
private WriteableBitmap wBmpToEdit = null;
private bool drawingEnabled = false;
private SupportedShapes selectedShapeType = SupportedShapes.Line;
private Tuple<int, List<int>> selectedPointsShapeAndPointIndices = null;
List<Shape> shapes = new List<Shape>();
//List<ClippingPolygon> clippingPolygons = new List<ClippingPolygon>();
bool guptaSproulAntiAliasingEnabled = false;
bool superSamplingEnabled = false;
bool drawPoints = true;
Point lastClickLocation = Point.Empty;
public static class cnvFilt
{
public static bool convFilterParametersSet = false;
public static int[,] Mat = null;
public static double divisor = -99999;
public static _coords anchorCoords; //indexed from 1
public static int offset = 0;
}
public MainForm()
{
InitializeComponent();
setComparisonViewImage(global::Computer_Graphics_1.Properties.Resources._default);
drawingCanvasPictureBox.Image = DrawFilledRectangle(drawingCanvasPictureBox.Width, drawingCanvasPictureBox.Height);
if (ogPictureBox.Image != null)
{
setApplicationInteractionEnabled(true);
zoomToolStripMenuItem_Click(null, null);
}
#region LAB3
if (selectedShapeType == SupportedShapes.Line)
lineRadioButton.Checked = true;
#if Lab3
labsTabControl.SelectedTab = lab3TabPage;
imagesTabControl.SelectedTab = drawingViewTabPage;
#endif
#endregion
//selectedFillPictureBox.Image = selectedFillPattern;
selectedFillPictureBox.BackColor = selectedFillColor;
selectedFillPictureBox.Image = null;
////labsTabControl.SelectedTab = lab2TabPage;
////labsTabControl.SelectedTab = lab3TabPage; imagesTabControl.SelectedTab = drawingViewTabPage;
////foreach (Control cntrl in lab1TabPage.Controls)
////{
//// cntrl.Enabled = false;
////}
////ogPictureBox.Image=
}
private void undoAllProcessingMenuItem_Click(object sender, EventArgs e)
{
if (ogBitmap == null)
return;
wBmpToEdit = ImgUtil.GetWritableBitmapFromBitmap(ogBitmap);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
inversionCheckBox.Checked = false;
if (drawingCanvasPictureBox.Image != null)
{
drawingCanvasPictureBox.Image = DrawFilledRectangle(drawingCanvasPictureBox.Width, drawingCanvasPictureBox.Height);
foreach (Shape shp in shapes)
{
shp.vertices.Clear();
}
if (drawingEnabled)
toggleDrawingButton_Click(null, null);
}
if(isCubeDrawnAndTransformed)
{
stopDrawTransformCubeAndResetImage();
}
}
private Bitmap DrawFilledRectangle(int x, int y, Brush brush = null)
{
if (brush == null)
{
brush = Brushes.White;
}
Bitmap bmp = new Bitmap(x, y);
using (Graphics graph = Graphics.FromImage(bmp))
{
Rectangle ImageSize = new Rectangle(0, 0, x, y);
graph.FillRectangle(brush, ImageSize);
}
return bmp;
}
#region testing if sub regions work!
#region do they?
//Turns out they do!
#endregion
#endregion
private void setApplicationInteractionEnabled(bool isEnabled)
{
imagesTabControl.Enabled = isEnabled;
labsTabControl.Enabled = isEnabled;
undoAllProcessingMenuItem.Enabled = isEnabled;
}
private void ogPictureBox_DoubleClick(object sender, EventArgs e)
{
if (ogPictureBox.Image == null)
{
openImageToolStripMenuItem_Click(null, null);
}
}
private void openImageToolStripMenuItem_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
//openFileDialog.InitialDirectory = "C:\\";
openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
openFileDialog.Filter = "Image files (*.bmp,*.jpg,*.png)|*.bmp;*.jpg;*.png|Bitmap image file (*.bmp)|*.bmp;|All files|*.*";
openFileDialog.FilterIndex = 1;
//openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
setComparisonViewImage(new Bitmap(openFileDialog.FileName));
}
else
{
MessageBox.Show("No image selected.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
}
private void setComparisonViewImage(Bitmap img = null)
{
if (img != null)
ogBitmap = img;
if (ogBitmap == null)//was already null.
throw new Exception("Dunno why, should be simple. Will write later, since it doesn't hit.");
ogPictureBox.Image = ogBitmap;
wBmpToEdit = ImgUtil.GetWritableBitmapFromBitmap(ogBitmap);
//wBmpToEdit = ImageUtil.GetWriteableBitmapFromAbsURI(openFileDialog.FileName);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
//foreach (Control cntrl in lab1TabPage.Controls)
//{
// cntrl.Enabled = true;
//}
setApplicationInteractionEnabled(true);
inversionCheckBox.Checked = false;
}
#region LAB1-SPECIFIC-REGION
//private void invertFilter_Click(object sender, EventArgs e)
//{
// ////WriteableBitmap writtenImg = new WriteableBitmap(wBmpToEdit);
// //WriteableBitmap writtenImg = ImageUtil.GetWritableBitmapFromBitmap(new Bitmap(newPictureBox.Image)); //If this is efficient enough, I could just use this and change wBmpToEdit to be "const".
// Lab1.FunctionalFilters.InvertWriteableBitmap(wBmpToEdit);//(writtenImg);
// newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);//(writtenImg);
// if (invertFilterButton.ForeColor == SystemColors.ActiveCaptionText|| invertFilterButton.ForeColor==Color.Black)
// invertFilterButton.ForeColor = Color.Green;
// else
// invertFilterButton.ForeColor = Color.Black;
//}
private void brightnessCorrection_Click(object sender, EventArgs e)
{
FunctionalFilters.BrightnessCorrectionWbmp(wBmpToEdit, 20);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void contrastEnhanceButton_Click(object sender, EventArgs e)
{
FunctionalFilters.ContrastEnhancementWbmp(wBmpToEdit, 1.25);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void gammaCorrectionButton_Click(object sender, EventArgs e)
{
FunctionalFilters.GammaCorrection(wBmpToEdit, 1.666);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void applyConvFiltButton_Click(object sender, EventArgs e)
{
if (cnvFilt.convFilterParametersSet)
{
int[,] blurConvMat3x3 = new int[3, 3];//[9, 9];
for (int i = 0; i < 3; i++)//i<9
{
for (int j = 0; j < 3; j++)//j<9
{
blurConvMat3x3[i, j] = 1;//0;//1;
}
}
/////the above sets for blur and it works
/////Gaussian Smoothening/blur: (should work)
////blurConvMat3x3[0, 0] = 0; blurConvMat3x3[0, 1] = 1; blurConvMat3x3[0, 2] = 0;
////blurConvMat3x3[1, 0] = 1; blurConvMat3x3[1, 1] = 4; blurConvMat3x3[1, 2] = 1;
////blurConvMat3x3[2, 0] = 0; blurConvMat3x3[2, 1] = 1; blurConvMat3x3[2, 2] = 0;
/////Sharpen: (works)
////blurConvMat3x3[0, 0] = 0; blurConvMat3x3[0, 1] = -1; blurConvMat3x3[0, 2] = 0;
////blurConvMat3x3[1, 0] = -1; blurConvMat3x3[1, 1] = 5; blurConvMat3x3[1, 2] = -1;
////blurConvMat3x3[2, 0] = 0; blurConvMat3x3[2, 1] = -1; blurConvMat3x3[2, 2] = 0;
/////Mean removal sharpen?: (probably works as it should)
////blurConvMat3x3[0, 0] = -1; blurConvMat3x3[0, 1] = -1; blurConvMat3x3[0, 2] = -1;
////blurConvMat3x3[1, 0] = -1; blurConvMat3x3[1, 1] = 9; blurConvMat3x3[1, 2] = -1;
////blurConvMat3x3[2, 0] = -1; blurConvMat3x3[2, 1] = -1; blurConvMat3x3[2, 2] = -1;
/////Edge detection: (Works! (USE 0.1 divisor THO)
blurConvMat3x3[0, 0] = 0; blurConvMat3x3[0, 1] = -1; blurConvMat3x3[0, 2] = 0;
blurConvMat3x3[1, 0] = 0; blurConvMat3x3[1, 1] = 1; blurConvMat3x3[1, 2] = 0;
blurConvMat3x3[2, 0] = 0; blurConvMat3x3[2, 1] = 0; blurConvMat3x3[2, 2] = 0;
/////Emboss: (works)
////blurConvMat3x3[0, 0] = -1; blurConvMat3x3[0, 1] = 0; blurConvMat3x3[0, 2] = 1;
////blurConvMat3x3[1, 0] = -1; blurConvMat3x3[1, 1] = 1; blurConvMat3x3[1, 2] = 1;
////blurConvMat3x3[2, 0] = -1; blurConvMat3x3[2, 1] = 0; blurConvMat3x3[2, 2] = 1;
////ConvolutionFilters.ConvolutionFilter(blurConvMat3x3, wBmpToEdit);//,ref newPictureBox);
_coords Coords; Coords.r = 2; Coords.c = 2; //7x7:5,6 //6,5 //indexed from 1
////ConvolutionFilters.Apply(blurConvMat3x3, Coords, wBmpToEdit, 0.1);//,0.1);//(blurConvMat3x3,Coords,wBmpToEdit,0); ,0.1
//ConvolutionFilters.Apply(cnvFilt.Mat, cnvFilt.anchorCoords, wBmpToEdit, cnvFilt.divisor, cnvFilt.offset);
ConvolutionFilters.ConvFilCleanCode(cnvFilt.Mat, cnvFilt.anchorCoords, wBmpToEdit, cnvFilt.divisor, cnvFilt.offset);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
}
private void inversionCheckBox_CheckedChanged(object sender, EventArgs e)
{
////WriteableBitmap writtenImg = new WriteableBitmap(wBmpToEdit);
//WriteableBitmap writtenImg = ImageUtil.GetWritableBitmapFromBitmap(new Bitmap(newPictureBox.Image)); //If this is efficient enough, I could just use this and change wBmpToEdit to be "const".
Lab1.FunctionalFilters.InvertWriteableBitmap(wBmpToEdit);//(writtenImg);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);//(writtenImg
//TBH, this checkbox should have been a button rather than a checkbox.
}
private void invertFilter_Click(object sender, EventArgs e)
{
}
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void edgeDetRadioButton_SelectIndexChanged(object sender, EventArgs e)
{
if (edgeDetRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 128;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
///Edge detection: (Works! (USE 0.1 divisor THO)
cnvFilt.Mat[0, 0] = 0; cnvFilt.Mat[0, 1] = 0; cnvFilt.Mat[0, 2] = 0;
cnvFilt.Mat[1, 0] = -1; cnvFilt.Mat[1, 1] = 1; cnvFilt.Mat[1, 2] = 0;
cnvFilt.Mat[2, 0] = 0; cnvFilt.Mat[2, 1] = 0; cnvFilt.Mat[2, 2] = 0;
cnvFilt.convFilterParametersSet = true;
}
}
private void blurRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (blurRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 0;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
for (int i = 0; i < 3; i++)//i<9
{
for (int j = 0; j < 3; j++)//j<9
{
cnvFilt.Mat[i, j] = 1;//0;//1;
}
}
cnvFilt.convFilterParametersSet = true;
}
}
private void gaussSmoothRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (gaussSmoothRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 0;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
///Gaussian Smoothening/blur: (should work)
cnvFilt.Mat[0, 0] = 0; cnvFilt.Mat[0, 1] = 1; cnvFilt.Mat[0, 2] = 0;
cnvFilt.Mat[1, 0] = 1; cnvFilt.Mat[1, 1] = 4; cnvFilt.Mat[1, 2] = 1;
cnvFilt.Mat[2, 0] = 0; cnvFilt.Mat[2, 1] = 1; cnvFilt.Mat[2, 2] = 0;
cnvFilt.convFilterParametersSet = true;
}
}
private void sharpenRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (sharpenRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 0;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
///Sharpen: (works)
cnvFilt.Mat[0, 0] = 0; cnvFilt.Mat[0, 1] = -1; cnvFilt.Mat[0, 2] = 0;
cnvFilt.Mat[1, 0] = -1; cnvFilt.Mat[1, 1] = 5; cnvFilt.Mat[1, 2] = -1;
cnvFilt.Mat[2, 0] = 0; cnvFilt.Mat[2, 1] = -1; cnvFilt.Mat[2, 2] = 0;
cnvFilt.convFilterParametersSet = true;
}
}
private void meanRemSharpRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (meanRemSharpRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 0;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
///Mean removal sharpen?: (probably works as it should)
cnvFilt.Mat[0, 0] = -1; cnvFilt.Mat[0, 1] = -1; cnvFilt.Mat[0, 2] = -1;
cnvFilt.Mat[1, 0] = -1; cnvFilt.Mat[1, 1] = 9; cnvFilt.Mat[1, 2] = -1;
cnvFilt.Mat[2, 0] = -1; cnvFilt.Mat[2, 1] = -1; cnvFilt.Mat[2, 2] = -1;
cnvFilt.convFilterParametersSet = true;
}
}
private void embossRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (embossRadioButton.Checked)
{
cnvFilt.Mat = new int[3, 3];
cnvFilt.divisor = -99999;
cnvFilt.offset = 0;
cnvFilt.anchorCoords.r = 2; cnvFilt.anchorCoords.c = 2;
///Emboss: (works)
cnvFilt.Mat[0, 0] = -1; cnvFilt.Mat[0, 1] = 0; cnvFilt.Mat[0, 2] = 1;
cnvFilt.Mat[1, 0] = -1; cnvFilt.Mat[1, 1] = 1; cnvFilt.Mat[1, 2] = 1;
cnvFilt.Mat[2, 0] = -1; cnvFilt.Mat[2, 1] = 0; cnvFilt.Mat[2, 2] = 1;
cnvFilt.convFilterParametersSet = true;
}
}
private void customizeConvFilterButton_Click(object sender, EventArgs e)
{
if (!cnvFilt.convFilterParametersSet)
{
MessageBox.Show("Select a base Convolution Filter to customize first.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
using (ConvFiltCustomizer cFCD = new ConvFiltCustomizer(cnvFilt.Mat, cnvFilt.anchorCoords, cnvFilt.divisor, cnvFilt.offset))
{
if (cFCD.ShowDialog() == DialogResult.OK)
{
//ConvolutionFilters.Apply(cFCD.sqrCnvMat, cFCD.anchorKernel, wBmpToEdit, cFCD.divisor, cFCD.offset);
ConvolutionFilters.ConvFilCleanCode(cFCD.sqrCnvMat, cFCD.anchorKernel, wBmpToEdit, cFCD.divisor, cFCD.offset);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
else
{
MessageBox.Show("Custom filter not applied.", "Action cancelled", MessageBoxButtons.OK);
}
}
}
private void medianFilterButtonClick(object sender, EventArgs e)
{
MedianFilter.MedianFilter3x3(wBmpToEdit); //passed by reference by default.
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void lab1TabPage_Click(object sender, EventArgs e)
{
}
#endregion
private void saveAsResultpngToolStripMenuItem_Click(object sender, EventArgs e)
{
wBmpToEdit.SaveAsPNG("result.png");
}
private void toolStripMenuItem2_Click(object sender, EventArgs e)
{
openImageToolStripMenuItem_Click(sender, e);
}
private void centerimageToolStripMenuItem_Click(object sender, EventArgs e)
{
ogPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
newPictureBox.SizeMode = PictureBoxSizeMode.CenterImage;
centerToolStripMenuItem.Checked = true;
stretchToolStripMenuItem.Checked = false;
zoomToolStripMenuItem.Checked = false;
autoSizescrollbarToolStripMenuItem.Checked = false;
}
private void stretchToolStripMenuItem_Click(object sender, EventArgs e)
{
ogPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
newPictureBox.SizeMode = PictureBoxSizeMode.StretchImage;
centerToolStripMenuItem.Checked = false;
stretchToolStripMenuItem.Checked = true;
zoomToolStripMenuItem.Checked = false;
autoSizescrollbarToolStripMenuItem.Checked = false;
}
private void zoomToolStripMenuItem_Click(object sender, EventArgs e)
{
ogPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
newPictureBox.SizeMode = PictureBoxSizeMode.Zoom;
centerToolStripMenuItem.Checked = false;
stretchToolStripMenuItem.Checked = false;
zoomToolStripMenuItem.Checked = true;
autoSizescrollbarToolStripMenuItem.Checked = false;
}
#region LAB2-SPECIFIC-REGION
private void averageDitheringButton_Click(object sender, EventArgs e)
{
AverageDithering.apply(wBmpToEdit, (int)colorperchannelNumericUpDown.Value);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void cnvrtToGrayscaleButton_Click(object sender, EventArgs e)
{
wBmpToEdit.ConvertRGB2GrayscaleRGB();
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void colorperchannelNumericUpDown_Validating(object sender, CancelEventArgs e)
{
if (colorperchannelNumericUpDown.Value % 2 != 0)
{
colorperchannelNumericUpDown.Value += 1;
}
}
private void octreeQuantizationButton_Click(object sender, EventArgs e)
{
OctreeQuantization.ApplyAverageBasedPerformanceIntensive(wBmpToEdit, Decimal.ToInt32(octreeColorsPerChannelNumericUpDown.Value));
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void autoSizescrollbarToolStripMenuItem_Click(object sender, EventArgs e)
{
ogPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
newPictureBox.SizeMode = PictureBoxSizeMode.AutoSize;
centerToolStripMenuItem.Checked = false;
stretchToolStripMenuItem.Checked = false;
zoomToolStripMenuItem.Checked = false;
autoSizescrollbarToolStripMenuItem.Checked = true;
}
private void popOctreeMemIntnsv_Button_Click(object sender, EventArgs e)
{
OctreeQuantization.ApplyPopularityBasedMemoryIntensive(wBmpToEdit, Decimal.ToInt32(octreeColorsPerChannelNumericUpDown.Value));
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
private void button1_Click(object sender, EventArgs e)
{
AverageDithering.applyYCbCr(wBmpToEdit, (int)colorperchannelNumericUpDown.Value);
newPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(wBmpToEdit);
}
#endregion
#region LAB3-SPECIFIC-REGION
//Shape shp = new PolyLine();
int indexLastSelectedShape = -1;
private void drawingCanvasPictureBox_Click(object sender, EventArgs e)
{
MouseEventArgs mE = (MouseEventArgs)e;
bool enableClippingButton = false;
lastClickLocation = mE.Location;
if (drawingEnabled)
{
resetAllShapes();
selectedPointsShapeAndPointIndices = null;
shapes.Last().AddVertices(mE.Location.X, mE.Location.Y); //x is col, y is row
//if(selectedShapeType==SupportedShapes.Line)
//drawingCanvasPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(((PolyLine)shp).draw(ImgUtil.GetWritableBitmapFromBitmap(new Bitmap(drawingCanvasPictureBox.Image)),false));
drawAllShapes(drawPoints);
if (shapes.Last().vertices.Count() == 2 && shapes.Last().isShapeType(SupportedShapes.Circle))
{
toggleDrawingButton_Click(null, null);
}
}
else
{
if (selectedPointsShapeAndPointIndices == null || Form.ModifierKeys == Keys.Control || emulateHoldControlCheckbox.Checked || emulateHoldingControlAltCheckbox.Checked)
{
#if _ENABLE_LAB3_MULTISELECT_EDGESELECT_CHANGEANYSHAPECOLORTHICKNESS
throw new NotImplementedException("Need to implement the selection logic for multiselection, edge selection, and selecting a previous shape in order to change it's color still"); //just got reminded that innerexceptions exist, they're nice. Keep in mind for future, will help somewhere in nested exception-ing.
neeed to make selectedPointShapeAndPointIndices a list of tuples or points.
int currentSelectedPointsCount = 1;
if(selectedPointShapeAndPointIndices!=null)
{
currentSelectedPointsCount=selectedPointShapeAndPointIndices.Count();
...
}
#endif
int detPixRadius = 6;
bool gotPoint = false;
for (int i = shapes.Count - 1; i >= 0; i--)//the following two loops select the latest placed point which was in click radius.
{
for (int j = shapes[i].vertices.Count - 1; j >= 0; j--)
{
//if ((1 / MathUtil.FastInverseSqRt((float)Math.Pow((mE.X - shapes[i].points[j].Item1), 2) + (float)Math.Pow((mE.Y - shapes[i].points[j].Item2), 2))) <= detPixRadius)
if (Math.Sqrt((float)Math.Pow((mE.X - shapes[i].vertices[j].X), 2) + (float)Math.Pow((mE.Y - shapes[i].vertices[j].Y), 2)) <= detPixRadius)
{
if (selectedPointsShapeAndPointIndices == null)
{
selectedPointsShapeAndPointIndices = new Tuple<int, List<int>>(i, new List<int>());
drawingCanvasPictureBox.Cursor = Cursors.Hand;
}
else
{
indexLastSelectedShape = selectedPointsShapeAndPointIndices.Item1;
selectedPointsShapeAndPointIndices = new Tuple<int, List<int>>(i, new List<int>());
drawingCanvasPictureBox.Cursor = Cursors.Hand;
}
selectedPointsShapeAndPointIndices.Item2.Add(j);
gotPoint = true;
break;
}
}
if (gotPoint)
break;
}
if (gotPoint)
{
Shape lastSelectedShape = null;
if (indexLastSelectedShape != -1)
{
lastSelectedShape = shapes[indexLastSelectedShape];
}
Shape currentSelectedShape = shapes[selectedPointsShapeAndPointIndices.Item1];
if (selectedPointsShapeAndPointIndices == null)
{
lastSelectedShape = null;
indexLastSelectedShape = -1;
}
if (lastSelectedShape != null) //this is dependent on the if above.
{
currentSelectedShape = shapes[selectedPointsShapeAndPointIndices.Item1];
if (lastSelectedShape.isShapeType(SupportedShapes.Polygon) && currentSelectedShape.isShapeType(SupportedShapes.Polygon))
{
if ((currentSelectedShape as Polygon).isConvex())
{
enableClippingButton = true;
}
}
}
}
else
{
indexLastSelectedShape = -1;
drawingCanvasPictureBox.Cursor = Cursors.Default;
}
}
else
{
indexLastSelectedShape = -1;
resetAllShapes();
drawingCanvasPictureBox.Cursor = Cursors.Default;
int i = selectedPointsShapeAndPointIndices.Item1;
int j = selectedPointsShapeAndPointIndices.Item2[0];
//we move according to the change in the first clicked point!
int deltaX = mE.X - shapes[i].vertices[j].X;
int deltaY = mE.Y - shapes[i].vertices[j].Y;
shapes[i].vertices[j] = new Point(mE.X, mE.Y);
//bool firstRun = true;//to skip the first vertice selected because we already moved it.
foreach (int k in selectedPointsShapeAndPointIndices.Item2)
{
//if(firstRun)
//{
// firstRun = false;
// continue;
//}
if (k == j)
continue;
int currX = shapes[i].vertices[k].X;
int currY = shapes[i].vertices[k].Y;
shapes[i].vertices[k] = new Point(currX + deltaX, currY + deltaY);
}
selectedPointsShapeAndPointIndices = null;
drawAllShapes(drawPoints);
}
}
enableOrDisableClippingButton(enableClippingButton);
}
private void enableOrDisableClippingButton(bool enableClippingButton)
{
if (enableClippingButton)
{
clipButton.Enabled = true;
clipButton.ForeColor = Color.Black;
}
else
{
clipButton.Enabled = false;
clipButton.ForeColor = Color.Gray;
}
}
private void resetAllShapes()
{
drawingCanvasPictureBox.Image = DrawFilledRectangle(drawingCanvasPictureBox.Image.Width, drawingCanvasPictureBox.Image.Height);
}
private void drawAllShapes(bool drawPoints)
{
if (superSamplingEnabled)
{
resetAllShapes();
WriteableBitmap downSampled = null;
Size superSampledSize = new Size(2 * drawingCanvasPictureBox.Image.Width, 2 * drawingCanvasPictureBox.Image.Height);
Bitmap superSampledImage = new Bitmap(drawingCanvasPictureBox.Image, superSampledSize);
foreach (Shape shp in shapes)
{
shp.thickness = shp.thickness * 2;
for (int i = 0; i < shp.vertices.Count(); i++)
{
shp.vertices[i] = new Point(2 * shp.vertices[i].X, 2 * shp.vertices[i].Y);
}
superSampledImage = ImgUtil.GetBitmapFromWriteableBitmap(shp.draw(ImgUtil.GetWritableBitmapFromBitmap(superSampledImage), drawPoints));
shp.thickness = shp.thickness / 2;
for (int i = 0; i < shp.vertices.Count(); i++)
{
shp.vertices[i] = new Point(shp.vertices[i].X / 2, shp.vertices[i].Y / 2);
}
}
downSampled = downSampling2x(ImgUtil.GetWritableBitmapFromBitmap(superSampledImage));
drawingCanvasPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(downSampled);
if (downSampled != null)
drawingCanvasPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(downSampled);
return;
}
foreach (Shape shp in shapes)
{
if (guptaSproulAntiAliasingEnabled && (shp.isShapeType(SupportedShapes.Line)))
{
drawingCanvasPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(shp.drawGSAA(ImgUtil.GetWritableBitmapFromBitmap(new Bitmap(drawingCanvasPictureBox.Image)), drawPoints));
}
else
{
drawingCanvasPictureBox.Image = ImgUtil.GetBitmapFromWriteableBitmap(shp.draw(ImgUtil.GetWritableBitmapFromBitmap(new Bitmap(drawingCanvasPictureBox.Image)), drawPoints));
}
}
}
private WriteableBitmap downSampling2x(WriteableBitmap upsampledWbmp)//,int scalingFactor=2
{
Size resultSize = new Size(upsampledWbmp.PixelWidth / 2, upsampledWbmp.PixelHeight / 2);
//Bitmap tempBMP = new Bitmap(drawingCanvasPictureBox.Image, resultSize);
Bitmap tempBMP = new Bitmap(drawingCanvasPictureBox.Image, resultSize);
WriteableBitmap result = ImgUtil.GetWritableBitmapFromBitmap(tempBMP);
for (int r = 0; r < upsampledWbmp.PixelHeight; r += 2)
{
for (int c = 0; c < upsampledWbmp.PixelWidth; c += 2)
{
unsafe
{
int redAvg = 0; int blueAvg = 0; int greenAvg = 0;
for (int i = r; i <= r + 1; i++)
{
for (int j = c; j <= c + 1; j++)
{
_pixel_bgr24_bgra32* ptrPx = (_pixel_bgr24_bgra32*)upsampledWbmp.GetPixelIntPtrAt(i, j);
blueAvg += ptrPx->blue;
greenAvg += ptrPx->green;
redAvg += ptrPx->red;
}
}
blueAvg = blueAvg / 4;
greenAvg = greenAvg / 4;
redAvg = redAvg / 4;
_pixel_bgr24_bgra32* ptrResPx = (_pixel_bgr24_bgra32*)result.GetPixelIntPtrAt(r / 2, c / 2);
ptrResPx->blue = (byte)blueAvg;
ptrResPx->green = (byte)greenAvg;
ptrResPx->red = (byte)redAvg;
}
}
}
return result;
}
private void lineRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (lineRadioButton.Checked)
selectedShapeType = SupportedShapes.Line;
}
private void polygonRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (polygonRadioButton.Checked)
{
selectedShapeType = SupportedShapes.Polygon;
}
}
private void circleRadioButton_CheckedChanged(object sender, EventArgs e)
{
if (circleRadioButton.Checked)
selectedShapeType = SupportedShapes.Circle;
}
private void toggleDrawingButton_Click(object sender, EventArgs e)
{
drawingEnabled = !drawingEnabled;
//if(clippingEnabled)
//{
// toggleDrawClippingPolygon(null, null);
//}
if (drawingEnabled)
{
Shape shp = Shape.ConstructRequiredShape(selectedShapeType);
this.shapes.Add(shp);
shapes.Last().thickness = (int)thicknessNumericUpDown.Value;
drawingCanvasPictureBox.Cursor = Cursors.Cross;
toggleDrawingButton.Text = "Stop Drawing";
}
else
{
drawingCanvasPictureBox.Cursor = Cursors.Default;
toggleDrawingButton.Text = "Start Drawing";
}
}
private void colorPickerButton_Click(object sender, EventArgs e)
{
if (shapes.Count == 0)
{
MessageBox.Show("Start drawing a shape before choosing color.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
}
if (drawingColorPicker.ShowDialog() == DialogResult.OK)
{
resetAllShapes();
shapes.Last().color = drawingColorPicker.Color;
drawAllShapes(drawPoints);
}
else
{
MessageBox.Show("No color selected.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
private void tableLayoutPanel4_Paint(object sender, PaintEventArgs e)
{
}
private void thicknessNumericUpDown_ValueChanged(object sender, EventArgs e)
{
if (shapes.Count > 0)
{
resetAllShapes();
shapes.Last().thickness = (int)thicknessNumericUpDown.Value;
drawAllShapes(drawPoints);
}
//else
//{
// thicknessNumericUpDown.Value = 1;
// MessageBox.Show("Start drawing a shape before choosing thickness.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
//}
}
private void deleteSelectedShapeButton_Click(object sender, EventArgs e)
{
if (selectedPointsShapeAndPointIndices != null)
{
resetAllShapes();
drawingCanvasPictureBox.Cursor = Cursors.Default;
shapes.RemoveAt(selectedPointsShapeAndPointIndices.Item1);
selectedPointsShapeAndPointIndices = null;
drawAllShapes(drawPoints);
}
else
{
MessageBox.Show("No shape selected.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}
private void antiAliasingGuptaSproulCheckBox_CheckedChanged(object sender, EventArgs e)
{
resetAllShapes();
guptaSproulAntiAliasingEnabled = antiAliasingGuptaSproulCheckBox.Checked;
drawAllShapes(drawPoints);
}
private void cleanShapesList()
{
if (drawingEnabled)
toggleDrawingButton_Click(null, null);
List<Shape> cleanedShapesList = new List<Shape>();
foreach (var shp in shapes)
{
if ((shp.isShapeType(SupportedShapes.Line) && shp.vertices.Count < 2)
|| (shp.isShapeType(SupportedShapes.Polygon) && shp.vertices.Count < 3)
|| (shp.isShapeType(SupportedShapes.Circle) && shp.vertices.Count != 2))
{
continue;
}
cleanedShapesList.Add(shp);
}
shapes = cleanedShapesList;
}
private void saveAllShapesButton_Click(object sender, EventArgs e)
{
if (drawingEnabled)
toggleDrawingButton_Click(null, null);
cleanShapesList();
shapes.SerializeToXML("kek.xml");
}
private void loadNewShapesButton_Click(object sender, EventArgs e)
{
if (drawingEnabled)
toggleDrawingButton_Click(null, null);
resetAllShapes();
shapes.Deserialize("kek.xml");
drawAllShapes(drawPoints);
}
private void superSamplingAntiAliasingCheckBox_CheckedChanged(object sender, EventArgs e)
{
superSamplingEnabled = superSamplingAntiAliasingCheckBox.Checked;
resetAllShapes();
drawAllShapes(drawPoints);
}
#endregion
private void labsTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (labsTabControl.SelectedTab == lab3TabPage || labsTabControl.SelectedTab == lab4TabPage) //can add ||s to let other pages behave the same way.
{
if (imagesTabControl.SelectedTab != drawingViewTabPage)
{
imagesTabControl.SelectedTab = drawingViewTabPage;
}
}
else if(labsTabControl.SelectedTab == lab1TabPage || labsTabControl.SelectedTab == lab2TabPage)
{
if (imagesTabControl.SelectedTab != comparisontViewTabPage)
imagesTabControl.SelectedTab = comparisontViewTabPage;
}
else if(labsTabControl.SelectedTab==lab5TabPage)
{
if (imagesTabControl.SelectedTab != graphics3DTabPage)
imagesTabControl.SelectedTab = graphics3DTabPage;
}
//else
//{
// if (imagesTabControl.SelectedTab == drawingViewTabPage)
// {
// imagesTabControl.SelectedTab = comparisontViewTabPage;
// }
//}
}
private void imagesTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (imagesTabControl.SelectedTab == drawingViewTabPage)
{
if (labsTabControl.SelectedTab != lab3TabPage && labsTabControl.SelectedTab != lab4TabPage) //can add ||s to let accomadate any other labs that might use the same.
{
labsTabControl.SelectedTab = lab3TabPage;
}
}
else if(imagesTabControl.SelectedTab==graphics3DTabPage)
{
if (labsTabControl.SelectedTab != lab5TabPage)
labsTabControl.SelectedTab = lab5TabPage;
sphereDraw();
}
else
{
if (labsTabControl.SelectedTab != lab2TabPage && labsTabControl.SelectedTab != lab2TabPage) //can add ||s to let accomadate any other labs that might not use the view like lab3TabPage here.
{
labsTabControl.SelectedTab = lab1TabPage;
}
}
}
//bool clippingEnabled = false;
//Polygon shpToEdit = null;
//int indxShpToEdit = -1;
//private void clipPolygons(object sender, EventArgs e)
//{
// if(clippingEnabled)
// {
// drawingEnabled = false;
// clippingEnabled = false;
// //might need to disabled clipping again here.
// ClippingPolygon clipper = (ClippingPolygon)shapes.Last();
// if (!clipper.isShapeType(SupportedShapes.ClippingPolygon))
// throw new Exception("lool");
// //clipper.clip(wBmpToEdit, ref shpToEdit);
// //shapes[indxShpToEdit] = shpToEdit;
// shapes.Count();
// WriteableBitmap canvasWbmp = ImgUtil.GetWritableBitmapFromBitmap(new Bitmap(drawingCanvasPictureBox.Image)); //probably inefficient.
// Polygon clippedShape = clipper.getClippedPolygon(canvasWbmp, shpToEdit);
// ref Color col = ref clippedShape.color;
// int red = 255 - col.R;
// int green = 255 - col.G;
// int blue = 255 - col.B;
// if (red > 230 && green > 230 && blue > 230)//to avoid having too light pixels on the white background.
// {
// red = 210;
// green = 230;
// blue = 220;
// }
// col = Color.FromArgb(red, green, blue);
// shapes.Add(clippedShape);
// shapes.Count();
// resetAllShapes();
// drawAllShapes(true);
// shapes.Count();
// //shpToEdit = null;
// lineRadioButton.Checked = true;
// //testButton.Text = "Test";
// return;
// }
// //MessageBox here: Select polygon/shape to be clipped first!
// //Actually, the button needs to be disabled unless the correct shape is selected.
// //if (drawingEnabled)
// //{
// //toggleDrawingButton_Click(null, null);
// //}
// clippingEnabled = true;
// if (shpToEdit == null)
// {
// shpToEdit = shapes[selectedPointsShapeAndPointIndices.Item1] as Polygon; //try catch to detect invalid shape?
// indxShpToEdit = selectedPointsShapeAndPointIndices.Item1;
// }
// polygonRadioButton.Checked = false;
// lineRadioButton.Checked = false;