fix palette functions and output palettes in 2bpp conversion

This commit is contained in:
yenatch 2013-06-12 14:25:36 -04:00
parent 5cf1754b08
commit 5d204ce569

View File

@ -1185,23 +1185,24 @@ def dmg2rgb(word):
return ((red<<3)+0b100, (green<<3)+0b100, (blue<<3)+0b100, alpha) return ((red<<3)+0b100, (green<<3)+0b100, (blue<<3)+0b100, alpha)
def rgb_to_dmg(color): def rgb_to_dmg(color):
word = (color['r'] / 8) << 10 word = (color['r'] / 8)
word += (color['g'] / 8) << 5 word += (color['g'] / 8) << 5
word += (color['b'] / 8) word += (color['b'] / 8) << 10
return word return word
def png_pal(filename): def png_pal(filename):
palette = [] palette = []
palette.append((255,255,255,255))
with open(filename, 'rb') as pal_data: with open(filename, 'rb') as pal_data:
words = pal_data.read() words = pal_data.read()
dmg_pals = [] dmg_pals = []
for word in range(len(words)/2): for word in range(len(words)/2):
dmg_pals.append(ord(words[word*2]) + ord(words[word*2+1])*0x100) dmg_pals.append(ord(words[word*2]) + ord(words[word*2+1])*0x100)
for word in dmg_pals: white = (255,255,255,255)
palette.append(dmg2rgb(word)) black = (000,000,000,255)
palette.append((000,000,000,255)) for word in dmg_pals: palette += [dmg2rgb(word)]
if white not in dmg_pals and len(palette) < 4: palette = [white] + palette
if black not in dmg_pals and len(palette) < 4: palette += [black]
return palette return palette
@ -1366,14 +1367,18 @@ def to_2bpp(filein, fileout=None, palout=None):
return sum(color[key] * -rough[key] for key in rough.keys()) return sum(color[key] * -rough[key] for key in rough.keys())
palette = sorted(palette, key=luminance) palette = sorted(palette, key=luminance)
# spit out new .pal file # spit out a new .pal file
#if palout != None: # disable this if it causes problems with paletteless images
# output = [] if palout == None:
# for color in palette[1:3]: if os.path.exists(os.path.splitext(fileout)[0]+'.pal'):
# word = rgb_to_dmg(color) palout = os.path.splitext(fileout)[0]+'.pal'
# output += [word & 0xff] if palout != None:
# output += [word >> 8] output = []
# to_file(palout, output) for color in palette:
word = rgb_to_dmg(color)
output += [word & 0xff]
output += [word >> 8]
to_file(palout, output)
# create a new map of quaternary color ids # create a new map of quaternary color ids
map = [] map = []