|
5 | 5 | using System.Collections;
|
6 | 6 | using System.Globalization;
|
7 | 7 | using System.Runtime.CompilerServices;
|
| 8 | +using System.Reflection; |
8 | 9 |
|
9 | 10 | namespace QRCoder
|
10 | 11 | {
|
@@ -310,12 +311,12 @@ private static BitArray GetFormatString(ECCLevel level, int maskVersion)
|
310 | 311 |
|
311 | 312 | int index = 0;
|
312 | 313 | int count = 15;
|
313 |
| - TrimLeadingZeros(); // modifies index and count |
| 314 | + TrimLeadingZeros(fStrEcc, ref index, ref count); |
314 | 315 | while (count > 10)
|
315 | 316 | {
|
316 | 317 | for (var i = 0; i < _getFormatGenerator.Length; i++)
|
317 | 318 | fStrEcc[index + i] ^= _getFormatGenerator[i];
|
318 |
| - TrimLeadingZeros(); // modifies index and count |
| 319 | + TrimLeadingZeros(fStrEcc, ref index, ref count); |
319 | 320 | }
|
320 | 321 | ShiftTowardsBit0(fStrEcc, index);
|
321 | 322 | fStrEcc.Length = 10 + 5;
|
@@ -343,19 +344,18 @@ void WriteEccLevelAndVersion()
|
343 | 344 | }
|
344 | 345 | DecToBin(maskVersion, 3, fStrEcc, 2);
|
345 | 346 | }
|
| 347 | + } |
346 | 348 |
|
347 | 349 | #if NETCOREAPP
|
348 |
| - [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| 350 | + [MethodImpl(MethodImplOptions.AggressiveInlining)] |
349 | 351 | #endif
|
350 |
| - void TrimLeadingZeros() |
| 352 | + private static void TrimLeadingZeros(BitArray fStrEcc, ref int index, ref int count) |
| 353 | + { |
| 354 | + while (!fStrEcc[index]) |
351 | 355 | {
|
352 |
| - while (!fStrEcc[index]) |
353 |
| - { |
354 |
| - index++; |
355 |
| - count--; |
356 |
| - } |
| 356 | + index++; |
| 357 | + count--; |
357 | 358 | }
|
358 |
| - |
359 | 359 | }
|
360 | 360 |
|
361 | 361 | private static void ShiftTowardsBit0(BitArray fStrEcc, int num)
|
@@ -389,20 +389,12 @@ private static BitArray GetVersionString(int version)
|
389 | 389 | DecToBin(version, 6, vStr, 0);
|
390 | 390 | var count = vStr.Length;
|
391 | 391 | var index = 0;
|
392 |
| - while (!vStr[index]) |
393 |
| - { |
394 |
| - index++; |
395 |
| - count--; |
396 |
| - } |
| 392 | + TrimLeadingZeros(vStr, ref index, ref count); |
397 | 393 | while (count > 12)
|
398 | 394 | {
|
399 | 395 | for (var i = 0; i < _getVersionGenerator.Length; i++)
|
400 | 396 | vStr[index + i] ^= _getVersionGenerator[i];
|
401 |
| - while (!vStr[index]) |
402 |
| - { |
403 |
| - index++; |
404 |
| - count--; |
405 |
| - } |
| 397 | + TrimLeadingZeros(vStr, ref index, ref count); |
406 | 398 | }
|
407 | 399 | ShiftTowardsBit0(vStr, index);
|
408 | 400 | vStr.Length = 12 + 6;
|
@@ -860,15 +852,15 @@ public static int Score(QRCodeData qrCode)
|
860 | 852 | }
|
861 | 853 |
|
862 | 854 | //Penalty 4
|
863 |
| - double blackModules = 0; |
| 855 | + int blackModules = 0; |
864 | 856 | foreach (var row in qrCode.ModuleMatrix)
|
865 | 857 | foreach (bool bit in row)
|
866 | 858 | if (bit)
|
867 | 859 | blackModules++;
|
868 | 860 |
|
869 |
| - var percent = (blackModules / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count)) * 100; |
870 |
| - var prevMultipleOf5 = Math.Abs((int)Math.Floor(percent / 5) * 5 - 50) / 5; |
871 |
| - var nextMultipleOf5 = Math.Abs((int)Math.Floor(percent / 5) * 5 - 45) / 5; |
| 861 | + var percentDiv5 = blackModules * 20 / (qrCode.ModuleMatrix.Count * qrCode.ModuleMatrix.Count); |
| 862 | + var prevMultipleOf5 = Math.Abs(percentDiv5 - 10); |
| 863 | + var nextMultipleOf5 = Math.Abs(percentDiv5 - 9); |
872 | 864 | score4 = Math.Min(prevMultipleOf5, nextMultipleOf5) * 10;
|
873 | 865 |
|
874 | 866 | return (score1 + score2) + (score3 + score4);
|
|
0 commit comments