import sys def try_decode_scanline(data, width, max_height=2000): """PCX-style per-scanline RLE decode: for each row, decode exactly `width` pixels (excess from a run discarded), continue until input consumed. Returns (rows_pixels_list, bytes_consumed, ok, error_msg).""" i = 0 n = len(data) rows = [] row = [] while i < n: if len(row) >= width: rows.append(row) row = [] if len(rows) > max_height: return rows, i, False, "too many rows" continue b = data[i] if (b & 0xC0) == 0xC0: count = b & 0x3F i += 1 if i >= n: return rows, i, False, "truncated run (missing value byte)" val = data[i] i += 1 need = width - len(row) take = min(count, need) row.extend([val]*take) # NOTE: if count > need, the overshoot pixels for the *next* row # are DISCARDED per classic PCX semantics (not carried over) else: row.append(b) i += 1 if row: # leftover partial row at EOF rows.append(row) return rows, i, False, f"EOF with partial row of {len(row)} px" return rows, i, True, "clean" def scan_widths(data, wmin=4, wmax=250): results = [] for w in range(wmin, wmax+1): rows, consumed, ok, msg = try_decode_scanline(data, w) # figure out leftover partial-row size from msg if present leftover = None if "partial row of" in msg: leftover = int(msg.split("partial row of")[1].split("px")[0].strip()) elif msg == "clean": leftover = 0 results.append((w, len(rows), leftover, ok, msg)) return results base = "/home/aria/kellogg_remake/extracted_pre/" files = ["A.BOB","B.BOB","E.BOB","N.BOB","O.BOB","D.BOB","J.BOB"] for fn in files: data = open(base+fn,"rb").read() print(f"=== {fn} (size={len(data)}) ===") results = scan_widths(data) clean = [r for r in results if r[2]==0] if clean: for r in clean: print(" CLEAN (leftover=0):", r) else: print(" no width in range gives leftover==0")