How to use the segno.encoder.Buffer function in segno

To help you get started, we’ve selected a few segno examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github heuer / segno / tests / test_encoder.py View on Github external
def test_make_error_block0():
    # 
    # 1-M
    codeword = '00100000 01011011 00001011 01111000 11010001 01110010 11011100 01001101 01000011 01000000 11101100 00010001 11101100 00010001 11101100 00010001'
    codeword_ints = [32, 91, 11, 120, 209, 114, 220, 77, 67, 64, 236, 17, 236, 17, 236, 17]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([196, 35, 39, 119, 235, 215, 231, 226, 93, 23])
    ec_infos = consts.ECC[1][consts.ERROR_LEVEL_M]
    assert 1 == len(ec_infos)
    ec_info = ec_infos[0]
    assert 1 == ec_info.num_blocks
    assert 10 == ec_info.num_total - ec_info.num_data
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert len(error_block) == len(error_blocks[0])
    assert error_block == error_blocks[0]
github heuer / segno / tests / test_encoder.py View on Github external
def test_write_segment_alphanumeric_standard_value_example(eci):
    # See ISO/IEC 18004:2006(E) -- 6.4.3 Numeric mode - EXAMPLE (page 26)
    s = b'AC-42'
    seg = encoder.make_segment(s, consts.MODE_ALPHANUMERIC)
    buff = Buffer()
    v, vrange = None, consts.VERSION_RANGE_01_09
    encoder.write_segment(buff, seg, v, vrange, eci=eci)
    assert bits('00100000001010011100111011100111001000010') == buff.getbits()
github heuer / segno / tests / test_encoder.py View on Github external
def test_make_error_block_iso_i2():
    # ISO/IEC 18004:2006(E) - I.2 Encoding a QR Code symbol -- page 94
    # Input: 01234567
    # Symbol: 1-M
    s = '00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001'
    data_block = Buffer(bits(s))
    error_s = '10100101 00100100 11010100 11000001 11101101 00110110 11000111 10000111 00101100 01010101'
    error_block = list(Buffer(bits(error_s)).toints())
    ec_infos = consts.ECC[1][consts.ERROR_LEVEL_M]
    ec_info = ec_infos[0]
    assert ec_info.num_total - ec_info.num_data == len(error_block)
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, data_block)
    assert bytearray(error_block) == error_blocks[0]
github heuer / segno / tests / test_encoder.py View on Github external
def test_make_error_block3():
    # 
    # 5-Q
    codeword = '10110110 11100110 11110111 01110111 00110010 00000111 01110110 10000110 01010111 00100110 01010010 00000110 10000110 10010111 00110010 00000111'
    codeword_ints = [182, 230, 247, 119, 50, 7, 118, 134, 87, 38, 82, 6, 134, 151, 50, 7]
    buff = Buffer(bits(codeword))
    assert codeword_ints == list(buff.toints())
    error_block = bytearray([148, 116, 177, 212, 76, 133, 75, 242, 238, 76, 195, 230, 189, 10, 108, 240, 192, 141])
    ec_infos = (consts.ECC[5][consts.ERROR_LEVEL_Q][1],)
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, buff)
    assert error_block == error_blocks[0]
github heuer / segno / tests / test_encoder.py View on Github external
def test_make_final_message_iso_i3():
    # ISO/IEC 18004:2015(E) - I.3 Encoding a Micro QR Code symbol  -- page 96
    # Input: 01234567
    # Symbol: M2-L
    s = '01000000 00011000 10101100 11000011 00000000'
    codewords = Buffer(bits(s))
    expected_s = '01000000 00011000 10101100 11000011 00000000 10000110 00001101 00100010 10101110 00110000'
    expected = bits(expected_s)
    assert expected == encoder.make_final_message(consts.VERSION_M2, consts.ERROR_LEVEL_L, codewords).getbits()
github heuer / segno / tests / test_encoder.py View on Github external
def test_write_pad_codewords_standard_value_i2():
    # See ISO/IEC 18004:2006(E) -- I.2 Encoding a QR Code symbol (page 94)
    data = bits('00010000 00100000 00001100 01010110 01100001 10000000')
    buff = Buffer(data)
    version = 1
    error = consts.ERROR_LEVEL_M
    capacity = consts.SYMBOL_CAPACITY[version][error]
    encoder.write_pad_codewords(buff, version, capacity, len(buff))
    assert data + bits('11101100000100011110110000010001111011000001000111101100000100011110110000010001') == buff.getbits()
github heuer / segno / tests / test_encoder.py View on Github external
def test_make_error_block_iso_i2():
    # ISO/IEC 18004:2006(E) - I.2 Encoding a QR Code symbol -- page 94
    # Input: 01234567
    # Symbol: 1-M
    s = '00010000 00100000 00001100 01010110 01100001 10000000 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001 11101100 00010001'
    data_block = Buffer(bits(s))
    error_s = '10100101 00100100 11010100 11000001 11101101 00110110 11000111 10000111 00101100 01010101'
    error_block = list(Buffer(bits(error_s)).toints())
    ec_infos = consts.ECC[1][consts.ERROR_LEVEL_M]
    ec_info = ec_infos[0]
    assert ec_info.num_total - ec_info.num_data == len(error_block)
    data_blocks, error_blocks = encoder.make_blocks(ec_infos, data_block)
    assert bytearray(error_block) == error_blocks[0]
github heuer / segno / tests / test_encoder.py View on Github external
def test_write_segment_numeric_standard_value_example2(eci):
    # See ISO/IEC 18004:2006(E) -- 6.4.3 Numeric mode - EXAMPLE 2 (page 25)
    s = b'0123456789012345'
    seg = encoder.make_segment(s, consts.MODE_NUMERIC)
    buff = Buffer()
    v, vrange = consts.VERSION_M3, consts.VERSION_M3
    encoder.write_segment(buff, seg, v, vrange, eci=eci)
    assert bits('0010000000000110001010110011010100110111000010100111010100101') == buff.getbits()
github heuer / segno / segno / encoder.py View on Github external
# Necessary since BYTE < KANJI and find_mode may return KANJI as (more)
    # appropriate mode and the encoder throws an exception
    # if "user provided mode" < "found mode"
    guessed_mode = find_mode(segment_data) if segment_mode != consts.MODE_BYTE else consts.MODE_BYTE
    if segment_mode is not None:
        # Check if user provided mode is applicable for the given segment_data
        if segment_mode < guessed_mode:
            raise ValueError('The provided mode "{0}" is not applicable for {1}. Proposal: {2}'
                             .format(get_mode_name(segment_mode),
                                     repr(segment_data),
                                     get_mode_name(guessed_mode)))
    else:
        segment_mode = guessed_mode
    if segment_mode != consts.MODE_BYTE:
        segment_encoding = None
    buff = Buffer()
    append_bits = buff.append_bits
    if segment_mode == consts.MODE_NUMERIC:
        # ISO/IEC 18004:2015(E) -- 7.4.3 Numeric mode (page 25)
        # The input data string is divided into groups of three digits, and
        # each group is converted to its 10-bit binary equivalent. If the number
        # of input digits is not an exact multiple of three, the final one or
        # two digits are converted to 4 or 7 bits respectively.
        for i in range(0, segment_length, 3):
            chunk = segment_data[i:i + 3]
            append_bits(int(chunk), len(chunk) * 3 + 1)
    elif segment_mode == consts.MODE_ALPHANUMERIC:
        # ISO/IEC 18004:2015(E) -- 7.4.4 Alphanumeric mode (page 26)
        to_byte = consts.ALPHANUMERIC_CHARS.find
        for i in range(0, segment_length, 2):
            chunk = segment_data[i:i + 2]
            # Input data characters are divided into groups of two characters
github heuer / segno / segno / encoder.py View on Github external
:param buff: Byte buffer.
    :return: Byte buffer representing the final message.
    """
    def to_binary(val, length=8):
        return ((val >> i) & 1 for i in reversed(range(length)))

    ec_infos = consts.ECC[version][error]
    data_blocks, error_blocks = make_blocks(ec_infos, buff)
    cw_four = None
    if version in (consts.VERSION_M1, consts.VERSION_M3):
        # All codewords are 8 bit by default, M1 and M3 symbols use 4 bits
        # to represent the last codeword.
        # datablocks[0] is save since Micro QR Codes use just one datablock and
        # one error block
        cw_four = to_binary(data_blocks[0].pop(-1) >> 4, 4)
    res = Buffer()
    # Write codewords
    res.extend(chain(*map(to_binary, (x for x in chain.from_iterable(zip_longest(*data_blocks)) if x is not None))))
    if cw_four is not None:
        res.extend(cw_four)
    # Write error codewords
    res.extend(chain(*map(to_binary, (x for x in chain.from_iterable(zip_longest(*error_blocks)) if x is not None))))
    # ISO/IEC 18004:2015(E) -- 7.6 Constructing the final message codeword sequence
    # [...] In certain QR Code versions, however, where the number of modules
    # available for data and error correction codewords is not an exact multiple
    # of 8, there may be a need for 3, 4 or 7 Remainder Bits to be appended to
    # the final message bit stream in order to fill exactly the number of
    # modules in the encoding region
    remainder = 0
    if version in (2, 3, 4, 5, 6):
        remainder = 7
    elif version in (14, 15, 16, 17, 18, 19, 20, 28, 29, 30, 31, 32, 33, 34):