Skip to content

Instantly share code, notes, and snippets.

@Epicpkmn11
Last active April 6, 2022 09:00
Show Gist options
  • Save Epicpkmn11/404217398b4f35263b5af37c9f9d5800 to your computer and use it in GitHub Desktop.
Save Epicpkmn11/404217398b4f35263b5af37c9f9d5800 to your computer and use it in GitHub Desktop.
Cut the Rope DSiWare image conversion scripts
#!/usr/bin/env python3
# Requirements:
# pip3 install pillow
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
"""
import argparse
import struct
from os import SEEK_CUR
from os.path import join
from PIL import Image
from sys import exit
def pkg2png(args):
for input in args.inputs:
print(input.name)
type, fileCount = struct.unpack("<LL", input.read(8))
if type == 1:
print("Error! This is an audio pkg")
exit()
elif type != 0:
print(f"Error! This is not a graphics pkg (type {type})")
exit()
# File names
files = []
for _ in range(fileCount):
strLen = struct.unpack("B", input.read(1))[0]
files.append(input.read(strLen).decode("utf-8"))
# File sizes
remainingSize, fileCount, something = struct.unpack("<LHH", input.read(8))
sizes = []
for _ in range(fileCount):
_, height, width = struct.unpack("<BHH", input.read(5))
sizes.append((width, height))
if something != 0:
print(f"Header thing: {something} (do -t{something} on re-conversion)")
# Image data
for i, output in enumerate(files):
size = sizes[i]
print(output)
# Why????
something = struct.unpack("<H", input.read(2))[0]
if(something != 1):
print(f"Weird 0x{something:X} (do -w on re-conversion)")
input.seek(-1, SEEK_CUR)
data = b""
for _ in range(size[0] * size[1]):
color = struct.unpack("<H", input.read(2))[0]
r, g, b = [round(((color >> (i * 5)) & 0x1F) * 255 / 31) for i in range(3)]
data += struct.pack("BBBB", r, g, b, 0xFF if color & 0x8000 else 0x00)
img = Image.frombytes("RGBA", size, data)
img.save((join(args.output, output) if args.output else output) + ".png")
if __name__ == "__main__":
pkg2pngarg = argparse.ArgumentParser(description="Converts a PKG file to images")
pkg2pngarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=argparse.FileType("rb"), help="input file(s)")
pkg2pngarg.add_argument("--output", "-o", metavar="out", type=str, help="output directory")
exit(pkg2png(pkg2pngarg.parse_args()))
#!/usr/bin/env python3
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
"""
import argparse
import struct
from collections import namedtuple
from os import system, remove
from os.path import join
from sys import exit
swavHeader = namedtuple("swavHeader",
"magic byteOrder version fileSize headerSize numBlocks data dataSize waveType loopFlag samplingRate time loopOffset soundLen"
)
swavFormat = "<LHHLHHLLBBHHHL"
def pkg2png(args):
for input in args.inputs:
print(input.name)
type, fileCount = struct.unpack("<LL", input.read(8))
if type == 0:
print("Error! This is a graphics pkg")
exit()
elif type != 1:
print(f"Error! This is not an audio pkg (type {type})")
exit()
# File names
files = []
for _ in range(fileCount):
strLen = struct.unpack("B", input.read(1))[0]
files.append(input.read(strLen).decode("utf-8"))
# Sound data
for output in files:
# Music or Effects
strLen = struct.unpack("B", input.read(1))[0]
type = input.read(strLen).decode("utf-8")
print(type)
outputName = f"{type}.{output}.raw"
print(outputName)
_, size = struct.unpack("<LL", input.read(8))
print(_) # always 1?
# SWAV header
header = swavHeader._make(struct.unpack(swavFormat, input.read(36)))
outputPath = join(args.output, outputName) if args.output else outputName
with open(outputPath, "wb") as f:
f.write(input.read(header.dataSize - 0x14))
if header.waveType == 0:
format = "s8"
codec = "pcm_s8"
elif header.waveType == 1:
format = "s16le"
codec = "pcm_s16le"
elif header.waveType == 2:
format = "s16le"
codec = "adpcm_ima_alp"
else:
print("Error: Invalid wave type")
exit()
system(f"ffmpeg -y -loglevel error -f {format} -acodec {codec} -ar {header.samplingRate} -i '{outputPath}' '{outputPath[:outputPath.rfind('.')]}.wav'")
remove(outputPath)
if __name__ == "__main__":
pkg2pngarg = argparse.ArgumentParser(description="Converts a PKG file to SWAVs")
pkg2pngarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=argparse.FileType("rb"), help="input file(s)")
pkg2pngarg.add_argument("--output", "-o", metavar="out", type=str, help="output directory")
exit(pkg2png(pkg2pngarg.parse_args()))
#!/usr/bin/env python3
# Requirements:
# pip3 install pillow
"""
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
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 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.
For more information, please refer to <http://unlicense.org/>
"""
import argparse
import struct
from os.path import basename
from PIL import Image
from sys import exit
def png2pkg(args):
if args.output:
output = args.output
else:
output = open(args.inputs[0][:args.inputs[0].rfind(".")] + ".pkg", "wb")
print(output.name)
# 4 bytes 00 then file count
output.write(struct.pack("<LL", 0, len(args.inputs)))
# Write file names
for input in args.inputs:
inputName = basename(input[:input.rfind(".")])
output.write(struct.pack("B", len(inputName)) + inputName.encode("utf-8"))
# File sizes
sizes = []
totalSize = 0 # Total size remaining after this point
for input in args.inputs:
with Image.open(input) as img:
sizes.append(img.size)
totalSize += (img.width * img.height * 2) + 2
if args.weird:
totalSize -= 1
totalSize += 4 + (5 * len(args.inputs))
output.write(struct.pack("<LHH", totalSize, len(args.inputs), args.headerthing))
for width, height in sizes:
# Not sure what the 0x80 means
output.write(struct.pack("<BHH", 0x80, height, width))
for i, input in enumerate(args.inputs):
print(basename(input))
# not sure what this is
output.write(struct.pack("B" if i == 0 and args.weird else "<H", 1))
img = Image.open(input)
data = b""
for y in range(img.height):
for x in range(img.width):
r, g, b, a = [round(x * 31 / 255) & 0x1F for x in img.getpixel((x, y))]
data += struct.pack("<H", (1 << 15 if a == 0x1F else 0) | b << 10 | g << 5 | r)
output.write(data)
if __name__ == "__main__":
png2pkgarg = argparse.ArgumentParser(description="Converts images to a PKG")
png2pkgarg.add_argument("inputs", metavar="in.pkg", nargs="*", type=str, help="input file(s)")
png2pkgarg.add_argument("--output", "-o", metavar="out", type=argparse.FileType("wb"), help="output file")
png2pkgarg.add_argument("--headerthing", "-t", default=0, type=int, help="somehing in the header, idk what")
png2pkgarg.add_argument("--weird", "-w", action="store_true", help="if this one is weird... idk why")
exit(png2pkg(png2pkgarg.parse_args()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment