dsh-watch-together 0.2.9 → 0.2.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +144 -0
- package/README.md +15 -5
- package/docs/INTERFACES.zh.md +29 -12
- package/dsh.plugin.json +12 -12
- package/examples/floating-chat/glass_capture.py +286 -0
- package/examples/floating-chat/glass_cpu.py +174 -0
- package/examples/floating-chat/glass_geometry.py +288 -0
- package/examples/floating-chat/glass_gpu.py +509 -0
- package/examples/floating-chat/glass_window.py +4 -7
- package/examples/floating-chat/glass_window_gpu.py +14 -516
- package/examples/floating-chat/requirements-glass.txt +5 -0
- package/examples/floating-chat/run-glass.cmd +5 -1
- package/examples/floating-chat/window.py +1210 -1932
- package/examples/floating-chat/window_widgets.py +288 -0
- package/package.json +115 -115
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
"""glass_cpu.py — CPU 玻璃合成:抓屏 → 模糊 → 边缘折射 → 内框透镜 → 染色 → 色散。
|
|
2
|
+
|
|
3
|
+
这就是原来 `window.LiveGlass._capture()` 那 130 行,拆成一条显式的管线:
|
|
4
|
+
|
|
5
|
+
grab → 缩小 → observe(moved/stale) → 模糊 → 折射 → 内框
|
|
6
|
+
→ 2x 合成 → 染色 → 色散(仅静止帧) → 放大 → 残影(仅移动帧) → 麦克风 → 圆角
|
|
7
|
+
|
|
8
|
+
渲染器不再知道 Tk、配置、聊天流的存在:给一张背景,还一张玻璃。
|
|
9
|
+
"""
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import numpy as np
|
|
13
|
+
from PIL import Image, ImageFilter
|
|
14
|
+
|
|
15
|
+
from glass_capture import CaptureSource, MotionTracker, finish, paste_mic, resample
|
|
16
|
+
from glass_geometry import LPF, Geometry
|
|
17
|
+
|
|
18
|
+
try:
|
|
19
|
+
import cv2 as _cv2
|
|
20
|
+
except Exception:
|
|
21
|
+
_cv2 = None
|
|
22
|
+
|
|
23
|
+
BLUR_EDGE = 1.45 # 1/4 分辨率上,边缘取这一档
|
|
24
|
+
BLUR_CENTER = 2.6 # 内框中心再叠一档(叠在已模糊图上,等效更厚)
|
|
25
|
+
CHROMA = 1.7 # 2x 图上的 R/B 错位像素
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def _blur(arr: np.ndarray, sigma: float) -> np.ndarray:
|
|
29
|
+
if _cv2 is not None:
|
|
30
|
+
return _cv2.GaussianBlur(arr, (0, 0), sigmaX=sigma, sigmaY=sigma,
|
|
31
|
+
borderType=_cv2.BORDER_REFLECT)
|
|
32
|
+
return np.asarray(Image.fromarray(arr).filter(ImageFilter.GaussianBlur(sigma)))
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def _smoothstep(a: float, b: float, x: np.ndarray) -> np.ndarray:
|
|
36
|
+
t = np.clip((x - a) / (b - a), 0.0, 1.0)
|
|
37
|
+
return t * t * (3.0 - 2.0 * t)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
class CpuRenderer:
|
|
41
|
+
"""把背景图变成玻璃帧。一个实例对应一套 `Geometry`。"""
|
|
42
|
+
|
|
43
|
+
def __init__(self, geo: Geometry, tracker: MotionTracker | None = None):
|
|
44
|
+
self.geo = geo
|
|
45
|
+
self.tracker = tracker or MotionTracker()
|
|
46
|
+
self._prev: Image.Image | None = None
|
|
47
|
+
self._chroma: dict = {}
|
|
48
|
+
|
|
49
|
+
# ---- 生命周期 ----
|
|
50
|
+
|
|
51
|
+
def set_geometry(self, geo: Geometry) -> None:
|
|
52
|
+
self.geo = geo
|
|
53
|
+
self._chroma.clear() # 旧尺寸的索引图不再用,别每缩放一次攒一份
|
|
54
|
+
self.invalidate()
|
|
55
|
+
|
|
56
|
+
def invalidate(self) -> None:
|
|
57
|
+
"""形状变了:旧签名和上一帧都不能再参与合成。"""
|
|
58
|
+
self.tracker.invalidate()
|
|
59
|
+
self._prev = None
|
|
60
|
+
|
|
61
|
+
# ---- 主入口 ----
|
|
62
|
+
|
|
63
|
+
def render(self, source: CaptureSource, sct, bbox) -> Image.Image | None:
|
|
64
|
+
"""返回一张 `geo.width × geo.height` 的 RGB 图;`None` = 本帧不必更新。"""
|
|
65
|
+
geo = self.geo
|
|
66
|
+
shot = source.grab(sct, bbox)
|
|
67
|
+
if shot is None:
|
|
68
|
+
return None
|
|
69
|
+
im = source.to_image(shot, bbox)
|
|
70
|
+
|
|
71
|
+
gw, gh = shot.shot.size
|
|
72
|
+
sw4, sh4 = max(2, gw // LPF), max(2, gh // LPF)
|
|
73
|
+
raw = resample(np.asarray(im), (sw4, sh4))
|
|
74
|
+
moved, stale = self.tracker.observe(raw, shot.box)
|
|
75
|
+
if stale:
|
|
76
|
+
# 静态跳帧:连模糊都不做。上一帧若带运动残影,绝不能留到下次移动。
|
|
77
|
+
self._prev = None
|
|
78
|
+
return None
|
|
79
|
+
|
|
80
|
+
small = _blur(raw, BLUR_EDGE)
|
|
81
|
+
strong = _blur(small, BLUR_CENTER)
|
|
82
|
+
|
|
83
|
+
warped, out_panel = self._lens(small, strong, raw, shot.offset)
|
|
84
|
+
panel = self._compose(warped, out_panel, moved)
|
|
85
|
+
out = resample(np.asarray(panel), (geo.width, geo.height))
|
|
86
|
+
out = Image.fromarray(out.astype(np.uint8))
|
|
87
|
+
|
|
88
|
+
if moved and self._prev is not None and self._prev.size == out.size:
|
|
89
|
+
blend = self.tracker.blend
|
|
90
|
+
if blend > 0.01:
|
|
91
|
+
out = Image.blend(self._prev, out, blend)
|
|
92
|
+
self._prev = out.copy()
|
|
93
|
+
# 先混再贴麦克风,避免把上一帧的麦克风位置糊到这一帧
|
|
94
|
+
return finish(geo, paste_mic(geo, out))
|
|
95
|
+
|
|
96
|
+
# ---- 步骤 ----
|
|
97
|
+
|
|
98
|
+
def _lens(self, small: np.ndarray, strong: np.ndarray, raw: np.ndarray, offset):
|
|
99
|
+
"""边缘折射 + 内框透镜,全在 1/4 分辨率完成。
|
|
100
|
+
|
|
101
|
+
`warped` 是整窗背景按圆角边缘位移后的结果;`out_panel` 在它的基础上
|
|
102
|
+
把每个内框区域换成「边缘折射 → 中心强模糊」的过渡。
|
|
103
|
+
"""
|
|
104
|
+
geo = self.geo
|
|
105
|
+
px, py = offset
|
|
106
|
+
h, w = small.shape[:2]
|
|
107
|
+
sx = np.clip(np.rint(geo.warp_x + px).astype(np.int32), 0, w - 1)
|
|
108
|
+
sy = np.clip(np.rint(geo.warp_y + py).astype(np.int32), 0, h - 1)
|
|
109
|
+
warped = small[sy, sx]
|
|
110
|
+
strong_panel = strong[sy, sx]
|
|
111
|
+
out_panel = warped.copy()
|
|
112
|
+
for qx0, qy0, qw, qh, alpha, weight, bsx, bsy in geo.box_maps:
|
|
113
|
+
base_patch = warped[qy0:qy0 + qh, qx0:qx0 + qw]
|
|
114
|
+
if base_patch.size == 0:
|
|
115
|
+
continue
|
|
116
|
+
edge_patch = raw[np.clip(bsy + py, 0, h - 1), np.clip(bsx + px, 0, w - 1)]
|
|
117
|
+
strong_patch = strong_panel[qy0:qy0 + qh, qx0:qx0 + qw]
|
|
118
|
+
patch = edge_patch * (1.0 - weight[..., None]) + strong_patch * weight[..., None]
|
|
119
|
+
a = alpha[..., None]
|
|
120
|
+
out_panel[qy0:qy0 + qh, qx0:qx0 + qw] = base_patch * (1.0 - a) + patch * a
|
|
121
|
+
return warped, out_panel
|
|
122
|
+
|
|
123
|
+
def _compose(self, warped: np.ndarray, out_panel: np.ndarray, moved: bool) -> Image.Image:
|
|
124
|
+
"""1/4 → 1/2 合成:框内用透镜、框外用底图,再叠染色与色散。
|
|
125
|
+
|
|
126
|
+
在 2x 上做而不是全分辨率:全分辨率逐像素色散会把这条 60fps 管线拖慢。
|
|
127
|
+
移动帧跳过色散(约 3.5ms/帧),优先保证拖动/滚动响应,停下后静止帧补上。
|
|
128
|
+
"""
|
|
129
|
+
geo = self.geo
|
|
130
|
+
hw, hh = geo.half_size
|
|
131
|
+
base_2x = Image.fromarray(resample(warped, (hw, hh)))
|
|
132
|
+
lens_2x = Image.fromarray(resample(out_panel, (hw, hh)))
|
|
133
|
+
panel = Image.composite(lens_2x, base_2x, geo.box_union_half)
|
|
134
|
+
panel = Image.alpha_composite(panel.convert("RGBA"), geo.overlay_half).convert("RGB")
|
|
135
|
+
if not moved:
|
|
136
|
+
panel = self._chroma_shift(panel, CHROMA)
|
|
137
|
+
return panel
|
|
138
|
+
|
|
139
|
+
def _chroma_maps(self, w: int, h: int, strength: float):
|
|
140
|
+
key = (w, h, round(float(strength), 2))
|
|
141
|
+
hit = self._chroma.get(key)
|
|
142
|
+
if hit is not None:
|
|
143
|
+
return hit
|
|
144
|
+
yy, xx = np.mgrid[0:h, 0:w].astype(np.float32)
|
|
145
|
+
dx = (xx + 0.5) / w - 0.5
|
|
146
|
+
dy = (yy + 0.5) / h - 0.5
|
|
147
|
+
r = np.hypot(dx, dy) + 1e-6
|
|
148
|
+
edge = _smoothstep(0.38, 0.48, r) * (1.0 - _smoothstep(0.52, 0.585, r))
|
|
149
|
+
k = strength * edge
|
|
150
|
+
ox = np.rint(dx / r * k).astype(np.int32)
|
|
151
|
+
oy = np.rint(dy / r * k).astype(np.int32)
|
|
152
|
+
xi, yi = xx.astype(np.int32), yy.astype(np.int32)
|
|
153
|
+
hit = (edge.astype(np.float32),
|
|
154
|
+
np.clip(xi + ox, 0, w - 1), np.clip(yi + oy, 0, h - 1),
|
|
155
|
+
np.clip(xi - ox, 0, w - 1), np.clip(yi - oy, 0, h - 1))
|
|
156
|
+
if len(self._chroma) > 4:
|
|
157
|
+
self._chroma.clear()
|
|
158
|
+
self._chroma[key] = hit
|
|
159
|
+
return hit
|
|
160
|
+
|
|
161
|
+
def _chroma_shift(self, im: Image.Image, strength: float) -> Image.Image:
|
|
162
|
+
"""外框径向色散:R 向外、B 向内错一位,只在圆角外缘的窄带里做。"""
|
|
163
|
+
arr = np.asarray(im.convert("RGB"), dtype=np.float32)
|
|
164
|
+
h, w = arr.shape[:2]
|
|
165
|
+
edge, xr, yr, xb, yb = self._chroma_maps(w, h, strength)
|
|
166
|
+
if not np.any(edge > 0.001):
|
|
167
|
+
return im
|
|
168
|
+
out = arr.copy()
|
|
169
|
+
out[:, :, 0] = arr[:, :, 0] * (1.0 - edge) + arr[yr, xr, 0] * edge
|
|
170
|
+
out[:, :, 2] = arr[:, :, 2] * (1.0 - edge) + arr[yb, xb, 2] * edge
|
|
171
|
+
return Image.fromarray(np.clip(out, 0, 255).astype(np.uint8))
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
__all__ = ["CpuRenderer"]
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"""glass_geometry.py — 悬浮窗玻璃的全部尺寸相关贴图,纯计算、无 Tk。
|
|
2
|
+
|
|
3
|
+
原来的 `window.py` 把 `W/H/SHAPES/MASK/OVERLAY/WARP_X/BOX_MAPS/...` 十几个量
|
|
4
|
+
挂成模块全局,靠 `global` 语句在 `rebuild_geometry()` 里一次性改写。副作用是:
|
|
5
|
+
|
|
6
|
+
- 任何模块都能读写它们,改一个尺寸要先数清楚谁在被引用;
|
|
7
|
+
- `import` 的那一刻就按默认尺寸建好一整套图,缩放/折叠都要再跑一遍;
|
|
8
|
+
- GPU 伴生文件只能 `import window as G` 去读全局,拿到的可能是「上一帧的尺寸」。
|
|
9
|
+
|
|
10
|
+
这里改成不可变值对象:`Geometry.for_size(w, h)` 给出完整一套;
|
|
11
|
+
气泡宽度变了就 `geo.with_shapes(shapes)` —— 尺寸相关的遮罩/折射图直接复用,
|
|
12
|
+
不需要重建。谁要什么就传 `geo` 进去,不再有隐式共享状态。
|
|
13
|
+
"""
|
|
14
|
+
from __future__ import annotations
|
|
15
|
+
|
|
16
|
+
from dataclasses import dataclass, field, replace
|
|
17
|
+
from typing import Iterable
|
|
18
|
+
|
|
19
|
+
import numpy as np
|
|
20
|
+
from PIL import Image, ImageDraw
|
|
21
|
+
|
|
22
|
+
# ---- 布局常量(改这些等于改外观,改完必须截图对比) ----
|
|
23
|
+
DEFAULT_W, DEFAULT_H = 760, 560
|
|
24
|
+
MIN_W, MIN_H = 480, 420
|
|
25
|
+
CORNER_R = 58
|
|
26
|
+
BAR_FRAC = 0.55
|
|
27
|
+
BAR_Y = 20
|
|
28
|
+
BAR_THICK = 4
|
|
29
|
+
# 抠色键:窗口背景色 = 这个值的地方被 Tk 的 -transparentcolor 抠成透明。
|
|
30
|
+
KEY = "#010203"
|
|
31
|
+
|
|
32
|
+
MESSAGE_SLOTS = 4
|
|
33
|
+
SLAB_OUT, SLAB_TOP_H, SLAB_BOT_H = 12, 54, 70
|
|
34
|
+
SLAB_GAP = 10
|
|
35
|
+
SLAB_RADIUS = 22
|
|
36
|
+
|
|
37
|
+
# 抓屏时向外扩一圈,边缘透镜才有「框外」的像素可采
|
|
38
|
+
PAD = 40
|
|
39
|
+
BAND = 64 # 全分辨率下的透镜带宽度
|
|
40
|
+
AMP = 30 # 全分辨率下的最大采样偏移
|
|
41
|
+
LPF = 4 # 透镜/模糊在 1/4 分辨率上算
|
|
42
|
+
|
|
43
|
+
BOX_BAND = 7 # 1/4 分辨率下的折射带
|
|
44
|
+
BOX_AMP = 8 # 1/4 分辨率下的最大采样偏移
|
|
45
|
+
|
|
46
|
+
MIC_SIZE = 28
|
|
47
|
+
MIC_MARGIN = (53, 67) # 距左下角
|
|
48
|
+
|
|
49
|
+
Box = tuple[int, int, int, int, int] # x0, y0, x1, y1, radius
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def slabs_for(w: int, h: int) -> tuple[Box, ...]:
|
|
53
|
+
"""三块独立的玻璃:标题栏 / 主体 / 输入栏,中间留透明缝透出桌面。
|
|
54
|
+
|
|
55
|
+
折叠态(高度只有标题栏)只返回那一块 —— 否则主体/输入栏会算出 y1 < y0
|
|
56
|
+
的负高度矩形,PIL 直接抛 `y1 must be greater than or equal to y0`。
|
|
57
|
+
"""
|
|
58
|
+
o = SLAB_OUT
|
|
59
|
+
top = (o, o, w - o, o + SLAB_TOP_H)
|
|
60
|
+
if h < SLAB_TOP_H + 3 * o:
|
|
61
|
+
return (top,)
|
|
62
|
+
bot = (o, h - o - SLAB_BOT_H, w - o, h - o)
|
|
63
|
+
body = (o, top[3] + SLAB_GAP, w - o, bot[1] - SLAB_GAP)
|
|
64
|
+
return (top, body, bot)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def chat_layout(h: int) -> tuple[list[int], int]:
|
|
68
|
+
"""四条消息槽位的 y 起点与高度;高度不足 560 时压缩,输入框始终贴底。"""
|
|
69
|
+
input_top = h - 80
|
|
70
|
+
if h >= 560:
|
|
71
|
+
return [104, 198, 292, 386], 88
|
|
72
|
+
avail = max(0, input_top - 124 - 8)
|
|
73
|
+
bh = min(60, max(36, (avail - 24) // 4))
|
|
74
|
+
gap = max(8, (avail - 4 * bh) // 3)
|
|
75
|
+
return [124 + i * (bh + gap) for i in range(4)], bh
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def shapes_for(w: int, h: int) -> tuple[Box, ...]:
|
|
79
|
+
"""内部框的纵向槽位;气泡的最终宽高由文字测量决定(见 window._message_shapes)。
|
|
80
|
+
|
|
81
|
+
折叠态(h=78)没有消息区和输入区,返回全零占位:`_box_maps`/`_overlay` 会跳过
|
|
82
|
+
空框,不会在负坐标上算出畸形折射图。
|
|
83
|
+
"""
|
|
84
|
+
input_top = h - 80
|
|
85
|
+
if input_top - 40 < SLAB_OUT + SLAB_TOP_H:
|
|
86
|
+
return tuple((0, 0, 0, 0, 0) for _ in range(MESSAGE_SLOTS + 2))
|
|
87
|
+
ys, bh = chat_layout(h)
|
|
88
|
+
boxes = [(38, y, w - 38, y + bh, 30) for y in ys]
|
|
89
|
+
boxes.append((36, input_top, w - 146, input_top + 54, 27))
|
|
90
|
+
boxes.append((w - 134, input_top, w - 36, input_top + 54, 27))
|
|
91
|
+
return tuple(boxes)
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _rounded_mask(w: int, h: int, boxes: Iterable[Box]) -> Image.Image:
|
|
95
|
+
ss = 4
|
|
96
|
+
m = Image.new("L", (w * ss, h * ss), 0)
|
|
97
|
+
d = ImageDraw.Draw(m)
|
|
98
|
+
for x0, y0, x1, y1 in boxes:
|
|
99
|
+
d.rounded_rectangle((x0 * ss, y0 * ss, x1 * ss, y1 * ss),
|
|
100
|
+
radius=SLAB_RADIUS * ss, fill=255)
|
|
101
|
+
return m.resize((w, h), Image.Resampling.LANCZOS)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _overlay(w: int, h: int, shapes: tuple[Box, ...]) -> Image.Image:
|
|
105
|
+
"""只画内部框的轻微染色;窗口本体完全透明,靠背景模糊出玻璃感。"""
|
|
106
|
+
layer = Image.new("RGBA", (w, h), (0, 0, 0, 0))
|
|
107
|
+
d = ImageDraw.Draw(layer)
|
|
108
|
+
tints = ((235, 242, 255, 5), (110, 175, 255, 14), (235, 242, 255, 4),
|
|
109
|
+
(235, 242, 255, 4), (235, 242, 255, 5), (110, 175, 255, 12))
|
|
110
|
+
for (x0, y0, x1, y1, r), fill in zip(shapes, tints):
|
|
111
|
+
if x1 > x0 and y1 > y0:
|
|
112
|
+
d.rounded_rectangle((x0, y0, x1, y1), radius=r, fill=fill)
|
|
113
|
+
return layer
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _warp_maps(w: int, h: int) -> tuple[np.ndarray, np.ndarray]:
|
|
117
|
+
"""1/4 分辨率的边缘位移图:真正采样「边界外」的背景,不是描边。
|
|
118
|
+
|
|
119
|
+
半径方向向外取样本,越靠边偏移越大,到 BAND 内归零。
|
|
120
|
+
"""
|
|
121
|
+
hw, hh = w // LPF, h // LPF
|
|
122
|
+
yy, xx = np.mgrid[0:hh, 0:hw].astype(np.float32)
|
|
123
|
+
r = CORNER_R / LPF
|
|
124
|
+
qx = np.abs(xx - hw / 2) - (hw / 2 - r)
|
|
125
|
+
qy = np.abs(yy - hh / 2) - (hh / 2 - r)
|
|
126
|
+
sdf = (np.hypot(np.maximum(qx, 0), np.maximum(qy, 0))
|
|
127
|
+
+ np.minimum(np.maximum(qx, qy), 0) - r)
|
|
128
|
+
gy, gx = np.gradient(sdf)
|
|
129
|
+
n = np.maximum(np.hypot(gx, gy), 1e-6)
|
|
130
|
+
d = -sdf
|
|
131
|
+
t = np.clip(1.0 - d / (BAND / LPF), 0.0, 1.0)
|
|
132
|
+
mag = (AMP / LPF) * t * t
|
|
133
|
+
return (xx + gx / n * mag).astype(np.float32), (yy + gy / n * mag).astype(np.float32)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _box_maps(shapes: tuple[Box, ...], w: int, h: int) -> tuple[tuple, ...]:
|
|
137
|
+
"""每个内部框的 alpha / 边缘权重 / 采样坐标,全部在 1/4 分辨率。"""
|
|
138
|
+
out = []
|
|
139
|
+
for x0, y0, x1, y1, r in shapes:
|
|
140
|
+
if x1 <= x0 or y1 <= y0:
|
|
141
|
+
continue
|
|
142
|
+
qx0, qy0 = x0 // LPF, y0 // LPF
|
|
143
|
+
qw, qh = max(2, (x1 - x0) // LPF), max(2, (y1 - y0) // LPF)
|
|
144
|
+
xx, yy = np.meshgrid(np.arange(qw, dtype=np.float32),
|
|
145
|
+
np.arange(qh, dtype=np.float32))
|
|
146
|
+
rr = r / LPF
|
|
147
|
+
cx, cy = qw / 2.0, qh / 2.0
|
|
148
|
+
qx = np.abs(xx - cx) - (cx - rr)
|
|
149
|
+
qy = np.abs(yy - cy) - (cy - rr)
|
|
150
|
+
sdf = (np.hypot(np.maximum(qx, 0), np.maximum(qy, 0))
|
|
151
|
+
+ np.minimum(np.maximum(qx, qy), 0) - rr)
|
|
152
|
+
gy, gx = np.gradient(sdf)
|
|
153
|
+
n = np.maximum(np.hypot(gx, gy), 1e-6)
|
|
154
|
+
d = -sdf
|
|
155
|
+
alpha = np.clip((d + 0.6) / 1.2, 0.0, 1.0).astype(np.float32)
|
|
156
|
+
t = np.clip(1.0 - d / BOX_BAND, 0.0, 1.0)
|
|
157
|
+
mag = BOX_AMP * t * t
|
|
158
|
+
sx = np.rint(qx0 + xx + gx / n * mag).astype(np.int32)
|
|
159
|
+
sy = np.rint(qy0 + yy + gy / n * mag).astype(np.int32)
|
|
160
|
+
np.clip(sx, 0, w // LPF - 1, out=sx)
|
|
161
|
+
np.clip(sy, 0, h // LPF - 1, out=sy)
|
|
162
|
+
# 边缘→中心用 smoothstep 过渡,二值硬切会看到一圈割裂
|
|
163
|
+
weight = np.clip(d / BOX_BAND, 0.0, 1.0).astype(np.float32)
|
|
164
|
+
weight = (weight * weight * (3.0 - 2.0 * weight)).astype(np.float32)
|
|
165
|
+
out.append((qx0, qy0, qw, qh, alpha, weight, sx, sy))
|
|
166
|
+
return tuple(out)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _box_union(w: int, h: int, ow: int, oh: int, shapes: tuple[Box, ...]) -> Image.Image:
|
|
170
|
+
"""目标分辨率下的圆角联合遮罩,4x 超采样后缩小,边界平滑。"""
|
|
171
|
+
ss = 4
|
|
172
|
+
fx, fy = ow / w, oh / h
|
|
173
|
+
m = Image.new("L", (ow * ss, oh * ss), 0)
|
|
174
|
+
d = ImageDraw.Draw(m)
|
|
175
|
+
for x0, y0, x1, y1, r in shapes:
|
|
176
|
+
if x1 <= x0 or y1 <= y0:
|
|
177
|
+
continue
|
|
178
|
+
d.rounded_rectangle((x0 * fx * ss, y0 * fy * ss,
|
|
179
|
+
x1 * fx * ss, y1 * fy * ss),
|
|
180
|
+
radius=r * min(fx, fy) * ss, fill=255)
|
|
181
|
+
return m.resize((ow, oh), Image.Resampling.LANCZOS)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
@dataclass(frozen=True)
|
|
185
|
+
class Geometry:
|
|
186
|
+
"""一套尺寸下的全部贴图。不可变:改形状请用 `with_shapes()`。
|
|
187
|
+
|
|
188
|
+
`version` 每次变更 +1,GPU 侧靠它判断要不要重建坐标纹理
|
|
189
|
+
(对应旧代码里那个模块级的 `SHAPES_VERSION`)。
|
|
190
|
+
"""
|
|
191
|
+
|
|
192
|
+
width: int
|
|
193
|
+
height: int
|
|
194
|
+
version: int = 0
|
|
195
|
+
slabs: tuple[Box, ...] = ()
|
|
196
|
+
mask_hard: Image.Image | None = None
|
|
197
|
+
warp_x: np.ndarray | None = None
|
|
198
|
+
warp_y: np.ndarray | None = None
|
|
199
|
+
shapes: tuple[Box, ...] = ()
|
|
200
|
+
overlay: Image.Image | None = None
|
|
201
|
+
overlay_half: Image.Image | None = None
|
|
202
|
+
box_maps: tuple[tuple, ...] = ()
|
|
203
|
+
box_union_half: Image.Image | None = None
|
|
204
|
+
_mic_pos: tuple[int, int] = field(default=(0, 0), repr=False)
|
|
205
|
+
|
|
206
|
+
# ---- 构造 ----
|
|
207
|
+
|
|
208
|
+
@classmethod
|
|
209
|
+
def for_size(cls, w: int, h: int, version: int = 0) -> "Geometry":
|
|
210
|
+
shapes = shapes_for(w, h)
|
|
211
|
+
mask = _rounded_mask(w, h, slabs_for(w, h))
|
|
212
|
+
# color-key 透明不能用半透明边缘:半透明像素会和 KEY 混合成黑边,
|
|
213
|
+
# 所以遮罩二值硬切。
|
|
214
|
+
mask_hard = mask.point(lambda v: 255 if v >= 128 else 0)
|
|
215
|
+
warp_x, warp_y = _warp_maps(w, h)
|
|
216
|
+
base = cls(width=w, height=h, version=version,
|
|
217
|
+
slabs=slabs_for(w, h), mask_hard=mask_hard,
|
|
218
|
+
warp_x=warp_x, warp_y=warp_y,
|
|
219
|
+
_mic_pos=(MIC_MARGIN[0], h - MIC_MARGIN[1]))
|
|
220
|
+
return base.with_shapes(shapes, version=version)
|
|
221
|
+
|
|
222
|
+
def with_shapes(self, shapes: Iterable[Box], version: int | None = None) -> "Geometry":
|
|
223
|
+
shapes = tuple(shapes)
|
|
224
|
+
overlay = _overlay(self.width, self.height, shapes)
|
|
225
|
+
hw, hh = max(1, self.width // 2), max(1, self.height // 2)
|
|
226
|
+
return replace(
|
|
227
|
+
self,
|
|
228
|
+
version=(self.version + 1) if version is None else version,
|
|
229
|
+
shapes=shapes,
|
|
230
|
+
overlay=overlay,
|
|
231
|
+
overlay_half=overlay.resize((hw, hh), Image.Resampling.BILINEAR),
|
|
232
|
+
box_maps=_box_maps(shapes, self.width, self.height),
|
|
233
|
+
box_union_half=_box_union(self.width, self.height, hw, hh, shapes),
|
|
234
|
+
)
|
|
235
|
+
|
|
236
|
+
# ---- 派生量 ----
|
|
237
|
+
|
|
238
|
+
@property
|
|
239
|
+
def mic_pos(self) -> tuple[int, int]:
|
|
240
|
+
return self._mic_pos
|
|
241
|
+
|
|
242
|
+
@property
|
|
243
|
+
def input_shape(self) -> Box:
|
|
244
|
+
return self.shapes[MESSAGE_SLOTS]
|
|
245
|
+
|
|
246
|
+
@property
|
|
247
|
+
def send_shape(self) -> Box:
|
|
248
|
+
return self.shapes[MESSAGE_SLOTS + 1]
|
|
249
|
+
|
|
250
|
+
@property
|
|
251
|
+
def half_size(self) -> tuple[int, int]:
|
|
252
|
+
return max(1, self.width // 2), max(1, self.height // 2)
|
|
253
|
+
|
|
254
|
+
@property
|
|
255
|
+
def input_top(self) -> int:
|
|
256
|
+
return self.height - 80
|
|
257
|
+
|
|
258
|
+
def collapsed_frame(self, frame: Image.Image | None) -> Image.Image | None:
|
|
259
|
+
"""折叠态:只保留标题栏那一块玻璃,按 78px 重新贴圆角。
|
|
260
|
+
|
|
261
|
+
直接裁上一帧会让底部变成硬切边(遮罩还是整窗高度),看起来像渲染坏了。
|
|
262
|
+
"""
|
|
263
|
+
if frame is None:
|
|
264
|
+
return None
|
|
265
|
+
try:
|
|
266
|
+
w, h = self.width, self.height
|
|
267
|
+
crop = frame.convert("RGB").crop((0, 0, w, h))
|
|
268
|
+
mask = Image.new("L", (w, h), 0)
|
|
269
|
+
ImageDraw.Draw(mask).rounded_rectangle(
|
|
270
|
+
(SLAB_OUT, SLAB_OUT, w - SLAB_OUT, SLAB_OUT + SLAB_TOP_H),
|
|
271
|
+
radius=SLAB_RADIUS, fill=255)
|
|
272
|
+
out = Image.new("RGB", (w, h), KEY)
|
|
273
|
+
out.paste(crop, (0, 0), mask)
|
|
274
|
+
return out
|
|
275
|
+
except Exception:
|
|
276
|
+
return None
|
|
277
|
+
|
|
278
|
+
|
|
279
|
+
def collapsed_height() -> int:
|
|
280
|
+
return 2 * SLAB_OUT + SLAB_TOP_H
|
|
281
|
+
|
|
282
|
+
|
|
283
|
+
__all__ = [
|
|
284
|
+
"AMP", "BAND", "BAR_FRAC", "BAR_THICK", "BOX_AMP", "BOX_BAND", "Box",
|
|
285
|
+
"CORNER_R", "DEFAULT_H", "DEFAULT_W", "Geometry", "KEY", "LPF",
|
|
286
|
+
"MESSAGE_SLOTS", "MIC_MARGIN", "MIC_SIZE", "MIN_H", "MIN_W", "PAD",
|
|
287
|
+
"SLAB_RADIUS", "chat_layout", "collapsed_height", "shapes_for", "slabs_for",
|
|
288
|
+
]
|