import numpy as np from uuid import UUID class StdPacket: size = 16 + 4 + 4 + 768 def __init__(self, uuid: UUID, x: int, y: int, array: np.ndarray): self.uuid = uuid self.x = x self.y = y self.array = array def to_bytestr(self) -> bytes: bytestr = b"" bytestr += self.uuid.bytes bytestr += self.x.to_bytes(length=4, signed = False) bytestr += self.y.to_bytes(length=4, signed = False) bytestr += self.array.tobytes() return bytestr def from_bytes_std(b: bytes) -> StdPacket: uuid = UUID(bytes = b[0:16]) x = int.from_bytes(b[16:20], signed = False) y = int.from_bytes(b[20:24], signed = False) array = np.frombuffer(b[24:], np.uint8).reshape(16, 16, 3) return StdPacket(uuid, x, y, array) class InterlacedPacket: size = 16 + 4 + 4 + 4 + 768 def __init__(self, uuid: UUID, x: int, y: int, even: bool, array: np.ndarray): self.uuid = uuid self.x = x self.y = y self.even = even self.array = array def to_bytestr(self) -> bytes: bytestr = b"" bytestr += self.uuid.bytes bytestr += self.x.to_bytes(length=4, signed=False) bytestr += self.y.to_bytes(length=4, signed=False) bytestr += self.even.to_bytes(length=4) bytestr += self.array.tobytes() return bytestr def from_bytes_int(b: bytes) -> InterlacedPacket: uuid = UUID(bytes=b[0:16]) x = int.from_bytes(b[16:20], signed=False) y = int.from_bytes(b[20:24], signed=False) even = bool.from_bytes(b[24:28]) array = np.frombuffer(b[28:], np.uint8).reshape(16, 16, 3) return InterlacedPacket(uuid, x, y, even, array) class DoublyInterlacedPacket: size = 16 + 4 + 4 + 4 + 768 def __init__(self, uuid: UUID, x: int, y: int, even_x: bool, even_y: bool, array: np.ndarray): self.uuid = uuid self.x = x self.y = y self.even_x = even_x self.even_y = even_y self.array = array def to_bytestr(self) -> bytes: bytestr = b"" bytestr += self.uuid.bytes bytestr += self.x.to_bytes(length=4, signed=False) bytestr += self.y.to_bytes(length=4, signed=False) bytestr += self.even_x.to_bytes(length=2) bytestr += self.even_y.to_bytes(length=2) bytestr += self.array.tobytes() return bytestr def from_bytes_dint(b: bytes) -> DoublyInterlacedPacket: uuid = UUID(bytes=b[0:16]) x = int.from_bytes(b[16:20], signed=False) y = int.from_bytes(b[20:24], signed=False) even_x = bool.from_bytes(b[24:26]) even_y = bool.from_bytes(b[26:28]) array = np.frombuffer(b[28:], np.uint8).reshape(16, 16, 3) return DoublyInterlacedPacket(uuid, x, y, even_x, even_y, array)