[Horus] font fixes (#5901)
[opentx.git] / radio / util / img2lbm.py
blob341502e00bbf998210be2646bfca4cee944bdf88
1 #!/usr/bin/env python
3 from __future__ import division, print_function
5 import sys
6 from PIL import Image
8 image = Image.open(sys.argv[1])
9 width, height = image.size
11 image_fmt = image.format
13 def writeSize(f, width, height):
14 if lcdwidth > 255:
15 f.write("%d,%d,%d,%d,\n" % (width % 256, width // 256, height % 256, height // 256))
16 else:
17 f.write("%d,%d,\n" % (width, height))
20 with open(sys.argv[2], "w") as f:
21 lcdwidth = int(sys.argv[3])
23 if len(sys.argv) > 4:
24 what = sys.argv[4]
25 else:
26 for s in ("03x05", "04x06", "05x07", "08x10", "10x14", "22x38"):
27 if s in sys.argv[2]:
28 what = s
29 break
31 if what == "1bit":
32 rows = 1
33 if len(sys.argv) > 5:
34 rows = int(sys.argv[5])
35 image = image.convert(mode='1')
36 writeSize(f, width, height // rows)
37 for y in range(0, height, 8):
38 for x in range(width):
39 value = 0
40 for z in range(8):
41 if y + z < height:
42 if image_fmt == 'XBM':
43 if image.getpixel((x, y + z)) > 0:
44 value += 1 << z
45 else:
46 if image.getpixel((x, y + z)) == 0:
47 value += 1 << z
48 f.write("0x%02x," % value)
49 f.write("\n")
50 elif what == "4/4/4/4":
51 constant = sys.argv[2].upper()[:-4]
52 values = []
53 for y in range(height):
54 for x in range(width):
55 pixel = image.getpixel((x, y))
56 val = ((pixel[3] // 16) << 12) + ((pixel[0] // 16) << 8) + ((pixel[1] // 16) << 4) + ((pixel[2] // 16) << 0)
57 values.append(str(val))
58 f.write("const uint16_t __%s[] __ALIGNED = { %s };\n" % (constant, ",".join(values)))
59 f.write("const Bitmap %s(BMP_ARGB4444, %d, %d, __%s);\n" % (constant, width, height, constant))
60 elif what == "4/4/4/4-R":
61 constant = sys.argv[2].upper()[:-4]
62 values = []
63 for y in range(height):
64 for x in range(width):
65 pixel = image.getpixel((width-x-1, height-y-1))
66 val = ((pixel[3] // 16) << 12) + ((pixel[0] // 16) << 8) + ((pixel[1] // 16) << 4) + ((pixel[2] // 16) << 0)
67 values.append(str(val))
68 f.write("const uint16_t __%s[] __ALIGNED = { %s };\n" % (constant, ",".join(values)))
69 f.write("const Bitmap %s(BMP_ARGB4444, %d, %d, __%s);\n" % (constant, width, height, constant))
70 elif what == "5/6/5":
71 constant = sys.argv[2].upper()[:-4]
72 values = []
73 for y in range(height):
74 for x in range(width):
75 pixel = image.getpixel((x, y))
76 val = ((pixel[0] >> 3) << 11) + ((pixel[1] >> 2) << 5) + ((pixel[2] >> 3) << 0)
77 values.append(str(val))
78 f.write("const uint16_t __%s[] __ALIGNED = { %s };\n" % (constant, ",".join(values)))
79 f.write("const Bitmap %s(BMP_RGB565, %d, %d, __%s);\n" % (constant, width, height, constant))
80 elif what == "5/6/5-R":
81 constant = sys.argv[2].upper()[:-4]
82 values = []
83 for y in range(height):
84 for x in range(width):
85 pixel = image.getpixel((width-x-1, height-y-1))
86 val = ((pixel[0] >> 3) << 11) + ((pixel[1] >> 2) << 5) + ((pixel[2] >> 3) << 0)
87 values.append(str(val))
88 f.write("const uint16_t __%s[] __ALIGNED = { %s };\n" % (constant, ",".join(values)))
89 f.write("const Bitmap %s(BMP_RGB565, %d, %d, __%s);\n" % (constant, width, height, constant))
90 # elif what == "5/6/5/8":
91 # colors = []
92 # writeSize(f, width, height)
93 # for y in range(height):
94 # for x in range(width):
95 # pixel = image.pixel(x, y)
96 # val = ((Qt.qRed(pixel) >> 4) << 12) + ((Qt.qGreen(pixel) >> 4) << 7) + ((Qt.qBlue(pixel) >> 4) << 1)
97 # f.write("%d,%d,%d," % (val % 256, val // 256, Qt.qAlpha(pixel)))
98 # f.write("\n")
99 elif what == "4bits":
100 colors = []
101 writeSize(f, width, height)
102 image = image.convert(mode='L')
103 for y in range(0, height, 2):
104 for x in range(width):
105 value = 0xFF
106 gray1 = image.getpixel((x, y))
107 if y + 1 < height:
108 gray2 = image.getpixel((x, y + 1))
109 else:
110 gray2 = 255
111 for i in range(4):
112 if (gray1 & (1 << (4 + i))):
113 value -= 1 << i
114 if (gray2 & (1 << (4 + i))):
115 value -= 1 << (4 + i)
116 f.write("0x%02x," % value)
117 f.write("\n")
118 elif what == "8bits":
119 colors = []
120 writeSize(f, width, height)
121 image = image.convert(mode='L')
122 for y in range(height):
123 for x in range(width):
124 value = image.getpixel((x, y))
125 value = 0x0f - (value >> 4)
126 f.write("0x%02x," % value)
127 f.write("\n")
128 elif what == "03x05":
129 image = image.convert(mode='L')
130 for y in range(0, height, 5):
131 for x in range(width):
132 value = 0
133 for z in range(5):
134 if image.getpixel((x, y + z)) == 0:
135 value += 1 << z
136 f.write("0x%02x," % value)
137 f.write("\n")
138 elif what == "04x06":
139 image = image.convert(mode='L')
140 for y in range(0, height, 7):
141 for x in range(width):
142 value = 0
143 for z in range(7):
144 if image.getpixel((x, y + z)) == 0:
145 value += 1 << z
146 if value == 0x7f:
147 value = 0xff
148 f.write("0x%02x," % value)
149 f.write("\n")
150 elif what == "05x07":
151 image = image.convert(mode='L')
152 for y in range(0, height, 8):
153 for x in range(width):
154 value = 0
155 for z in range(8):
156 if image.getpixel((x, y + z)) == 0:
157 value += 1 << z
158 f.write("0x%02x," % value)
159 f.write("\n")
160 elif what == "08x10":
161 image = image.convert(mode='L')
162 for y in range(0, height, 12):
163 for x in range(width):
164 skip = True
165 for l in range(0, 12, 8):
166 value = 0
167 for z in range(8):
168 if l + z < 12:
169 if image.getpixel((x, y + l + z)) == 0:
170 value += 1 << z
171 else:
172 skip = False
173 if skip and l == 8:
174 value = 0xff
175 f.write("0x%02x," % value)
176 #print ("0x%02x (%d,%d,%d)" % (value,x,y,l))
177 f.write("\n")
178 elif what == "10x14":
179 image = image.convert(mode='L')
180 for y in range(0, height, 16):
181 for x in range(width):
182 for l in range(0, 16, 8):
183 value = 0
184 for z in range(8):
185 if image.getpixel((x, y + l + z)) == 0:
186 value += 1 << z
187 f.write("0x%02x," % value)
188 f.write("\n")
189 elif what == "22x38":
190 image = image.convert(mode='L')
191 for y in range(0, height, 40):
192 for x in range(width):
193 for l in range(0, 40, 8):
194 value = 0
195 for z in range(8):
196 if image.getpixel((x, y + l + z)) == 0:
197 value += 1 << z
198 f.write("0x%02x," % value)
199 f.write("\n")
200 else:
201 print("wrong argument", sys.argv[4])