Last active
April 22, 2017 11:54
-
-
Save RoadrunnerWMC/33c28d6d13b411f1e55471936d36c990 to your computer and use it in GitHub Desktop.
A little script I wrote that converts Super Mario Maker .tnl course thumbnails to and from JPEG.
This file contains hidden or 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
# 6/16/16 | |
# A little script I wrote that converts Super Mario Maker .tnl course | |
# thumbnails to and from JPEG. (Spoiler: TNL is an incredibly thin JPEG wrapper | |
# format.) | |
# Licensed under the MIT License: | |
# Copyright (c) 2016 RoadrunnerWMC | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
import binascii | |
import os.path | |
import struct | |
import sys | |
def tnl2Jpeg(tnlData): | |
(jpegLen,) = struct.unpack_from('>I', tnlData, 4) | |
return tnlData[8:8+jpegLen] | |
def jpeg2Tnl(jpegData): | |
jpegLen = struct.pack('>I', len(jpegData)) | |
if len(jpegData) > 0xC7F8: | |
raise ValueError('JPEG is too long! Maximum length is 0xC7F8 bytes.') | |
padding = b'\0' * (0xC800 - len(jpegData) - 8) | |
fileWithoutCRC = jpegLen + jpegData + padding | |
crcInt = binascii.crc32(fileWithoutCRC) & 0xFFFFFFFF | |
crc = bytes([crcInt >> 24, (crcInt >> 16) & 0xFF, (crcInt >> 8) & 0xFF, crcInt & 0xFF]) | |
return crc + fileWithoutCRC | |
def main(argv): | |
if len(argv) not in (2, 3): | |
print('Incorrect number of arguments.') | |
print('Usage: tnl_conv filename [-j|-t]') | |
print(' -j: Convert TNL to JPEG') | |
print(' -t: Convert JPEG to TNL') | |
print(' Default is -j.') | |
return | |
inpath = argv[1] | |
with open(inpath, 'rb') as f: | |
indata = f.read() | |
conv = 'j' | |
if len(argv) == 3 and argv[2] == '-t': | |
conv = 't' | |
if conv == 'j': | |
outdata = tnl2Jpeg(indata) | |
outpath = os.path.splitext(inpath)[0] + '.jpg' | |
elif conv == 't': | |
outdata = jpeg2Tnl(indata) | |
outpath = os.path.splitext(inpath)[0] + '.tnl' | |
with open(outpath, 'wb') as f: | |
f.write(outdata) | |
if __name__ == '__main__': main(sys.argv) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment