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
// 色调映射(纯 MoonBit):Reinhard + Gamma
// 将 HDR float 图像 (ImageF) 映射到 LDR 8-bit 图像 (Image)
///|
fn read_float_le(data : Bytes, offset : Int) -> Float {
let b0 = data[offset].to_int()
let b1 = data[offset + 1].to_int()
let b2 = data[offset + 2].to_int()
let b3 = data[offset + 3].to_int()
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
Float::reinterpret_from_int(bits)
}
///|
fn log_f(x : Float) -> Float {
Float::from_double(@math.log2(x.to_double()) * 0.6931471805599453)
}
///|
/// Reinhard 全局色调映射。
/// key 控制整体亮度(默认 0.18),值越大越亮。
/// 公式:L_out = L_scaled / (1 + L_scaled),其中 L_scaled = key * L / L_avg。
pub fn reinhard_tonemap(
imgf : @types.ImageF,
key? : Float = 0.18F,
) -> @types.Image raise @types.LoadError {
// 输入验证
if imgf.width <= 0 || imgf.height <= 0 {
raise @types.LoadError::DecodeFailed(
"reinhard_tonemap: 图像尺寸必须大于0",
)
}
if imgf.channels < 1 || imgf.channels > 4 {
raise @types.LoadError::DecodeFailed(
"reinhard_tonemap: 通道数必须在1-4之间",
)
}
let expected_size = imgf.width * imgf.height * imgf.channels * 4
if imgf.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"reinhard_tonemap: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
imgf.data.length().to_string(),
)
}
if key <= 0.0F {
raise @types.LoadError::DecodeFailed("reinhard_tonemap: key必须大于0")
}
let w = imgf.width
let h = imgf.height
let ch = imgf.channels
let n = w * h
let color_ch = if ch >= 3 { 3 } else { ch }
// 计算对数平均亮度
let mut log_sum = 0.0F
for i in 0..<n {
let r = read_float_le(imgf.data, i * ch * 4)
let g = read_float_le(imgf.data, i * ch * 4 + 4)
let b = read_float_le(imgf.data, i * ch * 4 + 8)
let lum = 0.2126F * r + 0.7152F * g + 0.0722F * b
let lum_safe = if lum > 0.0F { lum } else { 0.0001F }
log_sum += log_f(lum_safe)
}
let log_avg = @math.expf(log_sum / Float::from_int(n))
let scale = key / log_avg
// 应用 Reinhard 映射
let out : Array[Byte] = Array::make(n * ch, b'\x00')
for i in 0..<n {
for c in 0..<color_ch {
let v = read_float_le(imgf.data, (i * ch + c) * 4)
let scaled = v * scale
let mapped = scaled / (1.0F + scaled)
let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255)
out[i * ch + c] = val.to_byte()
}
if ch == 4 {
out[i * ch + 3] = b'\xFF'
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}
///|
/// Gamma 色调映射。
/// 先归一化到 [0,1],再应用 gamma 校正:L_out = L^(1/gamma)。
/// gamma > 1 提亮暗部,gamma < 1 压暗。
pub fn gamma_tonemap(
imgf : @types.ImageF,
gamma : Float,
) -> @types.Image raise @types.LoadError {
// 输入验证
if imgf.width <= 0 || imgf.height <= 0 {
raise @types.LoadError::DecodeFailed(
"gamma_tonemap: 图像尺寸必须大于0",
)
}
if imgf.channels < 1 || imgf.channels > 4 {
raise @types.LoadError::DecodeFailed(
"gamma_tonemap: 通道数必须在1-4之间",
)
}
let expected_size = imgf.width * imgf.height * imgf.channels * 4
if imgf.data.length() != expected_size {
raise @types.LoadError::DecodeFailed(
"gamma_tonemap: 数据大小不匹配,期望 " +
expected_size.to_string() +
",实际 " +
imgf.data.length().to_string(),
)
}
if gamma <= 0.0F {
raise @types.LoadError::DecodeFailed("gamma_tonemap: gamma必须大于0")
}
let w = imgf.width
let h = imgf.height
let ch = imgf.channels
let n = w * h
let color_ch = if ch >= 3 { 3 } else { ch }
// 找最大值用于归一化
let mut max_val = 0.0F
for i in 0..<n {
for c in 0..<color_ch {
let v = read_float_le(imgf.data, (i * ch + c) * 4)
if v > max_val {
max_val = v
}
}
}
if max_val <= 0.0F {
return @types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(Array::make(n * ch, b'\x00')),
}
}
let inv_gamma = 1.0F / gamma
let out : Array[Byte] = Array::make(n * ch, b'\x00')
for i in 0..<n {
for c in 0..<color_ch {
let v = read_float_le(imgf.data, (i * ch + c) * 4)
let normalized = v / max_val
let mapped = if normalized > 0.0F {
@math.powf(normalized, inv_gamma)
} else {
0.0F
}
let val = (mapped * 255.0F + 0.5F).to_int().clamp(min=0, max=255)
out[i * ch + c] = val.to_byte()
}
if ch == 4 {
out[i * ch + 3] = b'\xFF'
}
}
@types.Image::{
width: w,
height: h,
channels: ch,
data: Bytes::from_array(out),
}
}