File size: 7,620 Bytes
26cba34
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
"""
debug_grid.py β€” 1024x1024 curve-graph visualization for Apply H3 RefMod.

Renders the per-frame strength envelope (direction/shape/value) across the
full canvas, with the "concept zone" shaded where the concept shows up.  It
is both the Apply's optional debug IMAGE output and the saved graph-preset
PNG (graph embedded in the image's tEXt metadata).

Pure PIL; ``pil_to_tensor`` converts the result to a torch IMAGE tensor.
"""

from __future__ import annotations

import json
import math
import os
from typing import Dict, Optional, Tuple

import numpy as np
import torch
from PIL import Image, ImageDraw, ImageFont, PngImagePlugin

from .core import CURVE_DIRECTIONS, CURVE_SHAPES, curve_strengths

SIZE = 1024

BG = (18, 20, 24)
TITLE = (232, 230, 227)
TEXT = (207, 203, 196)
DIM = (107, 111, 118)
GRID_LINE = (42, 46, 54)
ACCENT = (255, 158, 61)
ACCENT_FILL = (58, 34, 15)
ACCENT_DIM = (122, 69, 21)

_CURVE_SAMPLES = 160

# Regions with strong weighting on the reference's own timeline.
_ZONES = {
    "concept_at_start": ((0.0, 1.0 / 3.0),),
    "concept_at_middle": ((1.0 / 3.0, 2.0 / 3.0),),
    "concept_at_end": ((2.0 / 3.0, 1.0),),
    "concept_at_ends": ((0.0, 1.0 / 3.0), (2.0 / 3.0, 1.0)),
}


# ── fonts ────────────────────────────────────────────────────────────────────

_FONT_CACHE: Dict[Tuple[int, bool, bool], ImageFont.FreeTypeFont] = {}


def _font(size: int, bold: bool = False, mono: bool = False):
    key = (size, bold, mono)
    if key in _FONT_CACHE:
        return _FONT_CACHE[key]
    candidates = []
    if os.name == "nt":
        base = "C:/Windows/Fonts/"
        name = "consola.ttf" if mono else ("arialbd.ttf" if bold else "arial.ttf")
        candidates = [base + name]
    else:
        if mono:
            candidates = [
                "/usr/share/fonts/truetype/dejavu/DejaVuSansMono.ttf",
                "/System/Library/Fonts/Menlo.ttc",
            ]
        else:
            candidates = [
                "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf" if bold
                else "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf",
                "/System/Library/Fonts/Supplemental/Arial.ttf",
            ]
    for path in candidates:
        if os.path.isfile(path):
            try:
                f = ImageFont.truetype(path, size)
                _FONT_CACHE[key] = f
                return f
            except OSError:
                pass
    try:
        f = ImageFont.load_default(size=size)
    except Exception:
        f = ImageFont.load_default()
    _FONT_CACHE[key] = f
    return f


def _text_w(draw: ImageDraw.ImageDraw, text: str, font) -> int:
    l, _t, r, _b = draw.textbbox((0, 0), text, font=font)
    return r - l


# ── the curve graph ──────────────────────────────────────────────────────────

def render_debug_grid(spec, preset_name: str = "") -> Image.Image:
    """Render the 1024x1024 curve graph for an Apply configuration.

    ``spec`` is the resolved (direction, shape, value) curve tuple and
    ``preset_name`` the active graph preset (shown when one is loaded).
    """
    img = Image.new("RGB", (SIZE, SIZE), BG)
    draw = ImageDraw.Draw(img)

    draw.text((32, 26), "curve graph", font=_font(28, bold=True), fill=TITLE)
    if preset_name:
        draw.text((SIZE - 32, 34), f"preset: {preset_name}",
                  font=_font(20), fill=DIM, anchor="rs")

    x0, y0, x1, y1 = 90, 120, SIZE - 10, 940
    direction = spec[0]

    # concept zone shading (where the concept shows up in the output)
    for start, end in _ZONES.get(direction, ()):
        zx0 = x0 + start * (x1 - x0)
        zx1 = x0 + end * (x1 - x0)
        draw.rectangle([zx0, y0, zx1, y1], fill=ACCENT_FILL)
        draw.text((zx0 + 8, y1 + 8), "strong refs", font=_font(20), fill=ACCENT_DIM)

    # grid + axis labels
    for f in (0.0, 0.25, 0.5, 0.75, 1.0):
        gy = y1 - f * (y1 - y0)
        draw.line([x0, gy, x1, gy], fill=GRID_LINE, width=2)
        draw.text((16, gy - 12), f"{f:.2f}", font=_font(20), fill=DIM)
    for fx, label in ((0.0, "0"), (0.25, "0.25"), (0.5, "0.5"),
                      (0.75, "0.75"), (1.0, "1")):
        gx = x0 + fx * (x1 - x0)
        draw.line([gx, y0, gx, y1], fill=GRID_LINE, width=2)
        draw.text((gx, y1 + 8), label, font=_font(20), fill=DIM, anchor="ma")
    draw.text((x1 - 6, y1 + 42), "reference frames β†’", font=_font(20), fill=DIM, anchor="ra")

    # the envelope
    strengths = curve_strengths(spec, _CURVE_SAMPLES)
    if strengths is None:
        strengths = [1.0] * _CURVE_SAMPLES
    pts = [(x0 + i * (x1 - x0) / (_CURVE_SAMPLES - 1),
            y1 - s * (y1 - y0)) for i, s in enumerate(strengths)]
    draw.polygon([(x0, y1)] + pts + [(x1, y1)], fill=ACCENT_FILL)
    draw.line(pts, fill=ACCENT, width=6)

    # peak marker
    peak = max(range(_CURVE_SAMPLES), key=lambda i: strengths[i])
    px, py = pts[peak]
    draw.line([px, y0, px, y1], fill=ACCENT_DIM, width=2)
    draw.ellipse([px - 9, py - 9, px + 9, py + 9], fill=ACCENT)

    # spec readout on a dark backdrop
    lines = [f"direction: {direction}", f"shape:     {spec[1]}",
             f"value:     {float(spec[2]):.2f}"]
    font = _font(24, mono=True)
    tw = max(_text_w(draw, ln, font) for ln in lines)
    draw.rectangle([x0 + 18, y0 + 18, x0 + 18 + tw + 24, y0 + 18 + 3 * 34 + 12],
                   fill=(14, 17, 22))
    for i, ln in enumerate(lines):
        draw.text((x0 + 30, y0 + 28 + i * 34), ln, font=font, fill=TEXT)

    return img


# ── graph presets (PNG metadata) ────────────────────────────────────────────

# The graph spec is embedded in the preset PNG's tEXt chunk, so a saved curve
# graph IS the preset: share the image, and the curve comes with it.
GRAPH_META_KEY = "graph"


def graph_pnginfo(spec):
    """PIL pnginfo carrying the (direction, shape, value) spec as tEXt."""
    info = PngImagePlugin.PngInfo()
    info.add_text(GRAPH_META_KEY, json.dumps({
        "direction": spec[0], "shape": spec[1], "value": float(spec[2])}))
    return info


def read_graph_meta(path: str) -> Optional[tuple]:
    """Read a (direction, shape, value) spec from a PNG's tEXt metadata.

    Returns None for a missing file, a missing/empty chunk, or an invalid
    spec (unknown direction/shape, bad value) so callers can fall back.
    """
    try:
        with Image.open(path) as img:
            raw = img.text.get(GRAPH_META_KEY)
    except Exception:
        return None
    if not raw:
        return None
    try:
        data = json.loads(raw)
    except ValueError:
        return None
    if not isinstance(data, dict):
        return None
    direction, shape = data.get("direction"), data.get("shape")
    if direction not in CURVE_DIRECTIONS or shape not in CURVE_SHAPES:
        return None
    try:
        value = float(data.get("value", 1.0))
    except (TypeError, ValueError):
        return None
    if isinstance(data.get('value'), bool) or not math.isfinite(value) or not 0 <= value <= 1:
        return None
    return (direction, shape, value)


def pil_to_tensor(img: Image.Image) -> torch.Tensor:
    """Convert a rendered PIL graph to a [1, 1024, 1024, 3] float32 tensor."""
    arr = np.asarray(img).copy()
    return torch.from_numpy(arr).float().unsqueeze(0) / 255.0  # [1, 1024, 1024, 3]