-
Notifications
You must be signed in to change notification settings - Fork 6
/
md1702_gfx.py
executable file
·199 lines (178 loc) · 6.31 KB
/
md1702_gfx.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
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Copyright 2019 Pavel Moravec
# This tool is meant to manipulate boot logo images from Baofeng DM-1702/DM-X
# radios to make it possible to create custom own boot logos. Please note
# that present firmware implementations have a bug where a line between light
# blue background and buttons is not drawn and remains with what was in boot
# logo until some editor widget with white background is used.
from PIL import Image
import os.path
import sys
# For now works only with the known DM-1702/DM-X display format. If needed,
# the following two values may have to be changed to reflect other radio
# display dimensions and 16-byte header
gfx_size = 160, 128
logo_hdr = B'\x02\xa0\x80\x00\x50\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'
python_v2 = (sys.version.split('.')[0] == '2')
verbose_err = True
def usage():
print("""
Usage: md1702-gfx <command> <arguments>
Read a Boot image from a file and write it to bootlogo image
md1702-gfx toimage <bootlogo.bin> <bootlogo.png>
Read a bootlogo image and write it to Boot image
md1702-gfx fromimage <bootlogo.png> <bootlogo.bin>
Read a bootlogo image and show it on screen
md1702-gfx show <bootlogo.bin>
""")
def gfx_from_image(data):
odata = bytearray()
i = 0
while i < len(data):
if python_v2:
rgb8 = ((ord(data[i]) & 0xe0) | ((ord(data[i+1]) & 0xe0) >> 3) | (ord(data[i+2]) >> 6))
else:
rgb8 = (((data[i]) & 0xe0) | (((data[i+1]) & 0xe0) >> 3) | ((data[i+2]) >> 6))
i += 3
if python_v2:
odata += chr(rgb8)
else:
odata.append(rgb8)
return bytes(odata)
def gfx_to_image(data):
odata = bytearray()
for b in data:
if python_v2:
b = ord(b)
red = b & 0xe0
if red == 0xe0:
red = 0xff;
else:
red |= red >> 4
green = (b << 3) & 0xe0
if green == 0xe0:
green = 0xff;
else:
green |= green >> 4
blue = (b << 6) & 0xc0
if blue == 0xc0:
blue = 0xff;
else:
blue|= blue >> 4
if python_v2:
rgb = chr(red) + chr(green) + chr(blue)
odata += rgb
else:
odata.append(red)
odata.append(green)
odata.append(blue)
return bytes(odata)
def read_file(infile):
if infile.split(".")[-1] == 'txt':
odata=bytearray()
with open(infile, 'r') as f:
data = f.read()
for line in data.split('\n'):
line = line.split('//')[0]
for val in line.split(','):
val = val.strip().lower()
if val == '' : continue
x = (int(val, 16) if val[0:2] == '0x' else int(val))
if python_v2:
odata += chr(x)
else:
odata.append(x)
f.close()
#sys.stderr.write("Input text file\n")
else:
with open(infile, 'rb') as f:
odata = f.read()
f.close()
return bytes(odata)
def write_file(outfile, hdr,data):
if outfile.split(".")[-1] == 'txt':
with open(outfile, 'w') as f:
i=0
for h in hdr: # Very convoluted format of the text file, probably not needed
if python_v2: h = ord(h)
if (i < 5):
f.write("0x%02x," % h)
else:
f.write("%i," % h)
i += 1
i=0
for d in data:
if i % 16 == 0: f.write('\n')
i += 1
if python_v2: d = ord(d)
f.write("0X%02X," % d)
f.write('\n')
f.close()
else:
with open(outfile, 'wb') as f:
f.write(hdr)
f.write(data)
f.close()
def main():
try:
if len(sys.argv) == 4:
infile=sys.argv[2]
outfile=sys.argv[3]
if sys.argv[1] == 'fromimage':
hdrfile = infile + '.hdr'
im = Image.open(infile)
if im is None:
print("The image could not be opened")
return
im.thumbnail(gfx_size)
im=im.convert("RGB")
hdr = None
if os.path.isfile(hdrfile):
with open(infile + '.hdr', 'rb') as f:
hdr = f.read()
f.close()
if hdr is None:
hdr = logo_hdr
data = gfx_from_image(im.tobytes())
write_file(outfile, hdr, data)
elif sys.argv[1] == 'toimage':
data=read_file(infile)
data1 = data[0:16]
data2 = data[16:gfx_size[1] * gfx_size[0] + 16]
if data1 != logo_hdr:
print("The image header has changed, proceed with caution")
if len(data2) < gfx_size[1] * gfx_size[0]: # Accept larger image dumps
print("The image size does not match, probably a different model, giving up")
return
f2 = open(outfile + '.hdr', 'wb')
f2.write(data1)
f2.close()
data2 = gfx_to_image(data2)
#im = Image.new("RGB", gfx_size, 0xffffff)
im = Image.frombytes("RGB", gfx_size, data2)
im.save(outfile)
else:
usage()
elif len(sys.argv) == 3:
if sys.argv[1] == 'show':
data=read_file(sys.argv[2])
data2 = data[16:gfx_size[1] * gfx_size[0] + 16]
if len(data2) != gfx_size[1] * gfx_size[0]:
print("The image size does not match, probably a different model, giving up")
return
im = Image.frombytes("RGB", gfx_size, gfx_to_image(data2))
im.show()
else:
usage()
else:
usage()
except (RuntimeError, Exception) as e:
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
if verbose_err:
print(exc_type, fname, exc_tb.tb_lineno)
print(e)
exit(1)
if __name__ == '__main__':
main()