Created
May 4, 2019 22:17
-
-
Save Uradamus/cc81e54297cbec18fdd1c358e5ebe881 to your computer and use it in GitHub Desktop.
This is a pretty simple conversion script to get .ptf texture files into a format that ImageMagick could further process into any desired output formats.
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
"""Copyright 2019 Uradamus | |
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. | |
""" | |
from sys import argv | |
DEBUG = True | |
def translate_565(pixel): | |
blue = int((pixel % 32) * 8.225806451612904 + 0.5) | |
green = int(((pixel >> 5) % 64) * 4.0476190476190474 + 0.5) | |
red = int((pixel >> 11) * 8.225806451612904 + 0.5) | |
return (red, green, blue) | |
def translate_4444(pixel): | |
blue = (pixel % 16) * 17 | |
green = ((pixel >> 4) % 16) * 17 | |
red = ((pixel >> 8) % 16) * 17 | |
alpha = (pixel >> 12) * 17 | |
return (red, green, blue, alpha) | |
### Open file and populate the input bytearray | |
with open(argv[1], 'rb') as f: | |
input = bytearray(f.read()) | |
### Capture the header portion of the input. | |
# ptf_header has 1 str for each of the 7 lines in the header. | |
ptf_header = ['', '', '', '', '', '', ''] | |
# index keeps track of which header str we are concatenating to. | |
index = 0 | |
# count keeps track of how many characters are in the header, | |
# so we know where the body starts. | |
count = 0 | |
for byte in input: | |
count += 1 | |
# 10 is the ASCII value for the newline character, | |
# which is the delimiter found at the end of each | |
# section of the header. | |
if byte != 10: | |
ptf_header[index] += str(chr(byte)) | |
else: | |
# move to next section of header when \n is encountered. | |
if index < 6: | |
index += 1 | |
# until the end of the header. | |
else: | |
break | |
if DEBUG: | |
print('PTF Header:', ptf_header) | |
### Parse ptf_header to create pam_header. | |
color_format = ptf_header[2][8:] | |
depth = 0 | |
tuple_type = '' | |
if color_format == 'RGB565': | |
depth = 3 | |
tuple_type = 'RGB' | |
elif color_format == 'ARGB4444': | |
depth = 4 | |
tuple_type = 'RGB_ALPHA' | |
else: | |
print('Unknown Color Format:', color_format) | |
quit() | |
width = int(ptf_header[3][7:]) | |
height = int(ptf_header[4][8:]) | |
pam_header = 'P7\nWIDTH ' + str(width) + '\nHEIGHT ' + str(height) + '\nDEPTH ' + str(depth) + '\nMAXVAL 255\nTUPLTYPE ' + tuple_type + '\nENDHDR\n' | |
if DEBUG: | |
print('PAM Header:', pam_header) | |
### Convert pixel data from input. | |
pixel_buffer = list() | |
marker = count | |
for row in range(height): | |
pixel_buffer.append(bytearray(width * depth)) | |
column_index = 0 | |
for pixel in range(width): | |
if color_format == 'RGB565': | |
pixel = translate_565((input[marker] << 8) + input[marker + 1]) | |
elif color_format == 'ARGB4444': | |
pixel = translate_4444((input[marker] << 8) + input[marker + 1]) | |
marker += 2 | |
for component in pixel: | |
pixel_buffer[row][column_index] = component | |
column_index += 1 | |
#if DEBUG: | |
# print(pixel_buffer) | |
### Output | |
output = argv[1][:-3] + 'pam' | |
with open(output, 'wb') as f: | |
f.write(bytearray(pam_header, 'ascii')) | |
for row in range(height): | |
f.write(pixel_buffer[row]) | |
if DEBUG: | |
print(output) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment