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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
// HDR 高动态范围(v11.7.0)
// 多曝光融合 + 色调映射(Reinhard / Gamma / Sigmoid)
///|
/// HDR 配置
pub(all) struct HdrConfig {
fusion_method : String // "average" / "weighted" / "mertens"
tone_mapping : String // "reinhard" / "gamma" / "sigmoid"
gamma : Double // Gamma 校正值
reinhard_key : Double // Reinhard 色调映射关键值
sigmoid_strength : Double // Sigmoid 强度
}
///|
/// 创建默认 HDR 配置
pub fn hdr_default_config() -> HdrConfig {
HdrConfig::{
fusion_method: "weighted",
tone_mapping: "reinhard",
gamma: 1.0 / 2.2,
reinhard_key: 0.18,
sigmoid_strength: 5.0,
}
}
///|
/// 多曝光融合(平均法)
/// 将多张不同曝光的图像取平均
pub fn hdr_fusion_average(
images : Array[@types.Image],
) -> @types.Image raise @types.LoadError {
// 输入验证
if images.length() == 0 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_average: 图像数组不能为空",
)
}
let w = images[0].width
let h = images[0].height
let channels = images[0].channels
// 验证第一张图像
if w <= 0 || h <= 0 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_average: 图像尺寸必须大于0",
)
}
if channels < 1 || channels > 4 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_average: 通道数必须在1-4之间",
)
}
// 验证所有图像尺寸一致且数据大小正确
let expected_size = w * h * channels
for img_idx in 0..<images.length() {
let img = images[img_idx]
if img.width != w || img.height != h || img.channels != channels {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_average: 第 " +
img_idx.to_string() +
" 张图像尺寸/通道不一致",
)
}
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_average: 第 " +
img_idx.to_string() +
" 张图像数据大小不匹配",
)
}
}
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
let n = images.length().to_double()
for i in 0..<(w * h) {
for c in 0..<channels {
let mut sum = 0.0
for img in images {
sum += img.data[i * channels + c].to_double()
}
let avg = sum / n
out_data[i * channels + c] = avg.min(255.0).max(0.0).to_int().to_byte()
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 多曝光融合(加权法)
/// 根据图像亮度计算权重,亮部和暗部权重低,中间调权重高
pub fn hdr_fusion_weighted(
images : Array[@types.Image],
) -> @types.Image raise @types.LoadError {
// 输入验证
if images.length() == 0 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_weighted: 图像数组不能为空",
)
}
let w = images[0].width
let h = images[0].height
let channels = images[0].channels
// 验证第一张图像
if w <= 0 || h <= 0 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_weighted: 图像尺寸必须大于0",
)
}
if channels < 1 || channels > 4 {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_weighted: 通道数必须在1-4之间",
)
}
// 验证所有图像尺寸一致且数据大小正确
let expected_size = w * h * channels
for img_idx in 0..<images.length() {
let img = images[img_idx]
if img.width != w || img.height != h || img.channels != channels {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_weighted: 第 " +
img_idx.to_string() +
" 张图像尺寸/通道不一致",
)
}
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"hdr_fusion_weighted: 第 " +
img_idx.to_string() +
" 张图像数据大小不匹配",
)
}
}
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
// 计算每张图像的权重(基于亮度,中间调权重高)
let weights : Array[Array[Double]] = []
for img in images {
let img_weight : Array[Double] = []
for i in 0..<(w * h) {
let idx = i * channels
let r = img.data[idx].to_double()
let g = if channels >= 2 { img.data[idx + 1].to_double() } else { r }
let b = if channels >= 3 { img.data[idx + 2].to_double() } else { r }
let luminance = (0.299 * r + 0.587 * g + 0.114 * b) / 255.0
// 高斯权重:在 0.5 处最大,向两端递减
let weight = @math.exp(-(luminance - 0.5) * (luminance - 0.5) / 0.08)
img_weight.push(weight)
}
weights.push(img_weight)
}
// 加权融合
for i in 0..<(w * h) {
let mut weight_sum = 0.0
for img_idx in 0..<images.length() {
weight_sum += weights[img_idx][i]
}
for c in 0..<channels {
if weight_sum > 0.0 {
let mut sum = 0.0
for img_idx in 0..<images.length() {
let weight = weights[img_idx][i] / weight_sum
sum += images[img_idx].data[i * channels + c].to_double() * weight
}
out_data[i * channels + c] = sum.min(255.0).max(0.0).to_int().to_byte()
} else {
out_data[i * channels + c] = images[0].data[i * channels + c]
}
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// Reinhard 色调映射
/// L_ mapped = L / (1 + L) (全局色调映射)
pub fn tone_mapping_reinhard(
img : @types.Image,
key? : Double = 0.18,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_reinhard: 图像尺寸必须大于0",
)
}
if img.channels < 1 || img.channels > 4 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_reinhard: 通道数必须在1-4之间",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"tone_mapping_reinhard: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if key <= 0.0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_reinhard: key必须大于0",
)
}
let k = key
let w = img.width
let h = img.height
let channels = img.channels
let data = img.data
// 计算对数平均亮度
let mut log_sum = 0.0
let delta = 0.0001
for i in 0..<(w * h) {
let idx = i * channels
let r = data[idx].to_double() / 255.0
let g = if channels >= 2 { data[idx + 1].to_double() / 255.0 } else { r }
let b = if channels >= 3 { data[idx + 2].to_double() / 255.0 } else { r }
let luminance = 0.299 * r + 0.587 * g + 0.114 * b
log_sum += @math.ln(luminance + delta)
}
let log_avg = @math.exp(log_sum / (w * h).to_double())
// 应用 Reinhard 色调映射
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for i in 0..<(w * h) {
let idx = i * channels
for c in 0..<channels {
let val = data[idx + c].to_double() / 255.0
// 缩放
let scaled = val * k / (log_avg + delta)
// Reinhard 映射
let mapped = scaled / (1.0 + scaled)
out_data[idx + c] = (mapped * 255.0)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// Gamma 校正色调映射
/// out = in ^ gamma
pub fn tone_mapping_gamma(
img : @types.Image,
gamma? : Double = 1.0 / 2.2,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_gamma: 图像尺寸必须大于0",
)
}
if img.channels < 1 || img.channels > 4 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_gamma: 通道数必须在1-4之间",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"tone_mapping_gamma: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if gamma <= 0.0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_gamma: gamma必须大于0",
)
}
let g = gamma
let w = img.width
let h = img.height
let channels = img.channels
let data = img.data
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for i in 0..<(w * h) {
let idx = i * channels
for c in 0..<channels {
let val = data[idx + c].to_double() / 255.0
let mapped = @math.pow(val, g)
out_data[idx + c] = (mapped * 255.0)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// Sigmoid 色调映射
/// out = 1 / (1 + exp(-strength * (in - 0.5)))
pub fn tone_mapping_sigmoid(
img : @types.Image,
strength? : Double = 5.0,
) -> @types.Image raise @types.LoadError {
// 输入验证
if img.width <= 0 || img.height <= 0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_sigmoid: 图像尺寸必须大于0",
)
}
if img.channels < 1 || img.channels > 4 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_sigmoid: 通道数必须在1-4之间",
)
}
let expected_size = img.width * img.height * img.channels
if img.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"tone_mapping_sigmoid: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
img.data.length().to_string(),
)
}
if strength <= 0.0 {
raise @types.LoadError::DecodeFailed(
"tone_mapping_sigmoid: strength必须大于0",
)
}
let s = strength
let w = img.width
let h = img.height
let channels = img.channels
let data = img.data
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for i in 0..<(w * h) {
let idx = i * channels
for c in 0..<channels {
let val = data[idx + c].to_double() / 255.0
let mapped = 1.0 / (1.0 + @math.exp(-s * (val - 0.5)))
out_data[idx + c] = (mapped * 255.0)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 完整 HDR 流水线(多曝光融合 + 色调映射)
pub fn hdr_process(
images : Array[@types.Image],
config? : HdrConfig,
) -> @types.Image raise @types.LoadError {
// 输入验证
if images.length() == 0 {
raise @types.LoadError::DecodeFailed(
"hdr_process: 图像数组不能为空",
)
}
let cfg = match config {
Some(c) => c
None => hdr_default_config()
}
// 配置验证
if cfg.gamma <= 0.0 {
raise @types.LoadError::DecodeFailed("hdr_process: gamma必须大于0")
}
if cfg.reinhard_key <= 0.0 {
raise @types.LoadError::DecodeFailed(
"hdr_process: reinhard_key必须大于0",
)
}
if cfg.sigmoid_strength <= 0.0 {
raise @types.LoadError::DecodeFailed(
"hdr_process: sigmoid_strength必须大于0",
)
}
// 1. 多曝光融合
let fused = match cfg.fusion_method {
"average" => hdr_fusion_average(images)
_ => hdr_fusion_weighted(images)
}
// 2. 色调映射
let tonemapped = match cfg.tone_mapping {
"gamma" => tone_mapping_gamma(fused, gamma=cfg.gamma)
"sigmoid" => tone_mapping_sigmoid(fused, strength=cfg.sigmoid_strength)
_ => tone_mapping_reinhard(fused, key=cfg.reinhard_key)
}
tonemapped
}