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.

fix(appshot): place SDR white at PQ reference white in HDR masters and recordings

rubenluetke10-beepf3f2dc5ba5c3
diff --git a/docs/os-parity.md b/docs/os-parity.md
index f45744047a..6a3f545928 100644
--- a/docs/os-parity.md
+++ b/docs/os-parity.md
@@ -35,6 +35,10 @@ desktop holds more than 8-bit sRGB (`jarvis/platform/display_color.py`).
plus a 16-bit BT.2020 PQ PNG with `cICP`/`cLLi`
(`jarvis/appshot/master.py`, `jarvis/platform/hdr_image.py`); Save writes
both (`appshot-….png`, `appshot-…-hdr.png`), the library keeps both.
+ - Both PQ outputs place the monitor's SDR white at PQ reference white
+ (203 nits, ITU-R BT.2408), the level HDR viewers and players show at
+ their screen's SDR white; highlights keep their ratio to it. Encoding
+ the absolute SDR white (e.g. 284 nits) made white pages glare.
- A rotated monitor, a rectangle across two monitors, or any duplication
failure falls back to the 8-bit path; a still desktop gets one repaint
nudge before that fallback.
diff --git a/jarvis/appshot/master.py b/jarvis/appshot/master.py
index e41ae1825b..3c57294a09 100644
--- a/jarvis/appshot/master.py
+++ b/jarvis/appshot/master.py
@@ -10,7 +10,8 @@ way — and is encoded here once the markings are known:
the clipboard, chat and every SDR viewer get.
* ``hdr_png`` — only from an HDR or wide-gamut monitor: 16-bit BT.2020 PQ
with ``cICP``/``cLLi`` (:mod:`jarvis.platform.hdr_image`), which keeps the
- highlights and the gamut the monitor showed.
+ highlights and the gamut the monitor showed. SDR white sits at PQ reference
+ white, so HDR viewers show white pages no brighter than the screen did.
"""
from __future__ import annotations
@@ -46,8 +47,10 @@ def encode_master(master: Any, markup: Any = None) -> MasterFiles:
scrgb = apply_to_scrgb(scrgb, markup, master.sdr_white_nits)
sdr = hdr_image.scrgb_to_srgb8(scrgb, master.sdr_white_nits)
+ white = master.sdr_white_nits
hdr_png = hdr_image.encode_png16(
- hdr_image.scrgb_to_pq16(scrgb), light=hdr_image.light_levels(scrgb)
+ hdr_image.scrgb_to_pq16(scrgb, white),
+ light=hdr_image.light_levels(scrgb, white),
)
height, width = sdr.shape[:2]
return MasterFiles(hdr_image.encode_png8(sdr), width, height, hdr_png)
diff --git a/jarvis/platform/hdr_image.py b/jarvis/platform/hdr_image.py
index 22649906bb..9cb3d351de 100644
--- a/jarvis/platform/hdr_image.py
+++ b/jarvis/platform/hdr_image.py
@@ -6,8 +6,11 @@ sRGB. Two things are made from it:
* the **master**, lossless: a 16-bit PNG in BT.2020 primaries with the PQ
transfer (HDR10's signal), tagged with a ``cICP`` chunk (PNG, third
- edition) and a ``cLLi`` chunk with the content's light levels. Viewers that
- read ``cICP`` show it exactly as the monitor did.
+ edition) and a ``cLLi`` chunk with the content's light levels. SDR white
+ is placed at PQ's reference white, 203 nits (ITU-R BT.2408), because that
+ is the level every PQ viewer shows at the viewing screen's own SDR white;
+ highlights keep their ratio to it. Writing the monitor's absolute SDR white
+ instead (e.g. 284 nits) makes white pages glare 1.4x above SDR white.
* the **SDR stand-in**, 8-bit sRGB, for everything that cannot take HDR — the
model's image, the clipboard, chat. SDR white maps to sRGB white, so normal
UI looks the same as on screen; only highlights above SDR white clip.
@@ -34,6 +37,8 @@ _PQ_M1, _PQ_M2 = 0.1593017578125, 78.84375
_PQ_C1, _PQ_C2, _PQ_C3 = 0.8359375, 18.8515625, 18.6875
#: scRGB 1.0 in nits.
SCRGB_NITS = 80.0
+#: Where SDR white sits in a PQ signal (ITU-R BT.2408 "graphics white").
+PQ_REFERENCE_WHITE_NITS = 203.0
#: cICP: BT.2020 primaries, PQ transfer, RGB (identity matrix), full range.
CICP_BT2100_PQ = (9, 16, 0, 1)
@@ -66,17 +71,31 @@ def scrgb_to_bt2020_nits(scrgb: Any) -> Any:
return out
-def scrgb_to_pq16(scrgb: Any) -> Any:
- """scRGB -> BT.2020 PQ, ``uint16 (h, w, 3)`` full range — the PNG master's pixels."""
+def _reference_scale(sdr_white_nits: float) -> float:
+ """Factor that moves the capture's SDR white onto PQ reference white."""
+ return PQ_REFERENCE_WHITE_NITS / max(1.0, float(sdr_white_nits))
+
+
+def scrgb_to_pq16(scrgb: Any, sdr_white_nits: float = PQ_REFERENCE_WHITE_NITS) -> Any:
+ """scRGB -> BT.2020 PQ, ``uint16 (h, w, 3)`` full range — the PNG master's pixels.
+
+ ``sdr_white_nits`` is the monitor's SDR white when the capture was taken;
+ it lands at :data:`PQ_REFERENCE_WHITE_NITS`.
+ """
import numpy as np # noqa: PLC0415
- signal = pq_encode(scrgb_to_bt2020_nits(scrgb))
+ nits = scrgb_to_bt2020_nits(scrgb)
+ nits *= _reference_scale(sdr_white_nits)
+ signal = pq_encode(nits)
return np.rint(signal * 65535.0).astype(np.uint16)
-def light_levels(scrgb: Any) -> tuple[float, float]:
- """``(MaxCLL, MaxFALL)`` in nits: the brightest pixel and the average frame light."""
+def light_levels(
+ scrgb: Any, sdr_white_nits: float = PQ_REFERENCE_WHITE_NITS
+) -> tuple[float, float]:
+ """``(MaxCLL, MaxFALL)`` in nits of the PQ master (see :func:`scrgb_to_pq16`)."""
nits = scrgb_to_bt2020_nits(scrgb)
+ nits *= _reference_scale(sdr_white_nits)
peak = nits.max(axis=-1)
return float(peak.max(initial=0.0)), float(peak.mean()) if peak.size else 0.0
diff --git a/jarvis/platform/win_duplication.py b/jarvis/platform/win_duplication.py
index 403bd762fd..81db30dc2a 100644
--- a/jarvis/platform/win_duplication.py
+++ b/jarvis/platform/win_duplication.py
@@ -92,8 +92,11 @@ static const float3x3 To2020 = {
0.016391, 0.088013, 0.895595 };
static const float3 Luma = float3(0.2627, 0.6780, 0.0593);
+// SDR white (Scales.x) lands on PQ reference white, 203 nits (BT.2408):
+// players show that level at their screen's SDR white, so white UI does not
+// glare above it; highlights keep their ratio to SDR white.
float3 Pq(float3 l) {
- float3 y = saturate(max(l, 0.0) * (80.0 / 10000.0));
+ float3 y = saturate(max(l, 0.0) * (203.0 / 10000.0) / Scales.x);
float3 p = pow(y, 0.1593017578125);
return pow((0.8359375 + 18.8515625 * p) / (1.0 + 18.6875 * p), 78.84375);
}
@@ -632,7 +635,8 @@ class DesktopDuplication:
Returns ``(y, uv)``: ``uint16`` arrays of shape ``(h, w)`` and
``(h/2, w/2, 2)``, 10-bit codes in the high bits as P010 stores them.
- ``sdr_white`` is the scRGB value of SDR white, used for the pointer.
+ ``sdr_white`` is the scRGB value of SDR white: the pointer is drawn
+ at it, and it is encoded at PQ reference white (203 nits).
"""
import numpy as np # noqa: PLC0415
@@ -649,7 +653,7 @@ class DesktopDuplication:
float(self._pointer_pos[0]), float(self._pointer_pos[1]),
float(self._pointer_size[0]), float(self._pointer_size[1]),
]
- params.scales[0] = float(sdr_white)
+ params.scales[0] = max(float(sdr_white), 1e-3)
ctx = self._context
ctx.call(
_CTX_UPDATE_SUBRESOURCE, ctypes.c_void_p(planes["cbuffer"].ptr), ctypes.c_uint(0),
diff --git a/tests/unit/platform/test_hdr_image.py b/tests/unit/platform/test_hdr_image.py
index d4b4a1eaba..dbe1923db5 100644
--- a/tests/unit/platform/test_hdr_image.py
+++ b/tests/unit/platform/test_hdr_image.py
@@ -74,6 +74,21 @@ def test_wide_gamut_red_stays_inside_bt2020() -> None:
assert nits[0, 0, 0] > nits[0, 0, 1] > 0
+def test_sdr_white_lands_on_pq_reference_white() -> None:
+ # A white web page on a monitor with SDR white at 284 nits must reach
+ # viewers at PQ reference white, not 284 nits (which they show 1.4x
+ # brighter than their own SDR white).
+ white = 284.0 / hdr_image.SCRGB_NITS
+ scrgb = np.array([[[white, white, white, 1.0], [2 * white, 2 * white, 2 * white, 1.0]]])
+ pq = hdr_image.scrgb_to_pq16(scrgb, 284.0)
+ nits = hdr_image.pq_decode(pq / 65535.0)
+ assert nits[0, 0].tolist() == pytest.approx([203.0] * 3, rel=2e-3)
+ assert nits[0, 1].tolist() == pytest.approx([406.0] * 3, rel=2e-3)
+ max_cll, max_fall = hdr_image.light_levels(scrgb, 284.0)
+ assert max_cll == pytest.approx(406.0, rel=1e-3)
+ assert max_fall == pytest.approx(304.5, rel=1e-3)
+
+
def test_png16_carries_cicp_light_levels_and_exact_pixels() -> None:
rng = np.random.default_rng(7)
pixels = rng.integers(0, 65536, size=(5, 7, 3), dtype=np.uint16)