-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbufresize.py
More file actions
303 lines (249 loc) · 6.18 KB
/
bufresize.py
File metadata and controls
303 lines (249 loc) · 6.18 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
"""
Buffer resize, pack and unpack mod
-----------------------------------
| Copyright 2022 by Joel C. Alcarez |
| [[email protected]] |
|-----------------------------------|
| We make absolutely no warranty |
| of any kind, expressed or implied |
|-----------------------------------|
| Contact primary author |
| if you plan to use this |
| in a commercial product at |
-----------------------------------
"""
from array import array
from .mathlib import(
addvectinlist,
intscalarmulvect,
enumbits
)
from .buffersplit import(
altsplitbuf3way,
altsplitbufnway
)
from .colors import makeBGRbuf
def adjustbufsize(
bufsize: int, bits: int) -> int:
"""Adjust buffer size to
account for bit depth
Args:
bufsize: initial estimate
of buffer size
bits : bit depth of bitmap
(1, 4, 8, 24) bits
Returns:
An adjusted int value of the
buffer size
"""
if bits == 1:
bufsize >>= 3
elif bits == 24:
bufsize *= 3
elif bits == 4:
bufsize >>= 1
return bufsize
def adjustxbufsize(bmp: array,
x1: int, x2: int) -> int:
"""Returns a 32 -bit padded
int buffer size for a
buffer with the bit depth
stored in byte number 28
of the bitmap bmp and an
int length of x2 - x1 + 1
Args:
bmp : unsigned byte array
with bmp format
x1, x2: int params for
a x coord line
in the bitmap
Returns:
int adjusted buffer size
"""
return adjustbufsize(x2 - x1 + 1,
bmp[28])
def packbitlisttobuf(blist: list[int]
) -> list[int]:
"""Packs literal list of ones and
zeros to a list of bytes
Args:
blist: literal list
of ones and zeros
Returns:
list
"""
retval = []
j = len(blist) + 1
i = 1
b = 0
while i < j:
m = i % 8
b += blist[i - 1] << (7 - m)
if m == 0 and i > 1:
retval += [b]
b = 0
i += 1
return retval
def resizebitpattenNtimesbigger(
byteval: int, n: int):
"""Resize byte n times bigger
bit wise
Args:
buf: unsigned byte
n : buffer multiplier
Returns:
list of ones and zeroes
"""
retval = []
for bit in enumbits(byteval):
retval += [bit] * n
return retval
def resize1bitbufNtimesbigger(
buf: array, n: int):
"""Resize a 1-bit buffer
n times bigger
Args:
buf: unsigned byte array
n : buffer multiplier
Returns:
unsigned byte array
"""
retval = []
for b in buf:
retval += packbitlisttobuf(
resizebitpattenNtimesbigger(
b, n))
return array('B', retval)
def unpack4bitbuf(buf: list[int]
) ->list[int]:
"""Unpacks a 4-bit buffer
into a list
Args:
buf: unsigned byte array
Returns:
list
"""
retval = []
for b in buf:
retval += [b >> 4, b & 0xf]
return retval
def unpack4bitbufresizeNtimesbigger(
buf: list[int], n: int
) -> list[int]:
"""Unpacks a 4-bit buffer into a
list and repeats 4-bit units
to resize the buffer int n
times bigger
Args:
buf: unsigned byte array
n : unsigned int multiplier
to resize buffer
Returns:
list
"""
retval = []
for b in buf:
retval += [b >> 4] * n
retval += [b & 0xf] * n
return retval
def pack4bitbuf(
unpackedbuf: list[int]
) -> list[int]:
"""Packs an unpacked 4-bit buffer
into a list
Args:
buf: unsigned byte array
or list
Returns:
list
"""
retval = []
j = len(unpackedbuf) - 1
i = 0
while i < j:
retval += \
[(unpackedbuf[i] << 4) + \
unpackedbuf[i + 1]]
i += 2
return retval
def resize4bitbufNtimesbigger(
buf: array, n: int
) -> array:
"""Resize a 4-bit buffer n times
bigger
Args:
buf: An unsigned byte array
n : buffer multiplier
Returns:
unsigned byte array
"""
return array('B', pack4bitbuf(
unpack4bitbufresizeNtimesbigger(
buf, n)))
def resize8bitbufNtimesbigger(
buf: array, n: int
) -> array:
"""Resize a 8-bit buffer n times
bigger
Args:
buf: unsigned byte array
n : buffer multiplier
Returns:
unsigned byte array
"""
retval = []
for b in buf:
retval += [b] * n
return array('B', retval)
def resizesmaller24bitbuf(buf: array
) -> array:
"""Resize a 24-bit buffer
n times smaller
Args:
buf: unsigned byte array
n : buffer multiplier
Returns:
unsigned byte array
"""
n = len(buf)
f = 1 / n**2
b = altsplitbuf3way(
addvectinlist(buf))
return makeBGRbuf(
*[intscalarmulvect(
addvectinlist(
altsplitbufnway(i, n)), f)
for i in b])
def resize24bitbufNtimesbigger(
buf: array, n: int):
"""Resize a 24-bit buffer n times
bigger
Args:
buf: unsigned byte array
n : buffer multiplier
Returns:
unsigned byte array
"""
buf = altsplitbuf3way(buf)
return makeBGRbuf(
*[resize8bitbufNtimesbigger(
i, n) for i in buf])
def resizebufNtimesbigger(buf: array,
n: int, bits: int) -> array:
"""Resize a buffer n times bigger
given a particular bit depth n
Args:
buf : array to resize
n : resize factor
bits: bit depth of
color info
(1, 4, 8, 24) bits
Returns:
list
"""
f={24: resize24bitbufNtimesbigger,
8: resize8bitbufNtimesbigger,
4: resize4bitbufNtimesbigger,
1: resize1bitbufNtimesbigger}[bits]
return f(buf, n)