Mercurial > repos > blastem
annotate img2tiles.py @ 951:fec4a59ae5d7
Merge
author | Michael Pavone <pavone@retrodev.com> |
---|---|
date | Mon, 11 Apr 2016 20:56:54 -0700 |
parents | 40703069bb68 |
children | dbbf0100f249 |
rev | line source |
---|---|
864
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
2 from PIL import Image |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
3 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
4 def gchannel(Val): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
5 return (Val >> 4) & 0xE |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
6 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
7 threshold = 127 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
9 def get_rgba(im, pixels=None, idx=None, color=None): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
10 A = 255 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
11 if color is None: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
12 color = pixels[idx] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
13 if type(color) == int: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
14 R, G, B = im.getpalette()[color*3:color*3+3] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
15 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
16 if len(color) == 4: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
17 R, G, B, A = color |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
18 elif len(color) == 3: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
19 R, G, B = color |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
20 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
21 L, A = color |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
22 R = G = B = L |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
23 return (R, G, B, A) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
24 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
25 def get_gcolor(im, threshold, pixels=None, idx=None, color=None): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
26 R,G,B,A = get_rgba(im, pixels, idx, color) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
27 if A > threshold: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
28 return (gchannel(R), gchannel(G), gchannel(B)) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
29 return 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
30 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
31 def get_color_info(im, pixels, rng, threshold, exclude={}): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
32 gencolors = {} |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
33 A = 255 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
34 pal = None |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
35 transparent = False |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
36 for idx in rng: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
37 gcolor = get_gcolor(im, threshold, pixels, idx) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
38 if type(gcolor) == tuple: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
39 if not gcolor in exclude: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
40 if gcolor in gencolors: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
41 gencolors[gcolor] += 1 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
42 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
43 gencolors[gcolor] = 1 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
44 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
45 transparent = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
46 glist = [(gencolors[color], color) for color in gencolors] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
47 glist.sort() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
48 glist.reverse() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
49 return glist |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
50 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
51 def get_color_info_both(im, pixels, rng, threshold, exclude={}): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
52 gencolors = {} |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
53 A = 255 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
54 pal = None |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
55 transparent = False |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
56 for idx in rng: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
57 gcolor = get_gcolor(im, threshold, pixels, idx) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
58 if type(gcolor) == tuple: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
59 if not gcolor in exclude: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
60 if not gcolor in gencolors: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
61 _,best = best_match(gcolor, (exclude,)) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
62 gencolors[gcolor] = (color_dist(gcolor, best), 1) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
63 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
64 (dist, count) = gencolors[gcolor] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
65 gencolors[gcolor] = (dist, count+1) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
66 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
67 transparent = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
68 glist = [(gencolors[color][0] * gencolors[color][1], color) for color in gencolors] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
69 glist.sort() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
70 glist.reverse() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
71 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
72 return glist |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
73 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
74 def make_palette(im, trans_thresh, max_global, max_line): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
75 pixels = im.getdata() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
76 (width, height) = im.size |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
77 colors = get_color_info(im, pixels, xrange(0, height * width), trans_thresh) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
78 print len(colors), 'distinct 9-bit colors in image' |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
79 glob_pal = {} |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
80 print 'Static Palette:' |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
81 while len(glob_pal) < max_global and len(colors): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
82 idx = len(glob_pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
83 (count, color) = colors[0] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
84 print str(idx) + ':', color |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
85 glob_pal[color] = idx |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
86 colors = get_color_info_both(im, pixels, xrange(0, height * width), trans_thresh, glob_pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
87 line_pals = [] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
88 if max_global < len(colors): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
89 for line in xrange(0, height): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
90 linestart = line * width |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
91 if len(glob_pal): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
92 linecolors = get_color_info_both(im, pixels, xrange(linestart, linestart+width), trans_thresh, glob_pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
93 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
94 linecolors = get_color_info(im, pixels, xrange(linestart, linestart+width), trans_thresh) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
95 line_pal = {} |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
96 while len(line_pal) < max_line and len(linecolors): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
97 (score, color) = linecolors[0] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
98 line_pal[color] = len(line_pal) + max_global |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
99 if len(line_pal) < max_line: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
100 combo = dict(glob_pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
101 for color in line_pal: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
102 combo[color] = line_pal[color] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
103 linecolors = get_color_info_both(im, pixels, xrange(linestart, linestart+width), trans_thresh, combo) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
104 #for idx in xrange(0, min(max_line, len(linecolors))): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
105 # (count, color) = linecolors[idx] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
106 # line_pal[color] = idx + max_global |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
107 line_pals.append(line_pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
108 return (glob_pal, line_pals, max_global, max_line) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
109 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
110 def color_dist(a, b): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
111 (ra, ga, ba) = a |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
112 (rb, gb, bb) = b |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
113 return (ra-rb)**2 + (ga-gb)**2 + (ba-bb)**2 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
114 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
115 def best_match(gpixel, pals): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
116 bestdist = color_dist((0,0,0), (15, 15, 15)) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
117 bestpalidx = 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
118 bestcolor = (0,0,0) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
119 for i in xrange(0, len(pals)): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
120 pal = pals[i] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
121 for cur in pal: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
122 curdist = color_dist(gpixel, cur) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
123 if curdist < bestdist: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
124 bestdist = curdist |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
125 bestpalidx = pal[cur] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
126 bestcolor = cur |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
127 return (bestpalidx, bestcolor) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
128 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
129 def trans_image(im, trans_thresh, pal, chunky): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
130 (global_pal, line_pals, _, _) = pal |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
131 pixels = im.getdata() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
132 (width, height) = im.size |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
133 gpixels = [] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
134 A = 255 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
135 pal = None |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
136 x = 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
137 y = 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
138 numchannels = len(im.getbands()) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
139 offset = 1 if numchannels == 4 or numchannels == 2 else 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
140 for pixel in pixels: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
141 if x == width: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
142 x = 0 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
143 y += 1 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
144 if width % 8 and not chunky: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
145 for i in xrange(0, 8-(width%8)): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
146 gpixels.append(0) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
147 gpixel = get_gcolor(im, trans_thresh, color=pixel) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
148 if type(gpixel) == tuple: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
149 if gpixel in global_pal: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
150 val = global_pal[gpixel] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
151 elif gpixel in line_pals[y]: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
152 val = line_pals[y][gpixel] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
153 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
154 bestpal,color = best_match(gpixel, (global_pal, line_pals[y])) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
155 val = bestpal |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
156 gpixels.append(offset+val) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
157 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
158 gpixels.append(gpixel) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
159 x += 1 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
160 if width % 8 and not chunky: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
161 for i in xrange(0, 8-(width%8)): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
162 gpixels.append(0) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
163 width += 8-(width%8) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
164 if height % 8 and not chunky: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
165 for y in xrange(0, 8-(height%8)): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
166 for x in xrange(0, width): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
167 gpixels.append(0) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
168 height += 8-(height%8) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
169 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
170 return (width, height, gpixels) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
171 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
172 def appendword(b, word): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
173 b.append(word >> 8) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
174 b.append(word & 0xff) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
175 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
176 def to_tiles(palpix, raw=False, chunky=False, tile_height=8, sprite_order = False): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
177 (width, height, pixels) = palpix |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
178 b = bytearray() |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
179 if chunky: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
180 if not raw: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
181 appendword(b, width) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
182 appendword(b, height) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
183 for pixel in pixels: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
184 b.append(pixel) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
185 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
186 cwidth = width/8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
187 cheight = height/tile_height |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
188 words = len(pixels)/4 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
189 if not raw: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
190 appendword(b, words) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
191 appendword(b, cwidth) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
192 appendword(b, cheight) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
193 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
194 if sprite_order: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
195 for cx in xrange(0, cwidth): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
196 xstart = cx * 8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
197 for cy in xrange(0, cheight): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
198 startoff = cy*tile_height*width + xstart |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
199 for row in xrange(0, tile_height): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
200 rowoff = startoff + row*width |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
201 for bytecol in xrange(0, 4): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
202 boff = bytecol * 2 + rowoff |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
203 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
204 #print 'pixels[boff]:', pixels[boff] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
205 b.append(pixels[boff] << 4 | pixels[boff+1]) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
206 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
207 for cy in xrange(0, cheight): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
208 ystart = cy*tile_height*width |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
209 for cx in xrange(0, cwidth): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
210 startoff = (cx*8) + ystart |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
211 for row in xrange(0, tile_height): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
212 rowoff = startoff + row*width |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
213 for bytecol in xrange(0, 4): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
214 boff = bytecol * 2 + rowoff |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
215 #print 'boff:', boff, 'len(pixels)', len(pixels), 'cx', cx, 'cy', cy, 'cwidth', cwidth, 'cheight', cheight |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
216 #print 'pixels[boff]:', pixels[boff] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
217 b.append(pixels[boff] << 4 | pixels[boff+1]) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
218 return b |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
219 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
220 def add_pal_entries(tiles, pal): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
221 (global_pal, line_pals, max_global, max_line) = pal |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
222 tiles.append(max_global) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
223 tiles.append(max_line) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
224 pal_list = [(0, 0, 0)] * max_global |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
225 for entry in global_pal: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
226 pal_list[global_pal[entry]] = entry |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
227 for entry in pal_list: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
228 (R, G, B) = entry |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
229 tiles.append(B) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
230 tiles.append(G << 4 | R) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
231 for line in line_pals: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
232 pal_list = [(0, 0, 0)] * max_line |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
233 for entry in line: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
234 pal_list[line[entry]-max_global] = entry |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
235 for entry in pal_list: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
236 (R, G, B) = entry |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
237 tiles.append(B) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
238 tiles.append(G << 4 | R) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
239 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
240 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
241 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
242 def main(argv): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
243 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
244 posargs = [] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
245 omit_pal = raw = chunky = False |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
246 expect_option = None |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
247 options = {} |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
248 tile_height = 8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
249 sprite_order = False |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
250 for i in xrange(1, len(argv)): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
251 if argv[i].startswith('-'): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
252 if argv[i] == '-r': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
253 raw = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
254 elif argv[i] == '-p': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
255 omit_pal = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
256 elif argv[i] == '-o': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
257 sprite_order = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
258 elif argv[i] == '-c': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
259 chunky = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
260 elif argv[i] == '-i': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
261 tile_height = 16 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
262 elif argv[i] == '-s' or argv[i] == '--spec': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
263 expect_option = 'specfile' |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
264 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
265 print 'Unrecognized switch', argv[i] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
266 return |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
267 elif not expect_option is None: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
268 options[expect_option] = argv[i] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
269 expect_option = None |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
270 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
271 posargs.append(argv[i]) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
272 if len(posargs) < 2 and not ('specfile' in options and len(posargs) >= 1): |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
273 print "Usage: img2tiles.py [OPTIONS] infile outfile [STATIC_COLORS [DYNAMIC_COLORS]]" |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
274 return |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
275 if 'specfile' in options: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
276 props = open(options['specfile']).read().strip().split(',') |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
277 fname,static_colors,dynamic_colors = props[0:3] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
278 for prop in props[3:]: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
279 if prop == 'chunky': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
280 chunky = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
281 elif prop == 'raw': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
282 raw = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
283 elif prop == 'nopal': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
284 omit_pal = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
285 elif prop == 'interlace': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
286 tile_height = 16 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
287 elif prop == 'sprite': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
288 sprite_order = True |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
289 static_colors = int(static_colors) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
290 dynamic_colors = int(dynamic_colors) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
291 outfile = posargs[0] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
292 else: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
293 fname = posargs[0] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
294 outfile = posargs[1] |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
295 static_colors = 8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
296 dynamic_colors = 8 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
297 if len(posargs) > 2: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
298 static_colors = int(posargs[2]) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
299 dynamic_colors = 16-static_colors |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
300 if len(posargs) > 3: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
301 dynamic_colors = int(posargs[3]) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
302 if dynamic_colors + static_colors > 16: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
303 print "No more than 16 combined dynamic and static colors are allowed" |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
304 return |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
305 im = Image.open(fname) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
306 pal = make_palette(im, threshold, static_colors, dynamic_colors) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
307 palpix = trans_image(im, threshold, pal, chunky) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
308 tiles = to_tiles(palpix, raw, chunky, tile_height, sprite_order) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
309 if not omit_pal: |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
310 bits = add_pal_entries(tiles, pal) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
311 out = open(outfile, 'wb') |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
312 out.write(tiles) |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
313 |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
314 if __name__ == '__main__': |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
315 import sys |
40703069bb68
Get everything needed to build menu.bin either committed or buildable via the Makefile
Michael Pavone <pavone@retrodev.com>
parents:
diff
changeset
|
316 main(sys.argv) |