From d48a2fb2d2b95fce2cdd35dfacdff68544e60c61 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Sun, 19 May 2024 18:25:50 -0400 Subject: [PATCH 1/3] Change MaskPattern.Patterns to a list --- .../QRCodeGenerator.ModulePlacer.MaskPattern.cs | 8 ++++---- QRCoder/QRCodeGenerator.ModulePlacer.cs | 16 +++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs b/QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs index 61aae144..995dd54f 100644 --- a/QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs +++ b/QRCoder/QRCodeGenerator.ModulePlacer.MaskPattern.cs @@ -17,10 +17,10 @@ private static class MaskPattern /// /// A dictionary mapping each mask pattern index to its corresponding function that calculates whether a given pixel should be masked. /// - public static readonly Dictionary> Patterns = - new Dictionary>(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> Patterns = + new List>(8) { + MaskPattern.Pattern1, MaskPattern.Pattern2, MaskPattern.Pattern3, MaskPattern.Pattern4, + MaskPattern.Pattern5, MaskPattern.Pattern6, MaskPattern.Pattern7, MaskPattern.Pattern8 }; /// diff --git a/QRCoder/QRCodeGenerator.ModulePlacer.cs b/QRCoder/QRCodeGenerator.ModulePlacer.cs index 28196f4f..53b8a859 100644 --- a/QRCoder/QRCodeGenerator.ModulePlacer.cs +++ b/QRCoder/QRCodeGenerator.ModulePlacer.cs @@ -115,8 +115,10 @@ public static int MaskCode(QRCodeData qrCode, int version, List 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++) { @@ -127,7 +129,7 @@ public static int MaskCode(QRCodeData qrCode, int version, List 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. @@ -144,14 +146,14 @@ public static int MaskCode(QRCodeData qrCode, int version, List 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); } } @@ -160,7 +162,7 @@ public static int MaskCode(QRCodeData qrCode, int version, List 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; } } @@ -182,7 +184,7 @@ public static int MaskCode(QRCodeData qrCode, int version, List block qrCode.ModuleMatrix[x][x] ^= MaskPattern.Patterns[selectedPattern.Value](x, x); } } - return selectedPattern.Value - 1; + return selectedPattern.Value; } /// From 26a87e654ee10a08227bbffac6ce9539ad957b48 Mon Sep 17 00:00:00 2001 From: Shane32 Date: Sun, 19 May 2024 18:40:02 -0400 Subject: [PATCH 2/3] Update --- QRCoder/QRCodeGenerator.ModulePlacer.cs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/QRCoder/QRCodeGenerator.ModulePlacer.cs b/QRCoder/QRCodeGenerator.ModulePlacer.cs index 53b8a859..d89be355 100644 --- a/QRCoder/QRCodeGenerator.ModulePlacer.cs +++ b/QRCoder/QRCodeGenerator.ModulePlacer.cs @@ -168,20 +168,21 @@ public static int MaskCode(QRCodeData qrCode, int version, List block } // Apply the best mask pattern to the actual QR code. + var selectedPatternFunc = MaskPattern.Patterns[selectedPattern.Value]; 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; From 5c652f31610b21b75214ec1b125f41dae7c034a0 Mon Sep 17 00:00:00 2001 From: Shane Krueger Date: Wed, 22 May 2024 16:34:39 -0400 Subject: [PATCH 3/3] Change from int? to int --- QRCoder/QRCodeGenerator.ModulePlacer.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/QRCoder/QRCodeGenerator.ModulePlacer.cs b/QRCoder/QRCodeGenerator.ModulePlacer.cs index 35eba79b..d5fe88d9 100644 --- a/QRCoder/QRCodeGenerator.ModulePlacer.cs +++ b/QRCoder/QRCodeGenerator.ModulePlacer.cs @@ -107,8 +107,8 @@ public static void PlaceFormat(QRCodeData qrCode, BitArray formatStr, bool offse /// The index of the selected mask pattern. 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; @@ -165,7 +165,7 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke 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 = maskPattern; patternScore = score; @@ -173,7 +173,7 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke } // Apply the best mask pattern to the actual QR code. - var selectedPatternFunc = MaskPattern.Patterns[selectedPattern.Value]; + var selectedPatternFunc = MaskPattern.Patterns[selectedPattern]; for (var x = 0; x < size; x++) { for (var y = 0; y < x; y++) @@ -191,7 +191,7 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke } } - return selectedPattern.Value; + return selectedPattern; } ///