"""Deterministic source-raster renderer for GQA scene-graph interventions. GQA is the real-image slice of EVI-PO. Its source photographs cannot be faithfully regenerated from the released scene graph, so the certified operation is deliberately narrow: normalize the released image once, then replace exactly one source-annotated object box with an opaque constant patch. The paired node map is generated only from released object boxes; it is an annotation projection, not an inferred segmentation. """ from __future__ import annotations import io from collections.abc import Iterable, Mapping from dataclasses import dataclass from typing import Any from PIL import Image, ImageDraw from ..hashing import canonical_json_hash, sha256_bytes from ..sources.base import World from ..sources.world_ops import world_sha256 from .base import RenderError, _encode_uint32_png GQA_RENDERER_NAME = "gqa_source_bbox_renderer" GQA_RENDERER_VERSION = "1" GQA_RENDERER_ID = "gqa_source_bbox_v1" GQA_JPEG_QUALITY = 88 _MASK_RGB = (127, 127, 127) @dataclass(frozen=True) class GQARenderedObservation: """One content-addressed GQA observation and its annotation projection.""" image_jpeg: bytes node_map_png: bytes render_manifest: dict[str, Any] text_manifest: dict[str, Any] @property def image_sha256(self) -> str: return sha256_bytes(self.image_jpeg) @property def node_map_sha256(self) -> str: return sha256_bytes(self.node_map_png) def normalize_gqa_source_image(source_image: bytes, *, width: int, height: int) -> bytes: """Decode a released source image and freeze deterministic JPEG bytes.""" try: with Image.open(io.BytesIO(source_image)) as opened: image = opened.convert("RGB") except (OSError, ValueError) as exc: raise RenderError(f"cannot decode GQA source image: {exc}") from exc if image.size != (width, height): raise RenderError( f"GQA source image dimensions {image.size!r} != scene graph {(width, height)!r}" ) return _jpeg_bytes(image) def render_gqa_observation( normalized_source_jpeg: bytes, world: World, *, masked_node_id: str | None, seed: int, ) -> GQARenderedObservation: """Render FULL or one opaque source-bbox mask with a deterministic node map.""" if world.get("world_schema") != "gqa_scene_graph_v1": raise RenderError("GQA renderer requires gqa_scene_graph_v1") width, height = _dimensions(world) objects = _objects(world) try: with Image.open(io.BytesIO(normalized_source_jpeg)) as opened: image = opened.convert("RGB") except (OSError, ValueError) as exc: raise RenderError(f"cannot decode normalized GQA image: {exc}") from exc if image.size != (width, height): raise RenderError("normalized GQA image dimensions drifted") if masked_node_id is not None: try: bbox = _bbox(objects[masked_node_id]) except KeyError as exc: raise RenderError(f"GQA mask target {masked_node_id!r} is absent") from exc ImageDraw.Draw(image).rectangle( (bbox[0], bbox[1], bbox[2] - 1, bbox[3] - 1), fill=_MASK_RGB, ) image_jpeg = _jpeg_bytes(image) else: # Avoid a second lossy generation for FULL and for replay input. image_jpeg = bytes(normalized_source_jpeg) node_ids = sorted(objects) node_table = ["__background__", *node_ids] import numpy as np owners = np.zeros((height, width), dtype="