def get_frequencies(filepath):
	freqs = arithmeticcoding.SimpleFrequencyTable([0] * 257)
	with open(filepath, "rb") as input:
		while True:
			b = input.read(1)
			if len(b) == 0:
				break
			b = b[0] if python3 else ord(b)
			freqs.increment(b)
	return freqs
def read_frequencies(bitin):
    def read_int(n):
        result = 0
        for _ in range(n):
            result = (result << 1) | bitin.read_no_eof()  # Big endian
        return result

    freqs = [read_int(32) for _ in range(256)]
    freqs.append(1)  # EOF symbol
    return arithmeticcoding.SimpleFrequencyTable(freqs)
def decompress(bitin, out):
    initfreqs = arithmeticcoding.FlatFrequencyTable(257)
    freqs = arithmeticcoding.SimpleFrequencyTable(initfreqs)
    dec = arithmeticcoding.ArithmeticDecoder(32, bitin)
    while True:
        # Decode and write one byte
        symbol = dec.read(freqs)
        if symbol == 256:  # EOF symbol
            break
        out.write(bytes((symbol, )))
        freqs.increment(symbol)
def compress(inp, bitout):
    initfreqs = arithmeticcoding.FlatFrequencyTable(257)
    freqs = arithmeticcoding.SimpleFrequencyTable(initfreqs)
    enc = arithmeticcoding.ArithmeticEncoder(32, bitout)
    while True:
        # Read and encode one byte
        symbol = inp.read(1)
        if len(symbol) == 0:
            break
        symbol = symbol[0] if python3 else ord(symbol)
        enc.write(freqs, symbol)
        freqs.increment(symbol)
    enc.write(freqs, 256)  # EOF
    enc.finish()  # Flush remaining code bits
示例#5
0
 def __init__(self, symbols, hassubctx):
     self.frequencies = arithmeticcoding.SimpleFrequencyTable([0] *
                                                              symbols)
     self.subcontexts = ([None] * symbols) if hassubctx else None