Beispiel #1
0
def test_setgetword():
    for dtype in [numpy.ubyte, numpy.int32, numpy.float64]:
        arr = numpy.array(list(range(-256,256)), dtype=dtype)
        arr2 = numpy.zeros(arr.shape, dtype=arr.dtype)
        for i in range (arr.nbytes):
            word, next = bittools.getword (arr,i*8,8)
            bittools.setword (arr2,i*8,8,word)
        assert (arr==arr2).all(),repr((arr,arr2))
    print('ok')
Beispiel #2
0
def test_wordbits ():
    for width in range (1,64+1):
        arr = numpy.array([131,17,235], dtype=numpy.int64)
        arr2 = numpy.zeros((1+width//8), dtype=arr.dtype)
        bstr = tobinary(arr)
        word, next = bittools.getword(arr, 0, width)
        if width > 7:
            assert word==arr[0],repr((word, arr[0], bstr))
        bittools.setword (arr2, 0, width, word)
        assert bittools.getword (arr2, 0, width)[0]==word
        assert tobinary(arr2)[:width] == bstr[:width],repr((tobinary(arr2)[:width], bstr[:width]))
Beispiel #3
0
def encode_bittools(seq, max_bits=12):
    """ Compress sequence using Lempel-Ziv-Welch algorithm for TIFF.

    Parameters
    ----------
    seq : {str, numpy.ndarray}
    max_bits : int
      Specify maximum bits for encoding table.

    Returns
    -------
    bseq : numpy.ndarray

    See also
    --------
    decode_bittools
    """
    if isinstance(seq, numpy.ndarray):
        nbytes = seq.nbytes * 2
        seq = seq.tostring()
    else:
        nbytes = len(seq) * 2
    r = numpy.zeros((nbytes, ), dtype=numpy.ubyte)

    init_table = [(chr(code), code) for code in range(256)]

    table = {}
    table_get = table.get
    table_clear = table.clear
    table_update = table.update

    sup_code2 = (1 << max_bits) - 2
    next_code = CODESTART
    bits = 9
    max_code = (1 << bits)
    s = ''

    table_update(init_table)

    index = setword(r, 0, bits, CODECLEAR, 1)
    for c in seq:
        s1 = s + c
        if s1 in table:
            s = s1
        else:
            index = setword(r, index, bits, table_get(s), 1)
            table[s1] = next_code
            next_code += 1
            s = c
            if next_code == sup_code2:
                index = setword(r, index, bits, table_get(s), 1)
                index = setword(r, index, bits, CODECLEAR, 1)
                s = ''
                table_clear()
                table_update(init_table)
                next_code = CODESTART
                bits = 9
                max_code = (1 << bits)
            elif next_code == max_code:
                bits += 1
                max_code = (1 << bits)
    if s:
        index = setword(r, index, bits, table_get(s), 1)
    index = setword(r, index, bits, CODEEOI)
    bytes = index // 8
    if 8 * bytes < index:
        bytes += 1
    return r[:bytes]
Beispiel #4
0
def encode_bittools(seq, max_bits=12):
    """ Compress sequence using Lempel-Ziv-Welch algorithm for TIFF.

    Parameters
    ----------
    seq : {str, numpy.ndarray}
    max_bits : int
      Specify maximum bits for encoding table.

    Returns
    -------
    bseq : numpy.ndarray

    See also
    --------
    decode_bittools
    """
    if isinstance (seq, numpy.ndarray):
        nbytes = seq.nbytes*2
        seq = seq.tostring()
    else:
        nbytes = len(seq)*2
    r = numpy.zeros((nbytes,), dtype=numpy.ubyte)

    init_table = [(chr(code),code) for code in range (256)]

    table = {}
    table_get = table.get
    table_clear = table.clear
    table_update = table.update

    sup_code2 = (1<<max_bits) - 2
    next_code = CODESTART
    bits = 9
    max_code = (1<<bits)
    s = ''

    table_update(init_table)

    index = setword(r, 0, bits, CODECLEAR, 1)
    for c in seq:
        s1 = s + c
        if s1 in table:
            s = s1
        else:
            index = setword(r, index, bits, table_get(s), 1)
            table[s1] = next_code
            next_code += 1
            s = c
            if next_code==sup_code2:
                index = setword(r, index, bits, table_get(s), 1)
                index = setword(r, index, bits, CODECLEAR, 1)
                s = ''
                table_clear()
                table_update(init_table)
                next_code = CODESTART
                bits = 9
                max_code = (1<<bits)
            elif next_code==max_code:
                bits += 1
                max_code = (1<<bits)
    if s:
        index = setword(r, index, bits, table_get(s), 1)
    index = setword(r, index, bits, CODEEOI)
    bytes = index//8
    if 8*bytes < index:
        bytes += 1
    return r[:bytes]