Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change MaskPattern.Patterns to a list #531

Merged
merged 4 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
23 changes: 13 additions & 10 deletions QRCoder/QRCodeGenerator.ModulePlacer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,10 @@ public static int MaskCode(QRCodeData qrCode, int version, List<Rectangle> block

// Temporary QRCodeData object to test different mask patterns without altering the original.
var qrTemp = new QRCodeData(version);
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 @@ -127,7 +129,7 @@ public static int MaskCode(QRCodeData qrCode, int version, List<Rectangle> block
}

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

// Place version information if applicable.
Expand All @@ -144,14 +146,14 @@ public static int MaskCode(QRCodeData qrCode, int version, List<Rectangle> block
{
if (!IsBlocked(new Rectangle(x, y, 1, 1), blockedModules))
{
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 (!IsBlocked(new Rectangle(x, x, 1, 1), blockedModules))
{
qrTemp.ModuleMatrix[x][x] ^= pattern.Value(x, x);
qrTemp.ModuleMatrix[x][x] ^= patternFunc(x, x);
}
}

Expand All @@ -160,29 +162,30 @@ public static int MaskCode(QRCodeData qrCode, int version, List<Rectangle> block
// Select the pattern with the lowest score, indicating better QR code readability.
if (!selectedPattern.HasValue || patternScore > score)
{
selectedPattern = pattern.Key;
selectedPattern = maskPattern;
patternScore = score;
}
}

// Apply the best mask pattern to the actual QR code.
var selectedPatternFunc = MaskPattern.Patterns[selectedPattern.Value];
codebude marked this conversation as resolved.
Show resolved Hide resolved
for (var x = 0; x < size; x++)
{
for (var y = 0; y < x; y++)
{
if (!IsBlocked(new Rectangle(x, y, 1, 1), blockedModules))
{
qrCode.ModuleMatrix[y][x] ^= MaskPattern.Patterns[selectedPattern.Value](x, y);
qrCode.ModuleMatrix[x][y] ^= MaskPattern.Patterns[selectedPattern.Value](y, x);
qrCode.ModuleMatrix[y][x] ^= selectedPatternFunc(x, y);
qrCode.ModuleMatrix[x][y] ^= selectedPatternFunc(y, x);
}
}

if (!IsBlocked(new Rectangle(x, x, 1, 1), blockedModules))
{
qrCode.ModuleMatrix[x][x] ^= MaskPattern.Patterns[selectedPattern.Value](x, x);
qrCode.ModuleMatrix[x][x] ^= selectedPatternFunc(x, x);
}
}
return selectedPattern.Value - 1;
return selectedPattern.Value;
codebude marked this conversation as resolved.
Show resolved Hide resolved
}

/// <summary>
Expand Down