Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _make_figure22_matrix():
version = consts.VERSION_M4
matrix = encoder.make_matrix(version)
for row in matrix:
for i in range(len(row)):
if row[i] == 0x2:
row[i] = 0x0
encoder.add_finder_patterns(matrix, True)
return matrix
def test_thonky_add_format_info():
#
version = 1
matrix = encoder.make_matrix(version, reserve_regions=False)
encoder.add_finder_patterns(matrix, is_micro=False)
encoder.add_format_info(matrix, version, consts.ERROR_LEVEL_L, 4)
ref_matrix = read_matrix('thonky_format')
assert ref_matrix == matrix
def test_codeword_placement_iso_i3():
# ISO/IEC 18004:2015(E) - page 96
# 01234567 as M2-L symbol
s = '01000000 00011000 10101100 11000011 00000000'
codewords = Buffer(bits(s))
version = consts.VERSION_M2
buff = encoder.make_final_message(version, consts.ERROR_LEVEL_L, codewords)
expected_s = '01000000 00011000 10101100 11000011 00000000 10000110 00001101 00100010 10101110 00110000'
expected = bits(expected_s)
assert expected == buff.getbits()
matrix = encoder.make_matrix(version)
encoder.add_finder_patterns(matrix, is_micro=version < 1)
encoder.add_codewords(matrix, buff, version=version)
ref_matrix = read_matrix('iso-i3_code_placement')
assert ref_matrix == matrix
def test_codeword_placement_iso_i2():
# ISO/IEC 18004:2015(E) - page 94
# 01234567 as 1-M symbol
s = '00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001'
codewords = Buffer(bits(s))
version = 1
buff = encoder.make_final_message(version, consts.ERROR_LEVEL_M, codewords)
expected_s = '00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 10100101 00100100 11010100 11000001 11101101 00110110 11000111 10000111 00101100 01010101'
expected = bits(expected_s)
assert expected == buff.getbits()
matrix = encoder.make_matrix(version)
encoder.add_finder_patterns(matrix, is_micro=version < 1)
encoder.add_codewords(matrix, buff, version=version)
ref_matrix = read_matrix('iso-i2_code_placement')
assert ref_matrix == matrix
def test_thonky_add_version_info():
#
version = 7
matrix = encoder.make_matrix(version, reserve_regions=False)
encoder.add_finder_patterns(matrix, is_micro=False)
encoder.add_alignment_patterns(matrix, version)
encoder.add_version_info(matrix, version)
matrix[-8][8] = 0x1 # dark module
ref_matrix = read_matrix('thonky_version')
assert ref_matrix == matrix
:param matrix: An iterable of bytearrays.
:param int version: A version constant.
:param int scale: The scaling factor (default: ``1``).
:param int border: The border size or ``None`` to specify the
default quiet zone (4 for QR Codes, 2 for Micro QR Codes).
:raises: :py:exc:`ValueError` if an illegal scale or border value is provided
"""
from segno import encoder
check_valid_border(border)
scale = int(scale)
check_valid_scale(scale)
border = get_border(version, border)
width, height = get_symbol_size(version, scale=1, border=0)
is_micro = version < 1
# Create an empty matrix with invalid 0x2 values
alignment_matrix = encoder.make_matrix(version, reserve_regions=False, add_timing=False)
encoder.add_alignment_patterns(alignment_matrix, version)
def get_bit(i, j):
# Check if we operate upon the matrix or the "virtual" border
if 0 <= i < height and 0 <= j < width:
val = matrix[i][j]
if not is_micro:
# Alignment pattern
alignment_val = alignment_matrix[i][j]
if alignment_val != 0x2:
return (consts.TYPE_ALIGNMENT_PATTERN_LIGHT, consts.TYPE_ALIGNMENT_PATTERN_DARK)[alignment_val]
if version > 6: # Version information
if i < 6 and width - 12 < j < width - 8 \
or height - 12 < i < height - 8 and j < 6:
return (consts.TYPE_VERSION_LIGHT, consts.TYPE_VERSION_DARK)[val]
# Dark module