def _lost_point_level3(modules, modules_count): modules_range_short = xrange(modules_count-6) lost_point = 0 for row in xrange(modules_count): this_row = modules[row] for col in modules_range_short: if (this_row[col] and not this_row[col + 1] and this_row[col + 2] and this_row[col + 3] and this_row[col + 4] and not this_row[col + 5] and this_row[col + 6]): lost_point += 40 for col in xrange(modules_count): for row in modules_range_short: if (modules[row][col] and not modules[row + 1][col] and modules[row + 2][col] and modules[row + 3][col] and modules[row + 4][col] and not modules[row + 5][col] and modules[row + 6][col]): lost_point += 40 return lost_point
def _lost_point_level4(modules, modules_count): modules_range = xrange(modules_count) dark_count = 0 for row in modules_range: this_row = modules[row] for col in modules_range: if this_row[col]: dark_count += 1 ratio = abs(100 * dark_count / modules_count / modules_count - 50) / 5 return ratio * 10
def _lost_point_level3(modules, modules_count): modules_range_short = xrange(modules_count - 6) lost_point = 0 for row in xrange(modules_count): this_row = modules[row] for col in modules_range_short: if (this_row[col] and not this_row[col + 1] and this_row[col + 2] and this_row[col + 3] and this_row[col + 4] and not this_row[col + 5] and this_row[col + 6]): lost_point += 40 for col in xrange(modules_count): for row in modules_range_short: if (modules[row][col] and not modules[row + 1][col] and modules[row + 2][col] and modules[row + 3][col] and modules[row + 4][col] and not modules[row + 5][col] and modules[row + 6][col]): lost_point += 40 return lost_point
def _lost_point_level1(modules, modules_count): lost_point = 0 modules_range = xrange(modules_count) row_range_first = (0, 1) row_range_last = (-1, 0) row_range_standard = (-1, 0, 1) col_range_first = ((0, 1), (1,)) col_range_last = ((-1, 0), (-1,)) col_range_standard = ((-1, 0, 1), (-1, 1)) for row in modules_range: if row == 0: row_range = row_range_first elif row == modules_count-1: row_range = row_range_last else: row_range = row_range_standard for col in modules_range: sameCount = 0 dark = modules[row][col] if col == 0: col_range = col_range_first elif col == modules_count-1: col_range = col_range_last else: col_range = col_range_standard for r in row_range: row_offset = row + r if r != 0: col_idx = 0 else: col_idx = 1 for c in col_range[col_idx]: if dark == modules[row_offset][col + c]: sameCount += 1 if sameCount > 5: lost_point += (3 + sameCount - 5) return lost_point
def _lost_point_level1(modules, modules_count): lost_point = 0 modules_range = xrange(modules_count) row_range_first = (0, 1) row_range_last = (-1, 0) row_range_standard = (-1, 0, 1) col_range_first = ((0, 1), (1, )) col_range_last = ((-1, 0), (-1, )) col_range_standard = ((-1, 0, 1), (-1, 1)) for row in modules_range: if row == 0: row_range = row_range_first elif row == modules_count - 1: row_range = row_range_last else: row_range = row_range_standard for col in modules_range: sameCount = 0 dark = modules[row][col] if col == 0: col_range = col_range_first elif col == modules_count - 1: col_range = col_range_last else: col_range = col_range_standard for r in row_range: row_offset = row + r if r != 0: col_idx = 0 else: col_idx = 1 for c in col_range[col_idx]: if dark == modules[row_offset][col + c]: sameCount += 1 if sameCount > 5: lost_point += (3 + sameCount - 5) return lost_point
def write(self, buffer): if self.mode == MODE_NUMBER: for i in xrange(0, len(self.data), 3): chars = self.data[i:i + 3] bit_length = NUMBER_LENGTH[len(chars)] buffer.put(int(chars), bit_length) elif self.mode == MODE_ALPHA_NUM: for i in xrange(0, len(self.data), 2): chars = self.data[i:i + 2] if len(chars) > 1: buffer.put( ALPHA_NUM.find(chars[0]) * 45 + ALPHA_NUM.find(chars[1]), 11) else: buffer.put(ALPHA_NUM.find(chars), 6) else: if six.PY3: # Iterating a bytestring in Python 3 returns an integer, # no need to ord(). data = self.data else: data = [ord(c) for c in self.data] for c in data: buffer.put(c, 8)
def _lost_point_level2(modules, modules_count): lost_point = 0 modules_range = xrange(modules_count - 1) for row in modules_range: this_row = modules[row] next_row = modules[row+1] for col in modules_range: count = 0 if this_row[col]: count += 1 if next_row[col]: count += 1 if this_row[col + 1]: count += 1 if next_row[col + 1]: count += 1 if count == 0 or count == 4: lost_point += 3 return lost_point
def _lost_point_level2(modules, modules_count): lost_point = 0 modules_range = xrange(modules_count - 1) for row in modules_range: this_row = modules[row] next_row = modules[row + 1] for col in modules_range: count = 0 if this_row[col]: count += 1 if next_row[col]: count += 1 if this_row[col + 1]: count += 1 if next_row[col + 1]: count += 1 if count == 0 or count == 4: lost_point += 3 return lost_point
G15 = ( (1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)) G18 = ( (1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0)) G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1) PAD0 = 0xEC PAD1 = 0x11 # Precompute bit count limits, indexed by error correction level and code size _data_count = lambda block: block.data_count BIT_LIMIT_TABLE = [ [0] + [8*sum(map(_data_count, base.rs_blocks(version, error_correction))) for version in xrange(1, 41)] for error_correction in xrange(4) ] def BCH_type_info(data): d = data << 10 while BCH_digit(d) - BCH_digit(G15) >= 0: d ^= (G15 << (BCH_digit(d) - BCH_digit(G15))) return ((data << 10) | d) ^ G15_MASK def BCH_type_number(data): d = data << 12 while BCH_digit(d) - BCH_digit(G18) >= 0:
[6, 30, 58, 86, 114, 142, 170]] G15 = ((1 << 10) | (1 << 8) | (1 << 5) | (1 << 4) | (1 << 2) | (1 << 1) | (1 << 0)) G18 = ((1 << 12) | (1 << 11) | (1 << 10) | (1 << 9) | (1 << 8) | (1 << 5) | (1 << 2) | (1 << 0)) G15_MASK = (1 << 14) | (1 << 12) | (1 << 10) | (1 << 4) | (1 << 1) PAD0 = 0xEC PAD1 = 0x11 # Precompute bit count limits, indexed by error correction level and code size _data_count = lambda block: block.data_count BIT_LIMIT_TABLE = [[0] + [ 8 * sum(map(_data_count, base.rs_blocks(version, error_correction))) for version in xrange(1, 41) ] for error_correction in xrange(4)] def BCH_type_info(data): d = data << 10 while BCH_digit(d) - BCH_digit(G15) >= 0: d ^= (G15 << (BCH_digit(d) - BCH_digit(G15))) return ((data << 10) | d) ^ G15_MASK def BCH_type_number(data): d = data << 12 while BCH_digit(d) - BCH_digit(G18) >= 0: d ^= (G18 << (BCH_digit(d) - BCH_digit(G18)))