|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | +from Decoder.Decoder import Decoder |
| 4 | + |
| 5 | +__author__ = 'N.A. Supun Kavinda' |
| 6 | + |
| 7 | + |
| 8 | +class DecoderVersion(ABC): |
| 9 | + def __init__(self, unmask_matrix): |
| 10 | + self.__unmask_matrix = unmask_matrix |
| 11 | + |
| 12 | + def decoder(self): |
| 13 | + __encode_mode = self.__get_encode_mode() |
| 14 | + __binary_array = self.__binary_data_array() |
| 15 | + if __encode_mode == '0100': |
| 16 | + return Decoder.decode_bytes_array(__binary_array) |
| 17 | + else: |
| 18 | + return NotImplementedError(__encode_mode, ' This encode mode is not implemented') |
| 19 | + |
| 20 | + def __get_encode_mode(self): |
| 21 | + rows = len(self.__unmask_matrix) |
| 22 | + cols = len(self.__unmask_matrix[0]) |
| 23 | + return str(self.__unmask_matrix[rows - 1][cols - 1]) + str(self.__unmask_matrix[rows - 1][cols - 2]) + str( |
| 24 | + self.__unmask_matrix[rows - 2][cols - 1]) + str(self.__unmask_matrix[rows - 2][cols - 2]) |
| 25 | + |
| 26 | + def __binary_data_array(self): |
| 27 | + """ |
| 28 | + This method return total binary values included in word. |
| 29 | + :return: array |
| 30 | + """ |
| 31 | + self.data_array = [] |
| 32 | + __is_down_to_up = True |
| 33 | + j = len(self.__unmask_matrix[0]) - 1 # cols |
| 34 | + while j > 0: |
| 35 | + if __is_down_to_up: |
| 36 | + for i in range(len(self.__unmask_matrix) - 1, -1, -1): # rows |
| 37 | + self.__fill_array(i, j) |
| 38 | + __is_down_to_up = False |
| 39 | + else: |
| 40 | + for i in range(len(self.__unmask_matrix)): |
| 41 | + self.__fill_array(i, j) |
| 42 | + __is_down_to_up = True |
| 43 | + if j is 8: |
| 44 | + j = j - 3 |
| 45 | + else: |
| 46 | + j = j - 2 |
| 47 | + return self.data_array |
| 48 | + |
| 49 | + def __fill_array(self, i, j): |
| 50 | + """ |
| 51 | + Add binary value to the array. If binary value is part of QRCode included word. |
| 52 | + :param i: |
| 53 | + :param j: |
| 54 | + """ |
| 55 | + if not self.is_skip_point(i, j): |
| 56 | + self.data_array.append(str(self.__unmask_matrix[i][j])) |
| 57 | + if not self.is_skip_point(i, j - 1): |
| 58 | + self.data_array.append(str(self.__unmask_matrix[i][j - 1])) |
| 59 | + |
| 60 | + @abstractmethod |
| 61 | + def is_skip_point(self, i, j): |
| 62 | + """ |
| 63 | + This method return the true when that specific point should skip. otherwise return false |
| 64 | + :type i: int |
| 65 | + :type j: int |
| 66 | + """ |
| 67 | + pass |
0 commit comments