-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixarter.py
297 lines (228 loc) · 9.04 KB
/
pixarter.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
'''
An image file parser that checks for DCPU16 compliance
and returns unique font characters and colors for the color palette
'''
import sys
from PIL import Image
FG = 0
BG = 1
class Palette:
def __init__(self, index_to_color_fgbgs):
self.index_to_color_fgbgs = index_to_color_fgbgs
self.colors_to_index = dict()
for idx in self.index_to_color_fgbgs:
self.colors_to_index[self.index_to_color_fgbgs[idx][0]] = idx
def get_index_from_color(self, color):
#print self.colors_to_index
return self.colors_to_index[color]
def get_color_from_index(self, index):
return self.index_to_color_fgbgs[index][0]
def get_fgbg_from_color(self,color):
index = self.get_index_from_color(color)
return self.index_to_color_fgbgs[index][1]
class CharPalette:
def __init__(self):
self.unique_words = list()
self.loc_to_words = dict()
self.loc_to_color_idx = dict()
self.unique_loc_to_words = dict()
def add_word(self, loc, words, fgcolor, bgcolor):
self.loc_to_words[loc] = words
self.loc_to_color_idx[loc] = (bgcolor,fgcolor)
if not words in self.unique_words:
self.unique_words.append(words)
self.unique_loc_to_words[words] = loc
class ImageMap:
def __init__(self, colors, chars):
self.colors = colors
self.chars = chars
def get_chars(self):
#print self.chars.loc_to_words
for char in self.chars.unique_words:
gen_palette_comment(char)
def create_image(self, name, size):
#image = image.copy()
#image_width, image_height = image.size
image = Image.new('RGBA',size)
image_width, image_height = size
image_pixels = image.load()
'''for x in range(int(image_width/4)):
for y in range(int(image_height/8)):
image_pixels[x,y] = (255,0,0,255)
'''
for x in range(31):
for y in range(12):
char = self.chars.loc_to_words[(x,y)]
l = self.chars.unique_loc_to_words[char]
write_to_image(char,image_pixels,x*4,y*8)
#write_to_image(char,image_pixels,l[0]*4,l[1]*8)
image.save(name)
def get_splash_screen(self, char_offset=0):
def make_len_3(hex_thing):
hex_thing = hex_thing[2:]
return '0x%s%s' % ('0'*(3 - len(hex_thing)), hex_thing)
for i,char in enumerate(self.chars.unique_words):
#gen_palette_comment(char)
print "DAT %s, %s ; %s = %s" % (char[0], char[1], i+char_offset, hex(i+char_offset))
print
print
print
x_offset = 0
y_offset = 0
for x in range(32):
for y in range(12):
char = self.chars.loc_to_words[(x+x_offset,y+y_offset)]
char_idx = hex(self.chars.unique_words.index(char)+char_offset)[2:]
if len(char_idx) == 1:
char_idx = "0"+char_idx
bg_col, fg_col = self.chars.loc_to_color_idx[(x+x_offset,y+y_offset)]
assembly_loc = x+32*y
assembly_loc = make_len_3(hex(assembly_loc))[2:]
#print "MAD"+str(act_y)
#print hex(act_y)[2:]
if char != ('0xffff','0xffff'):
print "SET [0x8%s], 0x%s%s%s ; %s Loc %s=0x%s; char_idx: %s=0x%s" % (assembly_loc, bg_col, fg_col, char_idx, (x+x_offset,y+y_offset), x+32*y, assembly_loc, self.chars.unique_words.index(char)+char_offset, char_idx)
def get_anim(self, char_offset=0):
#char_offset = 117
# Jumping
frame_width = 3
frame_height = 2
frame_count_x = 3
frame_count_y = 1 # It Just Don' work ATM
x_offset = 18
y_offset = 0 # Maybe doesn't work?
anim_name = "jumpingPixel"
'''
# Walking
frame_width = 3
frame_height = 2
frame_count_x = 3
frame_count_y = 1
x_offset = 0
y_offset = 0
'''
def make_len_2(hex_thing):
hex_thing = hex_thing[2:]
return '0x%s%s' % ('0'*(2 - len(hex_thing)), hex_thing)
for i,char in enumerate(self.chars.unique_words):
#gen_palette_comment(char)
print "DAT %s, %s ; %s = %s" % (char[0], char[1], i+char_offset, hex(i+char_offset))
curr_anim = 0
for x in range(frame_width):
for y in range(frame_height):
print ":"+anim_name+"_"+str(curr_anim)
for i in range(frame_count_x):
bg_col, fg_col = self.chars.loc_to_color_idx[(x,y)]
loc = (x_offset+x+i*frame_width,y_offset+y*frame_count_y)
char = self.chars.loc_to_words[loc]
char_idx = make_len_2(hex(self.chars.unique_words.index(char)+char_offset))[2:]
print " DAT 0x%s%s%s ; %s" % (bg_col, fg_col, char_idx, loc)
curr_anim += 1
def get_num_from_rgb(col):
col = col[0:3]
# col is a tuple in (r,g,b) format with colors from 0 to 255
def format_el(el):
int_val = int(round(el/255.0*15.0))
str_val = hex(int_val)
return str_val[2:]
col = ''.join([format_el(el) for el in col])
return int('0x%s%s' % ('0'*(4 - len(col)), col),16)
def read_grid_square(pixels, startx, starty, color_palette):
all_words = list()
local_palette = set()
startx *= 4
starty *= 8
for x in range(startx,startx+4):
for y in range(starty+7,starty-1,-1):
curr_col = get_num_from_rgb(pixels[x,y])
local_palette.add(curr_col)
if len(local_palette) > 2:
print [hex(c) for c in local_palette]
raise Exception("A character can only use 2 colors!!!")
fgbg = color_palette.get_fgbg_from_color(curr_col)
all_words.append(fgbg)
def make_len_4(hex_thing):
hex_thing = hex_thing[2:]
return '0x%s%s' % ('0'*(4 - len(hex_thing)), hex_thing)
all_words = ''.join([str(i) for i in all_words])
words = (make_len_4(hex(int('0b'+all_words[0:16],2))),
make_len_4(hex(int('0b'+all_words[16:32],2))))
return words, local_palette # 0s are FG
def write_to_image(char,image_pixels,start_x,start_y):
def make_bin_len_16(bin_thing):
bin_thing = bin_thing[2:]
return '0b%s%s' % ('0'*(16 - len(bin_thing)), bin_thing)
word1 = bin(int(char[0],16))
word2 = bin(int(char[1],16))
char = make_bin_len_16(word1)[2:]+make_bin_len_16(word2)[2:]
char = char
displayChar = list()
for y in range(8):
displayChar.append(['x']*4)
for y in range(7,-1,-1):
for x in range(4):
displayChar[y][x] = char[x*8 + 7 - y]
def nice_format(c):
if c == '0':
return (0,0,0,255)
else:
return (255,255,255,255)
for y in range(8):
for x,c in enumerate(displayChar[y]):
image_pixels[x+start_x,y+start_y] = nice_format(c)
def gen_palette_comment(char):
def make_bin_len_16(bin_thing):
bin_thing = bin_thing[2:]
return '0b%s%s' % ('0'*(16 - len(bin_thing)), bin_thing)
word1 = bin(int(char[0],16))
word2 = bin(int(char[1],16))
char = make_bin_len_16(word1)[2:]+make_bin_len_16(word2)[2:]
char = char
displayChar = list()
for y in range(8):
displayChar.append(['x']*4)
for y in range(7,-1,-1):
for x in range(4):
displayChar[y][x] = char[x*8 + 7 - y]
def nice_format(c):
if c == '0':
return ' '
else:
return '*'
for y in range(8):
print ' ;' + ' '.join([nice_format(c) for c in displayChar[y]]) + ';'
def parse_image(image, offset = 0):
color_palette = Palette(
{
0: (0x0000, BG), # Black
1: (0x0e33, FG), # Red
2: (0x0777, FG), # Person gray
3: (0x0ddd, FG), # Building gray
4: (0x0fff, FG), # White
})
char_palette = CharPalette()
image_map = ImageMap(color_palette, char_palette)
image_width, image_height = image.size
image_pixels = image.load()
for x in range(int(image_width/4)):
for y in range(int(image_height/8)):
char_words, local_palette = read_grid_square(image_pixels, x, y, color_palette)
cols = [0,0]
for col in local_palette:
cols[color_palette.get_fgbg_from_color(col)] = color_palette.get_index_from_color(col)
char_palette.add_word((x,y), char_words, cols[FG], cols[BG])
return image_map
if __name__ == "__main__":
char_offset = 0
image_name = ""
if len(sys.argv) > 1:
image_name = sys.argv[1]
else:
raise Exception("You have not specified an image file!")
if len(sys.argv) > 2:
char_offset = int(sys.argv[2])
image = Image.open(image_name)
image_map = parse_image(image, offset = char_offset)
#image_map.create_image("moo.png", image.size)
image_map.get_splash_screen()
#image_map.get_anim()