116 lines
3.6 KiB
Python

from abc import ABC, abstractmethod
import numpy as np
from uuid import UUID
class Packet(ABC):
size: int
def __init__(self, uuid: UUID, x: int, y: int, array: np.ndarray):
self.uuid = uuid
self.x = x
self.y = y
self.array = array
@abstractmethod
def to_bytestr(self) -> bytes:
pass
@abstractmethod
def apply(self, image: np.ndarray) -> np.ndarray:
pass
class StdPacket(Packet):
size = 16 + 4 + 4 + 768
def __init__(self, uuid: UUID, x: int, y: int, array: np.ndarray):
super().__init__(uuid, x, y, 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 apply(self, image: np.ndarray) -> np.ndarray:
x = self.x
y = self.y
arr = self.array
image[y:y + 16, x:x + 16] = arr
return image
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(Packet):
size = 16 + 4 + 4 + 4 + 768
def __init__(self, uuid: UUID, x: int, y: int, even: bool, array: np.ndarray):
super().__init__(uuid, x, y, array)
self.even = even
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 apply(self, image: np.ndarray) -> np.ndarray:
x = self.x
y = self.y
arr = self.array
image[y + self.even:y + 32:2, x:x + 16] = arr
return image
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(Packet):
size = 16 + 4 + 4 + 4 + 768
def __init__(self, uuid: UUID, x: int, y: int, even_x: bool, even_y: bool, array: np.ndarray):
super().__init__(uuid, x, y, array)
self.even_x = even_x
self.even_y = even_y
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 apply(self, image: np.ndarray) -> np.ndarray:
x = self.x
y = self.y
arr = self.array
image[y + self.even_y:y + 32:2, x + self.even_x:x + 32:2] = arr
return image
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)