Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

perf(appshot): convert float16 HDR frames to sRGB with an exact lookup table

rubenluetke10-beep8355ffebeb5f
diff --git a/jarvis/platform/hdr_image.py b/jarvis/platform/hdr_image.py
index 9cb3d351de..612685bb6c 100644
--- a/jarvis/platform/hdr_image.py
+++ b/jarvis/platform/hdr_image.py
@@ -23,6 +23,7 @@ Pure numpy + zlib (+ Pillow for 8-bit); no platform code.
from __future__ import annotations
+import functools
import struct
import zlib
from typing import Any
@@ -104,7 +105,31 @@ def scrgb_to_srgb8(scrgb: Any, sdr_white_nits: float = SCRGB_NITS) -> Any:
"""scRGB -> 8-bit sRGB ``(h, w, 3)``, SDR white at full white, highlights clipped."""
import numpy as np # noqa: PLC0415
- rgb = np.asarray(scrgb)[..., :3].astype(np.float32)
+ source = np.asarray(scrgb)
+ if source.dtype == np.float16:
+ # Desktop Duplication hands over float16: one table lookup per value
+ # gives the exact result of the float path ~10x faster (a 4K frame in
+ # well under 100 ms, fast enough to freeze the screen for the picker).
+ table = _srgb8_table(round(float(sdr_white_nits), 3))
+ return table[source[..., :3].view(np.uint16)]
+ return _scrgb_to_srgb8_float(source, sdr_white_nits)
+
+
[email protected]_cache(maxsize=4)
+def _srgb8_table(sdr_white_nits: float) -> Any:
+ """``uint8[65536]``: every float16 bit pattern -> its 8-bit sRGB value."""
+ import numpy as np # noqa: PLC0415
+
+ values = np.arange(65536, dtype=np.uint32).astype(np.uint16).view(np.float16)
+ values = np.nan_to_num(values.astype(np.float32), nan=0.0, posinf=1e4, neginf=0.0)
+ return _scrgb_to_srgb8_float(values, sdr_white_nits, channels=False)
+
+
+def _scrgb_to_srgb8_float(scrgb: Any, sdr_white_nits: float, *, channels: bool = True) -> Any:
+ import numpy as np # noqa: PLC0415
+
+ rgb = np.asarray(scrgb)
+ rgb = (rgb[..., :3] if channels else rgb).astype(np.float32)
rgb *= SCRGB_NITS / max(1.0, float(sdr_white_nits))
np.clip(rgb, 0.0, 1.0, out=rgb)
low = rgb * 12.92
diff --git a/tests/unit/platform/test_hdr_image.py b/tests/unit/platform/test_hdr_image.py
index dbe1923db5..8c221a351c 100644
--- a/tests/unit/platform/test_hdr_image.py
+++ b/tests/unit/platform/test_hdr_image.py
@@ -60,6 +60,17 @@ def test_sdr_white_becomes_srgb_white_and_highlights_clip() -> None:
assert srgb[0, 1].tolist() == [255, 0, 0]
+def test_float16_lookup_matches_the_float_path_exactly() -> None:
+ rng = np.random.default_rng(3)
+ scrgb = (rng.random((64, 64, 4)) * 8.0 - 0.5).astype(np.float16)
+ scrgb[0, 0, :3] = (np.nan, np.inf, -np.inf)
+ fast = hdr_image.scrgb_to_srgb8(scrgb, 284.0)
+ exact = hdr_image.scrgb_to_srgb8(scrgb.astype(np.float32), 284.0)
+ assert fast.dtype == np.uint8 and fast.shape == (64, 64, 3)
+ assert np.array_equal(fast[1:], exact[1:])
+ assert fast[0, 0].tolist() == [0, 255, 0]
+
+
def test_srgb_mid_grey_survives_the_trip_through_scrgb() -> None:
grey = np.full((1, 1, 3), 128, np.uint8)
scrgb = hdr_image.srgb8_to_scrgb(grey, 200.0)