-
Notifications
You must be signed in to change notification settings - Fork 0
/
CsPotrace.cs
2339 lines (2106 loc) · 53.8 KB
/
CsPotrace.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
/*
Copyright (C) 2001-2016 Peter Selinger
Copyright (C) 2009-2016 Wolfgang Nagl
Copyright (C) 2017 Dileep Miriyala :(Reorganised)
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;
namespace CsPotrace
{
/// <summary>
///
/// </summary>
struct ViewBox
{
public decimal MinX;
public decimal MinY;
public decimal Width;
public decimal Height;
}
/// <summary>
/// Kind of Curve : Line or Bezier
/// </summary>
enum CurveKind
{
Line,
Bezier
}
/// <summary>
/// Curve tags
/// </summary>
enum Tags
{
Corner = 1,
CurveTo = 2
}
/// <summary>
/// Holds the rounded coordinates of a Point
/// </summary>
struct Point
{
/// <summary>
/// ctor
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public Point(int x, int y)
{
this.X = x;
this.Y = y;
}
public int X;
public int Y;
}
/// <summary>
/// Holds the precision coordinates of a Point
/// </summary>
class dPoint
{
/// <summary>
/// x-coordinate
/// </summary>
public double x;
/// <summary>
/// y-coordinate
/// </summary>
public double y;
/// <summary>
/// Creates a point
/// </summary>
/// <param name="x">x-coordinate</param>
/// <param name="y">y-coordinate</param>
public dPoint(double x, double y)
{
this.x = x;
this.y = y;
}
public dPoint Copy()
{
return new dPoint(x, y);
}
/// <summary>
/// ctor
/// </summary>
public dPoint()
{
}
}
/// <summary>
/// Holds the information about der produced curves
/// </summary>
struct Curve
{
/// <summary>
/// Bezier or Line
/// </summary>
public CurveKind Kind;
/// <summary>
/// Startpoint
/// </summary>
public dPoint A;
/// <summary>
/// ControlPoint
/// </summary>
public dPoint ControlPointA;
/// <summary>
/// ControlPoint
/// </summary>
public dPoint ControlPointB;
/// <summary>
/// Endpoint
/// </summary>
public dPoint B;
/// <summary>
/// Creates a curve
/// </summary>
/// <param name="Kind"></param>
/// <param name="A">Startpoint</param>
/// <param name="ControlPointA">Controlpoint</param>
/// <param name="ControlPointB">Controlpoint</param>
/// <param name="B">Endpoint</param>
public Curve(CurveKind Kind, dPoint A, dPoint ControlPointA, dPoint ControlPointB, dPoint B)
{
this.Kind = Kind;
this.A = A;
this.B = B;
this.ControlPointA = ControlPointA;
this.ControlPointB = ControlPointB;
}
public override string ToString()
{
return this.Kind.ToString();
}
}
/// <summary>
/// Turn Policy : Line or Bezier
/// </summary>
enum TurnPolicy
{
Minority,
Majority,
Right,
Black,
White
}
/// <summary>
/// Quad
/// </summary>
class Quad
{
/// <summary>
/// ctor
/// </summary>
public Quad()
{
}
/// <summary>
///
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public double At(int x, int y)
{
return this.Data[x * 3 + y];
}
/// <summary>
///
/// </summary>
public double[] Data = new double[9];
}
/// <summary>
/// Holds the binary bitmap
/// </summary>
class BinaryMatrix
{
public byte[] data = null;
public BinaryMatrix(int w, int h)
{
this.w = w;
this.h = h;
data = new byte[Size];
}
public int w = 0;
public int h = 0;
public int Size
{
get
{
return w * h;
}
}
public bool At(int x, int y)
{
return ((x >= 0) && (x < this.w) && (y >= 0) && (y < this.h) && (this.data[this.w * y + x] == 1));
}
public Point Index(int i)
{
int y = i / w;
return new Point(i - y * w, y);
}
public void Flip(int x, int y)
{
if (this.At(x, y))
{
this.data[this.w * y + x] = 0;
}
else
{
this.data[this.w * y + x] = 1;
}
}
public BinaryMatrix Copy()
{
BinaryMatrix Result = new BinaryMatrix(w, h);
for (int i = 0; i < Size; i++)
{
Result.data[i] = data[i];
}
return Result;
}
}
/// <summary>
/// Optimization Penalty
/// </summary>
class Optimization
{
public double Pen = 0;
public dPoint[] C = new dPoint[2];
public double T = 0;
public double S = 0;
public double Alpha = 0;
}
/// <summary>
/// Path
/// </summary>
class Path
{
public int m = 0;
public int Area = 0;
public int Length = 0;
public string Sign = "?";
public List<Point> Points = new List<Point>();
public int minX = 100000;
public int minY = 100000;
public int maxX = -1;
public int maxY = -1;
public double x0;
public double y0;
public int[] po;
public int[] lon = null;
public List<Sum> Sums = new List<Sum>();
public PrimitiveCurve Curve = null;
public override string ToString()
{
return Sign;
}
}
/// <summary>
/// Primitive Curve
/// </summary>
class PrimitiveCurve
{
/// <summary>
/// Number of segments
/// </summary>
public int N;
/// <summary>
/// Tag[n]: POTRACE_CORNER or POTRACE_CURVETO
/// </summary>
public Tags[] Tag;
/// <summary>
/// For POTRACE_CORNER, this equals c[1]
/// </summary>
public dPoint[] Vertex;
/// <summary>
/// Help Point
/// </summary>
public dPoint[] C = null;
/// <summary>
/// Applicable for POTRACE_CURVETO
/// </summary>
public double[] Alpha;
/// <summary>
/// Uncropped alpha parameter
/// </summary>
public double[] Alpha0;
/// <summary>
/// Beta
/// </summary>
public double[] Beta;
/// <summary>
/// Alpha Curve
/// </summary>
public int AlphaCurve = 0;
public PrimitiveCurve(int Count)
{
N = Count;
Tag = new Tags[N];
Vertex = new dPoint[N];
Alpha = new double[N];
Alpha0 = new double[N];
Beta = new double[N];
C = new dPoint[N * 3];
}
}
struct Sum
{
public Sum(double x, double y, double xy, double x2, double y2)
{
this.x = x;
this.y = y;
this.xy = xy;
this.x2 = x2;
this.y2 = y2;
}
public double x, y, xy, x2, y2;
}
static class MathUtil
{
/// <summary>
/// Range over the straight line segment [a,b] when lambda ranges over [0,1]
/// </summary>
/// <param name="lambda"></param>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static dPoint Interval(double lambda, dPoint a, dPoint b)
{
var res = new dPoint();
res.x = a.x + lambda * (b.x - a.x);
res.y = a.y + lambda * (b.y - a.y);
return res;
}
/// <summary>
/// Direction that is 90 degrees counterclockwise from p2-p0 But then restricted to one of the major wind directions (n, nw, w, etc)
/// </summary>
/// <param name="p0"></param>
/// <param name="p2"></param>
/// <returns>Direction that is 90 degrees counterclockwise from p2-p0</returns>
public static dPoint dorth_infty(dPoint p0, dPoint p2)
{
var r = new dPoint();
r.y = MathUtil.Sign(p2.x - p0.x);
r.x = -MathUtil.Sign(p2.y - p0.y);
return r;
}
/// <summary>
/// ddenom/dpara have the property that the square of radius 1 centered
/// at p1 intersects the line p0p2 iff |MathUtil.dpara(p0,p1,p2)| <= MathUtil.ddenom(p0,p2)
/// </summary>
/// <param name="p0"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static double ddenom(dPoint p0, dPoint p2)
{
var r = dorth_infty(p0, p2);
return r.y * (p2.x - p0.x) - r.x * (p2.y - p0.y);
}
/// <summary>
/// Area of the parallelogram
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns>(p1-p0)x(p2-p0), the area of the parallelogram </returns>
public static double dpara(dPoint p0, dPoint p1, dPoint p2)
{
double x1, y1, x2, y2;
x1 = p1.x - p0.x;
y1 = p1.y - p0.y;
x2 = p2.x - p0.x;
y2 = p2.y - p0.y;
return x1 * y2 - x2 * y1;
}
/// <summary>
/// Calculates (p1-p0)x(p3-p2)
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <returns></returns>
public static double cprod(dPoint p0, dPoint p1, dPoint p2, dPoint p3)
{
double x1, y1, x2, y2;
x1 = p1.x - p0.x;
y1 = p1.y - p0.y;
x2 = p3.x - p2.x;
y2 = p3.y - p2.y;
return x1 * y2 - x2 * y1;
}
/// <summary>
/// Calculates (p1-p0)*(p2-p0
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static double iprod(dPoint p0, dPoint p1, dPoint p2)
{
double x1, y1, x2, y2;
x1 = p1.x - p0.x;
y1 = p1.y - p0.y;
x2 = p2.x - p0.x;
y2 = p2.y - p0.y;
return x1 * x2 + y1 * y2;
}
/// <summary>
/// Calculates (p1-p0)*(p3-p2)
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <returns></returns>
public static double iprod(dPoint p0, dPoint p1, dPoint p2, dPoint p3)
{
double x1, y1, x2, y2;
x1 = p1.x - p0.x;
y1 = p1.y - p0.y;
x2 = p3.x - p2.x;
y2 = p3.y - p2.y;
return x1 * x2 + y1 * y2;
}
/// <summary>
/// Calculate distance between two points
/// </summary>
/// <param name="p"></param>
/// <param name="q"></param>
/// <returns></returns>
public static double Distance(dPoint p, dPoint q)
{
return Math.Sqrt((p.x - q.x) * (p.x - q.x) + (p.y - q.y) * (p.y - q.y));
}
/// <summary>
/// Calculates p1 x p2
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static double Product(dPoint p1, dPoint p2)
{
return p1.x * p2.y - p1.y * p2.x;
}
/// <summary>
/// Calculates p1 x p2
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static int Product(Point p1, Point p2)
{
return p1.X * p2.Y - p1.Y * p2.X;
}
/// <summary>
/// return 1 if a <= b < c < a, in a cyclic sense (MathUtil.mod n)
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <param name="c"></param>
/// <returns></returns>
public static bool Cyclic(double a, double b, double c)
{
if (a <= c)
{
return (a <= b && b < c);
}
else
{
return (a <= b || b < c);
}
}
/// <summary>
/// Sign
/// </summary>
/// <param name="i"></param>
/// <returns></returns>
public static int Sign(double i)
{
return i > 0 ? 1 : i < 0 ? -1 : 0;
}
/// <summary>
/// Apply quadratic form Q to vector w = (w.x,w.y)
/// </summary>
/// <param name="Q"></param>
/// <param name="w"></param>
/// <returns></returns>
public static double quadform(Quad Q, dPoint w)
{
double sum = 0;
double[] v = new double[3];
v[0] = w.x;
v[1] = w.y;
v[2] = 1;
sum = 0.0;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
sum += v[i] * Q.At(i, j) * v[j];
}
}
return sum;
}
/// <summary>
/// Calculates point of a bezier curve
/// </summary>
/// <param name="t"></param>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <returns></returns>
public static dPoint Bezier(double t, dPoint p0, dPoint p1, dPoint p2, dPoint p3)
{
double s = 1 - t;
dPoint res = new dPoint();
/*
* Note:
* A good optimizing compiler (such as gcc-3) reduces the
* following to 16 multiplications, using common subexpression elimination.
*/
res.x = s * s * s * p0.x + 3 * (s * s * t) * p1.x + 3 * (t * t * s) * p2.x + t * t * t * p3.x;
res.y = s * s * s * p0.y + 3 * (s * s * t) * p1.y + 3 * (t * t * s) * p2.y + t * t * t * p3.y;
return res;
}
/// <summary>
/// Calculates the point t in [0..1] on the (convex) bezier curve
/// (p0,p1,p2,p3) which is MathUtil.tangent to q1-q0. Return -1.0 if there is no solution in [0..1].
/// </summary>
/// <param name="p0"></param>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <param name="p3"></param>
/// <param name="q0"></param>
/// <param name="q1"></param>
/// <returns></returns>
public static double Tangent(dPoint p0, dPoint p1, dPoint p2, dPoint p3, dPoint q0, dPoint q1)
{
double A, B, C; /* (1-t)^2 A + 2(1-t)t B + t^2 C = 0 */
double a, b, c; /* a t^2 + b t + c = 0 */
double d, s, r1, r2;
A = MathUtil.cprod(p0, p1, q0, q1);
B = MathUtil.cprod(p1, p2, q0, q1);
C = MathUtil.cprod(p2, p3, q0, q1);
a = A - 2 * B + C;
b = -2 * A + 2 * B;
c = A;
d = b * b - 4 * a * c;
if (a == 0 || d < 0)
{
return -1.0;
}
s = Math.Sqrt(d);
r1 = (-b + s) / (2 * a);
r2 = (-b - s) / (2 * a);
if (r1 >= 0 && r1 <= 1)
{
return r1;
}
else if (r2 >= 0 && r2 <= 1)
{
return r2;
}
else
{
return -1.0;
}
}
/// <summary>
/// Determine the center and slope of the line i..j. Assume i<j. Needs "sum" components of p to be set
/// </summary>
/// <param name="path"></param>
/// <param name="i"></param>
/// <param name="j"></param>
/// <param name="ctr"></param>
/// <param name="dir"></param>
public static void Slope(Path path, int i, int j, dPoint ctr, dPoint dir)
{
int n = path.Length;
List<Sum> sums = path.Sums;
double x, y, x2, xy, y2;
double a, b, c, lambda2, l;
int k = 0;
/* assume i<j */
int r = 0; /* rotations from i to j */
while (j >= n)
{
j -= n;
r += 1;
}
while (i >= n)
{
i -= n;
r -= 1;
}
while (j < 0)
{
j += n;
r -= 1;
}
while (i < 0)
{
i += n;
r += 1;
}
x = sums[j + 1].x - sums[i].x + r * sums[n].x;
y = sums[j + 1].y - sums[i].y + r * sums[n].y;
x2 = sums[j + 1].x2 - sums[i].x2 + r * sums[n].x2;
xy = sums[j + 1].xy - sums[i].xy + r * sums[n].xy;
y2 = sums[j + 1].y2 - sums[i].y2 + r * sums[n].y2;
k = j + 1 - i + r * n;
ctr.x = x / k;
ctr.y = y / k;
a = (x2 - x * x / k) / k;
b = (xy - x * y / k) / k;
c = (y2 - y * y / k) / k;
lambda2 = (a + c + Math.Sqrt((a - c) * (a - c) + 4 * b * b)) / 2; /* larger e.value */
a -= lambda2;
c -= lambda2;
if (Math.Abs(a) >= Math.Abs(c))
{
l = Math.Sqrt(a * a + b * b);
if (l != 0)
{
dir.x = -b / l;
dir.y = a / l;
}
}
else
{
l = Math.Sqrt(c * c + b * b);
if (l != 0)
{
dir.x = -c / l;
dir.y = b / l;
}
}
if (l == 0)
{
dir.x = dir.y = 0; /* sometimes this can happen when k=4:
the two eigenvalues coincide */
}
}
/// <summary>
/// Absolute of the given value
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static int Abs(int a)
{
return ((a) > 0 ? (a) : -(a));
}
/// <summary>
/// Minimum of the given values
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int Min(int a, int b)
{
return ((a) < (b) ? (a) : (b));
}
/// <summary>
/// Maximum of the given values
/// </summary>
/// <param name="a"></param>
/// <param name="b"></param>
/// <returns></returns>
public static int Max(int a, int b)
{
return ((a) > (b) ? (a) : (b));
}
/// <summary>
/// Square
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static int Square(int a)
{
return ((a) * (a));
}
/// <summary>
/// Cube
/// </summary>
/// <param name="a"></param>
/// <returns></returns>
public static int Cube(int a)
{
return ((a) * (a) * (a));
}
/// <summary>
/// Mod
/// </summary>
/// <param name="a"></param>
/// <param name="n"></param>
/// <returns></returns>
public static int Mod(int a, int n)
{
return a >= n ? a % n : a >= 0 ? a : n - 1 - (-1 - a) % n;
}
/// <summary>
/// Floor Division
/// </summary>
/// <param name="a"></param>
/// <param name="n"></param>
/// <returns></returns>
public static int FloorDivision(int a, int n)
{
return a >= 0 ? a / n : -1 - (-1 - a) / n;
}
/// <summary>
///
/// </summary>
/// <param name="p1"></param>
/// <param name="p2"></param>
/// <returns></returns>
public static double Slope(dPoint p1, dPoint p2)
{
return (p2.y - p1.y) / (p2.x - p1.x);
}
}
class Configurations
{
/// <summary>
/// Area of largest path to be ignored
/// </summary>
public int TurdSize = 2;
/// <summary>
/// Corner threshold
/// </summary>
public double AlphaMax = 1.0;
/// <summary>
/// Use curve optimization
/// optimize the path p, replacing sequences of Bezier segments by a
/// single segment when possible.
/// </summary>
public bool CurveOptimizing = true;
/// <summary>
/// Curve optimization tolerance
/// </summary>
public double OptTolerance = 0.2;
/// <summary>
/// Threshold
/// </summary>
public double Treshold = 0.5;
/// <summary>
/// Turn Policy
/// </summary>
public TurnPolicy TurnPolicy = TurnPolicy.Minority;
/// <summary>
/// Rotate directions: Left to Right
/// </summary>
public bool Clockwise = true;
}
internal class Potrace
{
/// <summary>
/// Produces a binary Matrix with Dimensions
/// For the threshold, we take the sum of weighted R,g,b value. The sum of weights must be 1.
/// The result fills the field bm;
/// </summary>
/// <param name="bitmap"> A Bitmap, which will be transformed to a binary Matrix</param>
/// <returns>Returns a binary boolean Matrix </returns>
static BinaryMatrix ConvertBitmap(Bitmap bitmap, double Treshold)
{
byte[] Result = new byte[bitmap.Width * bitmap.Height];
BitmapData SourceData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
unsafe
{
byte* SourcePtr = (byte*)(void*)SourceData.Scan0;
int l = Result.Length;
for (int i = 0; i < l; i++)
{
// if ((0.2126 * (double)SourcePtr[4 * i + 2] + 0.7153 * (double)SourcePtr[4 * i + 1] + 0.0721 * (double)SourcePtr[4 * i]) < Treshold*255)
if (((double)SourcePtr[4 * i + 2] + (double)SourcePtr[4 * i + 1] + (double)SourcePtr[4 * i]) < Treshold * 255 * 3)
Result[i] = 1;
else
Result[i] = 0;
}
}
bitmap.UnlockBits(SourceData);
BinaryMatrix bm = null;
bm = new BinaryMatrix(bitmap.Width, bitmap.Height);
bm.data = Result;
return bm;
}
/// <summary>
/// Compute a path in the binary matrix.
/// Start path at the point (x0,x1), which must be an upper left corner
/// of the path. Also compute the area enclosed by the path. Return a
/// new path_t object, or NULL on error (note that a legitimate path
/// cannot have length 0).
/// </summary>
/// <param name="copy"></param>
/// <param name="point"></param>
/// <returns></returns>
static Path FindPath(BinaryMatrix original, BinaryMatrix copy, Point point, TurnPolicy turnPolicy)
{
Path path = new Path();
int x = point.X;
int y = point.Y;
int dirx = 0;
int diry = 1;
int tmp = -1;
path.Sign = original.At(point.X, point.Y) ? "+" : "-";
while (true)
{
path.Points.Add(new Point(x, y));
if (x > path.maxX)
path.maxX = x;
if (x < path.minX)
path.minX = x;
if (y > path.maxY)
path.maxY = y;
if (y < path.minY)
path.minY = y;
path.Length++;
x += dirx;
y += diry;
path.Area -= x * diry;
if (x == point.X && y == point.Y)
break;
var l = copy.At(x + (dirx + diry - 1) / 2, y + (diry - dirx - 1) / 2);
var r = copy.At(x + (dirx - diry - 1) / 2, y + (diry + dirx - 1) / 2);
if (r && !l)
{
if ((turnPolicy == TurnPolicy.Right) ||
(((turnPolicy == TurnPolicy.Black) && (path.Sign == "+"))) ||
(((turnPolicy == TurnPolicy.White) && (path.Sign == "-"))) ||
(((turnPolicy == TurnPolicy.Majority) && (Majority(copy, x, y)))) ||
((turnPolicy == TurnPolicy.Minority && !Majority(copy, x, y))))
{
tmp = dirx;
dirx = -diry;
diry = tmp;
}
else
{
tmp = dirx;
dirx = diry;
diry = -tmp;
}
}
else if (r)
{
tmp = dirx;
dirx = -diry;
diry = tmp;
}
else if (!l)
{
tmp = dirx;
dirx = diry;
diry = -tmp;
}
}
return path;
}
/// <summary>
/// Searches a x and a y such that source[x,y] = 1 and source[x+1,y] 0.
/// If this not exists, false will be returned else the result is true.
/// </summary>
/// <param name="bm"></param>
/// <param name="point"></param>
/// <param name="result"></param>
/// <returns></returns>
static bool FindNext(BinaryMatrix bm, Point point, ref Point result)