3 from __future__
import division
, print_function
13 def __init__(self
, out
):
14 self
.state
= self
.RLE_BYTE
19 def eq_prev_byte(self
, in_byte
):
20 if self
.prev_byte
is None:
22 return in_byte
== self
.prev_byte
24 def encode_byte(self
, in_byte
):
26 if self
.state
== self
.RLE_BYTE
:
28 if self
.eq_prev_byte(in_byte
):
29 self
.state
= self
.RLE_SEQ
32 self
.prev_byte
= in_byte
34 elif self
.state
== self
.RLE_SEQ
:
35 if self
.eq_prev_byte(in_byte
):
40 self
.state
= self
.RLE_BYTE
44 self
.prev_byte
= in_byte
45 self
.state
= self
.RLE_BYTE
48 if self
.state
== self
.RLE_SEQ
:
52 def __init__(self
, out
):
55 def encode_byte(self
, in_byte
):
61 def writeSize(f
, width
, height
):
63 f
.write("%d,%d,%d,%d,\n" % (width
% 256, width
// 256, height
% 256, height
// 256))
65 f
.write("%d,%d,\n" % (width
, height
))
68 def writeValue(value
):
69 f
.write("0x%02x," % value
)
72 def F_getPixelCoord():
77 def F_getPixelCoord_R(w
,h
):
84 image
= Image
.open(sys
.argv
[1])
85 width
, height
= image
.size
87 image_fmt
= image
.format
88 output_filename
= sys
.argv
[2]
90 with
open(output_filename
, "w") as f
:
91 lcdwidth
= int(sys
.argv
[3])
94 s
= sys
.argv
[4].split('-')
96 pixel_coord
= F_getPixelCoord()
97 if (len(s
) > 1) and (s
[1] == 'R'):
98 pixel_coord
= F_getPixelCoord_R(width
,height
)
100 for s
in ("03x05", "04x06", "05x07", "08x10", "10x14", "22x38"):
105 extension
= os
.path
.splitext(output_filename
)[1]
106 out
= F_writeValue(f
)
108 if extension
== ".rle" or ((len(sys
.argv
) > 5) and (sys
.argv
[5] == "rle")):
109 encoder
= RLE_encoder(out
)
111 encoder
= dummy_encoder(out
)
115 if len(sys
.argv
) > 5:
116 rows
= int(sys
.argv
[5])
117 image
= image
.convert(mode
='1')
118 writeSize(f
, width
, height
// rows
)
119 for y
in range(0, height
, 8):
120 for x
in range(width
):
124 if image_fmt
== 'XBM':
125 if image
.getpixel((x
, y
+ z
)) > 0:
128 if image
.getpixel((x
, y
+ z
)) == 0:
130 encoder
.encode_byte(value
)
133 elif what
== "4bits":
135 writeSize(f
, width
, height
)
136 image
= image
.convert(mode
='L')
137 for y
in range(0, height
, 2):
138 for x
in range(width
):
140 gray1
= image
.getpixel((x
, y
))
142 gray2
= image
.getpixel((x
, y
+ 1))
146 if (gray1
& (1 << (4 + i
))):
148 if (gray2
& (1 << (4 + i
))):
149 value
-= 1 << (4 + i
)
150 encoder
.encode_byte(value
)
153 elif what
== "8bits":
155 writeSize(f
, width
, height
)
156 image
= image
.convert(mode
='L')
157 for y
in range(height
):
158 for x
in range(width
):
159 value
= image
.getpixel((x
, y
))
160 value
= 0x0f - (value
>> 4)
161 encoder
.encode_byte(value
)
164 elif what
== "4/4/4/4":
165 constant
= sys
.argv
[2].upper()[:-4]
167 writeSize(f
, width
, height
)
168 for y
in range(height
):
169 for x
in range(width
):
170 pixel
= image
.getpixel(pixel_coord(x
, y
))
171 val
= ((pixel
[3] // 16) << 12) + ((pixel
[0] // 16) << 8) + ((pixel
[1] // 16) << 4) + ((pixel
[2] // 16) << 0)
172 encoder
.encode_byte(val
& 255)
173 encoder
.encode_byte(val
>> 8)
176 #if extension != ".rle":
177 # f.write("const Bitmap %s(BMP_ARGB4444, %d, %d, (uint16_t*)__%s);\n" % (constant, width, height, constant))
178 elif what
== "5/6/5":
179 constant
= sys
.argv
[2].upper()[:-4]
181 writeSize(f
, width
, height
)
182 for y
in range(height
):
183 for x
in range(width
):
184 pixel
= image
.getpixel(pixel_coord(x
, y
))
185 val
= ((pixel
[0] >> 3) << 11) + ((pixel
[1] >> 2) << 5) + ((pixel
[2] >> 3) << 0)
186 encoder
.encode_byte(val
& 255)
187 encoder
.encode_byte(val
>> 8)
190 #if extension != ".rle":
191 # f.write("const Bitmap %s(BMP_RGB565, %d, %d, (uint16_t*)__%s);\n" % (constant, width, height, constant))
192 elif what
== "03x05":
193 image
= image
.convert(mode
='L')
194 for y
in range(0, height
, 5):
195 for x
in range(width
):
198 if image
.getpixel((x
, y
+ z
)) == 0:
200 f
.write("0x%02x," % value
)
202 elif what
== "04x06":
203 image
= image
.convert(mode
='L')
204 for y
in range(0, height
, 7):
205 for x
in range(width
):
208 if image
.getpixel((x
, y
+ z
)) == 0:
212 f
.write("0x%02x," % value
)
214 elif what
== "05x07":
215 image
= image
.convert(mode
='L')
216 for y
in range(0, height
, 8):
217 for x
in range(width
):
220 if image
.getpixel((x
, y
+ z
)) == 0:
222 f
.write("0x%02x," % value
)
224 elif what
== "08x10":
225 image
= image
.convert(mode
='L')
226 for y
in range(0, height
, 12):
227 for x
in range(width
):
229 for l
in range(0, 12, 8):
233 if image
.getpixel((x
, y
+ l
+ z
)) == 0:
239 f
.write("0x%02x," % value
)
241 elif what
== "10x14":
242 image
= image
.convert(mode
='L')
243 for y
in range(0, height
, 16):
244 for x
in range(width
):
245 for l
in range(0, 16, 8):
248 if image
.getpixel((x
, y
+ l
+ z
)) == 0:
250 f
.write("0x%02x," % value
)
252 elif what
== "22x38":
253 image
= image
.convert(mode
='L')
254 for y
in range(0, height
, 40):
255 for x
in range(width
):
256 for l
in range(0, 40, 8):
259 if image
.getpixel((x
, y
+ l
+ z
)) == 0:
261 f
.write("0x%02x," % value
)
264 print("wrong argument", sys
.argv
[4])