File size: 1,640 Bytes
e1ced61
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Node-map ownership mask (``docs/02`` §9, ``docs/07`` §8 P6).

``node_map.uint32.png`` records, per pixel, the index of the semantic node that
owns it (``0`` = background / unowned). The encoding is little-endian ``uint32``
packed into the RGBA channels of a standard PNG — lossless and renderer-version
independent. Anti-alias boundaries are resolved by an alpha-aware ownership mask
(the highest-alpha node wins, ties break to the lower index), built in the same
draw pass as the RGBA canvas in :mod:`renderers.base`.

This module exposes the encode/decode helpers and a coverage verdict the P6 gate
("every visible semantic node has node-map coverage") consumes.
"""

from __future__ import annotations

from collections.abc import Mapping, Sequence

from .base import (
    _encode_uint32_png,  # noqa: F401 (re-exported for tests/CLI)
    decode_uint32_png,
)


def coverage_verdict(
    visible_node_ids: Sequence[str], node_pixel_counts: Mapping[str, int]
) -> tuple[bool, list[str]]:
    """``(all_covered, missing)`` — every visible node needs ≥1 node-map pixel."""
    missing = [nid for nid in visible_node_ids if node_pixel_counts.get(nid, 0) <= 0]
    return (not missing, missing)


def read_owners(png: bytes) -> object:
    """Decode a node-map PNG to its ``uint32`` owner-index array."""
    return decode_uint32_png(png)


def index_table(visible_node_ids: Sequence[str]) -> list[str]:
    """The index→node-id table (index 0 is the background sentinel)."""
    return ["__background__", *visible_node_ids]


__all__ = [
    "coverage_verdict",
    "decode_uint32_png",
    "index_table",
    "read_owners",
]