Mercurial > repos > ghero
annotate img2tiles.py @ 10:eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
author | Mike Pavone <pavone@retrodev.com> |
---|---|
date | Fri, 06 Sep 2013 19:08:56 -0700 |
parents | 997690aa0507 |
children |
rev | line source |
---|---|
3 | 1 #!/usr/bin/env python |
2 from PIL import Image | |
3 | |
4 def gchannel(Val): | |
5 return (Val >> 4) & 0xE | |
6 | |
7 threshold = 127 | |
8 | |
9 def get_color_info(pixels, rng, threshold, exclude={}): | |
10 gencolors = {} | |
11 A = 255 | |
12 for idx in rng: | |
13 color = pixels[idx] | |
14 if len(color) == 4: | |
15 (R, G, B, A) = color | |
16 else: | |
17 (R, G, B) = color | |
18 if A > threshold: | |
19 gcolor = (gchannel(R), gchannel(G), gchannel(B)) | |
20 if not gcolor in exclude: | |
21 if gcolor in gencolors: | |
22 gencolors[gcolor] += 1 | |
23 else: | |
24 gencolors[gcolor] = 1 | |
25 glist = [(gencolors[color], color) for color in gencolors] | |
26 glist.sort() | |
27 glist.reverse() | |
28 return glist | |
29 | |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
30 def get_color_info_error(pixels, rng, threshold, exclude={}): |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
31 gencolors = {} |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
32 A = 255 |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
33 for idx in rng: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
34 color = pixels[idx] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
35 if len(color) == 4: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
36 (R, G, B, A) = color |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
37 else: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
38 (R, G, B) = color |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
39 if A > threshold: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
40 gcolor = (gchannel(R), gchannel(G), gchannel(B)) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
41 if not gcolor in exclude: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
42 if not gcolor in gencolors: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
43 _,best = best_match(gcolor, (exclude,)) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
44 gencolors[gcolor] = color_dist(gcolor, best) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
45 glist = [(gencolors[color], color) for color in gencolors] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
46 glist.sort() |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
47 glist.reverse() |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
48 return glist |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
49 |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
50 def get_color_info_both(pixels, rng, threshold, exclude={}): |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
51 gencolors = {} |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
52 A = 255 |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
53 for idx in rng: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
54 color = pixels[idx] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
55 if len(color) == 4: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
56 (R, G, B, A) = color |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
57 else: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
58 (R, G, B) = color |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
59 if A > threshold: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
60 gcolor = (gchannel(R), gchannel(G), gchannel(B)) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
61 if not gcolor in exclude: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
62 if not gcolor in gencolors: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
63 _,best = best_match(gcolor, (exclude,)) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
64 gencolors[gcolor] = (color_dist(gcolor, best), 1) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
65 else: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
66 (dist, count) = gencolors[gcolor] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
67 gencolors[gcolor] = (dist, count+1) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
68 glist = [(gencolors[color][0] * gencolors[color][1], color) for color in gencolors] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
69 glist.sort() |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
70 glist.reverse() |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
71 return glist |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
72 |
3 | 73 def totiles(im, palette): |
74 pass | |
75 | |
76 def make_palette(im, trans_thresh, max_global, max_line): | |
77 pixels = im.getdata() | |
78 (width, height) = im.size | |
79 colors = get_color_info(pixels, xrange(0, height * width), trans_thresh) | |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
80 print len(colors), 'distinct 9-bit colors in image' |
3 | 81 glob_pal = {} |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
82 print 'Static Palette:' |
10
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
83 while len(glob_pal) < max_global: |
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
84 (score, color) = colors[0] |
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
85 idx = len(glob_pal) |
3 | 86 (count, color) = colors[idx] |
10
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
87 print str(idx) + ':', color |
3 | 88 glob_pal[color] = idx |
10
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
89 colors = get_color_info_both(pixels, xrange(0, height * width), trans_thresh, glob_pal) |
3 | 90 line_pals = [] |
91 if max_global < len(colors): | |
92 for line in xrange(0, height): | |
93 linestart = line * width | |
10
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
94 if len(glob_pal): |
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
95 linecolors = get_color_info_both(pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal) |
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
96 else: |
eddc4ba6b0c3
Use error*count method for choosing static palette entries, not just dynamic ones
Mike Pavone <pavone@retrodev.com>
parents:
9
diff
changeset
|
97 linecolors = get_color_info(pixels, xrange(linestart, linestart+width), trans_thresh) |
3 | 98 line_pal = {} |
9
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
99 while len(line_pal) < max_line and len(linecolors): |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
100 (score, color) = linecolors[0] |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
101 line_pal[color] = len(line_pal) + max_global |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
102 if len(line_pal) < max_line: |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
103 combo = dict(glob_pal) |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
104 for color in line_pal: |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
105 combo[color] = line_pal[color] |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
106 linecolors = get_color_info_both(pixels, xrange(linestart, linestart+width), trans_thresh, combo) |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
107 #for idx in xrange(0, min(max_line, len(linecolors))): |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
108 # (count, color) = linecolors[idx] |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
109 # line_pal[color] = idx + max_global |
3 | 110 line_pals.append(line_pal) |
111 return (glob_pal, line_pals, max_global, max_line) | |
112 | |
113 def color_dist(a, b): | |
114 (ra, ga, ba) = a | |
115 (rb, gb, bb) = b | |
116 return (ra-rb)**2 + (ga-gb)**2 + (ba-bb)**2 | |
117 | |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
118 def best_match(gpixel, pals): |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
119 bestdist = color_dist((0,0,0), (15, 15, 15)) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
120 bestpalidx = 0 |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
121 bestcolor = (0,0,0) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
122 for i in xrange(0, len(pals)): |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
123 pal = pals[i] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
124 for cur in pal: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
125 curdist = color_dist(gpixel, cur) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
126 if curdist < bestdist: |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
127 bestdist = curdist |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
128 bestpalidx = pal[cur] |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
129 bestcolor = cur |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
130 return (bestpalidx, bestcolor) |
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
131 |
3 | 132 def trans_image(im, trans_thresh, pal): |
133 (global_pal, line_pals, _, _) = pal | |
134 pixels = im.getdata() | |
135 (width, height) = im.size | |
136 gpixels = [] | |
137 A = 255 | |
138 x = 0 | |
139 y = 0 | |
140 for pixel in pixels: | |
141 if x == width: | |
142 x = 0 | |
143 y += 1 | |
144 if width % 8: | |
145 for i in xrange(0, 8-(width%8)): | |
146 gpixels.append(0) | |
147 if len(pixel) == 4: | |
148 (R, G, B, A) = pixel | |
149 else: | |
150 (R, G, B) = pixel | |
151 if A > trans_thresh: | |
152 gpixel = (gchannel(R), gchannel(G), gchannel(B)) | |
153 if gpixel in global_pal: | |
154 gpixels.append(global_pal[gpixel]) | |
155 elif gpixel in line_pals[y]: | |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
156 gpixels.append(line_pals[y][gpixel]) |
3 | 157 else: |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
158 bestpal,color = best_match(gpixel, (global_pal, line_pals[y])) |
3 | 159 gpixels.append(bestpal) |
160 else: | |
161 gpixels.append(0) | |
162 x += 1 | |
163 if width % 8: | |
164 for i in xrange(0, 8-(width%8)): | |
165 gpixels.append(0) | |
166 width += 8-(width%8) | |
167 if height % 8: | |
168 for y in xrange(0, 8-(height%8)): | |
169 for x in xrange(0, width): | |
170 gpixels.append(0) | |
171 height += 8-(height%8) | |
172 | |
173 return (width, height, gpixels) | |
174 | |
175 def appendword(b, word): | |
176 b.append(word >> 8) | |
177 b.append(word & 0xff) | |
178 | |
179 def to_tiles(palpix): | |
180 (width, height, pixels) = palpix | |
181 b = bytearray() | |
182 cwidth = width/8 | |
183 cheight = height/8 | |
184 words = len(pixels)/4 | |
185 appendword(b, words) | |
186 appendword(b, cwidth) | |
187 appendword(b, cheight) | |
188 | |
189 for cy in xrange(0, cheight): | |
190 ystart = cy*8*width | |
191 for cx in xrange(0, cwidth): | |
192 startoff = (cx*8) + ystart | |
193 for row in xrange(0, 8): | |
194 rowoff = startoff + row*width | |
195 for bytecol in xrange(0, 4): | |
196 boff = bytecol * 2 + rowoff | |
197 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight | |
198 #print 'pixels[boff]:', pixels[boff] | |
199 b.append(pixels[boff] << 4 | pixels[boff+1]) | |
200 return b | |
201 | |
202 def add_pal_entries(tiles, pal): | |
203 (global_pal, line_pals, max_global, max_line) = pal | |
9
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
204 tiles.append(max_global) |
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
205 tiles.append(max_line) |
3 | 206 pal_list = [(0, 0, 0)] * max_global |
207 for entry in global_pal: | |
208 pal_list[global_pal[entry]] = entry | |
209 for entry in pal_list: | |
210 (R, G, B) = entry | |
211 tiles.append(B) | |
212 tiles.append(G << 4 | R) | |
213 for line in line_pals: | |
214 pal_list = [(0, 0, 0)] * max_line | |
215 for entry in line: | |
5
a74776d80f95
Fix a bug in selecting the best match color when an exact match is unavailable. Add some more options for choosing a line palette
Mike Pavone <pavone@retrodev.com>
parents:
3
diff
changeset
|
216 pal_list[line[entry]-max_global] = entry |
3 | 217 for entry in pal_list: |
218 (R, G, B) = entry | |
219 tiles.append(B) | |
220 tiles.append(G << 4 | R) | |
221 | |
222 | |
223 | |
224 def main(argv): | |
225 if len(argv) < 3: | |
226 print "Not enough arguments" | |
227 return | |
228 fname = argv[1] | |
8
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
229 static_colors = 8 |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
230 dynamic_colors = 8 |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
231 if len(argv) > 3: |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
232 static_colors = int(argv[3]) |
9
997690aa0507
Improve method for choosing line palette entries. Read number of global and dynamic colors from image binary. Go back to doing a word, longword, word pattern of writes for turning off display, doing DMA and turning it back on again to make things work correctly on the real hardware.
Mike Pavone <pavone@retrodev.com>
parents:
8
diff
changeset
|
233 dynamic_colors = 16-static_colors |
8
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
234 if len(argv) > 4: |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
235 dynamic_colors = int(argv[4]) |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
236 if dynamic_colors + static_colors > 16: |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
237 print "No more than 16 combined dynamic and static colors are allowed" |
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
238 return |
3 | 239 im = Image.open(fname) |
8
a049de420cc1
Make number of static and dynamic colors command line arguments to img2tiles.py
Mike Pavone <pavone@retrodev.com>
parents:
5
diff
changeset
|
240 pal = make_palette(im, threshold, static_colors, dynamic_colors) |
3 | 241 palpix = trans_image(im, threshold, pal) |
242 tiles = to_tiles(palpix) | |
243 bits = add_pal_entries(tiles, pal) | |
244 out = open(argv[2], 'wb') | |
245 out.write(tiles) | |
246 | |
247 if __name__ == '__main__': | |
248 import sys | |
249 main(sys.argv) |