2025-01-31 20:38:27 -06:00

55 lines
1.7 KiB
Python

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)