File size: 3,094 Bytes
8cdbe7c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared bedmatrix → thermal heatmap rendering for the preview scripts.

The `bedmatrix` column is the raw 32×24 IR bed-surface temperature grid
(row-major °C). We colormap it ourselves rather than reuse the legacy
`frame_thermal` GIF because:

  * `frame_thermal` was a firmware-rendered heatmap whose colormap and scale we
    never controlled and can't reproduce for new builds — and it's null for
    builds recorded after 2026-07-12 (see CLAUDE.md).
  * Rendering from `bedmatrix` gives one consistent look across every build and
    a temperature scale we choose, so the thermal panel reads as an honest
    temperature: the same color means the same °C in every build.

Colormap is an inferno approximation (11 anchor colors linearly interpolated to
256 entries) — faithful enough for a preview without pulling in matplotlib.

Temperature → color uses a FIXED absolute range (`THERMAL_VMIN`..`THERMAL_VMAX`,
°C). The observed bedmatrix span across builds is ~51–191 °C during sintering;
the default 20–200 °C covers ambient through peak sinter with headroom and uses
the full colormap. Override via the THERMAL_VMIN / THERMAL_VMAX env vars.
"""
import os

import numpy as np
from PIL import Image

THERMAL_VMIN = float(os.environ.get("THERMAL_VMIN", 20.0))
THERMAL_VMAX = float(os.environ.get("THERMAL_VMAX", 200.0))

# Inferno anchor colors at t = 0.0, 0.1, ... 1.0 (RGB 0–255). Sampled from
# matplotlib's inferno; linearly interpolated per channel to a 256-entry LUT.
_INFERNO_ANCHORS = np.array([
    (0,   0,   4),
    (20,  11,  52),
    (57,  9,   98),
    (100, 16,  108),
    (143, 33,  102),
    (186, 54,  85),
    (221, 81,  58),
    (243, 120, 25),
    (252, 165, 10),
    (246, 215, 70),
    (252, 255, 164),
], dtype=np.float64)

_LUT: np.ndarray | None = None


def _lut() -> np.ndarray:
    """256×3 uint8 inferno LUT, built once."""
    global _LUT
    if _LUT is None:
        xs = np.linspace(0.0, 1.0, len(_INFERNO_ANCHORS))
        grid = np.linspace(0.0, 1.0, 256)
        lut = np.empty((256, 3), dtype=np.uint8)
        for c in range(3):
            lut[:, c] = np.interp(grid, xs, _INFERNO_ANCHORS[:, c]).round().astype(np.uint8)
        _LUT = lut
    return _LUT


def bedmatrix_to_image(bm) -> Image.Image | None:
    """Render a `bedmatrix` struct → PIL RGB heatmap at native grid resolution.

    `bm` is the struct dict {width, height, values (row-major °C), path} or None.
    Returns a (height × width) RGB image; the caller upscales / letterboxes it.
    None when the struct is missing or malformed.
    """
    if bm is None:
        return None
    vals = bm.get("values")
    w = bm.get("width")
    h = bm.get("height")
    if not vals or not w or not h or len(vals) != w * h:
        return None
    a = np.asarray(vals, dtype=np.float32).reshape(int(h), int(w))
    a = np.nan_to_num(a, nan=THERMAL_VMIN)
    t = (a - THERMAL_VMIN) / (THERMAL_VMAX - THERMAL_VMIN)
    idx = np.clip(t, 0.0, 1.0)
    idx = (idx * 255.0).round().astype(np.uint8)
    rgb = _lut()[idx]  # (h, w, 3)
    return Image.fromarray(rgb, "RGB")