# -*- coding: utf-8 -*-
"""
Created on Mon Aug 12 10:02:39 2024

@author: Fajia Sun
"""

import pandas as pd
import numpy as np
from bch_codes import bchencode
from sparse import sparse_seq
import pickle

#Step 1: transform AVIF file to binary sequence (omitted)
with open('panda.avif', 'rb') as f:
    pass

with open('binary_info.txt', 'r') as f:
    info = f.read()
    
#Step 2: grouping
n_of_group = int(np.ceil(len(info) / 91))
grouped_info = []
for i in range(n_of_group):
    try:
        grouped_info.append(info[91*i:91*(i+1)])
    except:
        grouped_info.append(info[91*i:])
        
last_group = grouped_info[-1]
while len(last_group) < 91:
    last_group += '0'
grouped_info[-1] = last_group

#Step 3: BCH encoding
encoded = []
for i in range(len(grouped_info)):
    encoded.append(bchencode(grouped_info[i]))

#Step 4: sparse
seed = []
sparsed = []
for i in range(len(encoded)):
    s, seq = sparse_seq(encoded[i])
    seed.append(s)
    sparsed.append(seq)
    print(i, end='-')
    
#Step 5: save files
with open('seed.pkl', 'wb') as f:
    pickle.dump(seed, f)
    
with open('encoded_info.txt', 'w') as f2:
    for line in sparsed:
        f2.write(line)
        f2.write('\n')



