-
Notifications
You must be signed in to change notification settings - Fork 11
/
bitshuffle.py
55 lines (43 loc) · 1.36 KB
/
bitshuffle.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
# pythonic bitshuffle like but slow
# it rearanges most significant bits in a byte like 8x8 or 16x16 by transposing the matrix given.
# It speedsup compression so it can be uses as a step before compressing.
# The only dependence is numpy.
# Author Dario Clavijo 2017
# License GPLv3
import numpy
def bintobit(tmp):
tmp = numpy.frombuffer(tmp, dtype=numpy.uint8)
tmp = numpy.unpackbits(tmp)
return tmp
def Transpose8X8(tmp):
tmp = bintobit(tmp)
tmp = numpy.reshape(tmp, (8, 8))
tmp = numpy.reshape(tmp.T, (1, 64))
tmp = numpy.packbits(tmp)
return tmp.tobytes()
def Transpose16X16(tmp):
tmp = bintobit(tmp)
tmp = numpy.reshape(tmp, (16, 16))
tmp = numpy.reshape(tmp.T, (1, 256))
tmp = numpy.packbits(tmp)
return tmp.tobytes()
def bitshuffle(buff, mode):
if len(buff) % 8 != 0:
raise Exception("Buffer must be a multiple of 8 bytes")
buff2 = ""
if mode == 8:
for i in range(0, len(buff) - 1, 8):
buff2 += Transpose8X8(buff[i : i + 8])
elif mode == 16:
for i in range(0, len(buff) - 1, 16):
buff2 += Transpose16X16(buff[i : i + 16])
return buff2
if __name__ == "__main__":
a = "Dario Clavijo "
print(a, len(a))
print(a.encode("hex"))
print(bintobit(a))
a = bitshuffle(a)
print(a.encode("hex"))
print(bintobit(a))
print(bitshuffle(a))