Last active
January 25, 2021 03:02
-
-
Save jaames/bd1a19c552c48ab3a022a03c71bb147e to your computer and use it in GitHub Desktop.
basic decoding for 3ds game note files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
from PIL import Image | |
from sys import argv | |
# format structure: | |
# header | |
# 4 bytes seem to be a checksum of some kind? they're definitely not a timestamp | |
# 8 bytes are some kind of magic/ident? (10RC1000 for me) | |
# 4 bytes padding ? | |
#data | |
# 34560 bytes for note image data | |
# 1428 bytes for note thumbnail image | |
# note images are 320 x 216 px | |
# note thumbnails are 68 x 42 px | |
# both images use the same format: | |
# bitmap; 1 nibble (4 bits) per pixel; each pixel has a palette index value | |
# palette index values | |
palette = [ | |
# 0 - white | |
0xFFFFFFFF, | |
# 1 - black | |
0x313131FF, | |
# 2 - red | |
0xF63D39FF, | |
# 3 - blue | |
0x0085FEFF, | |
# 4 - grey | |
0x8A8A8AFF, | |
# 5 - light red | |
0xFB9795FF, | |
# 6 - light blue | |
0x7DC1FFFF | |
] | |
class gameMemo: | |
def __init__(self, filename): | |
with open(filename, 'rb') as f: | |
self.data = f.read() | |
def _readImageData(self, data): | |
imageData = np.fromstring(data, dtype=np.uint8) | |
pixelData_real = np.bitwise_and(imageData, 0x0f) | |
pixelData_imag = np.bitwise_and(imageData >> 4, 0x0f) | |
pixelData = np.array([palette[j] for i in zip(pixelData_imag, pixelData_real) for j in i], dtype=">u4") | |
return pixelData | |
def getImage(self): | |
image = self._readImageData(self.data[16:-1428]) | |
image = image.tobytes("C") | |
return Image.frombytes("RGBA", (320, 216), image) | |
def getThumb(self): | |
image = self._readImageData(self.data[-1428::]) | |
image = image.tobytes("C") | |
return Image.frombytes("RGBA", (68, 42), image) | |
memo = gameMemo(argv[1]) | |
memo.getThumb().save(argv[1] + '_thumb.png', 'png') | |
memo.getImage().save(argv[1] + '_image.png', 'png') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment