Beispiel #1
0
def compress(tree, uncompressed, compressed):
	'''First write the given tree to the stream 'compressed' using the
	write_tree function. Then use the same tree to encode the data
	from the input stream 'uncompressed' and write it to 'compressed'.
	If there are any partially-written bytes remaining at the end,
	write 0 bits to form a complete byte.

	Flush the bitwriter after writing the entire compressed file.

	Args:
	tree: A Huffman tree.
	uncompressed: A file stream from which you can read the input.
	compressed: A file stream that will receive the tree description
	and the coded input data.
	'''
	##let compressed be an instance of bitio.BitWriter
	Target = bitio.BitWriter(compressed)
	#let uncompressed be an instance of bitio.BitReader
	readingfile = bitio.BitReader(uncompressed)
	#Write the Huffman tree to compressed file.
	write_tree(tree, Target)

	#to get encoding table 
	encode = huffman.make_encoding_table(tree)
	while True:
		# try EOFError
		try:
			# can read a byte ?
			reading = readingfile.readbits(8)	
		except EOFError:
			# reach EOF encode eof,then get out of loop
			Target.writebit(0)
			Target.writebit(0)
			break
		#if no EOFError, get the sequence from tree using encoding table

		value = encode[reading]
		for v in value:
			# write v as a bit
			Target.writebit(v)

	Target.flush()
def compress(tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
    '''
    bitstream = bitio.BitWriter(compressed)
    write_tree(tree, bitstream)

    enc_table = huffman.make_encoding_table(tree)  # Create encoding table
    end_char = enc_table[None]  # Get end char

    input_stream = uncompressed.read(1)  # Read inn one byte

    while input_stream:

        input_char = ord(input_stream)  # Convert to binary
        compressed_char = enc_table[input_char]  # Get compressed byte

        for bit in compressed_char:  # Print the compressed byte
            if bit:
                bitstream.writebit(1)
            else:
                bitstream.writebit(0)

        input_stream = uncompressed.read(1)  # Read the next byte

    # Print end bit
    for bit in end_char:
        if bit:
            bitstream.writebit(1)
        else:
            bitstream.writebit(0)

    bitstream.flush()
Beispiel #3
0
def compress(tree, uncompressed, compressed):
    '''
    First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Flush the bitwriter after writing the entire compressed file.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
  '''
    # The given tree is written to the compressed
    write_tree(tree, compressed)
    # The bit writer object is created
    compressed_ = bitio.BitWriter(compressed)
    # The bit reader object is created
    uncompressed_ = bitio.BitReader(uncompressed)
    # Encoding table is made here
    encoding_table = huffman.make_encoding_table(tree)
    flag1 = 1
    while flag1 == 1:
        # To catch the End of File error
        try:
            # Reading 8 bits (byte)
            current_bits = uncompressed_.readbits(8)
            # These are stored in an encoding table
            compressed1 = encoding_table[current_bits]
            for bits in compressed1:
                compressed_.writebit(bits)

        except EOFError:
            for bits in encoding_table[None]:
                compressed_.writebit(bits)
                # Where the while loop will end
                flag1 = 0
    compressed_.flush()
Beispiel #4
0
def compress (tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
    '''
    #reader reads from the decoded uncompressed file
    reader = bitio.BitReader(uncompressed)
    #writer writes to a encoded compressed file
    writer = bitio.BitWriter(compressed)
    #intialize the table
    table = huffman.make_encoding_table(tree)
    #write the tree to file
    write_tree(tree, writer)

    while True:
        #read the encoded bits from table and write to file
        try:
            original_bits = reader.readbits(8)
            encoded_bits = table[original_bits]
            for i in encoded_bits:
                writer.writebit(i)
        #when end of file reached, write the end code and pad
        #the remaining bits with 0 to complete the byte
        except EOFError:
            encoded_bits = table[None]
            for j in encoded_bits:
                writer.writebit(j)
            remaining_bits = writer.bcount%8
            writer.writebits(0, remaining_bits)
            writer.flush()
            break
Beispiel #5
0
def compress(tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
        write_tree function. Then use the same tree to encode the data
        from the input stream 'uncompressed' and write it to 'compressed'.
        If there are any partially-written bytes remaining at the end,
        write 0 bits to form a complete byte.

        Flush the bitwriter after writing the entire compressed file.

        Args:
        tree: A Huffman tree.
        uncompressed: A file stream from which you can read the input.
        compressed: A file stream that will receive the tree description
        and the coded input data.
    '''
    # writes to encoded compressed file
    compressedstream = bitio.BitWriter(compressed)
    # reads from decoded uncompressed file
    uncompstream = bitio.BitReader(uncompressed)
    enctable = huffman.make_encoding_table(tree) # make encoding table
    write_tree(tree, compressedstream) # get that table sis

    while True: # read each bit and write to file until done
        try:
            current = uncompstream.readbits(8) # read 8 bits
            comptable = enctable[current] # store bits in encoding table
            for bit in comptable:
                compressedstream.writebit(bit)

        except EOFError: # when done going thru bytes
            for bit in enctable[None]:
                compressedstream.writebit(bit) # write EOF sequence
            print("EOF")
            break

    compressedstream.flush() # flush since done writing entire compressed file
Beispiel #6
0
def compress(tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Flush the bitwriter after writing the entire compressed file.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
    '''
    #set up reader and writer
    reader = bitio.BitReader(uncompressed)
    writer = bitio.BitWriter(compressed)

    #write tree to output file
    write_tree(tree, writer)
    #construct encoding table from tree
    encoder = huffman.make_encoding_table(tree)

    while (True):
        try:
            #read in a byte and find its bit representation using the
            #encoding table
            byte = reader.readbits(8)
            sequence = encoder[byte]
            #write out the sequence of bits
            for bit in sequence:
                writer.writebit(bit)
        #when eof occurs, break from loop you are done writing the file
        except EOFError:
            break
Beispiel #7
0
def compress(tree, uncompressed, compressed):
    # write the given tree to the stream 'compressed' using the write_tree function
    bitwriter = bitio.BitWriter(compressed)
    write_tree(tree, bitwriter)

    # for reading bytes from the uncompressed input file
    bitreader = bitio.BitReader(uncompressed)

    # The function huffman.make_encoding_table takes tree and produces a dictionary
    # mapping bytes to bit sequences; constructing this dictionary once before you
    # start coding the message
    encodingTable = huffman.make_encoding_table(tree)

    # # for terminating the program
    # reachEnd = False

    # Read each byte from the uncompressed input file
    while True:
        try:
            inputByte = bitreader.readbits(8)
            # encode it using tree
            encoded = encodingTable[inputByte]
            for i in encoded:
                if i == True:
                    bitwriter.writebit(1)
                else:
                    bitwriter.writebit(0)
        except EOFError:
            encoded = encodingTable[None]
            for i in encoded:
                if i == True:
                    bitwriter.writebit(1)
                else:
                    bitwriter.writebit(0)
            break
    bitwriter.flush()
Beispiel #8
0
def compress(tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Flush the bitwriter after writing the entire compressed file.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
    '''
    # first write the tree to new file using pickle and initialize bitWriter on file
    write_tree(tree, compressed)
    writeout = bitio.BitWriter(compressed)

    # encoding table for compression
    table = huffman.make_encoding_table(tree)
    
    # begin reading bytes from uncompressed file
    readin = bitio.BitReader(uncompressed)
    not_end_file = True
    
    while not_end_file:
          try:
            # look up each byte in encoding table
            for x in table[readin.readbits(8)]:
                  # print out new encoded value bit by bit
                  writeout.writebit(x)
          except EOFError:
            not_end_file = False
            writeout.flush()
    writeout.flush()
Beispiel #9
0
    util.write_tree(tree, writer)
    writer.flush()

with open("simple.txt", 'rb') as file:
    reader = bitio.BitReader(file)
    new_tree = util.read_tree(reader)

with open("simple.txt", 'wb') as file:
    writer = bitio.BitWriter(file)

    print("Hey bitch")
    util.write_tree(new_tree, writer)
    writer.flush()

#==============================================================================
#==============================================================================
with open("simple.txt",'wb') as f:
    f.write(b'00')
with open("simple.txt", 'rb') as file:
    reader = bitio.BitReader(file)
    tree = huffman.TreeBranch(huffman.TreeBranch(huffman.TreeLeaf(ord('A')),huffman.TreeLeaf(None)),huffman.TreeLeaf(ord('B')))
    print(util.decode_byte(tree,reader))
"""
tree = huffman.TreeBranch(
    huffman.TreeBranch(huffman.TreeLeaf(ord('A')), huffman.TreeLeaf(None)),
    huffman.TreeLeaf(ord('B')))
table = huffman.make_encoding_table(tree)
encoded = table[65]
for bit in table[None]:
    print(bit)
Beispiel #10
0
def compress(tree, uncompressed, compressed):
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
    # This function writes the given tree to the stream 'compressed' using the
    # write_tree function. Then use the same tree to encode the data
    # from the input stream 'uncompressed' and write it to 'compressed'.
    # If there are any partially-written bytes remaining at the end,
    # write 0 bits to form a complete byte.

    # Flush the bitwriter after writing the entire compressed file.

    # Args:
    #    tree: A Huffman tree.
    #    uncompressed: A file stream from which you can read the input.
    #    compressed: A file stream that will receive the tree description
    #                and the coded input data.
    # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #

    # checking if the file is readable or not before we compress
    check_mode(uncompressed, compressed)

    # writing the created tree to the tree file stream
    if pkl:
        write_tree(tree, compressed)

    # optional debugging output
    if debug >= 1:
        print('The tree is: ', tree)
        print()
        print('The root is: ', tree.root)
        print()

    # mapping the bytes to bit sequences to a dictionary
    bit2bytemap = huffman.make_encoding_table(tree.root)

    # optional debugging output
    if debug >= 1:
        print('The encoding table is: ', bit2bytemap)
        print()

    # creating bit reader object
    bitread = bitio.BitReader(uncompressed)

    # creating bit writer object
    bitwrite = bitio.BitWriter(compressed)

    # We don't know how many bytes there are, need to break
    # this loop once we reach end of file
    while True:
        # read each byte from the uncompressed file, encode it using the tree
        # io can fail... must cover it safely.
        try:
            byte = bitread.readbits(8)
            if debug > 1:
                print('The byte read is: ', byte)
                print('The character is: ', chr(byte))

            # find the corresponding bit sequence in our dictionary
            # and write to the compressed file stream
            if byte in bit2bytemap:
                # go through the bit sequence that corresponds
                # to the bits read in
                for i in range(len(bit2bytemap[byte])):

                    if debug > 1:
                        print(bit2bytemap[byte][i])

                    # If bit read is a True (1) or False (0)...
                    if bit2bytemap[byte][i] is True:
                        bitwrite.writebit(True)

                    elif bit2bytemap[byte][i] is False:
                        bitwrite.writebit(False)

        # If no more bits to read, break out of the while loop
        except EOFError:
            break

    # when writing to a disk, the OS 'buffers' the writing of the
    # bit for performance reasons, because there is buffering, when
    # you are done writing you have to tell the system that you are
    # done with writing. You need to flush and close...
    flushIt(bitwrite, compressed)
    pass
Beispiel #11
0
def compress(tree, uncompressed, compressed):
    '''First write the given tree to the stream 'compressed' using the
    write_tree function. Then use the same tree to encode the data
    from the input stream 'uncompressed' and write it to 'compressed'.
    If there are any partially-written bytes remaining at the end,
    write 0 bits to form a complete byte.

    Flush the bitwriter after writing the entire compressed file.

    Args:
      tree: A Huffman tree.
      uncompressed: A file stream from which you can read the input.
      compressed: A file stream that will receive the tree description
          and the coded input data.
    '''
    # write the tree to the compressed file
    write_tree(tree, compressed)

    # make an encoding table for compression
    encoding_table = huffman.make_encoding_table(tree)

    # Instantiate the Bit Writer and Bit Reader objects
    write_bit = bitio.BitWriter(compressed)
    bitreading = bitio.BitReader(uncompressed)

    # Continusly write the new bit sequences to the compresses file stream
    # until EOF of the uncompressed stream is reached
    write_fil = 1
    numbits = 0

    while write_fil:

        try:
            # Read the full uncompressed byte and find its encoded bit pattern
            onebyte = bitreading.readbits(8)
            comp_bits = encoding_table[onebyte]

            # Write the appropriate bits based on the encoded bit pattern
            for i in range(len(comp_bits)):
                if comp_bits[i] == True:
                    write_bit.writebit(True)
                elif comp_bits[i] == False:
                    write_bit.writebit(False)
                numbits += 1

        except:
            write_fil = 0

    # After writing the compressed bit sequences for the contents of the
    #uncompressed input stream, add the end of file bit sequence

    end_of_file = encoding_table[None]
    for i in range(len(end_of_file)):
        if end_of_file[i] == True:
            write_bit.writebit(True)
        elif end_of_file[i] == False:
            write_bit.writebit(False)
        numbits += 1

    # After counting alL the bits that were written, if the total is not a
    # multiple of 8, add zeros to form complete bytes
    add_zeros = numbits % 8
    for i in range(add_zeros):
        write_bit.writebit(False)

    # flush the bitwriter at the end of writing bits
    write_bit.flush()
    return