Skip to content

Commit 6ffdcad

Browse files
committed
Optimize score calculations
1 parent c3d9d8b commit 6ffdcad

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

QRCoder/QRCodeGenerator.cs

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections;
66
using System.Globalization;
77
using System.Runtime.CompilerServices;
8+
using System.Reflection;
89

910
namespace QRCoder
1011
{
@@ -310,12 +311,12 @@ private static BitArray GetFormatString(ECCLevel level, int maskVersion)
310311

311312
int index = 0;
312313
int count = 15;
313-
TrimLeadingZeros(); // modifies index and count
314+
TrimLeadingZeros(fStrEcc, ref index, ref count);
314315
while (count > 10)
315316
{
316317
for (var i = 0; i < _getFormatGenerator.Length; i++)
317318
fStrEcc[index + i] ^= _getFormatGenerator[i];
318-
TrimLeadingZeros(); // modifies index and count
319+
TrimLeadingZeros(fStrEcc, ref index, ref count);
319320
}
320321
ShiftTowardsBit0(fStrEcc, index);
321322
fStrEcc.Length = 10 + 5;
@@ -343,19 +344,18 @@ void WriteEccLevelAndVersion()
343344
}
344345
DecToBin(maskVersion, 3, fStrEcc, 2);
345346
}
347+
}
346348

347349
#if NETCOREAPP
348-
[MethodImpl(MethodImplOptions.AggressiveInlining)]
350+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
349351
#endif
350-
void TrimLeadingZeros()
352+
private static void TrimLeadingZeros(BitArray fStrEcc, ref int index, ref int count)
353+
{
354+
while (!fStrEcc[index])
351355
{
352-
while (!fStrEcc[index])
353-
{
354-
index++;
355-
count--;
356-
}
356+
index++;
357+
count--;
357358
}
358-
359359
}
360360

361361
private static void ShiftTowardsBit0(BitArray fStrEcc, int num)
@@ -389,20 +389,12 @@ private static BitArray GetVersionString(int version)
389389
DecToBin(version, 6, vStr, 0);
390390
var count = vStr.Length;
391391
var index = 0;
392-
while (!vStr[index])
393-
{
394-
index++;
395-
count--;
396-
}
392+
TrimLeadingZeros(vStr, ref index, ref count);
397393
while (count > 12)
398394
{
399395
for (var i = 0; i < _getVersionGenerator.Length; i++)
400396
vStr[index + i] ^= _getVersionGenerator[i];
401-
while (!vStr[index])
402-
{
403-
index++;
404-
count--;
405-
}
397+
TrimLeadingZeros(vStr, ref index, ref count);
406398
}
407399
ShiftTowardsBit0(vStr, index);
408400
vStr.Length = 12 + 6;
@@ -860,15 +852,15 @@ public static int Score(QRCodeData qrCode)
860852
}
861853

862854
//Penalty 4
863-
double blackModules = 0;
855+
int blackModules = 0;
864856
foreach (var row in qrCode.ModuleMatrix)
865857
foreach (bool bit in row)
866858
if (bit)
867859
blackModules++;
868860

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);
872864
score4 = Math.Min(prevMultipleOf5, nextMultipleOf5) * 10;
873865

874866
return (score1 + score2) + (score3 + score4);

0 commit comments

Comments
 (0)