Repository navigation
Expand file tree
/
Copy pathModel.js
More file actions
173 lines (160 loc) · 5.43 KB
/
Copy pathModel.js
File metadata and controls
173 lines (160 loc) · 5.43 KB
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
.pragma library
var DEFAULTS = {
inputDevice: "auto",
mode4k: true,
zoom: 30,
moveUp: 30,
moveRight: 100,
outputWidth: 1920,
outputHeight: 1080,
framerate: 30,
scaler: "bilinear",
decoder: "auto",
brightness: 0,
contrast: 0,
saturation: 0
}
function even(n) {
n = Math.floor(Number(n) || 0)
if (n % 2 !== 0) n -= 1
return n
}
function clamp(n, min, max) {
n = Number(n)
if (!isFinite(n)) n = min
if (n < min) return min
if (n > max) return max
return n
}
function baseSize(mode4k) {
return mode4k ? { w: 3840, h: 2160 } : { w: 1920, h: 1080 }
}
function mergeConfig(raw) {
var src = raw && typeof raw === "object" ? raw : {}
var out = {}
for (var key in DEFAULTS) out[key] = DEFAULTS[key]
if (src.inputDevice !== undefined && src.inputDevice !== null && String(src.inputDevice) !== "")
out.inputDevice = String(src.inputDevice)
if (src.mode4k !== undefined) out.mode4k = src.mode4k === true || src.mode4k === "true"
if (src.zoom !== undefined) out.zoom = Number(src.zoom)
if (src.moveUp !== undefined) out.moveUp = Number(src.moveUp)
if (src.moveRight !== undefined) out.moveRight = Number(src.moveRight)
if (src.outputWidth !== undefined) out.outputWidth = Number(src.outputWidth)
if (src.outputHeight !== undefined) out.outputHeight = Number(src.outputHeight)
if (src.framerate !== undefined) out.framerate = Number(src.framerate)
if (src.scaler !== undefined && String(src.scaler) !== "") out.scaler = String(src.scaler)
if (src.decoder !== undefined && src.decoder !== null && String(src.decoder) !== "")
out.decoder = String(src.decoder)
if (src.brightness !== undefined) out.brightness = Number(src.brightness)
if (src.contrast !== undefined) out.contrast = Number(src.contrast)
if (src.saturation !== undefined) out.saturation = Number(src.saturation)
return normalize(out)
}
function normalize(cfg) {
var base = baseSize(cfg.mode4k === true)
cfg.zoom = clamp(Math.round(Number(cfg.zoom) || 0), 0, 80)
cfg.outputWidth = even(clamp(cfg.outputWidth || 1920, 320, 3840))
cfg.outputHeight = even(clamp(cfg.outputHeight || 1080, 180, 2160))
cfg.framerate = clamp(Math.round(Number(cfg.framerate) || 30), 5, 60)
var decoder = String(cfg.decoder || "auto")
if (decoder !== "cpu" && decoder !== "cuda") decoder = "auto"
cfg.decoder = decoder
cfg.brightness = clamp(Math.round(Number(cfg.brightness) || 0), -100, 100)
cfg.contrast = clamp(Math.round(Number(cfg.contrast) || 0), -100, 100)
cfg.saturation = clamp(Math.round(Number(cfg.saturation) || 0), -100, 100)
var view = viewport(base.w, base.h, cfg.zoom, cfg.moveUp, cfg.moveRight)
cfg.moveRight = view.moveRight
cfg.moveUp = view.moveUp
return cfg
}
function viewport(baseW, baseH, zoom, moveUp, moveRight) {
var factor = 1 - (Number(zoom) || 0) / 100
if (factor < 0.2) factor = 0.2
if (factor > 1) factor = 1
var viewW = even(Math.max(2, Math.min(baseW, Math.floor(baseW * factor))))
var viewH = even(Math.max(2, Math.min(baseH, Math.floor(baseH * factor))))
var centerX = even(Math.floor((baseW - viewW) / 2))
var centerY = even(Math.floor((baseH - viewH) / 2))
var x = even((Number(moveRight) || 0) + centerX)
var y = even(centerY - (Number(moveUp) || 0))
if (x < 0) x = 0
if (y < 0) y = 0
if (x + viewW > baseW) x = even(baseW - viewW)
if (y + viewH > baseH) y = even(baseH - viewH)
if (x < 0) x = 0
if (y < 0) y = 0
return {
w: viewW,
h: viewH,
x: x,
y: y,
moveRight: x - centerX,
moveUp: centerY - y,
maxMoveRight: centerX,
maxMoveUp: centerY
}
}
function fromRect(baseW, baseH, x, y, w, h) {
w = even(clamp(w, 2, baseW))
h = even(Math.round(w * baseH / baseW))
if (h > baseH) {
h = even(baseH)
w = even(Math.round(h * baseW / baseH))
}
if (w < 2) w = 2
if (h < 2) h = 2
x = even(x)
y = even(y)
if (x < 0) x = 0
if (y < 0) y = 0
if (x + w > baseW) x = even(baseW - w)
if (y + h > baseH) y = even(baseH - h)
var zoom = Math.round((1 - (w / baseW)) * 100)
if (zoom < 0) zoom = 0
if (zoom > 80) zoom = 80
var centerX = Math.floor((baseW - w) / 2)
var centerY = Math.floor((baseH - h) / 2)
return normalize({
mode4k: baseW >= 3000,
zoom: zoom,
moveRight: x - centerX,
moveUp: centerY - y
})
}
function parseJson(text, fallback) {
try {
var parsed = JSON.parse(String(text || ""))
if (parsed && typeof parsed === "object") return parsed
} catch (e) {
}
return fallback
}
function previewRect(stageW, stageH, imageW, imageH) {
if (stageW <= 0 || stageH <= 0 || imageW <= 0 || imageH <= 0)
return { x: 0, y: 0, w: 0, h: 0 }
var scale = Math.min(stageW / imageW, stageH / imageH)
var w = imageW * scale
var h = imageH * scale
return { x: (stageW - w) / 2, y: (stageH - h) / 2, w: w, h: h }
}
function sourceToPreview(px, py, paint, sourceW, sourceH) {
if (!paint || paint.w <= 0 || paint.h <= 0 || sourceW <= 0 || sourceH <= 0)
return { x: 0, y: 0 }
return {
x: paint.x + (px / sourceW) * paint.w,
y: paint.y + (py / sourceH) * paint.h
}
}
function previewToSource(px, py, paint, sourceW, sourceH) {
if (!paint || paint.w <= 0 || paint.h <= 0 || sourceW <= 0 || sourceH <= 0)
return { x: 0, y: 0 }
return {
x: ((px - paint.x) / paint.w) * sourceW,
y: ((py - paint.y) / paint.h) * sourceH
}
}
function elide(text, max) {
var value = String(text || "").replace(/\s+/g, " ").trim()
if (value.length <= max) return value
return value.substring(0, Math.max(0, max - 1)) + "…"
}