@@ -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 :
0 commit comments