Esempio n. 1
0
 def __init__(self, infile, outfile):
     self.table = [0] * 256
     self.codes = [0] * 256
     self.infile = infile
     self.outfile = outfile
     self.bitstreamin = bitIO.BitReader(infile)
     self.bitstreamout = bitIO.BitWriter(outfile)
Esempio n. 2
0
# The file paths are declared, either from commandline arguments or simple inputs
if len(sys.argv) == 3:
    inPath = sys.argv[1]
    outPath = sys.argv[2]
else:
    inPath = input('Write name of compressed file: ')
    outPath = input('Write name of decompressed file: ')

# The files are opened in binary read or write mode
# seen as 'rb' and 'wb'
inFile = open(inPath, 'rb')
outFile = open(outPath, 'wb')

# Streams are created
bitstreamin = bitIO.BitReader(inFile)
bitstreamout = bitIO.BitWriter(outFile)

# The block size of the encoding is defined, in bytes
# and the required size of the table is calculated from that
blockSize = 1
tableSize = 2**(blockSize * 8)

# Initialize frequency table as an empty list
table = list()
totalBlocks = 0

# Populate the table, from the huffman encoded file
# We keep track of the number of blocks from the original file
for i in range(tableSize):
    x = bitstreamin.readint32bits()
Esempio n. 3
0
import Huffman
from Element import Element
import PQHeap
'''
Authors:
    Sofie Louise Madsen - [email protected]
    Joachim Bülow       - [email protected]
    Simon Soele Madsen  - [email protected]
'''

#Open the files in readbit and write binary mode
inputfile = open(sys.argv[1], "rb")
outputfile = open(sys.argv[2], "wb")

# Instantiate our bitreader for reading our input file
bitstreamin = bitIO.BitReader(inputfile)

# Array for keeping track of occurences of each character/byte.
occurrences = 256 * [0]


# creates an element for each character/byte holding the byte and its occurence and inserts into a min-heap.
def create_priority_queue():
    n = len(occurrences)
    q = []
    for i in range(n):
        element = Element(occurrences[i], i)
        PQHeap.insert(q, element)
    return q

Esempio n. 4
0
"""
@author: Mathias Østergaard Hansen - mathh17
         Joachim Skovbogaard - Josko20
         Andreas Klauber - ankla20
"""
#%%
import sys
import Encode
import bitIO

#%%
file = open(sys.argv[1], 'rb')
decoded = open(sys.argv[2], 'wb')

bitstreamin = bitIO.BitReader(file)
"""
Build and popluate frequency table
"""
freqTable = [0] * 256
sum_hyp = 0
for i in range(256):
    x = bitstreamin.readint32bits()
    freqTable[i] = x
    sum_hyp += x
"""
Call to Huffman Tree from Encode file to make sure its the same algorithm
"""
hf = Encode.huffmann(freqTable)
"""
Recursively traverses the Huffman tree to find the characters stored in leaves
T: Tree