Mercurial > repos > ghero
comparison tiles2img.py @ 6:3148b6389042
Set dynamic colors to 8 in tiles2img
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Tue, 03 Sep 2013 09:37:10 -0700 |
parents | 97ec271a513f |
children |
comparison
equal
deleted
inserted
replaced
5:a74776d80f95 | 6:3148b6389042 |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 from PIL import Image | 2 from PIL import Image |
3 | 3 |
4 def gchannel(Val): | 4 def gchannel(Val): |
5 return (Val >> 4) & 0xE | 5 return (Val >> 4) & 0xE |
6 | 6 |
7 threshold = 127 | 7 threshold = 127 |
8 | 8 |
9 def get_color_info(pixels, rng, threshold, exclude={}): | 9 def get_color_info(pixels, rng, threshold, exclude={}): |
10 gencolors = {} | 10 gencolors = {} |
11 A = 255 | 11 A = 255 |
24 gencolors[gcolor] = 1 | 24 gencolors[gcolor] = 1 |
25 glist = [(gencolors[color], color) for color in gencolors] | 25 glist = [(gencolors[color], color) for color in gencolors] |
26 glist.sort() | 26 glist.sort() |
27 glist.reverse() | 27 glist.reverse() |
28 return glist | 28 return glist |
29 | 29 |
30 def totiles(im, palette): | 30 def totiles(im, palette): |
31 pass | 31 pass |
32 | 32 |
33 def make_palette(im, trans_thresh, max_global, max_line): | 33 def make_palette(im, trans_thresh, max_global, max_line): |
34 pixels = im.getdata() | 34 pixels = im.getdata() |
35 (width, height) = im.size | 35 (width, height) = im.size |
36 colors = get_color_info(pixels, xrange(0, height * width), trans_thresh) | 36 colors = get_color_info(pixels, xrange(0, height * width), trans_thresh) |
37 glob_pal = {} | 37 glob_pal = {} |
47 for idx in xrange(0, min(max_line, len(linecolors))): | 47 for idx in xrange(0, min(max_line, len(linecolors))): |
48 (count, color) = linecolors[idx] | 48 (count, color) = linecolors[idx] |
49 line_pal[color] = idx | 49 line_pal[color] = idx |
50 line_pals.append(line_pal) | 50 line_pals.append(line_pal) |
51 return (glob_pal, line_pals) | 51 return (glob_pal, line_pals) |
52 | 52 |
53 def color_dist(a, b): | 53 def color_dist(a, b): |
54 (ra, ga, ba) = a | 54 (ra, ga, ba) = a |
55 (rb, gb, bb) = b | 55 (rb, gb, bb) = b |
56 return abs(ra-rb)**2 + abs(ga-gb)**2 + abs(ba-bb)**2 | 56 return abs(ra-rb)**2 + abs(ga-gb)**2 + abs(ba-bb)**2 |
57 | 57 |
104 if height % 8: | 104 if height % 8: |
105 for y in xrange(0, 8-(height%8)): | 105 for y in xrange(0, 8-(height%8)): |
106 for x in xrange(0, width): | 106 for x in xrange(0, width): |
107 gpixels.append(0) | 107 gpixels.append(0) |
108 height += 8-(height%8) | 108 height += 8-(height%8) |
109 | 109 |
110 return (width, height, gpixels) | 110 return (width, height, gpixels) |
111 | 111 |
112 def appendword(b, word): | 112 def appendword(b, word): |
113 b.append(word >> 8) | 113 b.append(word >> 8) |
114 b.append(word & 0xff) | 114 b.append(word & 0xff) |
115 | 115 |
116 def to_tiles(palpix): | 116 def to_tiles(palpix): |
120 cheight = height/8 | 120 cheight = height/8 |
121 words = len(pixels)/4 | 121 words = len(pixels)/4 |
122 appendword(b, words) | 122 appendword(b, words) |
123 appendword(b, cwidth) | 123 appendword(b, cwidth) |
124 appendword(b, cheight) | 124 appendword(b, cheight) |
125 | 125 |
126 for cy in xrange(0, cheight): | 126 for cy in xrange(0, cheight): |
127 ystart = cy*8*width | 127 ystart = cy*8*width |
128 for cx in xrange(0, cwidth): | 128 for cx in xrange(0, cwidth): |
129 startoff = (cx*8) + ystart | 129 startoff = (cx*8) + ystart |
130 for row in xrange(0, 8): | 130 for row in xrange(0, 8): |
133 boff = bytecol * 2 + rowoff | 133 boff = bytecol * 2 + rowoff |
134 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight | 134 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight |
135 #print 'pixels[boff]:', pixels[boff] | 135 #print 'pixels[boff]:', pixels[boff] |
136 b.append(pixels[boff] << 4 | pixels[boff+1]) | 136 b.append(pixels[boff] << 4 | pixels[boff+1]) |
137 return b | 137 return b |
138 | 138 |
139 def add_pal_entries(tiles, pal): | 139 def add_pal_entries(tiles, pal): |
140 (global_pal, line_pals) = pal | 140 (global_pal, line_pals) = pal |
141 pal_list = [()] * len(global_pal) | 141 pal_list = [()] * len(global_pal) |
142 for entry in global_pal: | 142 for entry in global_pal: |
143 pal_list[global_pal[entry]] = entry | 143 pal_list[global_pal[entry]] = entry |
166 else: | 166 else: |
167 palidx = (tiles[offset] >> 4) & 0xF | 167 palidx = (tiles[offset] >> 4) & 0xF |
168 if palidx < len(fixedpal): | 168 if palidx < len(fixedpal): |
169 return fixedpal[palidx] | 169 return fixedpal[palidx] |
170 return linepals[y][palidx - len(fixedpal)] | 170 return linepals[y][palidx - len(fixedpal)] |
171 | 171 |
172 def to_pixels(tiles, im): | 172 def to_pixels(tiles, im): |
173 (_, width, height), tiles, (fixedpal, linepals) = tiles | 173 (_, width, height), tiles, (fixedpal, linepals) = tiles |
174 for y in xrange(0, height): | 174 for y in xrange(0, height): |
175 for x in xrange(0, width): | 175 for x in xrange(0, width): |
176 color = getcolor(tiles, fixedpal, linepals, width, x, y) | 176 color = getcolor(tiles, fixedpal, linepals, width, x, y) |
177 im.putpixel((x,y), color) | 177 im.putpixel((x,y), color) |
178 return im | 178 return im |
179 | 179 |
180 def expand_pal(pal): | 180 def expand_pal(pal): |
181 def expand_pal_int(pal): | 181 def expand_pal_int(pal): |
182 return ((pal << 4) & 0xE0, pal & 0xE0, (pal >> 4) & 0xE0) | 182 return ((pal << 4) & 0xE0, pal & 0xE0, (pal >> 4) & 0xE0) |
183 fixedpal, linepals = pal | 183 fixedpal, linepals = pal |
184 fixedpal = [expand_pal_int(color) for color in fixedpal] | 184 fixedpal = [expand_pal_int(color) for color in fixedpal] |
185 linepals = [[expand_pal_int(color) for color in linepal] for linepal in linepals] | 185 linepals = [[expand_pal_int(color) for color in linepal] for linepal in linepals] |
186 return (fixedpal, linepals) | 186 return (fixedpal, linepals) |
187 | 187 |
188 def open_tiles(name): | 188 def open_tiles(name): |
189 from struct import unpack, unpack_from | 189 from struct import unpack, unpack_from |
190 f = open(name, 'rb') | 190 f = open(name, 'rb') |
191 header = f.read(6) | 191 header = f.read(6) |
192 words,width,height = unpack('>HHH', header) | 192 words,width,height = unpack('>HHH', header) |
193 width *= 8 | 193 width *= 8 |
194 height *= 8 | 194 height *= 8 |
195 meta = (words, width, height) | 195 meta = (words, width, height) |
196 pixels = bytearray(f.read(words * 2)) | 196 pixels = bytearray(f.read(words * 2)) |
197 numfixed = 12 | 197 numfixed = 8 |
198 numdynamic = 4 | 198 numdynamic = 8 |
199 fixedpalb = f.read(numfixed*2) | 199 fixedpalb = f.read(numfixed*2) |
200 fixedpal = [] | 200 fixedpal = [] |
201 for offset in xrange(0, numfixed*2, 2): | 201 for offset in xrange(0, numfixed*2, 2): |
202 color, = unpack_from('>H', fixedpalb, offset) | 202 color, = unpack_from('>H', fixedpalb, offset) |
203 fixedpal.append(color) | 203 fixedpal.append(color) |
221 im = Image.new('RGB', (tiles[0][1], tiles[0][2])) | 221 im = Image.new('RGB', (tiles[0][1], tiles[0][2])) |
222 tiles = (tiles[0], tiles[1], expand_pal(tiles[2])) | 222 tiles = (tiles[0], tiles[1], expand_pal(tiles[2])) |
223 to_pixels(tiles, im) | 223 to_pixels(tiles, im) |
224 out = open(argv[2], 'wb') | 224 out = open(argv[2], 'wb') |
225 im.save(out) | 225 im.save(out) |
226 | 226 |
227 if __name__ == '__main__': | 227 if __name__ == '__main__': |
228 import sys | 228 import sys |
229 main(sys.argv) | 229 main(sys.argv) |