33from __future__ import annotations
44
55from functools import cached_property
6- from typing import List , Optional
6+ from typing import List , Optional , Tuple
77
88import numpy as np
9+ import numpy .typing as npt
910import reedsolo
1011
1112from .enums import AztecTableType , AztecType , ReadingDirection
@@ -26,7 +27,7 @@ class CodewordReader:
2627 """Read the data spiral, apply Reed-Solomon correction and decode code-words.
2728
2829 :param matrix: Square binary matrix (0/1) representing the Aztec symbol.
29- :type matrix: numpy.ndarray
30+ :type matrix: npt.NDArray[np.int_]
3031 :param layers: Number of data layers (excluding the bull's-eye).
3132 :type layers: int
3233 :param data_words: Expected count of data code-words (script does *not* include ECC words).
@@ -54,7 +55,7 @@ class CodewordReader:
5455
5556 def __init__ (
5657 self ,
57- matrix : np .ndarray ,
58+ matrix : npt . NDArray [ np .int_ ] ,
5859 layers : int ,
5960 data_words : int ,
6061 aztec_type : AztecType ,
@@ -93,15 +94,15 @@ def _is_reference(self, r: int, c: int) -> bool:
9394 centre = self .matrix .shape [0 ] // 2
9495 return bool ((r - centre ) % 16 == 0 or (c - centre ) % 16 == 0 )
9596
96- def _read_bits (self ) -> np .ndarray :
97- bitmap = []
97+ def _read_bits (self ) -> npt . NDArray [ np .int_ ] :
98+ bitmap : List [ int ] = []
9899 square_size = self .matrix .shape [0 ]
99100 reading_direction = ReadingDirection .BOTTOM
100- start_point = (
101+ start_point : Tuple [ int , int ] = (
101102 0 ,
102103 0 ,
103104 ) # We start reading from the top left corner to the bottom left corner
104- end_point = (
105+ end_point : Tuple [ int , int ] = (
105106 square_size
106107 - 1
107108 - 2 , # - 2 because the two last lines are readed in a different direction
@@ -120,15 +121,15 @@ def _read_bits(self) -> np.ndarray:
120121 not self ._is_reference (i , start_point [1 ])
121122 or self .aztec_type == AztecType .COMPACT
122123 ):
123- bitmap .append (
124+ bitmap .extend (
124125 self .matrix [i , start_point [1 ] : end_point [1 ] + 1 ]
125126 )
126127 elif reading_direction == ReadingDirection .RIGHT :
127128 if (
128129 not self ._is_reference (start_point [0 ], i )
129130 or self .aztec_type == AztecType .COMPACT
130131 ):
131- bitmap .append (
132+ bitmap .extend (
132133 self .matrix [start_point [0 ] : end_point [0 ] - 1 : - 1 , i ]
133134 )
134135 elif reading_direction == ReadingDirection .TOP :
@@ -138,7 +139,7 @@ def _read_bits(self) -> np.ndarray:
138139 )
139140 or self .aztec_type == AztecType .COMPACT
140141 ):
141- bitmap .append (
142+ bitmap .extend (
142143 self .matrix [
143144 start_point [0 ] - i + apply_to_borns ,
144145 start_point [1 ] : end_point [1 ] - 1 : - 1 ,
@@ -151,7 +152,7 @@ def _read_bits(self) -> np.ndarray:
151152 )
152153 or self .aztec_type == AztecType .COMPACT
153154 ):
154- bitmap .append (
155+ bitmap .extend (
155156 self .matrix [
156157 start_point [0 ] : end_point [0 ] + 1 ,
157158 start_point [1 ] - i + apply_to_borns ,
@@ -198,14 +199,14 @@ def _read_bits(self) -> np.ndarray:
198199 "no data modules extracted - check bull's-eye/layer count"
199200 )
200201
201- return np .concatenate (bitmap ). astype ( int )
202+ return np .array (bitmap , dtype = int )
202203
203204 @cached_property
204- def bitmap (self ) -> np .ndarray :
205+ def bitmap (self ) -> npt . NDArray [ np .int_ ] :
205206 """Raw bit-stream extracted from the symbol (before ECC correction)."""
206207 return self ._read_bits ()
207208
208- def _correct (self ) -> List [ int ]:
209+ def _correct (self ) -> npt . NDArray [ np . int_ ]:
209210 if self .layers <= 2 :
210211 cw_size = 6
211212 elif self .layers <= 8 :
@@ -248,15 +249,15 @@ def _correct(self) -> List[int]:
248249 except reedsolo .ReedSolomonError as exc :
249250 raise ReedSolomonError (str (exc )) from exc
250251
251- corrected_bits = []
252+ corrected_bits : List [ int ] = []
252253 for sym in full_codeword :
253254 for shift in range (cw_size - 1 , - 1 , - 1 ):
254255 corrected_bits .append ((sym >> shift ) & 1 )
255256
256- return corrected_bits
257+ return np . array ( corrected_bits , dtype = int )
257258
258259 @cached_property
259- def corrected_bits (self ) -> List [ int ]:
260+ def corrected_bits (self ) -> npt . NDArray [ np . int_ ]:
260261 """Bit-stream after Reed-Solomon decoding and bit-stuff removal."""
261262 return self ._correct ()
262263
@@ -269,7 +270,7 @@ def _bits_to_bytes(cls, bits: List[int]) -> bytes:
269270 return bytes (cls ._bits_to_int (bits [i : i + 8 ]) for i in range (0 , len (bits ), 8 ))
270271
271272 def _remove_stuff_bits (
272- self , bits : List [ int ], cw_size : int , data_words : int
273+ self , bits : npt . NDArray [ np . int_ ], cw_size : int , data_words : int
273274 ) -> List [int ]:
274275 """Remove the stuffing bits from the bit-stream.
275276
@@ -279,7 +280,7 @@ def _remove_stuff_bits(
279280 Remove also the padding bits at the beginning of the stream.
280281
281282 :param bits: The bit-stream to clean.
282- :type bits: List[int ]
283+ :type bits: npt.NDArray[np.int_ ]
283284 :param cw_size: The size of the code-words (6, 8, 10 or 12).
284285 :type cw_size: int
285286 :param data_words: The number of data code-words in the symbol.
@@ -288,7 +289,7 @@ def _remove_stuff_bits(
288289 :return: The cleaned bit-stream without the stuffed bits and the padding bits.
289290 :rtype: List[int]
290291 """
291- cleaned = []
292+ cleaned : List [ int ] = []
292293 i = 0
293294 words_seen = 0
294295 while words_seen < data_words and i < len (bits ):
@@ -324,7 +325,7 @@ def _decode(self) -> str:
324325 bits = self ._remove_stuff_bits (self .bitmap , codeword_size , self .data_words )
325326
326327 i = 0
327- chars = []
328+ chars : List [ str ] = []
328329 current_mode = AztecTableType .UPPER # Default mode is UPPER
329330 previous_mode = AztecTableType .UPPER
330331 single_shift = False
0 commit comments