1
0
mirror of https://github.com/x-hw/amazing-qr.git synced 2025-07-11 01:10:17 +02:00
Files
amazing-qr/ecc.py

59 lines
1.2 KiB
Python
Raw Normal View History

2016-08-26 22:26:14 +08:00
# -*- coding: utf-8 -*-
2016-08-28 16:50:37 +08:00
from mylibs.constant import *
#ecc: Error Correction Codewords
def encode(ver, ecl, data_codewords):
en = ecc_num_per_block[ver-1][lindex[ecl]]
ecc = []
for dc in data_codewords:
ecc.append(get_ecc(dc, en))
return ecc
2016-08-26 22:26:14 +08:00
2016-08-28 16:50:37 +08:00
def get_ecc(dc, ecc_num):
gp = GP_list[ecc_num]
remainder = dc
for i in range(len(dc)):
remainder = divide(remainder, *gp)
2016-08-27 11:27:09 +08:00
return remainder
def divide(MP, *GP):
2016-08-26 22:26:14 +08:00
po2 = get_power_of_2_list()
log = get_log_list(po2)
2016-08-27 11:27:09 +08:00
GP = list(GP)
for i in range(len(GP)):
GP[i] += log[MP[0]]
if GP[i] > 255:
GP[i] %= 255
GP[i] = po2[GP[i]]
2016-08-26 22:26:14 +08:00
2016-08-27 11:27:09 +08:00
return XOR(MP, GP)
2016-08-26 22:26:14 +08:00
2016-08-27 11:27:09 +08:00
def XOR(MP, GP):
2016-08-26 22:26:14 +08:00
a = len(MP) - len(GP)
if a < 0:
MP += [0] * (-a)
elif a > 0:
GP += [0] * a
2016-08-27 11:27:09 +08:00
remainder = []
2016-08-28 23:07:55 +08:00
for i in range(len(MP)):
2016-08-27 11:27:09 +08:00
remainder.append(MP[i]^GP[i])
2016-08-28 23:07:55 +08:00
remainder = [i for i in remainder if i]
2016-08-27 11:27:09 +08:00
return remainder
2016-08-26 22:26:14 +08:00
def get_power_of_2_list():
po2 = [1]
for i in range(255):
a = po2[i] * 2
if a > 255:
a ^= 285
po2.append(a)
return po2
def get_log_list(po2):
log = [None]*256
for i in range(255):
log[po2[i]] = i
2016-08-27 11:27:09 +08:00
return log