-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlvgl.py
203 lines (153 loc) · 5.77 KB
/
lvgl.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
# Ported the compress function from https://github.com/lvgl/lv_font_conv/blob/master/LICENSE
'''
https://github.com/lvgl/lv_font_conv/blob/master/LICENSE
Copyright (c) 2018 authors
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.'''
class bitstream():
def __init__(self):
self.bs = bytearray()
self.b = 0x00
self.offset = 0
def clear(self):
self.bs.clear()
self.b = 0x00
self.offset = 0
def writebits(self, v, n):
'''for i in reversed(range(n)):
print((v >> i) & 0x01, end='')
'''
if n > 8:
raise ValueError("N should be <= 8")
v = v & ((1 << n)-1)
if (self.offset + n) >= 8:
#print('^b:{0},offset:{1},v:{2},n:{3}'.format(self.b, self.offset, v, n))
self.bs.append((self.b << (8-self.offset)) | (v >> (self.offset + n - 8)) )
self.offset = self.offset + n - 8
if self.offset:
self.b = v & ((1 << self.offset) - 1)
else:
self.b = 0x00
else:
#print(' b:{0},offset:{1},v:{2},n:{3}'.format(self.b, self.offset, v, n))
self.b = (self.b << n) | v
self.offset += n
def get_result(self):
if self.offset != 0:
#print('$b:{0},offset{1}'.format(self.b,self.offset))
self.bs.append(self.b << (8-self.offset))
self.offset = 0
self.b = 0x00
return self.bs
def count_same(arr, offset):
same = 1
val = arr[offset]
for idx in range(offset+1, len(arr)):
if(arr[idx] != val):
break
same += 1
return same
def prefilter(pixel_2d):
for idy in range(1, len(pixel_2d)):
for idx in range(len(pixel_2d[idy])):
pixel_2d[idy][idx] ^= pixel_2d[idy-1][idx]
return
def compress(bitstream, pixels, bpp):
# Minimal repetitions count to enable RLE mode.
RLE_SKIP_COUNT = 1
# Number of repeats, when `1` used to replace data
# If more - write as number
RLE_BIT_COLLAPSED_COUNT = 10
RLE_COUNTER_BITS = 6 # (2^bits - 1) - max value
RLE_COUNTER_MAX = (1 << RLE_COUNTER_BITS) - 1
# Force flush if counter dencity exceeded.
RLE_MAX_REPEATS = RLE_COUNTER_MAX + RLE_BIT_COLLAPSED_COUNT + 1
# let bits_start_offset = bitstream.index
offset = 0
while(offset < len(pixels)):
p = pixels[offset]
same = count_same(pixels, offset)
# Clamp value because RLE counter density is limited
if same > (RLE_MAX_REPEATS + RLE_SKIP_COUNT) :
same = RLE_MAX_REPEATS + RLE_SKIP_COUNT
#print('offset:{0}, same:{1}, p:{2}'.format(offset, same, hex(p)))
offset += same
# If not enough for RLE - write as is.
if same <= RLE_SKIP_COUNT:
for i in range(same):
bitstream.writebits(p, bpp)
continue
# First, write "skipped" head as is.
for i in range(RLE_SKIP_COUNT):
bitstream.writebits(p, bpp)
same -= RLE_SKIP_COUNT
# Not reached state to use counter => dump bit-extended
if same <= RLE_BIT_COLLAPSED_COUNT:
bitstream.writebits(p, bpp)
for i in range(same):
# eslint-disable max-depth
if i < (same - 1):
bitstream.writebits(1, 1)
else:
bitstream.writebits(0, 1)
continue
same -= RLE_BIT_COLLAPSED_COUNT + 1
bitstream.writebits(p, bpp)
for i in range(0, RLE_BIT_COLLAPSED_COUNT+1):
bitstream.writebits(1, 1)
bitstream.writebits(same, RLE_COUNTER_BITS)
def test_compress():
bs = bitstream()
bs.clear()
compress(bs, [1,2,3,2], 8)
if (bs.get_result() != b'\x01\x02\x03\x02'):
print('failed #1', bs.get_result())
exit(1)
bs.clear()
compress(bs, [1,2,3,2], 4)
if (bs.get_result() != b'\x12\x32'):
print('failed #2', bs.get_result())
exit(1)
bs.clear()
compress(bs, [0xFF, 0xF1, 0xFF], 3)
if (bs.get_result() != b'\xE7\x80'):
print('failed #3', bs.get_result())
exit(1)
bs.clear()
compress(bs, [0x1, 0x3, 0x3, 0x3, 0x1], 4)
if (bs.get_result() != b'\x13\x38\x40'):
print('failed #4', bs.get_result())
exit(1)
bs.clear()
pixels = [0 for i in range(15)]
pixels[14] = 3
compress(bs, pixels, 2)
if (bs.get_result() != b'\x0F\xFE\x16'):
print('failed #5', bs.get_result())
exit(1)
bs.clear()
pixels = [0 for i in range(77)]
pixels[76] = 3
compress(bs, pixels, 2)
if (bs.get_result() != b'\x0F\xFF\xF9\x80'):
print('failed #6', bs.get_result())
exit(1)
print('test finished')
#####################################################
#test_compress()