Skip to content

Commit 3c41562

Browse files
committed
docs: Adding doc and commentaries for some funcs
1 parent 403e128 commit 3c41562

3 files changed

Lines changed: 86 additions & 7 deletions

File tree

aztec_tool/codewords.py

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,38 @@ def __init__(
9090
self.auto_correct = auto_correct
9191

9292
def _is_reference(self, r: int, c: int) -> bool:
93+
"""Check if the given coordinates are part of the reference grid.
94+
A coordinate is part of the reference grid if it is a multiple of 16
95+
from the center of the matrix. The center is defined as the middle of the
96+
matrix (i.e. the middle row and column).
97+
98+
Parameters
99+
----------
100+
r : int
101+
Row index of the coordinate.
102+
c : int
103+
Column index of the coordinate.
104+
105+
Returns
106+
-------
107+
bool
108+
True if the coordinate is part of the reference grid, False otherwise.
109+
"""
93110
centre = self.matrix.shape[0] // 2
94111
return (r - centre) % 16 == 0 or (c - centre) % 16 == 0
95112

96113
def _read_bits(self) -> np.ndarray:
97114
bitmap = []
98115
square_size = self.matrix.shape[0]
99116
reading_direction = ReadingDirection.BOTTOM
100-
start_point = (0, 0)
117+
start_point = (0, 0) # We start reading from the top left corner to the bottom left corner
101118
end_point = (
102-
square_size - 1 - 2,
119+
square_size - 1 - 2, # - 2 because the two last lines are readed in a different direction
103120
1,
104-
) # - 2 because the two last lines are readed in a different direction
121+
)
122+
# The number of cells to skip when we are reading layers in the middle of the matrix
123+
# So, if we are reading the first layer, we skip 0 cells, if we are reading the second layer, we skip 2 cells, etc.
124+
# It's used to adjust the starting point and the ending point of the reading
105125
apply_to_borns = 0
106126

107127
for _ in range(1, self.layers * 4 + 1):
@@ -170,10 +190,13 @@ def _read_bits(self) -> np.ndarray:
170190
end_point = (end_point[0] + 1, end_point[1] - square_size + 1 + 2)
171191
reading_direction = ReadingDirection.LEFT
172192
elif reading_direction == ReadingDirection.LEFT:
193+
# When we are going to the next layer, the square size is reduced by 4
194+
# because we are skipping the two last lines and the two first lines of the previous layer
173195
square_size -= 4
174196
apply_to_borns += 2
175197
start_point = end_point
176198
start_point = (start_point[0] + 1, start_point[1])
199+
# If the start point is a reference, we can skip this "one cell layer"
177200
if self._is_reference(start_point[0], start_point[1]):
178201
start_point = (start_point[0] + 1, start_point[1] + 1)
179202
square_size -= 2
@@ -204,7 +227,7 @@ def _correct(self) -> List[int]:
204227
cw_size = 12
205228

206229
start_padding = len(self.bitmap) % cw_size
207-
bits = self.bitmap[start_padding:]
230+
bits = self.bitmap[start_padding:] # We need to have a multiple of cw_size, so we remove the padding bits
208231

209232
prim = self.PRIM_POLY[cw_size]
210233
nsize = (1 << cw_size) - 1
@@ -215,6 +238,7 @@ def _correct(self) -> List[int]:
215238
"data_words exceeds total code-words in the symbol"
216239
)
217240

241+
# We split the bits into code-words of size cw_size
218242
symbols = [
219243
int("".join(str(b) for b in bits[i * cw_size : (i + 1) * cw_size]), 2)
220244
for i in range(total_words)
@@ -255,6 +279,26 @@ def _bits_to_bytes(cls, bits: List[int]) -> bytes:
255279
def _remove_stuff_bits(
256280
self, bits: List[int], cw_size: int, data_words: int
257281
) -> List[int]:
282+
"""Remove the stuffing bits from the bit-stream.
283+
A stuffed bit is a bit that is added to the bit-stream when there are cw_size-1
284+
consecutive bits of the same value. The stuffed bit is the opposite of the last bit.
285+
We need to remove the stuffed bits from the bit-stream before decoding it.
286+
Remove also the padding bits at the beginning of the stream.
287+
288+
Parameters
289+
----------
290+
bits : List[int]
291+
The bit-stream to clean.
292+
cw_size : int
293+
The size of the code-words (6, 8, 10 or 12).
294+
data_words : int
295+
The number of data code-words in the symbol.
296+
297+
Returns
298+
-------
299+
List[int]
300+
The cleaned bit-stream without the stuffed bits and the padding bits.
301+
"""
258302
cleaned = []
259303
i = 0
260304
words_seen = 0
@@ -269,6 +313,7 @@ def _remove_stuff_bits(
269313
cleaned.extend(run)
270314
i += cw_size
271315
words_seen += 1
316+
# If the total number of bits is not a multiple of cw_size, we need to remove the padding bits
272317
start_padding = len(bits) % cw_size
273318
return cleaned[start_padding : data_words * cw_size]
274319

@@ -291,7 +336,7 @@ def _decode(self) -> str:
291336

292337
i = 0
293338
chars = []
294-
current_mode = AztecTableType.UPPER
339+
current_mode = AztecTableType.UPPER # Default mode is UPPER
295340
previous_mode = AztecTableType.UPPER
296341
single_shift = False
297342
single_consumed = 0
@@ -308,10 +353,10 @@ def _decode(self) -> str:
308353

309354
if current_mode == AztecTableType.DIGIT:
310355
symbol_bits = bits[i : i + 4]
311-
i += 4
356+
i += 4 # DIGIT mode uses 4 bits for each symbol
312357
else:
313358
symbol_bits = bits[i : i + 5]
314-
i += 5
359+
i += 5 # UPPER, LOWER, MIXED and PUNCT modes use 5 bits for each symbol
315360

316361
val = self._bits_to_int(symbol_bits)
317362
try:

aztec_tool/mode.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,14 @@ def __init__(
8282
self.auto_correct = auto_correct
8383

8484
def _read_mode_bits(self) -> List[int]:
85+
"""Read the mode message bits in clockwise order.
86+
The order is: top row, right column, bottom row, left column.
87+
88+
Returns
89+
-------
90+
List[int]
91+
The mode message bits in clockwise order.
92+
"""
8593
bits = []
8694
try:
8795
tl_y, tl_x, br_y, br_x = self.bounds
@@ -151,6 +159,7 @@ def _correct(self) -> List[int]:
151159
rs = reedsolo.RSCodec(nsym=nsym, nsize=15, fcr=1, generator=2, c_exp=4)
152160
if len(self.mode_bitmap) % 4 != 0:
153161
raise ModeFieldError("mode bitmap length not multiple of 4")
162+
# We split the mode bitmap into 4-bit symbols
154163
symbols = [
155164
int("".join(map(str, self.mode_bitmap[i : i + 4])), 2)
156165
for i in range(0, len(self.mode_bitmap), 4)
@@ -173,6 +182,22 @@ def mode_corrected_bits(self) -> List[int]:
173182
return self._correct()
174183

175184
def _extract_fields(self) -> Dict[str, Union[int, List[int]]]:
185+
"""Extract the mode message fields (layers, data words, ecc bits).
186+
For compact:
187+
- layers: 2 bits (max 4 layers)
188+
- data words: 6 bits (max 64 data words)
189+
- ecc bits: 20 bits
190+
For full:
191+
- layers: 5 bits (max 32 layers)
192+
- data words: 11 bits (max 2048 data words)
193+
- ecc bits: 24 bits
194+
195+
Returns
196+
-------
197+
Dict[str, Union[int, List[int]]]
198+
Dictionary with keys ``layers``, ``data_words``, and ``ecc_bits``.
199+
The values are the corresponding integers or lists of bits.
200+
"""
176201
if self.auto_correct:
177202
bits = self.mode_corrected_bits
178203
else:

aztec_tool/orientation.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ def __init__(self, matrix: np.ndarray, bounds: Tuple[int, int, int, int]) -> Non
5656
self.bounds = bounds
5757

5858
def _read_patterns(self) -> List[List[int]]:
59+
"""Read the four orientation patterns from the matrix.
60+
Orientation patterns are 3x3 matrices located at the corners of
61+
the bull's-eye.
62+
63+
Returns
64+
-------
65+
List[List[int]]
66+
The four orientation patterns read TL→TR→BR→BL.
67+
"""
5968
tl_y, tl_x, br_y, br_x = self.bounds
6069
tr_y, tr_x, bl_y, bl_x = tl_y, br_x, br_y, tl_x
6170

0 commit comments

Comments
 (0)