def run(fn_in, ref_words, dir_out=None, verbose=False): word_bits = 8 txtin, win, hin = mrom.load_txt(open(fn_in, "r"), None, None) print("Loaded %ux x %u h" % (win, hin)) txtdict = mrom.txt2dict(txtin, win, hin) tryi = 0 best_score = 0.0 best_algo_info = None exact_matches = [] for guess_bin, algo_info in guess_layout(txtdict, win, hin, word_bits): exact_match, score = check_binary(guess_bin, ref_words) print("%u match %s, score %0.3f" % (tryi, exact_match, score)) print(" %s" % algo_info) if score > best_score: best_score = score best_algo_info = algo_info if exact_match: exact_matches.append((algo_info, guess_bin)) tryi += 1 print("") print("Best score: %0.3f, %s" % (best_score, best_algo_info)) print("Exact matches: %s" % len(exact_matches)) if dir_out and len(exact_matches): if not os.path.exists(dir_out): os.mkdir(dir_out) for algo_info, guess_bin in exact_matches: fn_out = os.path.join(dir_out, algo_info + ".bin") print(" Writing %s" % fn_out) open(fn_out, "wb").write(guess_bin)
def run(verbose, rom1_fn, rom2_fn, fn_out): txt1, w1, h1 = mrom.load_txt(open(rom1_fn, "r"), None, None) txt2, w2, h2 = mrom.load_txt(open(rom2_fn, "r"), None, None) # strictly speaking don't need w assert, but keep for now assert w1 == w2 and h1 == h2, "size mismatch: left bits %u (%uw x %uh), right bits %u (%uw x %uh)" % ( len(txt1), w1, h1, len(txt2), w2, h2) w = w1 h = h1 wout = w * 2 hout = h print("%uw x %uh + %uw x %uh => %uw x %uh" % (w1, h1, w1, h2, wout, hout)) # txtdict1 = mrom.txt2dict(txt1, w1, h1) txtdict2 = mrom.txt2dict(txt2, w2, h2) txtdict = mrom.txt2dict(txt1, w1, h1) for x in range(w2): for y in range(h2): txtdict[(w1 + x, y)] = txtdict2[(x, y)] mrom.save_txt(open(fn_out, "w"), txtdict, wout, hout)
def run(verbose, rom1_fn, rom2_fn, fn_out=None, grows=None, gcols=None): txt1, w1, h1 = mrom.load_txt(open(rom1_fn, "r"), None, None) txt2, w2, h2 = mrom.load_txt(open(rom2_fn, "r"), None, None) assert w1 == w2 and h1 == h2, "size mismatch: left bits %u (%uw x %uh), right bits %u (%uw x %uh)" % ( len(txt1), w1, h1, len(txt2), w2, h2) l = len(txt1) w = w1 h = h1 print("0x%04X (%u) bytes, %uw x %uh" % (l, l, w, h)) txtdict1 = mrom.txt2dict(txt1, w1, h1) txtdict2 = mrom.txt2dict(txt2, w2, h2) txtdict = mrom.txt2dict(txt1, w2, h2) bitd = {} for x in range(w2): for y in range(h2): l = txtdict1[(x, y)] r = txtdict2[(x, y)] if l == r: continue bitd[(x, y)] = (l, r) txtdict[(x, y)] = "?" print("%u bits different" % len(bitd)) if verbose: for (x, y), (l, r) in sorted(bitd.items()): print(" %ux, %uy: %s vs %s" % (x, y, l, r)) if fn_out: print("Saving %s" % fn_out) mrom.save_txt(open(fn_out, "w"), txtdict, w, h, gcols=gcols, grows=grows)
def run(verbose, fn_ins, fn_out): w = None h = None txtdict = None for fn_in in fn_ins: txt1, w1, h1 = mrom.load_txt(open(fn_in, "r"), None, None) print("Check %s, %uw x %uh" % (fn_in, w1, h1)) if txtdict is None: w = w1 h = h1 else: # strictly speaking don't need w assert, but keep for now assert w1 == w and h1 == h, "size mismatch: left bits %u (%uw x %uh), right bits %u (%uw x %uh)" % ( len(txt1), w1, h1, len(w * h), w, h) txtdict1 = mrom.txt2dict(txt1, w1, h1) if txtdict is None: txtdict = txtdict1 else: for x in range(w): for y in range(h): l = txtdict1[(x, y)] r = txtdict[(x, y)] txtdict[(x, y)] = l if l == r else 'X' syms = dict() for x in range(w): for y in range(h): r = txtdict[(x, y)] syms[r] = syms.get(r, 0) + 1 print("Statistics") for sym in "01X": print(" %s: %u" % (sym, syms[sym])) mrom.save_txt(open(fn_out, "w"), txtdict, w, h)
def run(fn_in, fn_out, invert=None, rotate=None, flipx=False, flipy=False, grows=[], gcols=[], verbose=False): txtin, win, hin = mrom.load_txt(open(fn_in, "r"), None, None) txtdict, wout, hout = munge_txt(txtin, win, hin, rotate=rotate, flipx=flipx, flipy=flipy, invert=invert) # return mrom.dict2txt(txtdict, wout, hout) mrom.save_txt(open(fn_out, "w"), txtdict, wout, hout, grows=grows, gcols=gcols)
def run(fn_in, ref_words, dir_out=None, bin_out=None, txt_out=None, verbose=False, all=False, invert_force=None, rotate_force=None, flipx_force=None, interleave_force=1, interleave_dir_force=None, write_thresh=1.0, word_bits=8, endian_force=None, words=None, layout_alg_force=None): # verbose = True txtin, win, hin = mrom.load_txt(open(fn_in, "r"), None, None) txtbits = win * hin print("Loaded %ux x %u h => %u bits (%u words)" % (win, hin, txtbits, txtbits // word_bits)) if txtbits % word_bits != 0: print("Invalid geometery: got %uw x %uh => %u bits w/ word size %u" % (win, hin, txtbits, word_bits)) return if words is not None and txtbits // word_bits != words: print("Invalid geometery: need %u words but txt has %u words" % (word_bits, txtbits // word_bits)) return # hashdbg(txtin) txtdict = mrom.txt2dict(txtin, win, hin) tryi = 0 best_score = 0.0 best_algo_info = None keep_matches = [] for (can_words, can_bytes), algo_info, txt_base in guess_layout( txtdict, win, hin, word_bits=word_bits, endian_force=endian_force, invert_force=invert_force, rotate_force=rotate_force, flipx_force=flipx_force, interleave_force=interleave_force, interleave_dir_force=interleave_dir_force, layout_alg_force=layout_alg_force, words=words, verbose=verbose): # assert type(can_bytes) == bytearray, type(can_bytes) exact_match = None score = None keep = all if ref_words: exact_match, score = check_binary(can_words, ref_words, word_bits) keep = keep or exact_match or write_thresh and score >= write_thresh if verbose or keep: print("%u match %s, score %0.3f" % (tryi, exact_match, score)) print(" %s" % algo_info) if score > best_score: best_score = score best_algo_info = algo_info if keep: keep_matches.append({ "algorithm": algo_info, "bytes": can_bytes, "can_words": words, "txt_base": txt_base }) tryi += 1 verbose and print("") print("Tries: %u" % tryi) print("Best score: %0.3f, %s" % (best_score, best_algo_info)) print("Keep matches: %s" % len(keep_matches)) if dir_out and len(keep_matches): if not os.path.exists(dir_out): os.mkdir(dir_out) for keep_match in keep_matches: fn_out = os.path.join(dir_out, keep_match["algorithm"] + ".bin") print(" Writing %s" % fn_out) open(fn_out, "wb").write(keep_match["bytes"]) if bin_out is not None: assert len(keep_matches) == 1, len(keep_matches) for keep_match in keep_matches: print(" Writing %s" % (bin_out, )) open(bin_out, "wb").write(keep_match["bytes"]) if txt_out is not None: assert len(keep_matches) == 1, len(keep_matches) for keep_match in keep_matches: print(" Writing %s" % (txt_out, )) open(txt_out, "w").write(keep_match["txt_base"]) return keep_matches, tryi