def encode_file(self, input_file_name, output_file_name): """Use arithmetic coding to encode a file. This method generates a list of arithmetic code ranges for a file and then uses them to write out an encoded version of that file. Arguments: input_file_name - The name of the file to be encoded. output_file_name - The name of the file to write the encoded output to. Return Value(s): None. Side Effects: An arithmetically encoded output file is created. Exceptions Raised: ValueError - Raised when an object's input or output file streams are alreay open. """ if (self._infile is not None) or (self._outfile is not None): raise ValueError('I/O operation on opened file.') if self._static_model: # read through input file and compute ranges self._infile = open(input_file_name, 'rb') self.build_probability_range_list() self._infile.seek(0) # write header with ranges to output file self._outfile = bitfile.BitFile() self._outfile.open(output_file_name, 'wb') self.write_header() else: # initialize probability ranges assuming uniform distribution self.initialize_adaptive_probability_range_list() # open input and output files self._infile = open(input_file_name, 'rb') self._outfile = bitfile.BitFile() self._outfile.open(output_file_name, 'wb') # encode file 1 byte at at time c = self._infile.read(1) while (len(c) != 0): self.apply_symbol_range(c) self.write_encoded_bits() c = self._infile.read(1) self._infile.close() self.apply_symbol_range(EOF_CHAR) # encode an EOF self.write_encoded_bits() self.write_remaining() # write out least significant bits self._outfile.close()
def encode_file(input_file_name, output_file_name): global input_file global output_file # read through input file and compute ranges input_file = open(input_file_name, 'rb') build_probability_range_list() input_file.seek(0) # write header with ranges to output file # ranges must be in header or be stored elsewhere # for using an adaptive model, ranges in the header are easier output_file = bitfile.BitFile() output_file.open(output_file_name, 'wb') write_header() # encode file 1 byte at at time c = input_file.read(1) while (c != ''): apply_symbol_range(c) write_encoded_bits() c = input_file.read(1) input_file.close() # encode an EOF to signify the end of data apply_symbol_range(EOF_CHAR) write_encoded_bits() # write the least significant bits write_remaining() # close the encoded file output_file.close()
def decode_file(input_file_name, output_file_name): global input_file global output_file # open input and build probability ranges from header in file input_file = bitfile.BitFile() input_file.open(input_file_name, 'rb') read_header() # build probability ranges from header in file # read start of code and initialize bounds initialize_decoder() output_file = open(output_file_name, 'wb') # decode one symbol at a time while True: # get the unscaled probability of the current symbol unscaled = get_unscaled_code() # figure out which symbol has the above probability c = get_symbol_from_probability(unscaled) if c == EOF_CHAR: # no more symbols break output_file.write(chr(c)) # factor out symbol apply_symbol_range(c) read_encoded_bits() output_file.close() input_file.close()
def example(num_calls): bf = bitfile.BitFile() # Open bit file for writing. bf.open('testfile', 'w') write_test(bf, num_calls) bf.close() # Now read back writes # Open bit file for reading. bf.open('testfile', 'r') read_test(bf, num_calls) bf.close() # Open bit file for reading and writing. bf.open('testfile', 'r+') write_test(bf, num_calls) # Now read back writes # Go back to the beginning of the file (it was opened with r+). bf.seek(0) read_test(bf, num_calls) bf.close() os.remove('testfile')
def encode_data(input_list, output_file_name): global EOF global PRECISION global MAX_symbol global low global high global code global underflow global cum_prob global output_file global ranges init_en() probability_list(input_list) # create the probability of input list output_file = bitfile.BitFile() output_file.open(output_file_name,'wb') for i in xrange(EOF+2): prob = (ranges[i]) output_file.put_bits_ltom(prob,14) # write the probability of list for c in input_list: apply_symbol_range(c) # encode symbol c write_encoded_bits() apply_symbol_range(EOF) # encode the EOF symbol write_encoded_bits() write_remaining() output_file.close()
def decode_data(input_file_name, output_file_name): global EOF global PRECISION global low global high global cum_prob global output_file global input_file global ranges global code init_de() code = 0 out = [] input_file = bitfile.BitFile() input_file.open(input_file_name, 'rb') ranges = [0 for i in xrange(EOF + 2)] for i in xrange(EOF + 2): c = input_file.get_bits_ltom(14) # get probability of symbol ranges[i] = c cum_prob = sum([(ranges[i + 1] - ranges[i]) for i in xrange(EOF + 1) if (ranges[i + 1] > ranges[i]) ]) # get the sizeof encode symbol for i in xrange(PRECISION): # initialize code code <<= 1 try: next_bit = input_file.get_bit() except EOFError: pass else: code |= next_bit output_file = open(output_file_name, 'wb') while True: # decode unscaled = get_unscaled_code() c = get_symbol_from_probability(unscaled) if c == EOF: break output_file.write(chr(c)) out.append(c) apply_symbol_range(c) read_encoded_bits() output_file.close() input_file.close() return out
def decode_file(self, input_file_name, output_file_name): """Use arithmetic coding to decode a file. This method opens an arithmetically encoded file, reads it's header, and builds a list of probability ranges which it then uses to decode the rest of the file. Arguments: input_file_name - The name of the file to be decoded. output_file_name - The name of the file to write the decoded output to. Return Value(s): None. Side Effects: An arithmetically encoded file is decoded and written to an output file. Exceptions Raised: ValueError - Raised when an object's input or output file streams are alreay open. """ if (self._infile is not None) or (self._outfile is not None): raise ValueError('I/O operation on opened file.') # open input and build probability ranges from header in file self._infile = bitfile.BitFile() self._infile.open(input_file_name, 'rb') if self._static_model: self.read_header() # build probability ranges from header in file else: # initialize probability ranges assuming uniform distribution self.initialize_adaptive_probability_range_list() # read start of code and initialize bounds self.initialize_decoder() self._outfile = open(output_file_name, 'wb') # decode one symbol at a time while True: # get the unscaled probability of the current symbol unscaled = self.get_unscaled_code() # figure out which symbol has the above probability c = self.get_symbol_from_probability(unscaled) if c == EOF_CHAR: # no more symbols break ba = bytearray(1) ba[0] = c self._outfile.write(ba) # factor out symbol self.apply_symbol_range(c) self.read_encoded_bits() self._outfile.close() self._infile.close()
for cs in chip_selects : toplevel_values["chipselect_signals"] += cs.signal() toplevel_values["chipselect_logic"] += cs.logic() # do the substitution and write output file toplevel_in = open("toplevel.vhd", "r") toplevel_out = open(vhdl_fname, "w") toplevel_out.write(string.Template(toplevel_in.read()).substitute(toplevel_values)) # next we generate the data to be merged into the final .fpga file ram_template = "" symbol_values = {} #print instances for i in instances: ram_template += i.ram_template() symbol_values.update(i.symbol_table()) symbol_specs = {} for m in modspecs.values(): symbol_specs.update(m.symbol_table()) bf = bitfile.BitFile() # 't' section - template for RAM bf["t"] = repr(ram_template) # 'v' section - symbol values bf["v"] = repr(symbol_values) # 's' section - symbol specs bf["s"] = repr(symbol_specs) # write to file bf.tofilename(rspec_fname)
if opt == '-h': usage() sys.exit(2) if opt == "-b": bit_fname = value if opt == "-r": rspec_fname = value if opt == "-f": fpga_fname = value for name in bit_fname, rspec_fname, fpga_fname: if len(name) == 0: usage() sys.exit(2) xilinx = bitfile.BitFile.fromfilename(bit_fname) rspec = bitfile.BitFile.fromfilename(rspec_fname) fpga = bitfile.BitFile() # copy stuff from Xilinx bitfile fpga["a"] = xilinx["a"] fpga["b"] = xilinx["b"] fpga["c"] = xilinx["c"] fpga["d"] = xilinx["d"] fpga["e"] = xilinx["e"] # copy stuff from .rspec file fpga["t"] = rspec["t"] fpga["v"] = rspec["v"] fpga["s"] = rspec["s"] # generate stuff for info section info = {} info["bitfile"] = os.path.basename(bit_fname) info["rspec_file"] = os.path.basename(rspec_fname) info["orig_name"] = os.path.basename(fpga_fname)