@@ -107,8 +107,8 @@ public static void PlaceFormat(QRCodeData qrCode, BitArray formatStr, bool offse
107
107
/// <returns>The index of the selected mask pattern.</returns>
108
108
public static int MaskCode ( QRCodeData qrCode , int version , BlockedModules blockedModules , ECCLevel eccLevel )
109
109
{
110
- int ? selectedPattern = null ;
111
- var patternScore = 0 ;
110
+ int selectedPattern = - 1 ; // no pattern selected yet
111
+ var patternScore = int . MaxValue ; // lower score is better
112
112
113
113
var size = qrCode . ModuleMatrix . Count - 8 ;
114
114
@@ -121,8 +121,10 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
121
121
GetVersionString ( versionString , version ) ;
122
122
}
123
123
var formatStr = new BitArray ( 15 ) ;
124
- foreach ( var pattern in MaskPattern . Patterns )
124
+ for ( var maskPattern = 0 ; maskPattern < 8 ; maskPattern ++ )
125
125
{
126
+ var patternFunc = MaskPattern . Patterns [ maskPattern ] ;
127
+
126
128
// Reset the temporary QR code to the current state of the actual QR code.
127
129
for ( var y = 0 ; y < size ; y ++ )
128
130
{
@@ -133,7 +135,7 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
133
135
}
134
136
135
137
// Place format information using the current mask pattern.
136
- GetFormatString ( formatStr , eccLevel , pattern . Key - 1 ) ;
138
+ GetFormatString ( formatStr , eccLevel , maskPattern ) ;
137
139
ModulePlacer . PlaceFormat ( qrTemp , formatStr , false ) ;
138
140
139
141
// Place version information if applicable.
@@ -149,46 +151,47 @@ public static int MaskCode(QRCodeData qrCode, int version, BlockedModules blocke
149
151
{
150
152
if ( ! blockedModules . IsBlocked ( x , y ) )
151
153
{
152
- qrTemp . ModuleMatrix [ y ] [ x ] ^= pattern . Value ( x , y ) ;
153
- qrTemp . ModuleMatrix [ x ] [ y ] ^= pattern . Value ( y , x ) ;
154
+ qrTemp . ModuleMatrix [ y ] [ x ] ^= patternFunc ( x , y ) ;
155
+ qrTemp . ModuleMatrix [ x ] [ y ] ^= patternFunc ( y , x ) ;
154
156
}
155
157
}
156
158
157
159
if ( ! blockedModules . IsBlocked ( x , x ) )
158
160
{
159
- qrTemp . ModuleMatrix [ x ] [ x ] ^= pattern . Value ( x , x ) ;
161
+ qrTemp . ModuleMatrix [ x ] [ x ] ^= patternFunc ( x , x ) ;
160
162
}
161
163
}
162
164
163
165
var score = MaskPattern . Score ( qrTemp ) ;
164
166
165
167
// Select the pattern with the lowest score, indicating better QR code readability.
166
- if ( ! selectedPattern . HasValue || patternScore > score )
168
+ if ( patternScore > score )
167
169
{
168
- selectedPattern = pattern . Key ;
170
+ selectedPattern = maskPattern ;
169
171
patternScore = score ;
170
172
}
171
173
}
172
174
173
175
// Apply the best mask pattern to the actual QR code.
176
+ var selectedPatternFunc = MaskPattern . Patterns [ selectedPattern ] ;
174
177
for ( var x = 0 ; x < size ; x ++ )
175
178
{
176
179
for ( var y = 0 ; y < x ; y ++ )
177
180
{
178
181
if ( ! blockedModules . IsBlocked ( x , y ) )
179
182
{
180
- qrCode . ModuleMatrix [ y + 4 ] [ x + 4 ] ^= MaskPattern . Patterns [ selectedPattern . Value ] ( x , y ) ;
181
- qrCode . ModuleMatrix [ x + 4 ] [ y + 4 ] ^= MaskPattern . Patterns [ selectedPattern . Value ] ( y , x ) ;
183
+ qrCode . ModuleMatrix [ y + 4 ] [ x + 4 ] ^= selectedPatternFunc ( x , y ) ;
184
+ qrCode . ModuleMatrix [ x + 4 ] [ y + 4 ] ^= selectedPatternFunc ( y , x ) ;
182
185
}
183
186
}
184
187
185
188
if ( ! blockedModules . IsBlocked ( x , x ) )
186
189
{
187
- qrCode . ModuleMatrix [ x + 4 ] [ x + 4 ] ^= MaskPattern . Patterns [ selectedPattern . Value ] ( x , x ) ;
190
+ qrCode . ModuleMatrix [ x + 4 ] [ x + 4 ] ^= selectedPatternFunc ( x , x ) ;
188
191
}
189
192
}
190
193
191
- return selectedPattern . Value - 1 ;
194
+ return selectedPattern ;
192
195
}
193
196
194
197
/// <summary>
0 commit comments