-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbufferflip.py
More file actions
84 lines (67 loc) · 1.76 KB
/
bufferflip.py
File metadata and controls
84 lines (67 loc) · 1.76 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
"""
Bit rotation and buffer flipping 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 rotatebits
from .colors import RGB2BGRbuf
def rotatebitsinbuf(
buf: array) -> array:
"""Does a bit rotate to the bytes
in an unsigned byte array
Args:
buf: unsigned byte array
Returns:
unsigned byte array
"""
return array('B', [rotatebits(b)
for b in buf])
def flipnibbleinbuf(
buf: array) -> array:
"""Flips a 4-bit image buffer
Args:
buf: unsigned byte array
Returns:
unsigned byte array
"""
return array('B', [(b >> 4) + ((b % 16) << 4)
for b in buf])
def flip24bitbuf(buf: array) -> array:
"""Flips a 24-bit buffer
Args:
buf: unsigned byte array
Returns:
unsigned byte array
"""
buf.reverse()
RGB2BGRbuf(buf)
return array('B',buf)
def flipbuf(buf: array, bits: int
) -> array:
"""Flips/rotates bits in buffer
Args:
buf : unsigned byte array
bits: (1, 4, 8, 24) bits
Returns:
unsigned byte array
"""
if bits == 24:
buf = flip24bitbuf(buf)
else:
buf.reverse()
if bits == 4:
buf = flipnibbleinbuf(buf)
if bits == 1:
buf = rotatebitsinbuf(buf)
return buf