Skip to content

Commit

Permalink
Merge pull request #531 from Shane32/maskpattern_list
Browse files Browse the repository at this point in the history
Change MaskPattern.Patterns to a list
  • Loading branch information
codebude authored May 22, 2024
2 parents 8259416 + 5c652f3 commit e3664d1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 17 deletions.
8 changes: 4 additions & 4 deletions QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ private static class MaskPattern
/// <summary>
/// A dictionary mapping each mask pattern index to its corresponding function that calculates whether a given pixel should be masked.
/// </summary>
public static readonly Dictionary<int, Func<int, int, bool>> Patterns =
new Dictionary<int, Func<int, int, bool>>(8) {
{ 1, MaskPattern.Pattern1 }, {2, MaskPattern.Pattern2 }, {3, MaskPattern.Pattern3 }, {4, MaskPattern.Pattern4 },
{ 5, MaskPattern.Pattern5 }, {6, MaskPattern.Pattern6 }, {7, MaskPattern.Pattern7 }, {8, MaskPattern.Pattern8 }
public static readonly List<Func<int, int, bool>> Patterns =
new List<Func<int, int, bool>>(8) {
MaskPattern.Pattern1, MaskPattern.Pattern2, MaskPattern.Pattern3, MaskPattern.Pattern4,
MaskPattern.Pattern5, MaskPattern.Pattern6, MaskPattern.Pattern7, MaskPattern.Pattern8
};

/// <summary>
Expand Down
29 changes: 16 additions & 13 deletions QRCoder/QRCodeGenerator.ModulePlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ public static void PlaceFormat(QRCodeData qrCode, BitArray formatStr, bool offse
/// <returns>The index of the selected mask pattern.</returns>
public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blockedModules, ECCLevel eccLevel)
{
int? selectedPattern = null;
var patternScore = 0;
int selectedPattern = -1; // no pattern selected yet
var patternScore = int.MaxValue; // lower score is better

var size = qrCode.ModuleMatrix.Count - 8;

Expand All @@ -121,8 +121,10 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
GetVersionString(versionString, version);
}
var formatStr = new BitArray(15);
foreach (var pattern in MaskPattern.Patterns)
for (var maskPattern = 0; maskPattern < 8; maskPattern++)
{
var patternFunc = MaskPattern.Patterns[maskPattern];

// Reset the temporary QR code to the current state of the actual QR code.
for (var y = 0; y < size; y++)
{
Expand All @@ -133,7 +135,7 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
}

// Place format information using the current mask pattern.
GetFormatString(formatStr, eccLevel, pattern.Key - 1);
GetFormatString(formatStr, eccLevel, maskPattern);
ModulePlacer.PlaceFormat(qrTemp, formatStr, false);

// Place version information if applicable.
Expand All @@ -149,46 +151,47 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
{
if (!blockedModules.IsBlocked(x, y))
{
qrTemp.ModuleMatrix[y][x] ^= pattern.Value(x, y);
qrTemp.ModuleMatrix[x][y] ^= pattern.Value(y, x);
qrTemp.ModuleMatrix[y][x] ^= patternFunc(x, y);
qrTemp.ModuleMatrix[x][y] ^= patternFunc(y, x);
}
}

if (!blockedModules.IsBlocked(x, x))
{
qrTemp.ModuleMatrix[x][x] ^= pattern.Value(x, x);
qrTemp.ModuleMatrix[x][x] ^= patternFunc(x, x);
}
}

var score = MaskPattern.Score(qrTemp);

// Select the pattern with the lowest score, indicating better QR code readability.
if (!selectedPattern.HasValue || patternScore > score)
if (patternScore > score)
{
selectedPattern = pattern.Key;
selectedPattern = maskPattern;
patternScore = score;
}
}

// Apply the best mask pattern to the actual QR code.
var selectedPatternFunc = MaskPattern.Patterns[selectedPattern];
for (var x = 0; x < size; x++)
{
for (var y = 0; y < x; y++)
{
if (!blockedModules.IsBlocked(x, y))
{
qrCode.ModuleMatrix[y + 4][x + 4] ^= MaskPattern.Patterns[selectedPattern.Value](x, y);
qrCode.ModuleMatrix[x + 4][y + 4] ^= MaskPattern.Patterns[selectedPattern.Value](y, x);
qrCode.ModuleMatrix[y + 4][x + 4] ^= selectedPatternFunc(x, y);
qrCode.ModuleMatrix[x + 4][y + 4] ^= selectedPatternFunc(y, x);
}
}

if (!blockedModules.IsBlocked(x, x))
{
qrCode.ModuleMatrix[x + 4][x + 4] ^= MaskPattern.Patterns[selectedPattern.Value](x, x);
qrCode.ModuleMatrix[x + 4][x + 4] ^= selectedPatternFunc(x, x);
}
}

return selectedPattern.Value - 1;
return selectedPattern;
}

/// <summary>
Expand Down

0 comments on commit e3664d1

Please sign in to comment.