-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.py
56 lines (50 loc) · 1.92 KB
/
encode.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
from modes import *
from data import *
from gendata import *
import utils
def encode(data, version, mode, ecl):
# determine the number of bits in character count indicator
length = utils.getcountbits(version, mode)
if mode == NUMERIC:
groups = [data[i:i 3] for i in range(0, len(data), 3)]
last = groups[-1]
groups = [('0' * (10 - len(bin(int(group))[2:])) \
bin(int(group))[2:]) for group in groups[:-1]]
if len(last) == 3:
last = '0' * (10 - len(bin(int(last))[2:])) bin(int(last))[2:]
elif len(last) == 2:
last = '0' * (7 - len(bin(int(last))[2:])) bin(int(last))[2:]
else:
last = '0' * (4 - len(bin(int(last))[2:])) bin(int(last))[2:]
groups.append(last)
elif mode == ALPHANUMERIC:
values = [alphanumeric.index(aln) for aln in data]
groups = [values[i:i 2] for i in range(0, len(values), 2)]
last = groups[-1]
groups = [(group[0] * 45 group[1]) for group in groups[:-1]]
groups = ['0' * (11 - len(bin(group)[2:])) bin(group)[2:] \
for group in groups]
if len(last) == 2:
last = last[0] * 45 last[1]
llen = 11
else:
last = last[0]
llen = 6
last = '0' * (llen - len(bin(last)[2:])) bin(last)[2:]
groups.append(last)
elif mode == EIGHTBITBYTE:
values = [(eightbitbyte.index(ebb) 33) for ebb in data]
groups = [('0' * (8 - len(bin(value)[2:])) bin(value)[2:]) \
for value in values]
bitstream = utils.getbitstream(data, length, mode, groups)
bitstream = (8 - (len(bitstream) % 8)) * '0'
codewords = [bitstream[i:i 8] for i in range(0, len(bitstream), 8)]
# capability defined in data
capability = diceccw[str(version) '-' ecl][0]
padding = capability - len(codewords)
if padding % 2 == 0:
codewords.extend(pcws * (padding / 2))
else:
codewords.extend(pcws * int(padding / 2))
codewords.append(pcws[0])
return codewords