From 98cdaad09e5d621a6edb8dec2f5aafc2302cdf83 Mon Sep 17 00:00:00 2001 From: Dustin Thomas Date: Fri, 31 Jan 2025 22:05:57 -0600 Subject: [PATCH] refactor image application into packet classes --- common.py | 54 ++++++++++++++++++++++++++++++++++++++++++------------ server.py | 5 +---- 2 files changed, 43 insertions(+), 16 deletions(-) diff --git a/common.py b/common.py index b64a0eb..d09b1b9 100644 --- a/common.py +++ b/common.py @@ -1,13 +1,28 @@ +from abc import ABC, abstractmethod + import numpy as np from uuid import UUID -class StdPacket: - size = 16 + 4 + 4 + 768 +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"" @@ -17,6 +32,13 @@ class StdPacket: 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) @@ -26,14 +48,11 @@ def from_bytes_std(b: bytes) -> StdPacket: return StdPacket(uuid, x, y, array) -class InterlacedPacket: +class InterlacedPacket(Packet): 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 + super().__init__(uuid, x, y, array) self.even = even - self.array = array def to_bytestr(self) -> bytes: bytestr = b"" @@ -44,6 +63,13 @@ class InterlacedPacket: 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]) @@ -54,15 +80,12 @@ def from_bytes_int(b: bytes) -> InterlacedPacket: return InterlacedPacket(uuid, x, y, even, array) -class DoublyInterlacedPacket: +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): - self.uuid = uuid - self.x = x - self.y = y + super().__init__(uuid, x, y, array) self.even_x = even_x self.even_y = even_y - self.array = array def to_bytestr(self) -> bytes: bytestr = b"" @@ -74,6 +97,13 @@ class DoublyInterlacedPacket: 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]) diff --git a/server.py b/server.py index 43aa8d5..961cc96 100644 --- a/server.py +++ b/server.py @@ -21,10 +21,7 @@ class Client: self.frame = np.ndarray((HEIGHT, WIDTH, 3), dtype=np.uint8) def update(self, pkt: DoublyInterlacedPacket): - x = pkt.x - y = pkt.y - arr = pkt.array - self.frame[y + pkt.even_y:y + 32:2, x + pkt.even_x:x + 32:2] = arr + self.frame = pkt.apply(self.frame) self.last_updated = datetime.now()