47 lines
1.4 KiB
Python
47 lines
1.4 KiB
Python
import numpy as np
|
|
|
|
|
|
class StdPacket:
|
|
def __init__(self, x: int, y: int, array: np.ndarray):
|
|
self.x = x
|
|
self.y = y
|
|
self.array = array
|
|
|
|
def to_bytestr(self) -> bytes:
|
|
bytestr = b""
|
|
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:
|
|
x = int.from_bytes(b[0:4], signed = False)
|
|
y = int.from_bytes(b[4:8], signed = False)
|
|
array = np.frombuffer(b[8:], np.uint8).reshape(16, 16, 3)
|
|
|
|
return StdPacket(x, y, array)
|
|
|
|
|
|
class InterlacedPacket:
|
|
def __init__(self, x: int, y: int, even: bool, array: np.ndarray):
|
|
self.x = x
|
|
self.y = y
|
|
self.even = even
|
|
self.array = array
|
|
|
|
def to_bytestr(self) -> bytes:
|
|
bytestr = b""
|
|
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:
|
|
x = int.from_bytes(b[0:4], signed=False)
|
|
y = int.from_bytes(b[4:8], signed=False)
|
|
even = bool.from_bytes(b[8:12])
|
|
array = np.frombuffer(b[12:], np.uint8).reshape(16, 16, 3)
|
|
|
|
return InterlacedPacket(x, y, even, array) |