Skip to content

Commit

Permalink
+visualising the unknown areas
Browse files Browse the repository at this point in the history
  • Loading branch information
rjoberon committed Apr 21, 2024
1 parent 5641326 commit d0522d6
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
12 changes: 11 additions & 1 deletion _posts/2024-04-23-searching-for-the-index.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,14 @@ dd if=dsatnord.mp of=un3.dat bs=1 skip=13522709 count=2672062
dd if=dsatnord.mp of=un4.dat bs=1 skip=644833451 count=460
```

Now let
Now let us visualise the two larger segments `un2.dat` and `un3.dat`
as follows: we interpret each byte as a greyscale value in the range
0...255:

![](/img/un2.dat)

(image created with `./src/mp.py -c vis_bytes --out img/un2.png un2.dat`)

![](/img/un3.dat)

(image created with `./src/mp.py -c vis_bytes --out img/un3.png un3.dat`)
Binary file added img/un2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/un3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
32 changes: 30 additions & 2 deletions src/mp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# Changes:
# 2024-04-21 (rja)
# - implemented vis_content and accompanying functions
# - implemented vis_bytes and dump_bytes
# 2024-04-20 (rja)
# - added support to extract tiles
# 2024-04-02 (rja)
Expand All @@ -21,7 +22,7 @@
import os
import cod

version = "0.0.2"
version = "0.0.3"


def dump_offsets(fname):
Expand Down Expand Up @@ -170,10 +171,33 @@ def extract_tile(finname, foutname, offset):
fout.write(b)


def vis_bytes(fname, foutname):
with open(fname, "rb") as f:
fbytes = os.path.getsize(fname) # file size
width = 1024
height = fbytes // width + 1 # image height
img = Image.new('L', (width, height), "black")
pixels = img.load()
for pos in range(os.path.getsize(fname)):
f.seek(pos)
b = f.read(1)
pixels[pos % width, pos // width] = int.from_bytes(b, signed=False)
img.save(foutname, "PNG")


def dump_bytes(fname):
with open(fname, "rb") as f:
for pos in range(os.path.getsize(fname)):
f.seek(pos)
b = f.read(4)
lint = int.from_bytes(b, byteorder='little', signed=False)
print(pos, "{:10d}".format(lint), sep='\t')


if __name__ == '__main__':
parser = argparse.ArgumentParser(description='read .mp files.', formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('input', type=str, help='input file')
parser.add_argument('-c', '--command', choices=['offsets', 'extract', 'vis'], default='offsets')
parser.add_argument('-c', '--command', choices=['offsets', 'extract', 'vis', 'dump_bytes', 'vis_bytes'], default='offsets')
parser.add_argument('--offset', type=int, help='offset to extract')
parser.add_argument('--width', type=int, help='vis: image width', default=2**10)
parser.add_argument('--bpp', type=int, help='vis: bytes per pixel', default=2**10)
Expand All @@ -188,3 +212,7 @@ def extract_tile(finname, foutname, offset):
extract_tile(args.input, args.out, args.offset)
elif args.command == 'vis':
vis_content(args.input, args.out, args.bpp, args.width)
elif args.command == 'dump_bytes':
dump_bytes(args.input)
elif args.command == 'vis_bytes':
vis_bytes(args.input, args.out)

0 comments on commit d0522d6

Please sign in to comment.