refactor image application into packet classes
This commit is contained in:
parent
3e7ba62d3a
commit
98cdaad09e
54
common.py
54
common.py
@ -1,14 +1,29 @@
|
||||
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""
|
||||
bytestr += self.uuid.bytes
|
||||
@ -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])
|
||||
|
@ -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()
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user