Created
February 15, 2014 13:08
-
-
Save pekkavaa/9019110 to your computer and use it in GitHub Desktop.
Converts JASC palette files to raw bytes.
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
#!/usr/bin/env python | |
import argparse | |
from struct import * | |
import re | |
def write_raw_palette(palette, output_path, write_header): | |
data = "" | |
header = pack('I', len(palette)) | |
if write_header: | |
data += header | |
for entry in palette: | |
color = pack('BBB', entry[0], entry[1], entry[2]) | |
data += color | |
f = open(output_path, 'wb') | |
f.write(data) | |
f.close() | |
parser = argparse.ArgumentParser(description= | |
""" | |
Converts palettes from JASC format to raw bytes. | |
The output palette file will contain a 4-byte unsigned integer | |
with the number of palette entries. Rest of the file is three | |
byte color entries. | |
""" | |
) | |
parser.add_argument('jasc_palette', help="The JASC-PAL ASCII file") | |
parser.add_argument('raw_palette', help="Destination raw palette") | |
parser.add_argument("--no-header", action="store_true", help="write only the color entry bytes") | |
args = parser.parse_args() | |
path = args.jasc_palette | |
output_path = args.raw_palette | |
palette = [] | |
with open(path) as f: | |
data = list(f.read().splitlines()) | |
magic = data.pop(0) | |
version = data.pop(0) | |
amount = int(data.pop(0)) | |
print "%d colors" % (amount) | |
for line in data: | |
entry = line.strip().split(" ") | |
entry = map(int, entry) | |
palette.append(entry) | |
assert len(palette) == amount | |
write_raw_palette(palette, output_path, not args.no_header) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment