Commit 75259556 authored by Nathan/Eilisha Shiraini's avatar Nathan/Eilisha Shiraini
Browse files

Stuffing the building functions into another module

parent a976b266
"""
Aztec code generation
This package contains various elements to build Aztec codes.
In this case, "building" a code means generating a binary matrix
that contains the value of each module. It does not include
the image file generation.
"""
from . import encoder
from . import reedsolomon
from . import sizes
from . import positions
import numpy as np
def prepareData(data, forceLayers = None, forceCompact = None):
encoded = encoder.Encoder().encode(data, asBits = True)
if isinstance(forceLayers, int):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[forceLayers]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
layers = forceLayers
compact = bool(forceCompact) if forceCompact is not None else (forceLayers < 5 and len(stuffed) < sizes.COMPACT_WORD_COUNT[forceLayers])
if (compact and len(stuffed) > sizes.COMPACT_WORD_COUNT[forceLayers]) or ((not compact) and len(stuffed) > sizes.FULL_WORD_COUNT[forceLayers]):
raise ValueError("Data too large")
else:
for i in range(1, 33):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[i]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
if forceCompact is not None:
if forceCompact and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
if i < 5 and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
raise ValueError("Data too large")
wordcount = (len(stuffed), (sizes.COMPACT_WORD_COUNT[layers] if compact else sizes.FULL_WORD_COUNT[layers]) - len(stuffed))
rscode = sizes.GALLOIS_FIELD_FOR_LAYERS[layers].gen_rs_code(stuffed, wordcount[1])
result = list(reedsolomon.fromwords(stuffed + rscode, wordsize))
return result, layers, compact, wordcount
def prepareDataMatrix(data, layers, compact, wordcount):
size = positions.matrixSize(layers, compact)
result = np.zeros((size, size), dtype=np.bool8)
if compact:
poslist = positions.COMPACT_POS_LIST[layers -1]
else:
poslist = positions.FULL_POS_LIST[layers -1]
for i, bit in enumerate(data):
result[poslist[i]] = bit
return result, layers, compact, wordcount
"""
Aztec code builder
This module contains the class and functions
that run the full algorithm to build an Aztec code.
They are the one that use the other functions
provided in the main :py:mod:`aztec` package.
"""
import numpy as np
from . import encoder
from . import reedsolomon
from . import sizes
from . import positions
class AztecBuilder(object):
@staticmethod
def prepareData(data, forceLayers = None, forceCompact = None):
encoded = encoder.Encoder().encode(data, asBits = True)
if isinstance(forceLayers, int):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[forceLayers]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
layers = forceLayers
compact = bool(forceCompact) if forceCompact is not None else (forceLayers < 5 and len(stuffed) < sizes.COMPACT_WORD_COUNT[forceLayers])
if (compact and len(stuffed) > sizes.COMPACT_WORD_COUNT[forceLayers]) or ((not compact) and len(stuffed) > sizes.FULL_WORD_COUNT[forceLayers]):
raise ValueError("Data too large")
else:
for i in range(1, 33):
wordsize = sizes.WORD_SIZE_FOR_LAYERS[i]
stuffed = list(reedsolomon.towords(reedsolomon.stuff(encoded, wordsize), wordsize))
if forceCompact is not None:
if forceCompact and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
if i < 5 and len(stuffed) <= sizes.COMPACT_WORD_COUNT[i]:
compact = True
layers = i
break
elif len(stuffed) <= sizes.FULL_WORD_COUNT[i]:
compact = False
layers = i
break
else:
raise ValueError("Data too large")
wordcount = (len(stuffed), (sizes.COMPACT_WORD_COUNT[layers] if compact else sizes.FULL_WORD_COUNT[layers]) - len(stuffed))
rscode = sizes.GALLOIS_FIELD_FOR_LAYERS[layers].gen_rs_code(stuffed, wordcount[1])
result = list(reedsolomon.fromwords(stuffed + rscode, wordsize))
return result, layers, compact, wordcount
@staticmethod
def prepareDataMatrix(data, layers, compact, wordcount):
size = positions.matrixSize(layers, compact)
result = np.zeros((size, size), dtype=np.bool8)
if compact:
poslist = positions.COMPACT_POS_LIST[layers -1]
else:
poslist = positions.FULL_POS_LIST[layers -1]
for i, bit in enumerate(data):
result[poslist[i]] = bit
return result, layers, compact, wordcount
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment