-
Notifications
You must be signed in to change notification settings - Fork 1
/
RenderHandler.cs
1603 lines (1592 loc) · 62.6 KB
/
RenderHandler.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
using System.Drawing;
using System.Windows.Forms;
//using Microsoft.VisualBasic;
using System.Collections;
using System;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
// '======================================
// RENDER HANDLER
// COPYRIGHT (C) 2015-2022 J.J.HIRNIAK
// '======================================
namespace Handlers {
public class RenderHandler {
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr GetWindowDC(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
public static extern int ReleaseDC(IntPtr hwnd, IntPtr hdc);
[DllImport("gdi32.dll", SetLastError = true)]
public static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
public static int DPI() {
int DPI_INT = 96;
using (var bmp = new Bitmap(10, 10)) {
var g = Graphics.FromImage(bmp);
DPI_INT = Convert.ToInt32(g.DpiX);
}
return DPI_INT;
}
public static int REN_GetStandardSize(int Multiplier = 1) {
var bmp = new Bitmap(10, 10);
var g = Graphics.FromImage(bmp);
int SZ_INT = 10;
try {
SZ_INT = Convert.ToInt32(g.MeasureString("W", new Font("Courier", 9)).Height * Multiplier);
}
catch {
}
g.Dispose();
return SZ_INT;
}
public static int REN_GetTextSize(string Text, Font Fnt, bool UseUnit = true) {
int SZ_INT = 0;
using (var bmp = new Bitmap(10, 10)) {
var g = Graphics.FromImage(bmp);
try {
if (UseUnit == true)
SZ_INT = Convert.ToInt32(g.MeasureString("W", Fnt).Width);
else
SZ_INT = Convert.ToInt32(g.MeasureString(Text, Fnt).Width);
}
catch {
}
g.Dispose();
}
return SZ_INT;
}
public static Color AlphaChannelInsert(Color Input, int Alpha) {
return Color.FromArgb(Alpha, Input.R, Input.G, Input.B);
}
public static Color DeterministicDarkenColor(Color Input, Color BackColor, int Alpha) {
if (IsDark(BackColor) == true) {
decimal AlphaReduce = Convert.ToDecimal((255 - Alpha) / (double)255);
int AR = Convert.ToInt32(Math.Floor(Input.R * AlphaReduce));
int AG = Convert.ToInt32(Math.Floor(Input.G * AlphaReduce));
int AB = Convert.ToInt32(Math.Floor(Input.B * AlphaReduce));
return Color.FromArgb(AR, AG, AB);
}
else {
int AR = Convert.ToInt32(Math.Floor(Input.R + Alpha * ((255 - Input.R) / (double)255)));
int AG = Convert.ToInt32(Math.Floor(Input.G + Alpha * ((255 - Input.G) / (double)255)));
int AB = Convert.ToInt32(Math.Floor(Input.B + Alpha * ((255 - Input.B) / (double)255)));
return Color.FromArgb(AR, AG, AB);
}
}
public static Color DeterministicDarkenColorInverted(Color Input, Color BackColor, int Alpha) {
if (IsDark(BackColor) == false) {
decimal AlphaReduce = Convert.ToDecimal((255 - Alpha) / (double)255);
int AR = Convert.ToInt32(Math.Floor(Input.R * AlphaReduce));
int AG = Convert.ToInt32(Math.Floor(Input.G * AlphaReduce));
int AB = Convert.ToInt32(Math.Floor(Input.B * AlphaReduce));
return Color.FromArgb(AR, AG, AB);
}
else {
int AR = Convert.ToInt32(Math.Floor(Input.R + Alpha * ((255 - Input.R) / (double)255)));
int AG = Convert.ToInt32(Math.Floor(Input.G + Alpha * ((255 - Input.G) / (double)255)));
int AB = Convert.ToInt32(Math.Floor(Input.B + Alpha * ((255 - Input.B) / (double)255)));
return Color.FromArgb(AR, AG, AB);
}
}
public static Color BlackDarkenColor(Color Input, int Alpha) {
decimal AlphaReduce = Convert.ToDecimal((255 - Alpha) / (double)255);
int AR = Convert.ToInt32(Math.Floor(Input.R * AlphaReduce));
int AG = Convert.ToInt32(Math.Floor(Input.G * AlphaReduce));
int AB = Convert.ToInt32(Math.Floor(Input.B * AlphaReduce));
return Color.FromArgb(AR, AG, AB);
}
public static Color NormaliseDarkenColor(Color Input, int Alpha) {
decimal AlphaReduce = Convert.ToDecimal(Alpha / (double)255);
int AR = Convert.ToInt32(Math.Floor(Input.R * AlphaReduce));
int AG = Convert.ToInt32(Math.Floor(Input.G * AlphaReduce));
int AB = Convert.ToInt32(Math.Floor(Input.B * AlphaReduce));
return Color.FromArgb(AR, AG, AB);
}
public static Color WhiteLightenColor(Color Input, int Alpha) {
int AR = Convert.ToInt32(Math.Floor(Input.R + Alpha * ((255 - Input.R) / (double)255)));
int AG = Convert.ToInt32(Math.Floor(Input.G + Alpha * ((255 - Input.G) / (double)255)));
int AB = Convert.ToInt32(Math.Floor(Input.B + Alpha * ((255 - Input.B) / (double)255)));
return Color.FromArgb(AR, AG, AB);
}
public static bool IsDark(Color InputColor) {
// If InputColor.R <= 128 AndAlso InputColor.G <= 128 AndAlso InputColor.B <= 128 Then 'R(L),G(L),B(L)
// Return True
// ElseIf InputColor.R > 128 AndAlso InputColor.G <= 128 AndAlso InputColor.B <= 128 Then 'R(H),G(L),B(L)
// Return True
// ElseIf InputColor.R <= 128 AndAlso InputColor.G > 128 AndAlso InputColor.B <= 128 Then 'R(L),G(H),B(L)
// Return True
// ElseIf InputColor.R > 128 AndAlso InputColor.G > 128 AndAlso InputColor.B <= 128 Then 'R(H),G(H),B(L)
// Return False
// ElseIf InputColor.R <= 128 AndAlso InputColor.G <= 128 AndAlso InputColor.B > 128 Then 'R(L),G(L),B(H)
// Return True
// ElseIf InputColor.R > 128 AndAlso InputColor.G <= 128 AndAlso InputColor.B > 128 Then 'R(H),G(L),B(H)
// Return False
// ElseIf InputColor.R <= 128 AndAlso InputColor.G > 128 AndAlso InputColor.B > 128 Then 'R(L),G(H),B(H)
// Return False
// ElseIf InputColor.R > 128 AndAlso InputColor.G > 128 AndAlso InputColor.B > 128 Then 'R(H),G(H),B(H)
// Return False
// End If
// Dim IsDarka As Boolean = (InputColor.R <= 128) Or _
// (InputColor.G <= 128) Or _
// (InputColor.B <= 128)
// Return IsDarka
if (ConvertRGBtoHSV(InputColor.R, InputColor.G, InputColor.B).B >= 50)
return false;
else
return true;
}
public static bool IsDarkTheshold(Color InputColor, int Theshold) {
var HSVA = ConvertRGBtoHSV(InputColor.R, InputColor.G, InputColor.B);
if (ConvertRGBtoHSV(InputColor.R, InputColor.G, InputColor.B).B >= Theshold) {
if (HSVA.H >= 0 && HSVA.H <= 20 || HSVA.H > 192 && HSVA.H <= 360) {
if (HSVA.S == 0)
return false;
else
return true;
}
else
return false;
}
else
return true;
}
public static Image InvertImageColors(Image Input, bool ColorCorrect = false, int Hue = 0) {
Bitmap pic = new Bitmap(Input);
for (int y = 0; (y <= (pic.Height - 1)); y++) {
for (int x = 0; (x <= (pic.Width - 1)); x++) {
Color inv = pic.GetPixel(x, y);
inv = Color.FromArgb(inv.A, (255 - inv.R), (255 - inv.G), (255 - inv.B));
if (ColorCorrect == true) {
HSV Col = new HSV(inv);
Col.H = (Col.H + Hue) % 360;
inv = Col.ToColor();
}
pic.SetPixel(x, y, inv);
}
}
return pic;
}
public static Image ChangeImageHUE(Image Input, int Hue) {
try {
int c_h = 0;
int c_s = 0;
int c_b = 0;
if (Hue >= -180 && Hue <= 180)
c_h = Hue;
else
c_h = 0;
if (Input == null)
return null;
Bitmap b = null;
using (var imageAttr = new ImageAttributes()) {
var qm = new Imgx.QColorMatrix1();
qm.RotateHue(c_h);
imageAttr.SetColorMatrix(qm.ToColorMatrix());
b = new Bitmap(Input.Width, Input.Height);
using (var g = Graphics.FromImage(b)) {
var r = new Rectangle(0, 0, Input.Width, Input.Height);
g.DrawImage(Input, r, 0, 0, Input.Width, Input.Height, GraphicsUnit.Pixel, imageAttr);
}
}
if (b != null)
return b;
}
catch {
return null;
}
return null;
}
public static Color HeatMapping(Color ColourLow, Color ColourMiddle, Color ColourHigh, ushort Postion) {
if (Postion >= 256) {
ushort POS_REL = Convert.ToUInt16(Postion - 256);
int ELE_R = Convert.ToInt32((Convert.ToInt32(ColourHigh.R) - Convert.ToInt32(ColourMiddle.R)) / (double)255 * POS_REL + Convert.ToInt32(ColourMiddle.R));
int ELE_G = Convert.ToInt32((Convert.ToInt32(ColourHigh.G) - Convert.ToInt32(ColourMiddle.G)) / (double)255 * POS_REL + Convert.ToInt32(ColourMiddle.G));
int ELE_B = Convert.ToInt32((Convert.ToInt32(ColourHigh.B) - Convert.ToInt32(ColourMiddle.B)) / (double)255 * POS_REL + Convert.ToInt32(ColourMiddle.B));
return Color.FromArgb(ELE_R, ELE_G, ELE_B);
}
else {
int ELE_R = Convert.ToInt32((Convert.ToInt32(ColourMiddle.R) - Convert.ToInt32(ColourLow.R)) / (double)255 * Postion + Convert.ToInt32(ColourLow.R));
int ELE_G = Convert.ToInt32((Convert.ToInt32(ColourMiddle.G) - Convert.ToInt32(ColourLow.G)) / (double)255 * Postion + Convert.ToInt32(ColourLow.G));
int ELE_B = Convert.ToInt32((Convert.ToInt32(ColourMiddle.B) - Convert.ToInt32(ColourLow.B)) / (double)255 * Postion + Convert.ToInt32(ColourLow.B));
return Color.FromArgb(ELE_R, ELE_G, ELE_B);
}
}
public static Image ScaleImage(Image image, Size size, bool preserveAspectRatio = true) {
int newWidth;
int newHeight;
if (preserveAspectRatio) {
int originalWidth = image.Width;
int originalHeight = image.Height;
float percentWidth = Convert.ToSingle(size.Width) / Convert.ToSingle(originalWidth);
float percentHeight = Convert.ToSingle(size.Height) / Convert.ToSingle(originalHeight);
float percent = percentHeight < percentWidth ? percentHeight : percentWidth;
newWidth = Convert.ToInt32(originalWidth * percent);
newHeight = Convert.ToInt32(originalHeight * percent);
}
else {
newWidth = size.Width;
newHeight = size.Height;
}
Image newImage = new Bitmap(newWidth, newHeight);
using (var graphicsHandle = Graphics.FromImage(newImage)) {
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
public static Image GrayScale(Image image) {
try {
Bitmap bitmap;
bitmap = new Bitmap(image);
int x, y;
byte d;
var loopTo = image.Width - 1;
for (x = 0; x <= loopTo; x++) {
var loopTo1 = image.Height - 1;
for (y = 0; y <= loopTo1; y++) {
d = Convert.ToByte(Math.Round(bitmap.GetPixel(x, y).A * 0.299 + bitmap.GetPixel(x, y).R * 0.299 + bitmap.GetPixel(x, y).G * 0.587 + bitmap.GetPixel(x, y).B * 0.114));
bitmap.SetPixel(x, y, Color.FromArgb(d, d, d, d));
}
}
return bitmap;
}
catch {
return null;
}
}
public static Image ColorCorrection(Bitmap image, int Hue, int Saturation, int Brightness) {
try {
int c_h = 0;
int c_s = 0;
int c_b = 0;
if (Hue >= -180 && Hue <= 180)
c_h = Hue;
else
c_h = 0;
if (Saturation >= -100 && Saturation <= 300)
c_s = Saturation;
else
c_s = 0;
if (Brightness >= -100 && Brightness <= 100)
c_b = Brightness;
else
c_b = 0;
if (image == null)
return null;
Bitmap b = null;
using (var imageAttr = new ImageAttributes()) {
var qm = new Imgx.QColorMatrix1();
qm.RotateHue(c_h);
qm.SetSaturation2(c_s / 100.0F);
qm.SetBrightness(c_b / 100.0F);
imageAttr.SetColorMatrix(qm.ToColorMatrix());
b = new Bitmap(image.Width, image.Height);
using (var g = Graphics.FromImage(b)) {
var r = new Rectangle(0, 0, image.Width, image.Height);
g.DrawImage(image, r, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
}
if (b != null)
return b;
}
catch {
return null;
}
return null;
}
public static Image ShearImage(Bitmap image, float red, float green, float blue, ShearEnums Shear_Channel) {
try {
if (image == null)
return null;
Bitmap b = null;
using (var imageAttr = new ImageAttributes()) {
var qm = new Imgx.QColorMatrix1();
if (Shear_Channel == (int)ShearEnums.SHEAR_RED)
qm.ShearRed(green, blue);
else if ((int)Shear_Channel == (int)ShearEnums.SHEAR_GREEN)
qm.ShearGreen(red, blue);
else if ((int)Shear_Channel == (int)ShearEnums.SHEAR_BLUE)
qm.ShearBlue(red, green);
else if ((int)Shear_Channel == (int)ShearEnums.SHEAR_ALL) {
qm.ShearRed(green, blue);
qm.ShearGreen(red, blue);
qm.ShearBlue(red, green);
}
b = new Bitmap(image.Width, image.Height);
using (var g = Graphics.FromImage(b)) {
var r = new Rectangle(0, 0, image.Width, image.Height);
g.DrawImage(image, r, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, imageAttr);
}
}
if (b != null)
return b;
}
catch {
return null;
}
return null;
}
public static HSV ConvertRGBtoHSV(int R, int G, int B) {
// '# Normalize the RGB values by scaling them to be between 0 and 1
decimal red = Convert.ToDecimal(R / (double)255);
decimal green = Convert.ToDecimal(G / (double)255);
decimal blue = Convert.ToDecimal(B / (double)255);
decimal minValue = Math.Min(red, Math.Min(green, blue));
decimal maxValue = Math.Max(red, Math.Max(green, blue));
decimal delta = maxValue - minValue;
var h = default(decimal);
decimal s;
decimal v = maxValue;
// '# Calculate the hue (in degrees of a circle, between 0 and 360)
switch (maxValue) {
case var @case when @case == red: {
if (green >= blue) {
if (delta == 0)
h = 0;
else
h = 60 * (green - blue) / delta;
}
else if (green < blue)
h = 60 * (green - blue) / delta + 360;
break;
}
case var case1 when case1 == green: {
h = 60 * (blue - red) / delta + 120;
break;
}
case var case2 when case2 == blue: {
h = 60 * (red - green) / delta + 240;
break;
}
}
// '# Calculate the saturation (between 0 and 1)
if (maxValue == 0)
s = 0;
else
s = 1M - minValue / maxValue;
// '# Scale the saturation and value to a percentage between 0 and 100
s *= 100;
v *= 100;
// '# Return a color in the new color space
var HSV_VAL = new HSV();
{
var withBlock = HSV_VAL;
withBlock.H = Convert.ToSingle(h);
withBlock.S = Convert.ToSingle(s);
withBlock.B = Convert.ToSingle(v);
}
return HSV_VAL;
}
public static Color GetPixelColor(int x, int y) {
var hdc = GetWindowDC(IntPtr.Zero);
uint pixel = GetPixel(hdc, x, y);
Color color;
ReleaseDC(IntPtr.Zero, hdc);
// MsgBox(pixel)
color = Color.FromArgb(Convert.ToInt32(Convert.ToInt32(pixel & (long)0xFF)), Convert.ToInt32(Convert.ToInt32(pixel & (long)0xFF00) >> 8), Convert.ToInt32(Convert.ToInt32(pixel & (long)0xFF0000) >> 16));
return color;
}
public static Color ConverttHSVtoRGB(int a, double h, double s, double b) {
if (0 > a | 255 < a)
throw new ArgumentOutOfRangeException("a", a, "Invalid Alpha Value");
if (0.0F > h | 360.0F < h)
throw new ArgumentOutOfRangeException("h", h, "Invalid Hue Value");
if (0.0F > s | 1.0F < s)
throw new ArgumentOutOfRangeException("s", s, "Invalid Saturation Value");
if (0.0F > b | 1.0F < b)
throw new ArgumentOutOfRangeException("b", b, "Invalid Brightness Value");
if (0 == s)
return Color.FromArgb(a, int.Parse(Convert.ToString(b * 255)), int.Parse(Convert.ToString(b * 255)), int.Parse(Convert.ToString(b * 255)));
double fMax, fMid, fMin;
int iSextant, iMax, iMid, iMin;
if (0.5 < b) {
fMax = b - b * s + s;
fMin = b + b * s - s;
}
else {
fMax = b + b * s;
fMin = b - b * s;
}
iSextant = int.Parse(Convert.ToString(Math.Floor(h / 60.0F)));
if (300.0F <= h)
h -= 360.0F;
h /= 60.0F;
h -= 2.0F * double.Parse(Convert.ToString(Math.Floor((iSextant + 1.0F) % 6.0F / 2.0F)));
if (0 == iSextant % 2)
fMid = h * (fMax - fMin) + fMin;
else
fMid = fMin - h * (fMax - fMin);
iMax = Convert.ToInt32(fMax * 255);
iMid = Convert.ToInt32(fMid * 255);
iMin = Convert.ToInt32(fMin * 255);
switch (iSextant) {
case 1: {
return Color.FromArgb(a, iMid, iMax, iMin);
}
case 2: {
return Color.FromArgb(a, iMin, iMax, iMid);
}
case 3: {
return Color.FromArgb(a, iMin, iMid, iMax);
}
case 4: {
return Color.FromArgb(a, iMid, iMin, iMax);
}
case 5: {
return Color.FromArgb(a, iMax, iMin, iMid);
}
default: {
return Color.FromArgb(a, iMax, iMid, iMin);
}
}
}
public static Image GausianBlurImage(Image image, bool alphaEdgesOnly, Size blurSize) {
int PixelY;
int PixelX;
var bmp = (Bitmap)image;
var loopTo = bmp.Width - 1;
for (PixelY = 0; PixelY <= loopTo; PixelY++) {
var loopTo1 = bmp.Height - 1;
for (PixelX = 0; PixelX <= loopTo1; PixelX++) {
if (!alphaEdgesOnly)
bmp.SetPixel(PixelX, PixelY, AverageImageColor(image, blurSize, bmp.PhysicalDimension, PixelX, PixelY));
else if (bmp.GetPixel(PixelX, PixelY).A
!= 255)
bmp.SetPixel(PixelX, PixelY, AverageImageColor(image, blurSize, bmp.PhysicalDimension, PixelX, PixelY));
Application.DoEvents();
}
}
return (Image)bmp.Clone();
// bmp.Dispose();
}
private static Color AverageImageColor(Image image, Size Size, SizeF imageSize, int PixelX, int Pixely) {
var pixels = new ArrayList();
int x;
int y;
var bmp = (Bitmap)image;
var loopTo = PixelX + Convert.ToInt32(Size.Width / (double)2);
for (x = PixelX - Convert.ToInt32(Size.Width / (double)2); x <= loopTo; x++) {
var loopTo1 = Pixely + Convert.ToInt32(Size.Height / (double)2);
for (y = Pixely - Convert.ToInt32(Size.Height / (double)2); y <= loopTo1; y++) {
if (x > 0 & x < imageSize.Width & y > 0 & y < imageSize.Height)
pixels.Add(bmp.GetPixel(x, y));
}
}
//Color thisColor = Color.Black;
int alpha = 0;
int red = 0;
int green = 0;
int blue = 0;
foreach (Color thisColor in pixels) {
alpha += (int)thisColor.A;
red += (int)thisColor.R;
green += (int)thisColor.G;
blue += (int)thisColor.B;
}
return Color.FromArgb(Convert.ToInt32(alpha / (double)pixels.Count), Convert.ToInt32(red / (double)pixels.Count), Convert.ToInt32(green / (double)pixels.Count), Convert.ToInt32(blue / (double)pixels.Count));
}
public static Image DrawText(string Text, Font Font, Color ForeColor, Size FrameSize, Point Position) {
var bmp = new Bitmap(FrameSize.Width, FrameSize.Height);
var g = Graphics.FromImage(bmp);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
using (Brush br = new SolidBrush(ForeColor)) {
var stringsize = new SizeF(g.MeasureString(Text, Font));
var centrepos = new Point(Convert.ToInt32(Position.X - stringsize.Width / 2), Convert.ToInt32(Position.Y - stringsize.Height / 2));
g.DrawString(Text, Font, br, centrepos);
}
g.ResetClip();
g.SmoothingMode = SmoothingMode.HighSpeed;
return bmp;
//bmp.Dispose();
//g.Dispose();
}
public static Point CentrePoint(Size FrameSize, CentreEnums Centre) {
int half_x = Convert.ToInt32(FrameSize.Width / (double)2);
int half_y = Convert.ToInt32(FrameSize.Height / (double)2);
if ((int)Centre == (int)CentreEnums.CENTRE_BOTH)
return new Point(half_x, half_y);
else if (Centre == (int)CentreEnums.CENTRE_HORIZONTAL)
return new Point(half_x, FrameSize.Height);
else if ((int)Centre == (int)CentreEnums.CENTRE_VERTICAL)
return new Point(FrameSize.Width, half_y);
return default(Point);
}
public enum ShearEnums {
/// <summary>
/// Shear red Channel
/// </summary>
SHEAR_RED = 0,
/// <summary>
/// Shear green Channel
/// </summary>
SHEAR_GREEN = 1,
/// <summary>
/// Shear blue Channel
/// </summary>
SHEAR_BLUE = 2,
/// <summary>
/// Shear all Channel
/// </summary>
SHEAR_ALL = 3
}
public enum CentreEnums {
/// <summary>
/// Centre Horizontal
/// </summary>
CENTRE_HORIZONTAL = 0,
/// <summary>
/// Centre Vertical
/// </summary>
CENTRE_VERTICAL = 1,
/// <summary>
/// Centre Both
/// </summary>
CENTRE_BOTH = 2
}
}
namespace Imgx {
public class QColorMatrix1 {
private const int MatrixLength = 5;
private float[,] m = new float[5, 5];
private const float rad = 0.0174532925199F;//PI/180
public static float lumR = 0.3086F;
public static float lumG = 0.6094F;
public static float lumB = 0.082F;
private static QColorMatrix1 preHue = new QColorMatrix1();
private static QColorMatrix1 postHue = new QColorMatrix1();
private static bool initialized = false;
public enum MatrixOrder {
MatrixOrderPrepend = 0,
MatrixOrderAppend = 1
}
public QColorMatrix1() {
Reset();
}
public QColorMatrix1(float[,] m) {
if (m == null) {
Reset();
return;
}
Copy(m);
}
public QColorMatrix1(float[][] m) {
FromJaggedMatrix(m);
}
public QColorMatrix1(QColorMatrix1 qm) {
Copy(qm);
}
public QColorMatrix1(ColorMatrix cm) {
FromColorMatrix(cm);
}
public float[,] Matrix {
get {
return m;
}
}
public void FromJaggedMatrix(float[][] m) {
Reset();
if (m == null)
return;
for (int i = 0, loopTo = m.Length - 1; i <= loopTo; i++) {
if (m[i] == null)
throw new ArgumentException();
for (int j = 0, loopTo1 = m[i].Length - 1; j <= loopTo1; j++)
this.m[i, j] = m[i][j];
}
}
public float[][] ToJaggedMatrix() {
var t = new float[5][];
for (int i = 0, loopTo = t.Length - 1; i <= loopTo; i++) {
t[i] = new float[5];
for (int j = 0, loopTo1 = t[i].Length - 1; j <= loopTo1; j++)
t[i][j] = m[i, j];
}
return t;
}
public void FromColorMatrix(ColorMatrix cm) {
if (cm == null) {
Reset();
return;
}
for (int i = 0; i <= MatrixLength - 1; i++) {
for (int j = 0; j <= MatrixLength - 1; j++)
m[i, j] = cm[i, j];
}
}
public ColorMatrix ToColorMatrix() {
var cm = new ColorMatrix();
for (int i = 0; i <= MatrixLength - 1; i++) {
for (int j = 0; j <= MatrixLength - 1; j++)
cm[i, j] = m[i, j];
}
return cm;
}
public void Reset() {
for (int i = 0; i <= MatrixLength - 1; i++) {
for (int j = 0; j <= MatrixLength - 1; j++)
m[i, j] = i == j ? 1.0F : 0.0F;
}
}
public float[] TransformVector(float[] v) {
return TransformVector(v, false);
}
public static float[] Color2Vector(Color c) {
if (c == default(Color))
return null;
var p = new float[4];
p[0] = Convert.ToSingle(c.R);
p[1] = Convert.ToSingle(c.G);
p[2] = Convert.ToSingle(c.B);
p[3] = Convert.ToSingle(c.A);
return p;
}
public static Color Vector2Color(float[] p) {
if (p == null || p.Length < 4)
throw new ArgumentException();
return Color.FromArgb(Convert.ToInt32(Math.Truncate(p[3])), Convert.ToInt32(Math.Truncate(p[0])), Convert.ToInt32(Math.Truncate(p[1])), Convert.ToInt32(Math.Truncate(p[2])));
}
public float[] TransformVector(float[] v, bool normalize) {
if (v == null || v.Length < 4)
throw new ArgumentException();
var temp = new float[4];
for (int x = 0; x <= 3; x++) {
temp[x] = 255.0F * m[4, x];
for (int y = 0; y <= 3; y++)
temp[x] += v[y] * m[y, x];
}
for (int x = 0; x <= 3; x++) {
v[x] = temp[x];
if (normalize) {
if (v[x] < 0)
v[x] = 0.0F;
else if (v[x] > 255.0F)
v[x] = 255.0F;
}
}
return v;
}
public Color[] TransformColors(Color[] colors) {
if (colors == null)
return null;
for (int i = 0, loopTo = colors.Length - 1; i <= loopTo; i++)
colors[i] = Vector2Color(TransformVector(Color2Vector(colors[i]), true));
return colors;
}
public void Multiply(QColorMatrix1 matrix) {
Multiply(matrix, MatrixOrder.MatrixOrderPrepend);
}
public void Multiply(QColorMatrix1 matrix, MatrixOrder order) {
if (matrix == null)
throw new ArgumentException();
float[,] a = null;
float[,] b = null;
if ((int)order == (int)MatrixOrder.MatrixOrderAppend) {
a = matrix.m;
b = m;
}
else {
a = m;
b = matrix.m;
}
var temp = new float[5, 5];
for (int y = 0; y <= MatrixLength - 1; y++) {
for (int x = 0; x <= MatrixLength - 1; x++) {
float t = 0;
for (int i = 0; i <= MatrixLength - 1; i++)
t += b[y, i] * a[i, x];
temp[y, x] = t;
}
}
for (int y = 0; y <= MatrixLength - 1; y++) {
for (int x = 0; x <= MatrixLength - 1; x++)
m[y, x] = temp[y, x];
}
}
public void Scale(float scaleRed, float scaleGreen, float scaleBlue, float scaleOpacity) {
Scale(scaleRed, scaleGreen, scaleBlue, scaleOpacity, MatrixOrder.MatrixOrderPrepend);
}
public void Scale(float scaleRed, float scaleGreen, float scaleBlue, float scaleOpacity, MatrixOrder order) {
var qm = new QColorMatrix1();
qm.m[0, 0] = scaleRed;
qm.m[1, 1] = scaleGreen;
qm.m[2, 2] = scaleBlue;
qm.m[3, 3] = scaleOpacity;
Multiply(qm, order);
}
public void ScaleColors(float scale) {
ScaleColors(scale, MatrixOrder.MatrixOrderPrepend);
}
public void ScaleColors(float scale__1, MatrixOrder order) {
Scale(scale__1, scale__1, scale__1, 1.0F, order);
}
public void ScaleOpacity(float scaleOpacity__1) {
ScaleOpacity(scaleOpacity__1, MatrixOrder.MatrixOrderPrepend);
}
public void ScaleOpacity(float scaleOpacity__1, MatrixOrder order) {
Scale(1.0F, 1.0F, 1.0F, scaleOpacity__1, order);
}
public void Translate(float offsetRed, float offsetGreen, float offsetBlue, float offsetOpacity) {
Translate(offsetRed, offsetGreen, offsetBlue, offsetOpacity, MatrixOrder.MatrixOrderPrepend);
}
public void Translate(float offsetRed, float offsetGreen, float offsetBlue, float offsetOpacity, MatrixOrder order) {
var qm = new QColorMatrix1();
qm.m[4, 0] = offsetRed;
qm.m[4, 1] = offsetGreen;
qm.m[4, 2] = offsetBlue;
qm.m[4, 3] = offsetOpacity;
Multiply(qm, order);
}
public void TranslateColors(float offset) {
TranslateColors(offset, MatrixOrder.MatrixOrderPrepend);
}
public void TranslateColors(float offset, MatrixOrder order) {
Translate(offset, offset, offset, 0.0F, order);
}
public void TranslateOpacity(float offsetOpacity) {
TranslateOpacity(offsetOpacity, MatrixOrder.MatrixOrderPrepend);
}
public void TranslateOpacity(float offsetOpacity, MatrixOrder order) {
Translate(0.0F, 0.0F, 0.0F, offsetOpacity, order);
}
// Rotate the matrix around one of the color axes. The color of the rotation
// axis is unchanged, the other two colors are rotated in color space.
// The angle phi is in degrees (-180.0f... 180.0f).
public void RotateRed(float phi) {
RotateRed(phi, MatrixOrder.MatrixOrderPrepend);
}
public void RotateGreen(float phi) {
RotateGreen(phi, MatrixOrder.MatrixOrderPrepend);
}
public void RotateBlue(float phi) {
RotateBlue(phi, MatrixOrder.MatrixOrderPrepend);
}
public void RotateRed(float phi, MatrixOrder order) {
RotateColor(phi, 2, 1, order);
}
public void RotateGreen(float phi, MatrixOrder order) {
RotateColor(phi, 0, 2, order);
}
public void RotateBlue(float phi, MatrixOrder order) {
RotateColor(phi, 1, 0, order);
}
public void ShearRed(float green, float blue) {
ShearRed(green, blue, MatrixOrder.MatrixOrderPrepend);
}
public void ShearGreen(float red, float blue) {
ShearGreen(red, blue, MatrixOrder.MatrixOrderPrepend);
}
public void ShearBlue(float red, float green) {
ShearBlue(red, green, MatrixOrder.MatrixOrderPrepend);
}
public void ShearRed(float green, float blue, MatrixOrder order) {
ShearColor(0, 1, green, 2, blue, order);
}
public void ShearGreen(float red, float blue, MatrixOrder order) {
ShearColor(1, 0, red, 2, blue, order);
}
public void ShearBlue(float red, float green, MatrixOrder order) {
ShearColor(2, 0, red, 1, green, order);
}
public void SetSaturation(float saturation) {
SetSaturation(saturation, MatrixOrder.MatrixOrderPrepend);
}
public void SetSaturation(float saturation, MatrixOrder order) {
float satCompl = 1.0F - saturation;
float satComplR = lumR * satCompl;
float satComplG = lumG * satCompl;
float satComplB = lumB * satCompl;
var tm = new float[,] { { satComplR + saturation, satComplR, satComplR, 0.0F, 0.0F }, { satComplG, satComplG + saturation, satComplG, 0.0F, 0.0F }, { satComplB, satComplB, satComplB + saturation, 0.0F, 0.0F }, { 0.0F, 0.0F, 0.0F, 1.0F, 0.0F }, { 0.0F, 0.0F, 0.0F, 0.0F, 1.0F } };
var qm = new QColorMatrix1(tm);
Multiply(qm, order);
}
public void RotateHue(float phi) {
InitHue();
Multiply(preHue, MatrixOrder.MatrixOrderAppend);
RotateBlue(phi, MatrixOrder.MatrixOrderAppend);
Multiply(postHue, MatrixOrder.MatrixOrderAppend);
}
public void SetContrast(float scale) {
ScaleColors(scale);
}
public void SetBrightness(float offset) {
TranslateColors(offset, MatrixOrder.MatrixOrderAppend);
}
public void SetSaturation2(float saturation) {
SetSaturation(saturation, MatrixOrder.MatrixOrderAppend);
}
private static void InitHue() {
const float greenRotation = 35.0F;
if (!initialized) {
initialized = true;
preHue.RotateRed(45.0F);
preHue.RotateGreen(-greenRotation, MatrixOrder.MatrixOrderAppend);
var lum = new float[] { lumR, lumG, lumB, 1.0F };
preHue.TransformVector(lum);
float red = lum[0] / lum[2];
float green = lum[1] / lum[2];
preHue.ShearBlue(red, green, MatrixOrder.MatrixOrderAppend);
postHue.ShearBlue(-red, -green);
postHue.RotateGreen(greenRotation, MatrixOrder.MatrixOrderAppend);
postHue.RotateRed(-45.0F, MatrixOrder.MatrixOrderAppend);
}
}
private void RotateColor(float phi, int x, int y, MatrixOrder order) {
phi *= rad;
var qm = new QColorMatrix1();
var argtarget = qm.m[y, y];
qm.m[x, x] = InlineAssignHelper(ref argtarget, Convert.ToSingle(Math.Cos(phi)));
float s = Convert.ToSingle(Math.Sin(phi));
qm.m[y, x] = s;
qm.m[x, y] = -s;
Multiply(qm, order);
}
private void ShearColor(int x, int y1, float d1, int y2, float d2, MatrixOrder order) {
var qm = new QColorMatrix1();
qm.m[y1, x] = d1;
qm.m[y2, x] = d2;
Multiply(qm, order);
}
private void Copy(QColorMatrix1 qm) {
if (qm == null) {
Reset();
return;
}
Copy(qm.m);
}
private void Copy(float[,] m) {
if (m == null || m.Length != this.m.Length)
throw new ArgumentException();
Array.Copy(m, this.m, m.Length);
}
private static T InlineAssignHelper<T>(ref T target, T value) {
target = value;
return value;
}
}
}
public class HSV {
int a = 255;
private float h;
private float s;
private float b;
public int A {
get { return a; }
set {
if (value > 255) { a = 255; }
else if (value < 0) { a = 0; }
else { a = value; }
}
}
public float H {
get { return h; }
set {
if (value > 360.0F) { h = 360.0F; }
else if (value < 0) { h = 0; }
else { h = value; }
}
}
public float S {
get { return s; }
set {
if (value > 1.0F) { s = 1.0F; }
else if (value < 0) { s = 0; }
else { s = value; }
}
}
public float B {
get { return b; }
set {
if (value > 1.0F) { b = 1.0F; }
else if (value < 0) { b = 0; }
else { b = value; }
}
}
public HSV(HSL Input) {
H = Input.H;
b = Input.L + (Input.S * Math.Min(Input.L, 1 - Input.L));
if (b == 0) {
s = 0;
}
else {
S = 2 - (2 * Input.L / b);
}
}
public HSV(Color Input) {
ConvertFromColor(Input);
}
public HSV(float h, float s, float b) {
H = h;
S = s;
B = b;
A = 255;
}
public HSV(int a, float h, float s, float b) {
H = h;
S = s;
B = b;
A = a;
}
public HSV(YDbDr Input) {
ConvertFromColor(Input.ToColor());
}
public HSV(YCoCg Input) {
ConvertFromColor(Input.ToColor());
}
public HSV(YUV Input) {
ConvertFromColor(Input.ToColor());
}
private void ConvertFromColor(Color Input) {
a = Input.A;
float red = (float)(Input.R / (double)255);
float green = (float)(Input.G / (double)255);
float blue = (float)(Input.B / (double)255);
float minValue = Math.Min(red, Math.Min(green, blue));
float maxValue = Math.Max(red, Math.Max(green, blue));
float delta = maxValue - minValue;
h = 0;
b = maxValue;
switch (maxValue) {
case var @case when @case == red: {
if (green >= blue) {
if (delta == 0)
h = 0.0f;
else
h = 60.0f * (green - blue) / delta;
}
else if (green < blue)
h = 60.0f * (green - blue) / delta + 360.0f;
break;
}
case var case1 when case1 == green: {
h = 60.0f * (blue - red) / delta + 120.0f;
break;
}
case var case2 when case2 == blue: {
h = 60.0f * (red - green) / delta + 240.0f;
break;
}
}
if (maxValue == 0) {
s = 0.0f;
}
else {
s = (1.0f - minValue / maxValue);
}
}
public HSV() { }
public Color ToColor() {
int hi = Convert.ToInt32(Math.Floor(h / 60)) % 6;
double f = h / 60 - Math.Floor(h / 60);
int val = 0;
val = (int)(b * 255.0f);
int v = Convert.ToInt32(val);
int p = Convert.ToInt32(val * (1 - s));
int q = Convert.ToInt32(val * (1 - f * s));
int t = Convert.ToInt32(val * (1 - (1 - f) * s));
if (hi == 0)
return Color.FromArgb(a, v, t, p);
else if (hi == 1)
return Color.FromArgb(a, q, v, p);
else if (hi == 2)
return Color.FromArgb(a, p, v, t);
else if (hi == 3)