This repository has been archived by the owner on Dec 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
asset_packer.py
161 lines (125 loc) · 3.93 KB
/
asset_packer.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
from PIL import Image
import struct, os, sys
# open method used to open different extension image file
pwd = os.getcwdb().decode("utf-8")
decls = os.path.join(pwd, sys.argv[1])
outdir = os.path.join(pwd, sys.argv[2])
import hashlib, struct, math, gzip
def hexify(x):
f = str(hex(x))
if len(f) == 4:
return f
else:
return f.replace('0x', "0x0")
class BitArr:
def __init__(self):
self.arr=""
@classmethod
def fromBytes(cls, bys):
b = cls()
off = bys[0]
b.addBytes(bys[1:])
# print(off)
if off != 0:
b.arr = b.arr[8-off:]
return b
def addBit(self, boo):
self.arr+=str(1 if boo else 0)
def addBytes(self, by):
self.arr+=''.join(f'{b:08b}' for b in by)
def toBytes(self, prep=True):
v = int(self.arr, 2)
b = bytearray()
for i in range((len(self.arr)//8) + (1 if len(self.arr)%8!=0 else 0 )):
b.append(v & 0xff)
v >>= 8
return (struct.pack("B", (len(self.arr)%8)) if prep else b"")+bytes(b[::-1])
def popBit(self):
b = self.arr[0]
self.arr = self.arr[1:]
return b=='1'
def popByte(self):
by = self.arr[:8]
extra = self.arr[8:]
self.arr = by
try:
nby = self.toBytes(False)
self.arr = extra
return nby
except ValueError as e:
# print(e,"aaaa")
return 0x00
def toHex(self):
b = self.toBytes()
return ", ".join([hexify(bb) for bb in b])
data = BitArr()
dfile = open(decls, "w")
offsets = []
imgdata = BitArr()
for inur in os.listdir(os.path.join(pwd, "imgs")):
im = Image.open(os.path.join(pwd, "imgs", inur))
imgdata2 = BitArr()
rl=0
full = []
for y in range(im.height):
i = 0
row = []
for x in range(im.width):
pixel = im.getpixel((x, y))
px = pixel != (255, 255,255)
# print(pix)
byt = i//8
if len(row)-1!=byt:
row.append(0)
if px:
row[byt] |= 1<<(7-(i%8))
i+=1
rl = len(row)
print(row)
imgdata2.addBytes(bytes(row))
olen = len(imgdata.arr)
imgdata.addBytes(struct.pack("<II", rl, im.height))
dfile.write(f"#define {inur.split('.')[0]}_WIDTH_OFF {olen//8}\n")
dfile.write(f"#define {inur.split('.')[0]}_HEIGHT_OFF {olen//8+4}\n")
dfile.write(f"#define {inur.split('.')[0]}_IMG_OFF {olen//8+8}\n\n")
# dfile.write(f"#define {inur.split('.')[0]}_{iv}_OFF_LOC {len(offsets)}\n\n")
offsets.append(str(olen//8))
imgdata.addBytes(imgdata2.toBytes(False))
#for iv in range(8):
# imgdata2 = BitArr()
# rl=0
# full = []
# for y in range(im.height):
# i = iv
# row = []
# for x in range(im.width):
# pixel = im.getpixel((x, y))
# px = pixel != (255, 255,255)
# # print(pix)
# byt = i//8
# if len(row)-1!=byt:
# row.append(0)
# if px:
# row[byt] |= 1<<(7-(i%8))
# i+=1
# rl = len(row)
# imgdata2.addBytes(bytes(row))
# olen = len(imgdata.arr)
# imgdata.addBytes(struct.pack("<II", rl, im.height))
# dfile.write(f"#define {inur.split('.')[0]}_{iv}_WIDTH_OFF {olen//8}\n")
# dfile.write(f"#define {inur.split('.')[0]}_{iv}_HEIGHT_OFF {olen//8+4}\n")
# dfile.write(f"#define {inur.split('.')[0]}_{iv}_IMG_OFF {olen//8+8}\n\n")
# dfile.write(f"#define {inur.split('.')[0]}_{iv}_OFF_LOC {len(offsets)}\n\n")
# offsets.append(str(olen//8))
# imgdata.addBytes(imgdata2.toBytes(False))
dfile.close()
print("PUT THIS IN main.c :")
print("int offsets[] = {", ", ".join(offsets), "};")
f = open("/tmp/datatopack.dat.tmp", "wb")
f.write(imgdata.toBytes(False))
print(imgdata.toBytes(False))
f.close()
os.system(f"{os.path.join(os.path.dirname((os.path.abspath(__file__))), 'binary_to_appvar')} /tmp/datatopack.dat.tmp \"{outdir}\" {sys.argv[3]}" )
# rl = len(row)
# full.append(", ".join([hexify(p) for p in row]))
# print(f"const char {inur.split('.')[0]}_{iV}[] = "+"{"+",\n".join(full)+"};"+f" // HEIGHT:{im.height} // WIDTH:{(rl)} \n\n")