Skip to content

Commit 567d3be

Browse files
authored
Merge pull request #389 from Jaimeetxebarria/fix-mask-optimization
Fix mask optimization to include format and version information and dark module.
2 parents 0270ba8 + 5b32857 commit 567d3be

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

qrcode/main.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ def make(self, fit=True):
151151
if fit or (self.version is None):
152152
self.best_fit(start=self.version)
153153
if self.mask_pattern is None:
154-
self.makeImpl(False, self.best_mask_pattern())
154+
self.makeImpl(self.best_mask_pattern())
155155
else:
156-
self.makeImpl(False, self.mask_pattern)
156+
self.makeImpl(self.mask_pattern)
157157

158-
def makeImpl(self, test, mask_pattern):
158+
def makeImpl(self, mask_pattern):
159159
self.modules_count = self.version * 4 + 17
160160

161161
if self.version in precomputed_qr_blanks:
@@ -172,10 +172,10 @@ def makeImpl(self, test, mask_pattern):
172172

173173
precomputed_qr_blanks[self.version] = copy_2d_array(self.modules)
174174

175-
self.setup_type_info(test, mask_pattern)
175+
self.setup_type_info(mask_pattern)
176176

177177
if self.version >= 7:
178-
self.setup_type_number(test)
178+
self.setup_type_number()
179179

180180
if self.data_cache is None:
181181
self.data_cache = util.create_data(
@@ -240,7 +240,7 @@ def best_mask_pattern(self):
240240
pattern = 0
241241

242242
for i in range(8):
243-
self.makeImpl(True, i)
243+
self.makeImpl(i)
244244

245245
lost_point = util.lost_point(self.modules)
246246

@@ -429,24 +429,24 @@ def setup_position_adjust_pattern(self):
429429
else:
430430
self.modules[row + r][col + c] = False
431431

432-
def setup_type_number(self, test):
432+
def setup_type_number(self):
433433
bits = util.BCH_type_number(self.version)
434434

435435
for i in range(18):
436-
mod = not test and ((bits >> i) & 1) == 1
436+
mod = ((bits >> i) & 1) == 1
437437
self.modules[i // 3][i % 3 + self.modules_count - 8 - 3] = mod
438438

439439
for i in range(18):
440-
mod = not test and ((bits >> i) & 1) == 1
440+
mod = ((bits >> i) & 1) == 1
441441
self.modules[i % 3 + self.modules_count - 8 - 3][i // 3] = mod
442442

443-
def setup_type_info(self, test, mask_pattern):
443+
def setup_type_info(self, mask_pattern):
444444
data = (self.error_correction << 3) | mask_pattern
445445
bits = util.BCH_type_info(data)
446446

447447
# vertical
448448
for i in range(15):
449-
mod = not test and ((bits >> i) & 1) == 1
449+
mod = ((bits >> i) & 1) == 1
450450

451451
if i < 6:
452452
self.modules[i][8] = mod
@@ -457,7 +457,7 @@ def setup_type_info(self, test, mask_pattern):
457457

458458
# horizontal
459459
for i in range(15):
460-
mod = not test and ((bits >> i) & 1) == 1
460+
mod = ((bits >> i) & 1) == 1
461461

462462
if i < 8:
463463
self.modules[8][self.modules_count - i - 1] = mod
@@ -467,7 +467,7 @@ def setup_type_info(self, test, mask_pattern):
467467
self.modules[8][15 - i - 1] = mod
468468

469469
# fixed module
470-
self.modules[self.modules_count - 8][8] = not test
470+
self.modules[self.modules_count - 8][8] = True
471471

472472
def map_data(self, data, mask_pattern):
473473
inc = -1

qrcode/tests/test_qrcode.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,20 @@ def test_make_image_with_wrong_pattern():
119119
qrcode.QRCode(mask_pattern=42)
120120

121121

122+
def test_best_mask_pattern_includes_format_info():
123+
"""Mask evaluation should use the complete symbol per ISO 18004 §7.8.3.1."""
124+
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
125+
qr.add_data("hello")
126+
qr.best_fit()
127+
qr.data_cache = qrcode.util.create_data(
128+
qr.version, qr.error_correction, qr.data_list
129+
)
130+
# The old code zeroed out format info, version info, and the dark module
131+
# during mask evaluation, which violated the spec and selected mask 5 for
132+
# this input. With the complete symbol evaluated, the correct mask is 6.
133+
assert qr.best_mask_pattern() == 6
134+
135+
122136
def test_mask_pattern_setter():
123137
qr = qrcode.QRCode()
124138

0 commit comments

Comments
 (0)