Opt: [ALAS] Faster DroidCast_raw image parsing

This commit is contained in:
LmeSzinc 2024-05-01 23:59:09 +08:00
parent 2ae0f5e4b9
commit 22dc2cfc1c

View File

@ -228,11 +228,21 @@ class DroidCast(Uiautomator2):
if self.droidcast_height and self.droidcast_width:
shape = (self.droidcast_height, self.droidcast_width)
rotate = self.is_mumu_over_version_356 and self.orientation == 1
image = self.droidcast_session.get(self.droidcast_raw_url(), timeout=3).content
# DroidCast_raw returns a RGB565 bitmap
try:
arr = np.frombuffer(image, dtype=np.uint16).reshape(shape)
arr = np.frombuffer(image, dtype=np.uint16)
if rotate:
arr = arr.reshape(shape)
# arr = cv2.rotate(arr, cv2.ROTATE_90_CLOCKWISE)
# A little bit faster?
arr = cv2.transpose(arr)
cv2.flip(arr, 1, dst=arr)
else:
arr = arr.reshape(shape)
except ValueError as e:
if len(image) < 500:
logger.warning(f'Unexpected screenshot: {image}')
@ -260,31 +270,26 @@ class DroidCast(Uiautomator2):
# b = b.astype(np.uint8)
# image = cv2.merge([r, g, b])
# The same as the code above but costs about 5ms instead of 10ms.
# The same as the code above but costs about 3~4ms instead of 10ms.
# Note that cv2.convertScaleAbs is 5x fast as cv2.multiply, cv2.add is 8x fast as cv2.convertScaleAbs
# Note that cv2.convertScaleAbs includes rounding
r = cv2.bitwise_and(arr, 0b1111100000000000)
cv2.multiply(r, 0.00390625, dst=r)
r = np.uint8(r)
m = cv2.multiply(r, 0.03125)
r = cv2.convertScaleAbs(r, alpha=0.00390625)
m = cv2.convertScaleAbs(r, alpha=0.03125)
cv2.add(r, m, dst=r)
g = cv2.bitwise_and(arr, 0b0000011111100000)
cv2.multiply(g, 0.125, dst=g)
g = np.uint8(g)
m = cv2.multiply(g, 0.015625)
g = cv2.convertScaleAbs(g, alpha=0.125)
m = cv2.convertScaleAbs(g, alpha=0.015625, dst=m)
cv2.add(g, m, dst=g)
b = cv2.bitwise_and(arr, 0b0000000000011111)
cv2.multiply(b, 8, dst=b)
b = np.uint8(b)
m = cv2.multiply(b, 0.03125)
b = cv2.convertScaleAbs(b, alpha=8)
m = cv2.convertScaleAbs(b, alpha=0.03125, dst=m)
cv2.add(b, m, dst=b)
image = cv2.merge([r, g, b])
if self.is_mumu_over_version_356:
if self.orientation == 1:
image = cv2.rotate(image, cv2.ROTATE_90_CLOCKWISE)
return image
def droidcast_wait_startup(self):