Skip to content

Instantly share code, notes, and snippets.

@leuldereje
Forked from RoadrunnerWMC/tnl_conv.py
Created March 10, 2017 13:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leuldereje/6f7d7e1556a0a103d60f27312f4aeff7 to your computer and use it in GitHub Desktop.
Save leuldereje/6f7d7e1556a0a103d60f27312f4aeff7 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.
# 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