-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathEncodingTests.cs
More file actions
691 lines (607 loc) · 23.5 KB
/
EncodingTests.cs
File metadata and controls
691 lines (607 loc) · 23.5 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
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SkiaSharp;
using System;
using System.Collections.Generic;
using Waher.Content.Images;
using Waher.Content.QR.Encoding;
using Waher.Runtime.Console;
namespace Waher.Content.QR.Test
{
[TestClass]
public class EncodingTests
{
// Thanks to https://www.thonky.com/qr-code-tutorial/introduction
// for tutorial and test vectors.
private QrEncoder encoder;
[TestInitialize]
public void TestInitialize()
{
this.encoder = new QrEncoder();
}
[TestCleanup]
public void TestCleanup()
{
this.encoder?.Dispose();
this.encoder = null;
}
[TestMethod]
public void Test_01_GF256_Pow2()
{
byte[] Expected =
[
1,2,4,8,16,32,64,128,29,58,116,232,205,135,19,38,76,152,45,90,
180,117,234,201,143,3,6,12,24,48,96,192,157,39,78,156,37,74,
148,53,106,212,181,119,238,193,159,35,70,140,5,10,20,40,80,160,
93,186,105,210,185,111,222,161,95,190,97,194,153,47,94,188,101,
202,137,15,30,60,120,240,253,231,211,187,107,214,177,127,254,
225,223,163,91,182,113,226,217,175,67,134,17,34,68,136,13,26,
52,104,208,189,103,206,129,31,62,124,248,237,199,147,59,118,
236,197,151,51,102,204,133,23,46,92,184,109,218,169,79,158,33,
66,132,21,42,84,168,77,154,41,82,164,85,170,73,146,57,114,228,
213,183,115,230,209,191,99,198,145,63,126,252,229,215,179,123,
246,241,255,227,219,171,75,150,49,98,196,149,55,110,220,165,87,
174,65,130,25,50,100,200,141,7,14,28,56,112,224,221,167,83,166,
81,162,89,178,121,242,249,239,195,155,43,86,172,69,138,9,18,36,
72,144,61,122,244,245,247,243,251,235,203,139,11,22,44,88,176,
125,250,233,207,131,27,54,108,216,173,71,142,1
];
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(GF256.PowerOf2Table));
}
[TestMethod]
public void Test_02_GF256_Log2()
{
byte[] Expected =
[
0,0,1,25,2,50,26,198,3,223,51,238,27,104,199,75,
4,100,224,14,52,141,239,129,28,193,105,248,200,8,76,113,
5,138,101,47,225,36,15,33,53,147,142,218,240,18,130,69,
29,181,194,125,106,39,249,185,201,154,9,120,77,228,114,166,
6,191,139,98,102,221,48,253,226,152,37,179,16,145,34,136,
54,208,148,206,143,150,219,189,241,210,19,92,131,56,70,64,
30,66,182,163,195,72,126,110,107,58,40,84,250,133,186,61,
202,94,155,159,10,21,121,43,78,212,229,172,115,243,167,87,
7,112,192,247,140,128,99,13,103,74,222,237,49,197,254,24,
227,165,153,119,38,184,180,124,17,68,146,217,35,32,137,46,
55,63,209,91,149,188,207,205,144,135,151,178,220,252,190,97,
242,86,211,171,20,42,93,158,132,60,57,83,71,109,65,162,
31,45,67,216,183,123,164,118,196,23,73,236,127,12,111,246,
108,161,59,82,41,157,85,170,251,96,134,177,187,204,62,90,
203,89,95,176,156,169,160,81,11,245,22,235,122,117,44,215,
79,174,213,233,230,231,173,232,116,214,244,234,168,80,88,175
];
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(GF256.Log2Table));
}
[TestMethod]
public void Test_03_GF256Px_1()
{
byte[] Expected = [1, 1];
ReedSolomonEC EC = new(1);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(EC.GeneratorPolynomial.Coefficients));
}
[TestMethod]
public void Test_04_GF256Px_2()
{
byte[] Expected = [1, 3, 2];
ReedSolomonEC EC = new(2);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(EC.GeneratorPolynomial.Coefficients));
}
[TestMethod]
public void Test_05_GF256Px_3()
{
byte[] Expected = [1, 7, 14, 8];
ReedSolomonEC EC = new(3);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(EC.GeneratorPolynomial.Coefficients));
}
[TestMethod]
public void Test_06_GF256Px_7()
{
byte[] Expected =
[
GF256.PowerOf2Table[0],
GF256.PowerOf2Table[87],
GF256.PowerOf2Table[229],
GF256.PowerOf2Table[146],
GF256.PowerOf2Table[149],
GF256.PowerOf2Table[238],
GF256.PowerOf2Table[102],
GF256.PowerOf2Table[21]
];
ReedSolomonEC EC = new(7);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(EC.GeneratorPolynomial.Coefficients));
}
[TestMethod]
public void Test_07_GF256Px_30()
{
byte[] Expected =
[
GF256.PowerOf2Table[0],
GF256.PowerOf2Table[41],
GF256.PowerOf2Table[173],
GF256.PowerOf2Table[145],
GF256.PowerOf2Table[152],
GF256.PowerOf2Table[216],
GF256.PowerOf2Table[31],
GF256.PowerOf2Table[179],
GF256.PowerOf2Table[182],
GF256.PowerOf2Table[50],
GF256.PowerOf2Table[48],
GF256.PowerOf2Table[110],
GF256.PowerOf2Table[86],
GF256.PowerOf2Table[239],
GF256.PowerOf2Table[96],
GF256.PowerOf2Table[222],
GF256.PowerOf2Table[125],
GF256.PowerOf2Table[42],
GF256.PowerOf2Table[173],
GF256.PowerOf2Table[226],
GF256.PowerOf2Table[193],
GF256.PowerOf2Table[224],
GF256.PowerOf2Table[130],
GF256.PowerOf2Table[156],
GF256.PowerOf2Table[37],
GF256.PowerOf2Table[251],
GF256.PowerOf2Table[216],
GF256.PowerOf2Table[238],
GF256.PowerOf2Table[40],
GF256.PowerOf2Table[192],
GF256.PowerOf2Table[180]
];
ReedSolomonEC EC = new(30);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(EC.GeneratorPolynomial.Coefficients));
}
[TestMethod]
public void Test_08_EC_Code()
{
ReedSolomonEC EC = new(10);
byte[] Code = EC.GenerateCorrectionCode([32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17]);
byte[] Expected = [196, 35, 39, 119, 235, 215, 231, 226, 93, 23];
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(Code));
}
[TestMethod]
public void Test_09_TotalDataBytes_H()
{
int[] DataBytes =
[
9, 16, 26, 36, 46, 60, 66, 86, 100, 122, 140, 158, 180, 197,
223, 253, 283, 313, 341, 385, 406, 442, 464, 514, 538, 596,
628, 661, 701, 745, 793, 845, 901, 961, 986, 1054, 1096, 1142,
1222, 1276
];
int i = 0;
foreach (VersionInfo Info in Versions.HighVersions)
Assert.AreEqual(DataBytes[i++], Info.TotalDataBytes);
}
[TestMethod]
public void Test_10_TotalDataBytes_L()
{
int[] DataBytes =
[
19, 34, 55, 80, 108, 136, 156, 194, 232, 274, 324, 370, 428,
461, 523, 589, 647, 721, 795, 861, 932, 1006, 1094, 1174,
1276, 1370, 1468, 1531, 1631, 1735, 1843, 1955, 2071, 2191,
2306, 2434, 2566, 2702, 2812, 2956
];
int i = 0;
foreach (VersionInfo Info in Versions.LowVersions)
Assert.AreEqual(DataBytes[i++], Info.TotalDataBytes);
}
[TestMethod]
public void Test_11_TotalDataBytes_M()
{
int[] DataBytes =
[
16, 28, 44, 64, 86, 108, 124, 154, 182, 216, 254, 290, 334,
365, 415, 453, 507, 563, 627, 669, 714, 782, 860, 914, 1000,
1062, 1128, 1193, 1267, 1373, 1455, 1541, 1631, 1725, 1812,
1914, 1992, 2102, 2216, 2334
];
int i = 0;
foreach (VersionInfo Info in Versions.MediumVersions)
Assert.AreEqual(DataBytes[i++], Info.TotalDataBytes);
}
[TestMethod]
public void Test_12_TotalDataBytes_Q()
{
int[] DataBytes =
[
13, 22, 34, 48, 62, 76, 88, 110, 132, 154, 180, 206, 244,
261, 295, 325, 367, 397, 445, 485, 512, 568, 614, 664, 718,
754, 808, 871, 911, 985, 1033, 1115, 1171, 1231, 1286, 1354,
1426, 1502, 1582, 1666
];
int i = 0;
foreach (VersionInfo Info in Versions.QuartileVersions)
Assert.AreEqual(DataBytes[i++], Info.TotalDataBytes);
}
[TestMethod]
public void Test_13_ApplyErrorCorrection()
{
byte[] Message =
[
67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38,
246, 246, 66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199, 146, 6,
182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7,
70, 247, 118, 86, 194, 6, 151, 50, 16, 236, 17, 236, 17, 236, 17, 236
];
byte[] FinalMessage = this.encoder.ApplyErrorCorrection(5, CorrectionLevel.Q, Message);
byte[] Expected =
[
67, 246, 182, 70, 85, 246, 230, 247, 70, 66, 247, 118, 134, 7, 119,
86, 87, 118, 50, 194, 38, 134, 7, 6, 85, 242, 118, 151, 194, 7, 134,
50, 119, 38, 87, 16, 50, 86, 38, 236, 6, 22, 82, 17, 18, 198, 6, 236,
6, 199, 134, 17, 103, 146, 151, 236, 38, 6, 50, 17, 7, 236, 213, 87,
148, 235, 199, 204, 116, 159, 11, 96, 177, 5, 45, 60, 212, 173, 115,
202, 76, 24, 247, 182, 133, 147, 241, 124, 75, 59, 223, 157, 242, 33,
229, 200, 238, 106, 248, 134, 76, 40, 154, 27, 195, 255, 117, 129,
230, 172, 154, 209, 189, 82, 111, 17, 10, 2, 86, 163, 108, 131, 161,
163, 240, 32, 111, 120, 192, 178, 39, 133, 141, 236
];
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(FinalMessage));
}
[TestMethod]
public void Test_14_GenerateMatrix()
{
byte[] Message =
[
67, 85, 70, 134, 87, 38, 85, 194, 119, 50, 6, 18, 6, 103, 38,
246, 246, 66, 7, 118, 134, 242, 7, 38, 86, 22, 198, 199, 146, 6,
182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7,
70, 247, 118, 86, 194, 6, 151, 50, 16, 236, 17, 236, 17, 236, 17, 236
];
QrMatrix Matrix = this.encoder.GenerateMatrix(5, CorrectionLevel.Q, Message, true);
ConsoleOut.WriteLine(Matrix.ToFullBlockText());
ConsoleOut.WriteLine();
ConsoleOut.WriteLine();
ConsoleOut.WriteLine(Matrix.ToHalfBlockText());
ConsoleOut.WriteLine();
ConsoleOut.WriteLine();
ConsoleOut.WriteLine(Matrix.ToQuarterBlockText());
}
private static class PenaltyMatrix
{
private const DotType X = DotType.CodeForeground;
private const DotType _ = DotType.CodeBackground;
public static readonly DotType[,] M = new DotType[,]
{
{ X, X, X, X, X, X, X, _, X, X, _, _, _, _, X, X, X, X, X, X, X },
{ X, _, _, _, _, _, X, _, X, _, _, X, _, _, X, _, _, _, _, _, X },
{ X, _, X, X, X, _, X, _, X, _, _, X, X, _, X, _, X, X, X, _, X },
{ X, _, X, X, X, _, X, _, X, _, _, _, _, _, X, _, X, X, X, _, X },
{ X, _, X, X, X, _, X, _, X, _, X, _, _, _, X, _, X, X, X, _, X },
{ X, _, _, _, _, _, X, _, _, _, X, _, _, _, X, _, _, _, _, _, X },
{ X, X, X, X, X, X, X, _, X, _, X, _, X, _, X, X, X, X, X, X, X },
{ _, _, _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _ },
{ _, X, X, _, X, _, X, X, _, _, _, _, X, _, X, _, X, X, X, X, X },
{ _, X, _, _, _, _, _, _, X, X, X, X, _, _, _, _, X, _, _, _, X },
{ _, _, X, X, _, X, X, X, _, X, X, _, _, _, X, _, X, X, _, _, _ },
{ _, X, X, _, X, X, _, X, _, _, X, X, _, X, _, X, _, X, X, X, _ },
{ X, _, _, _, X, _, X, _, X, _, X, X, X, _, X, X, X, _, X, _, X },
{ _, _, _, _, _, _, _, _, X, X, _, X, _, _, X, _, _, _, X, _, X },
{ X, X, X, X, X, X, X, _, X, _, X, _, _, _, _, X, _, X, X, _, _ },
{ X, _, _, _, _, _, X, _, _, X, _, X, X, _, X, X, _, X, _, _, _ },
{ X, _, X, X, X, _, X, _, X, _, X, _, _, _, X, X, X, X, X, X, X },
{ X, _, X, X, X, _, X, _, _, X, _, X, _, X, _, X, _, _, _, X, _ },
{ X, _, X, X, X, _, X, _, X, _, _, _, X, X, X, X, _, X, _, _, X },
{ X, _, _, _, _, _, X, _, X, _, X, X, _, X, _, _, _, X, _, X, X },
{ X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, _, _, _, _, X },
};
}
private static class PenaltyMask
{
private const bool X = true;
private const bool _ = false;
public static readonly bool[,] M = new bool[,]
{
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X, X },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, X, X, X, X, X, X, X, X },
{ _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
{ X, X, X, X, X, X, X, X, _, _, _, _, _, _, _, _, _, _, _, _, _ },
};
}
[TestMethod]
public void Test_15_Penalty_HorizontalBands()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(88, M.PenaltyHorizontalBands());
}
[TestMethod]
public void Test_16_Penalty_VerticalBands()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(92, M.PenaltyVerticalBands());
}
[TestMethod]
public void Test_17_Penalty_Blocks()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(90, M.PenaltyBlocks());
}
[TestMethod]
public void Test_18_Penalty_HorizontalFinderPattern()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(40, M.PenaltyHorizontalFinderPattern());
}
[TestMethod]
public void Test_19_Penalty_VerticalFinderPattern()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(40, M.PenaltyVerticalFinderPattern());
}
[TestMethod]
public void Test_20_Penalty_Balance()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(0, M.PenaltyBalance());
}
[TestMethod]
public void Test_21_Penalty_Total()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
Assert.AreEqual(350, M.Penalty());
}
[TestMethod]
public void Test_22_Penalty_Total_Mask0()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
string s1 = M.ToFullBlockText();
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask0);
string s2 = M.ToFullBlockText();
Assert.AreEqual(s1, s2);
ConsoleOut.WriteLine(s2);
Assert.AreEqual(350, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_23_Penalty_Total_Mask1()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask1);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(421, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_24_Penalty_Total_Mask2()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask2);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(507, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_25_Penalty_Total_Mask3()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask3);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(443, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_26_Penalty_Total_Mask4()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask4);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(553, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_27_Penalty_Total_Mask5()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask5);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(547, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_28_Penalty_Total_Mask6()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask6);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(357, M.Penalty());
}
[TestMethod]
[Ignore]
public void Test_29_Penalty_Total_Mask7()
{
QrMatrix M = new((DotType[,])PenaltyMatrix.M.Clone(), (bool[,])PenaltyMask.M.Clone());
M.ApplyMask(QrMatrix.Mask0);
M.ApplyMask(QrMatrix.Mask7);
ConsoleOut.WriteLine(M.ToFullBlockText());
Assert.AreEqual(520, M.Penalty());
}
[TestMethod]
public void Test_30_HelloWorld()
{
CorrectionLevel Level = CorrectionLevel.Q;
string Message = "HELLO WORLD";
KeyValuePair<byte[], VersionInfo> Encoding = this.encoder.Encode(Level, Message);
byte[] Expected =
[
0b00100000, 0b01011011, 0b00001011, 0b01111000, 0b11010001,
0b01110010, 0b11011100, 0b01001101, 0b01000011, 0b01000000,
0b11101100, 0b00010001, 0b11101100
];
Assert.AreEqual(1, Encoding.Value.Version);
Assert.AreEqual(Convert.ToBase64String(Expected), Convert.ToBase64String(Encoding.Key));
}
[TestMethod]
public void Test_31_HelloWorld_QR()
{
CorrectionLevel Level = CorrectionLevel.Q;
string Message = "HELLO WORLD";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
ConsoleOut.WriteLine(M.ToHalfBlockText());
}
[TestMethod]
public void Test_32_EDaler_1_QR()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "edaler:id=8747cf73-85be-4db1-9117-a77a36f401fb;fi=27c6d6f2-568c-c07c-040e-dd8dd1fcf047@legal.cybercity.online;ti=27f4887a-ce90-ab4b-a033-cff1090fed0e@legal.sb.br.fi.tagroot.io;am=2;cu=SEK;cr=2021-03-29T13:08:31.770Z;ex=2021-04-01;em=w92rYkprOiCyOTZSDO4FEgKas5ct95EyCPBz6SxF/RA=;ep=O+yaNu5XWk37vyAxqVyCAyLo//cCDwLCoSjegX2BSkIcQJEFbjo0CM8exKN5DV/sbzZ8QnLHc1AA;s=VaM03Ulinilj9psaeyGqkHJhrvZ7EfDeq+KiJZe79VyGGTT8h+wu47ll/DaqAyCQMDLwpf60DtaA06EvTz5RgO8l8b576L92h974Q9nNnj9vjx4Dqe7uib8bKHJM4sOU5yP1s8cKRFAyRZeRbr2LgAcA";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
ConsoleOut.WriteLine(M.ToHalfBlockText());
}
[TestMethod]
public void Test_33_Custom_NoScale()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, null, "CustomNoScale.png", CustomColor, 1);
}
[TestMethod]
public void Test_34_Custom_NoAntiAlias()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 400, "CustomNoAntiAlias.png", CustomColor, 1);
}
[TestMethod]
public void Test_35_Custom_Quality_2()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 400, "CustomAntiAlias2.png", CustomColor, 2);
}
[TestMethod]
public void Test_36_Custom_Quality_3()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 400, "CustomAntiAlias3.png", CustomColor, 3);
}
[TestMethod]
public void Test_37_Custom_Quality_4()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 400, "CustomAntiAlias4.png", CustomColor, 4);
}
[TestMethod]
public void Test_38_Custom_Large_NoAntiAlias()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 2048, "CustomLargeNoAntiAlias.png", CustomColor, 1);
}
[TestMethod]
public void Test_39_Custom_Large_Quality_2()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 2048, "CustomLargeAntiAlias2.png", CustomColor, 2);
}
[TestMethod]
public void Test_40_Custom_Large_Quality_3()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 2048, "CustomLargeAntiAlias3.png", CustomColor, 3);
}
[TestMethod]
public void Test_41_Custom_Large_Quality_4()
{
CorrectionLevel Level = CorrectionLevel.H;
string Message = "iotid:2be3be1f-0f8f-7e1b-0808-cc7844e9e732@legal.lab.tagroot.io";
QrMatrix M = this.encoder.GenerateMatrix(Level, Message);
VersionTests.VersionTests.SaveImage(M, 2048, "CustomLargeAntiAlias4.png", CustomColor, 4);
}
private readonly static SKBitmap Icon = SvgPath.SvgPathToBitmap(
"M128 21.3335C69.1202 21.3335 21.3335 69.1202 21.3335 128C21.3335 186.88 69.1202 234.667 128 234.667C186.88 234.667 234.667 186.88 234.667 128C234.667 69.1202 186.88 21.3335 128 21.3335ZM128 53.3335C145.707 53.3335 160 67.6268 160 85.3335C160 103.04 145.707 117.333 128 117.333C110.293 117.333 96.0002 103.04 96.0002 85.3335C96.0002 67.6268 110.293 53.3335 128 53.3335ZM128 204.8C101.333 204.8 77.7602 191.147 64.0002 170.453C64.3202 149.227 106.667 137.6 128 137.6C149.227 137.6 191.68 149.227 192 170.453C178.24 191.147 154.667 204.8 128 204.8Z",
256, 256, SKColors.Green, SKColors.White);
private static uint CustomColor(float CodeX, float CodeY, float DotX, float DotY, DotType Type)
{
float dx = (CodeX - 0.5f);
float dy = (CodeY - 0.5f);
float d2 = dx * dx + dy * dy;
if (d2 < 0.01)
{
int IconX = (int)(128 + 1024 * dx + 0.5f);
int IconY = (int)(128 + 1024 * dy + 0.5f);
if (IconX >= 0 && IconX < Icon.Width && IconY >= 0 && IconY < Icon.Height)
{
SKColor cl = Icon.GetPixel(IconX, IconY);
uint Result = cl.Alpha;
Result <<= 8;
Result |= cl.Blue;
Result <<= 8;
Result |= cl.Green;
Result <<= 8;
Result |= cl.Red;
return Result;
}
}
if (d2 < 0.015)
return 0xffffffff; // white
switch (Type)
{
case DotType.CodeBackground:
default:
return 0xffffffff; // white
case DotType.CodeForeground:
dx = (DotX - 0.5f);
dy = (DotY - 0.5f);
d2 = dx * dx + dy * dy;
if (d2 <= 0.25f)
return 0xff000000; // black
else if (d2 <= 0.27f)
return 0xff404040; // dark gray
else if (d2 <= 0.29f)
return 0xff808080; // gray
else if (d2 <= 0.30f)
return 0xffc0c0c0; // light
else
return 0xffffffff; // white
case DotType.FinderMarkerBackground:
return 0xfff8f8ff; // light red
case DotType.FinderMarkerForegroundOuter:
return 0xff000080; // dark red
case DotType.FinderMarkerForegroundInner:
return 0xff000060; // darker red
case DotType.AlignmentMarkerBackground:
return 0xfffff8f8; // light blue
case DotType.AlignmentMarkerForegroundOuter:
return 0xff800000; // dark blue
case DotType.AlignmentMarkerForegroundInner:
return 0xff600000; // darker blue
}
;
}
}
}