This repository was archived by the owner on Feb 7, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCITLIBFilters.cs
More file actions
2036 lines (1610 loc) · 53.8 KB
/
CITLIBFilters.cs
File metadata and controls
2036 lines (1610 loc) · 53.8 KB
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
/* CITU DIP METHODS REPOSITORY
COPYRIGHT INTELLIGENT SYSTEMS LAB.
CEBU INSTITUTE OF TECHNOLOGY
THIS SOFTWARE IS FREE AND SUBJECT TO THE GNU LICENSE AGREEMENT
WWW.GETGNU.ORG
CHRIS JORDAN ALIAC
CITBADROBOT@GMAIL.COM
*/
using System.Drawing.Imaging;
using System.Runtime.CompilerServices;
namespace ImageProcess2
{
public class ConvMatrix
{
public int TopLeft = 0, TopMid = 0, TopRight = 0;
public int MidLeft = 0, Pixel = 1, MidRight = 0;
public int BottomLeft = 0, BottomMid = 0, BottomRight = 0;
public int Factor = 1;
public int Offset = 0;
public void SetAll(int nVal)
{
TopLeft = TopMid = TopRight = MidLeft = Pixel = MidRight = BottomLeft = BottomMid = BottomRight = nVal;
}
}
public struct FloatPoint
{
public double X;
public double Y;
}
public class Coins
{
public const float PESO_5 = 5f;
public const float PESO_1 = 1f;
public const float CENT_25 = .25f;
public const float CENT_10 = .10f;
public const float CENT_5 = .05f;
/// <summary>
/// Count the total value and number of coins in the image
/// </summary>
/// <param name="bmp"></param>
/// <param name="countLabel"></param>
/// <param name="valueLabel"></param>
public static void CountCoin(Bitmap bmp, ref Label countLabel, ref Label valueLabel)
{
int count = 0; //Expected 64
float value = 0; //Expected 46.45
int height = bmp.Height;
int width = bmp.Width;
bool[,] visited = new bool[height, width];
for (int i = 0; i < height; i++)
for (int j = 0; j < width; j++)
{
Color pixel = bmp.GetPixel(j, i);
if (!visited[i, j] && pixel.R == 0)
{
count++;
value += Classify(BFS(bmp, ref visited, j, i), ref count);
}
}
countLabel.Text = count.ToString();
valueLabel.Text = value.ToString("F2");
}
/// <summary>
/// Classify what coin given the pixel count
/// </summary>
/// <param name="pixel"></param>
/// <returns></returns>
private static float Classify(int pixel, ref int count)
{
if (pixel >= 18000)
return PESO_5;
else if (pixel >= 15000)
return PESO_1;
else if (pixel >= 11000)
return CENT_25;
else if (pixel >= 8000)
return CENT_10;
else if (pixel >= 6500)
return CENT_5;
count--;
return 0;
}
/// Given a bitmap image in binary format, visited 2d array, initial point(x, y),
/// do BFS and count all pixels
/// </summary>
/// <param name="bmp"></param>
/// <param name="visited"></param>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns>Total pixel count</returns>
private static int BFS(Bitmap bmp, ref bool[,] visited, int x, int y)
{
int height = bmp.Height;
int width = bmp.Width;
int count = 0;
Queue<Point> queue = new();
queue.Enqueue(new Point(x, y));
while (queue.Count > 0)
{
Point point = queue.Dequeue();
int dx = point.X, dy = point.Y;
if (visited[dy, dx])
continue;
count++;
visited[dy, dx] = true;
if (dx - 1 >= 0 && bmp.GetPixel(dx - 1, dy).R == 0 && !visited[dy, dx - 1])
queue.Enqueue(new Point(dx - 1, dy));
if (dx + 1 < width && bmp.GetPixel(dx + 1, dy).R == 0 && !visited[dy, dx + 1])
queue.Enqueue(new Point(dx + 1, dy));
if (dy - 1 >= 0 && bmp.GetPixel(dx, dy - 1).R == 0 && !visited[dy - 1, dx])
queue.Enqueue(new Point(dx, dy - 1));
if (dy + 1 < height && bmp.GetPixel(dx, dy + 1).R == 0 && !visited[dy + 1, dx])
queue.Enqueue(new Point(dx, dy + 1));
}
return count;
}
}
public class BitmapFilter
{
public const short EDGE_DETECT_KIRSH = 1;
public const short EDGE_DETECT_PREWITT = 2;
public const short EDGE_DETECT_SOBEL = 3;
public static bool Invert(Bitmap b)
{
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
int nWidth = b.Width * 3;
int height = b.Height;
for(int y = 0;y < height; ++y)
{
for(int x=0; x < nWidth; ++x )
{
p[0] = (byte)(255-p[0]);
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Binary(Bitmap src, Bitmap dst, int threshold)
{
if (threshold < 0 || threshold > 255)
return false;
int dstHeight = dst.Height;
int dstWidth = dst.Width;
int srcHeight = src.Height;
int srcWidth = src.Width;
BitmapData bmLoaded = src.LockBits(
new Rectangle(0, 0, srcWidth, srcHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dstWidth, dstHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
unsafe
{
int paddingProcessed = bmProcessed.Stride - dstWidth * 3;
int paddingLoaded = bmLoaded.Stride - srcWidth * 3;
byte* pProcessed = (byte*)bmProcessed.Scan0;
byte* pLoaded = (byte*)bmLoaded.Scan0;
for (int i = 0;
i < srcHeight;
i++, pProcessed += paddingProcessed, pLoaded += paddingLoaded)
for (int j = 0;
j < srcWidth;
j++, pProcessed += 3, pLoaded += 3)
pProcessed[0] = pProcessed[1] = pProcessed[2] = (byte)(
(pLoaded[0] + pLoaded[1] + pLoaded[2]) / 3 < threshold ? 0 : 255);
}
src.UnlockBits(bmLoaded);
dst.UnlockBits(bmProcessed);
return true;
}
public static void Copy(Bitmap src, Bitmap dst)
{
int srcHeight = src.Height;
int srcWidth = src.Width;
BitmapData bmLoaded = src.LockBits(
new Rectangle(0, 0, srcWidth, srcHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dst.Width, dst.Height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
int offSet = bmLoaded.Stride - srcWidth * 3; // Padding value
byte* pLoaded = (byte*)bmLoaded.Scan0;
byte* pProcessed = (byte*)bmProcessed.Scan0;
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth; j++)
{
pProcessed[0] = pLoaded[0];
pProcessed[1] = pLoaded[1];
pProcessed[2] = pLoaded[2];
pLoaded += 3;
pProcessed += 3;
}
pLoaded += offSet;
pProcessed += offSet;
}
}
src.UnlockBits(bmLoaded);
dst.UnlockBits(bmProcessed);
}
public static void Scale(Bitmap src, Bitmap dst)
{
int dstHeight = dst.Height;
int dstWidth = dst.Width;
int srcHeight = src.Height;
int srcWidth = src.Width;
BitmapData bmLoaded = src.LockBits(
new Rectangle(0, 0, srcWidth, srcHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dstWidth, dstHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
unsafe
{
int paddingProcessed = bmProcessed.Stride - dstWidth * 3;
int loadedStride = bmLoaded.Stride;
byte* pProcessed = (byte*)bmProcessed.Scan0;
byte* startLoaded = (byte*)bmLoaded.Scan0;
for (int i = 0;
i < dstHeight;
i++, pProcessed += paddingProcessed)
{
for (int j = 0;
j < dstWidth;
j++, pProcessed += 3)
{
byte* pTarget = (startLoaded + i * srcHeight / dstHeight * loadedStride)
+ j * srcWidth / dstWidth * 3;
pProcessed[0] = pTarget[0];
pProcessed[1] = pTarget[1];
pProcessed[2] = pTarget[2];
}
}
}
src.UnlockBits(bmLoaded);
dst.UnlockBits(bmProcessed);
}
public static void Rotate(Bitmap src, Bitmap dst, int degree)
{
int srcHeight = src.Height;
int srcWidth = src.Width;
int dstHeight = dst.Height;
int dstWidth = dst.Width;
BitmapData bmLoaded = src.LockBits(
new Rectangle(0, 0, srcWidth, srcHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dstWidth, dstHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
float radians = degree * (float)Math.PI / 180f;
int centerX = srcWidth / 2;
int centerY = srcHeight / 2;
float cosA = (float)Math.Cos(radians);
float sinA = (float)Math.Sin(radians);
unsafe
{
int paddingProcessed = bmProcessed.Stride - dstWidth * 3;
int srcStride = bmLoaded.Stride;
byte* pProcessed = (byte*)bmProcessed.Scan0;
byte* startLoaded = (byte*)bmLoaded.Scan0;
for (int i = 0;
i < srcHeight;
i++, pProcessed += paddingProcessed)
{
for (int j = 0;
j < srcWidth;
j++, pProcessed += 3)
{
int translatedX = j - centerX;
int translatedY = i - centerY;
int newX = (int)(translatedX * cosA - translatedY * sinA) + centerX;
int newY = (int)(translatedX * sinA + translatedY * cosA) + centerY;
if ( newX >= 0 && newX < srcWidth && newY >= 0 && newY < srcHeight )
{
byte* pTarget = (startLoaded + newY * srcStride) + newX * 3;
pProcessed[0] = pTarget[0];
pProcessed[1] = pTarget[1];
pProcessed[2] = pTarget[2];
}
}
}
}
src.UnlockBits(bmLoaded);
dst.UnlockBits(bmProcessed);
}
public static void Subtract(Bitmap foreground, Bitmap background, Bitmap result)
{
int foregroundHeight = foreground.Height;
int foregroundWidth = foreground.Width;
int backgroundHeight = background.Height;
int backgroundWidth = background.Width;
int resultHeight = result.Height;
int resultWidth = result.Width;
BitmapData bmLoaded = foreground.LockBits(
new Rectangle(0, 0, foregroundWidth, foregroundHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
BitmapData bmProcessed = background.LockBits(
new Rectangle(0, 0, backgroundWidth, backgroundHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
BitmapData bmSubtracted = result.LockBits(
new Rectangle(0, 0, resultWidth, resultHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb
);
int limitAve = 255 / 3;
int threshold = 5;
unsafe
{
int paddingLoaded = bmLoaded.Stride - foregroundWidth * 3;
int paddingProcessed = bmProcessed.Stride - backgroundWidth * 3;
int paddingSubtracted = bmSubtracted.Stride - resultWidth * 3;
int processedStride = bmProcessed.Stride;
byte* pLoaded = (byte*)bmLoaded.Scan0;
byte* pProcessed = (byte*)bmProcessed.Scan0;
byte* pSubtracted = (byte*)bmSubtracted.Scan0;
byte* start_p_processed = (byte*)bmProcessed.Scan0;
int backgroundLimitHeight = background.Height - 1;
int backgroundLimitWidth = background.Width - 1;
for (int i = 0;
i < foregroundHeight;
i++, pLoaded += paddingLoaded, pSubtracted += paddingSubtracted)
{
for (int j = 0;
j < foregroundWidth;
j++, pLoaded += 3, pSubtracted += 3)
{
if (Math.Abs(((pLoaded[0] + pLoaded[1] + pLoaded[2]) / 3) - limitAve) < threshold)
{
pSubtracted[0] = pProcessed[0];
pSubtracted[1] = pProcessed[1];
pSubtracted[2] = pProcessed[2];
}
else
{
pSubtracted[0] = pLoaded[0];
pSubtracted[1] = pLoaded[1];
pSubtracted[2] = pLoaded[2];
}
if (j < backgroundLimitWidth)
pProcessed += 3;
}
if (i < backgroundLimitHeight)
pProcessed = start_p_processed + i * processedStride;
}
}
foreground.UnlockBits(bmLoaded);
background.UnlockBits(bmProcessed);
result.UnlockBits(bmSubtracted);
}
public static void Sepia(Bitmap dst)
{
int dstHeight = dst.Height;
int dstWidth = dst.Width;
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dstWidth, dstHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int offSet = bmProcessed.Stride - dstWidth * 3;
unsafe
{
byte* p = (byte*)(void*)bmProcessed.Scan0;
for (int i = 0; i < dstHeight; i++)
{
for (int j = 0; j < dstWidth; j++)
{
p[0] = (byte)Math.Min(255, p[2] * 0.272 + p[1] * 0.534 + p[0] * 0.131);
p[1] = (byte)Math.Min(255, p[2] * 0.349 + p[1] * 0.686 + p[0] * 0.168);
p[2] = (byte)Math.Min(255, p[2] * 0.393 + p[1] * 0.769 + p[0] * 0.189);
p += 3;
}
p += offSet;
}
}
dst.UnlockBits(bmProcessed);
}
public static void Histogram(Bitmap src, Bitmap dst)
{
int srcHeight = src.Height;
int srcWidth = src.Width;
int dstHeight = dst.Height;
int dstWidth = dst.Width;
BitmapData bmLoaded = src.LockBits(
new Rectangle(0, 0, srcWidth, srcHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmProcessed = dst.LockBits(
new Rectangle(0, 0, dstWidth, dstHeight),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
int[] histData = new int[256];
Array.Fill(histData, 0);
int maxFreq = 420;
int offSet = bmLoaded.Stride - srcWidth * 3; // Padding value
byte* p = (byte*)(void*)bmLoaded.Scan0;
for (int i = 0; i < srcHeight; i++)
{
for (int j = 0; j < srcWidth; j++)
{
int ave = (int)(.299 * p[2] + .587 * p[1] + .114 * p[0]);
histData[ave]++;
if (histData[ave] > maxFreq)
maxFreq = histData[ave];
p += 3;
}
p += offSet;
}
int mFactor = maxFreq / 420;
int count;
int lastRow = (dst.Height - 1) * bmProcessed.Stride;
byte* pointerLast = (byte*)(void*)bmProcessed.Scan0 + lastRow;
int processedStride = bmProcessed.Stride;
for (int i = 0; i < 256; i++)
{
p = pointerLast + i * 3;
count = Math.Min(420, histData[i] / mFactor);
for (int j = 0; j < count; j++)
{
p[0] = p[1] = p[2] = 255;
p -= processedStride;
}
}
}
src.UnlockBits(bmLoaded);
dst.UnlockBits(bmProcessed);
}
public static int[] GetHistArray(Bitmap image)
{
int[] array = new int[256];
Array.Fill(array, 0);
int height = image.Height;
int width = image.Width;
BitmapData bmImage = image.LockBits(
new Rectangle(0, 0, width, height),
ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
unsafe
{
byte * p = (byte*)bmImage.Scan0;
int pad = bmImage.Stride - width * 3;
for (int i = 0; i < height; i++, p += pad)
for (int j = 0; j < width; j++, p += 3)
array[(int)(.299 * p[2] + .587 * p[1] + .114 * p[0])]++;
}
image.UnlockBits(bmImage);
return array;
}
public static bool GrayScale(Bitmap b)
{
int bmHeight = b.Height;
int bmWidth = b.Width;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, bmWidth, bmHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
for(int y = 0; y < bmHeight; ++y)
{
for(int x = 0; x < bmWidth; ++x )
{
p[0] = p[1] = p[2] = (byte)(.299 * p[2] + .587 * p[1] + .114 * p[0]);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Brightness(Bitmap b, int nBrightness)
{
if (nBrightness < -255 || nBrightness > 255)
return false;
int bmHeight = b.Height;
int bmWidth = b.Width;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, bmWidth, bmHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
int nVal = 0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - bmWidth*3;
int nWidth = bmWidth * 3;
for(int y = 0; y < bmHeight; ++y)
{
for(int x = 0; x < nWidth; ++x )
{
nVal = p[0] + nBrightness;
if (nVal < 0) nVal = 0;
if (nVal > 255) nVal = 255;
p[0] = (byte)nVal;
++p;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Contrast(Bitmap b, sbyte nContrast)
{
if (nContrast < -100 || nContrast > 100) return false;
int bmHeight = b.Height;
int bmWidth = b.Width;
double pixel = 0, contrast = (100.0+nContrast)/100.0;
contrast *= contrast;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, bmWidth, bmHeight), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - bmWidth *3;
for(int y = 0 ; y < bmHeight; ++y)
{
for(int x = 0; x < bmWidth; ++x)
{
pixel = p[2] /255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[2] = (byte) pixel;
pixel = p[1] /255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[1] = (byte) pixel;
pixel = p[0] /255.0;
pixel -= 0.5;
pixel *= contrast;
pixel += 0.5;
pixel *= 255;
if (pixel < 0) pixel = 0;
if (pixel > 255) pixel = 255;
p[0] = (byte) pixel;
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Gamma(Bitmap b, double red, double green, double blue)
{
if (red < .2 || red > 5) return false;
if (green < .2 || green > 5) return false;
if (blue < .2 || blue > 5) return false;
byte [] redGamma = new byte [256];
byte [] greenGamma = new byte [256];
byte [] blueGamma = new byte [256];
for (int i = 0; i< 256; ++i)
{
redGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/red)) + 0.5));
greenGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/green)) + 0.5));
blueGamma[i] = (byte)Math.Min(255, (int)(( 255.0 * Math.Pow(i/255.0, 1.0/blue)) + 0.5));
}
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
for(int y=0;y<b.Height;++y)
{
for(int x=0; x < b.Width; ++x )
{
p[2] = redGamma[ p[2] ];
p[1] = greenGamma[ p[1] ];
p[0] = blueGamma[ p[0] ];
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Color(Bitmap b, int red, int green, int blue)
{
if (red < -255 || red > 255) return false;
if (green < -255 || green > 255) return false;
if (blue < -255 || blue > 255) return false;
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
System.IntPtr Scan0 = bmData.Scan0;
unsafe
{
byte * p = (byte *)(void *)Scan0;
int nOffset = stride - b.Width*3;
int nPixel;
for(int y=0;y<b.Height;++y)
{
for(int x=0; x < b.Width; ++x )
{
nPixel = p[2] + red;
nPixel = Math.Max(nPixel, 0);
p[2] = (byte)Math.Min(255, nPixel);
nPixel = p[1] + green;
nPixel = Math.Max(nPixel, 0);
p[1] = (byte)Math.Min(255, nPixel);
nPixel = p[0] + blue;
nPixel = Math.Max(nPixel, 0);
p[0] = (byte)Math.Min(255, nPixel);
p += 3;
}
p += nOffset;
}
}
b.UnlockBits(bmData);
return true;
}
public static bool Conv3x3(Bitmap b, ConvMatrix m)
{
// Avoid divide by zero errors
if (0 == m.Factor) return false;
Bitmap bSrc = (Bitmap)b.Clone();
// GDI+ still lies to us - the return format is BGR, NOT RGB.
BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
BitmapData bmSrc = bSrc.LockBits(new Rectangle(0, 0, bSrc.Width, bSrc.Height), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
int stride = bmData.Stride;
int stride2 = stride * 2;
System.IntPtr Scan0 = bmData.Scan0;
System.IntPtr SrcScan0 = bmSrc.Scan0;
int topLeft = m.TopLeft;
int topMid = m.TopMid;
int topRight = m.TopRight;
int midLeft = m.MidLeft;
int midRight = m.MidRight;
int pixel = m.Pixel;
int bottomLeft = m.BottomLeft;
int bottomMid = m.BottomMid;
int bottomRight = m.BottomRight;
unsafe
{
byte * p = (byte *)(void *)Scan0;
byte * pSrc = (byte *)(void *)SrcScan0;
int nOffset = stride - b.Width*3;
int nWidth = b.Width - 2;
int nHeight = b.Height - 2;
int nPixel;
for(int y=0;y < nHeight;++y)
{
for(int x=0; x < nWidth; ++x )
{
nPixel = ( ( ( (pSrc[2] * topLeft) + (pSrc[5] * topMid) + (pSrc[8] * topRight) +
(pSrc[2 + stride] * midLeft) + (pSrc[5 + stride] * pixel) + (pSrc[8 + stride] * midRight) +
(pSrc[2 + stride2] * bottomLeft) + (pSrc[5 + stride2] * bottomMid) + (pSrc[8 + stride2] * bottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[5 + stride]= (byte)nPixel;
nPixel = ( ( ( (pSrc[1] * topLeft) + (pSrc[4] * topMid) + (pSrc[7] * topRight) +
(pSrc[1 + stride] * midLeft) + (pSrc[4 + stride] * pixel) + (pSrc[7 + stride] * midRight) +
(pSrc[1 + stride2] * bottomLeft) + (pSrc[4 + stride2] * bottomMid) + (pSrc[7 + stride2] * bottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[4 + stride] = (byte)nPixel;
nPixel = ( ( ( (pSrc[0] * topLeft) + (pSrc[3] * topMid) + (pSrc[6] * topRight) +
(pSrc[0 + stride] * midLeft) + (pSrc[3 + stride] * pixel) + (pSrc[6 + stride] * midRight) +
(pSrc[0 + stride2] * bottomLeft) + (pSrc[3 + stride2] * bottomMid) + (pSrc[6 + stride2] * bottomRight)) / m.Factor) + m.Offset);
if (nPixel < 0) nPixel = 0;
if (nPixel > 255) nPixel = 255;
p[3 + stride] = (byte)nPixel;
p += 3;
pSrc += 3;
}
p += nOffset;
pSrc += nOffset;
}
}
b.UnlockBits(bmData);
bSrc.UnlockBits(bmSrc);
return true;
}
public static bool Smooth(Bitmap b, int nWeight = 1)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(1);
m.Pixel = nWeight;
m.Factor = nWeight + 8;
return BitmapFilter.Conv3x3(b, m);
}
public static bool GaussianBlur(Bitmap b, int nWeight = 4)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(1);
m.Pixel = nWeight;
m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 2;
m.Factor = nWeight + 12;
return BitmapFilter.Conv3x3(b, m);
}
public static bool? MeanRemoval(Bitmap b, int nWeight = 9)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(-1);
m.Pixel = nWeight;
m.Factor = nWeight - 8;
return BitmapFilter.Conv3x3(b, m);
}
public static bool Sharpen(Bitmap b, int nWeight = 11)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(0);
m.Pixel = nWeight;
m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = -2;
m.Factor = nWeight - 8;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossLaplacian(Bitmap b)
{
ConvMatrix m = new ConvMatrix();
m.SetAll(-1);
m.TopMid = m.MidLeft = m.MidRight = m.BottomMid = 0;
m.Pixel = 4;
m.Offset = 127;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossHorzVertical(Bitmap b)
{
ConvMatrix m = new ConvMatrix();
m.TopLeft = m.TopRight = m.BottomLeft = m.BottomRight = 0;
m.TopMid = m.MidLeft = m.MidRight = -1;
m.Pixel = 4;
m.Offset = 127;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossAllDirections(Bitmap b)
{
ConvMatrix m = new();
m.SetAll(-1);
m.Pixel = 8;
m.Offset = 127;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossLossy(Bitmap b)
{
ConvMatrix m = new();
m.TopLeft = m.TopRight = m.BottomMid = 1;
m.TopMid = m.MidLeft = m.MidRight = m.BottomLeft = m.BottomRight = -2;
m.Pixel = 4;
m.Offset = 127;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossHorizontal(Bitmap b)
{
ConvMatrix m = new();
m.SetAll(0);
m.MidLeft = m.MidRight = -1;
m.Pixel = 2;
m.Offset = 127;
return BitmapFilter.Conv3x3(b, m);
}
public static bool EmbossVertical(Bitmap b)
{
ConvMatrix m = new();
m.SetAll(0);
m.TopMid = -1;
m.BottomMid = 1;
m.Offset = 127;