def compress(inp, bitout):
    # Set up encoder and model. In this PPM model, symbol 256 represents EOF;
    # its frequency is 1 in the order -1 context but its frequency
    # is 0 in all other contexts (which have non-negative order).
    enc = arithmeticcoding.ArithmeticEncoder(bitout)
    model = ppmmodel.PpmModel(MODEL_ORDER, 257, 256)
    history = []

    while True:
        # Read and encode one byte
        symbol = inp.read(1)
        if len(symbol) == 0:
            break
        symbol = symbol[0] if python3 else ord(symbol)
        encode_symbol(model, history, symbol, enc)
        model.increment_contexts(history, symbol)

        if model.model_order >= 1:
            # Append current symbol or shift back by one
            if len(history) == model.model_order:
                del history[0]
            history.append(symbol)

    encode_symbol(model, history, 256, enc)  # EOF
    enc.finish()  # Flush remaining code bits
def compress(inp, bitout):
    """
    Set up encoder and model. In this PPM model, symbol 256 represents EOF. Its frequency is 1 in the order -1
    context but its frequency is 0 in all other contexts (which have non-negative order).
    """
    enc = arithmeticcoding.ArithmeticEncoder(32, bitout)
    model = ppmmodel.PpmModel(MODEL_ORDER, 257, 256)
    history = []

    while True:
        # Read and encode one byte
        symbol = inp.read(1)
        if len(symbol) == 0:
            break
        symbol = symbol[0]
        encode_symbol(model, history, symbol, enc)
        model.increment_contexts(history, symbol)

        if model.model_order >= 1:
            # Prepend current symbol, dropping oldest symbol if necessary
            if len(history) == model.model_order:
                history.pop()
            history.insert(0, symbol)

    encode_symbol(model, history, 256, enc)  # EOF
    # Flush remaining code bits
    enc.finish()
def decompress(bitin, out):
    # Set up decoder and model
    dec = arithmeticcoding.ArithmeticDecoder(bitin)
    model = ppmmodel.PpmModel(MODEL_ORDER, 257, 256)
    history = []

    while True:
        # Decode and write one byte
        symbol = decode_symbol(dec, model, history)
        if symbol == 256:  # EOF symbol
            break
        out.write(bytes((symbol, )) if python3 else chr(symbol))
        model.increment_contexts(history, symbol)

        if model.model_order >= 1:
            # Append current symbol or shift back by one
            if len(history) == model.model_order:
                del history[0]
            history.append(symbol)
Exemple #4
0
def decompress(bitin, out):
	# Set up decoder and model. In this PPM model, symbol 256 represents EOF;
	# its frequency is 1 in the order -1 context but its frequency
	# is 0 in all other contexts (which have non-negative order).
	dec = arithmeticcoding.ArithmeticDecoder(32, bitin)
	model = ppmmodel.PpmModel(MODEL_ORDER, 257, 256)
	history = []
	
	while True:
		# Decode and write one byte
		symbol = decode_symbol(dec, model, history)
		if symbol == 256:  # EOF symbol
			break
		out.write(bytes((symbol,)) if python3 else chr(symbol))
		model.increment_contexts(history, symbol)
		
		if model.model_order >= 1:
			# Prepend current symbol, dropping oldest symbol if necessary
			if len(history) == model.model_order:
				history.pop()
			history.insert(0, symbol)
def compress(inp, bitout):
    # Set up encoder and model
    enc = arithmeticcoding.ArithmeticEncoder(bitout)
    model = ppmmodel.PpmModel(MODEL_ORDER, 257, 256)
    history = []

    while True:
        # Read and encode one byte
        symbol = inp.read(1)
        if len(symbol) == 0:
            break
        symbol = symbol[0] if python3 else ord(symbol)
        encode_symbol(model, history, symbol, enc)
        model.increment_contexts(history, symbol)

        if model.model_order >= 1:
            # Append current symbol or shift back by one
            if len(history) == model.model_order:
                del history[0]
            history.append(symbol)

    encode_symbol(model, history, 256, enc)  # EOF
    enc.finish()  # Flush remaining code bits