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

Fix mask optimization to include format and version information and dark module. #389

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
26 changes: 13 additions & 13 deletions qrcode/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,11 @@ def make(self, fit=True):
if fit or (self.version is None):
self.best_fit(start=self.version)
if self.mask_pattern is None:
self.makeImpl(False, self.best_mask_pattern())
self.makeImpl(self.best_mask_pattern())
else:
self.makeImpl(False, self.mask_pattern)
self.makeImpl(self.mask_pattern)

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

if self.version in precomputed_qr_blanks:
Expand All @@ -179,10 +179,10 @@ def makeImpl(self, test, mask_pattern):

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

self.setup_type_info(test, mask_pattern)
self.setup_type_info(mask_pattern)

if self.version >= 7:
self.setup_type_number(test)
self.setup_type_number()

if self.data_cache is None:
self.data_cache = util.create_data(
Expand Down Expand Up @@ -246,7 +246,7 @@ def best_mask_pattern(self):
pattern = 0

for i in range(8):
self.makeImpl(True, i)
self.makeImpl(i)

lost_point = util.lost_point(self.modules)

Expand Down Expand Up @@ -430,24 +430,24 @@ def setup_position_adjust_pattern(self):
else:
self.modules[row + r][col + c] = False

def setup_type_number(self, test):
def setup_type_number(self):
bits = util.BCH_type_number(self.version)

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

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

def setup_type_info(self, test, mask_pattern):
def setup_type_info(self, mask_pattern):
data = (self.error_correction << 3) | mask_pattern
bits = util.BCH_type_info(data)

# vertical
for i in range(15):
mod = not test and ((bits >> i) & 1) == 1
mod = ((bits >> i) & 1) == 1

if i < 6:
self.modules[i][8] = mod
Expand All @@ -458,7 +458,7 @@ def setup_type_info(self, test, mask_pattern):

# horizontal
for i in range(15):
mod = not test and ((bits >> i) & 1) == 1
mod = ((bits >> i) & 1) == 1

if i < 8:
self.modules[8][self.modules_count - i - 1] = mod
Expand All @@ -468,7 +468,7 @@ def setup_type_info(self, test, mask_pattern):
self.modules[8][15 - i - 1] = mod

# fixed module
self.modules[self.modules_count - 8][8] = not test
self.modules[self.modules_count - 8][8] = 1

def map_data(self, data, mask_pattern):
inc = -1
Expand Down