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
// 计算摄影增强(v12.15.0)
// 低光增强、去运动模糊、色彩增强、自适应直方图均衡化
///|
/// 低光增强配置
pub(all) struct LowLightConfig {
method_type : String // "gamma" / "log" / "retinex"
gamma : Double // Gamma 校正参数
strength : Double // 增强强度
}
///|
/// 创建默认低光增强配置
pub fn low_light_default_config() -> LowLightConfig {
LowLightConfig::{ method_type: "gamma", gamma: 0.5, strength: 1.0 }
}
///|
/// Gamma 校正低光增强
fn gamma_enhance(img : @types.Image, gamma : Double) -> @types.Image {
let w = img.width
let h = img.height
let channels = img.channels
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
// 预计算 Gamma 查找表
let lut : Array[Byte] = []
for i in 0..<256 {
let normalized = i.to_double() / 255.0
let enhanced = @math.pow(normalized, gamma)
let val = (enhanced * 255.0).to_int().min(255).max(0)
lut.push(val.to_byte())
}
for i in 0..<(w * h * channels) {
out_data[i] = lut[img.data[i].to_int()]
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 对数变换低光增强
fn log_enhance(img : @types.Image, strength : Double) -> @types.Image {
let w = img.width
let h = img.height
let channels = img.channels
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
let c = 255.0 / @math.ln(1.0 + 255.0)
for i in 0..<(w * h * channels) {
let val = img.data[i].to_double()
let enhanced = c * @math.ln(1.0 + val) * strength
out_data[i] = enhanced.min(255.0).max(0.0).to_int().to_byte()
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 简化版 Retinex 低光增强(单尺度 Retinex)
fn retinex_enhance(img : @types.Image, sigma? : Double = 50.0) -> @types.Image {
let _ = sigma
let w = img.width
let h = img.height
let channels = img.channels
// 简化版:使用高斯模糊作为光照估计
// 这里用简单的均值模糊代替(避免依赖外部高斯模糊)
let kernel_size = 15
let half = kernel_size / 2
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
for c in 0..<channels {
// 计算局部均值(光照估计)
let mut sum = 0.0
let mut count = 0
for ky in -half..<=half {
for kx in -half..<=half {
let ny = y + ky
let nx = x + kx
if ny >= 0 && ny < h && nx >= 0 && nx < w {
let nidx = (ny * w + nx) * channels
sum += img.data[nidx + c].to_double()
count += 1
}
}
}
let illumination = if count > 0 { sum / count.to_double() } else { 0.0 }
// Retinex: R = log(I) - log(L) = log(I/L)
let original = img.data[idx + c].to_double() + 1.0
let light = illumination + 1.0
let ratio = original / light
let reflectance = @math.ln(ratio)
// 映射回 0-255
let enhanced = (reflectance + 3.0) / 6.0 * 255.0
out_data[idx + c] = enhanced.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 low_light_enhance(
img : @types.Image,
config? : LowLightConfig,
) -> @types.Image {
let cfg = match config {
Some(v) => v
None => low_light_default_config()
}
match cfg.method_type {
"gamma" => gamma_enhance(img, cfg.gamma)
"log" => log_enhance(img, cfg.strength)
"retinex" => retinex_enhance(img, sigma=50.0)
_ => gamma_enhance(img, cfg.gamma)
}
}
///|
/// 去运动模糊(简化版维纳滤波)
pub fn deblur_motion(
img : @types.Image,
kernel_size? : Int = 15,
angle? : Double = 0.0,
) -> @types.Image {
let ks = kernel_size
let _ = angle
let w = img.width
let h = img.height
let channels = img.channels
// 简化版:使用非锐化掩模(Unsharp Mask)作为去模糊近似
let amount = 1.5
let threshold = 0
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
let blur_radius = ks / 2
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
for c in 0..<channels {
// 计算局部均值(模糊版本)
let mut sum = 0.0
let mut count = 0
for ky in -blur_radius..<=blur_radius {
for kx in -blur_radius..<=blur_radius {
let ny = y + ky
let nx = x + kx
if ny >= 0 && ny < h && nx >= 0 && nx < w {
let nidx = (ny * w + nx) * channels
sum += img.data[nidx + c].to_double()
count += 1
}
}
}
let blurred = if count > 0 { sum / count.to_double() } else { 0.0 }
let original = img.data[idx + c].to_double()
// 非锐化掩模:sharpened = original + amount * (original - blurred)
let diff = original - blurred
let sharpened = if diff.abs() > threshold.to_double() {
original + amount * diff
} else {
original
}
out_data[idx + c] = sharpened.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 color_enhance(
img : @types.Image,
saturation? : Double = 1.3,
contrast? : Double = 1.1,
) -> @types.Image {
let sat = saturation
let con = contrast
let w = img.width
let h = img.height
let channels = img.channels
if channels < 3 {
return img
}
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
let r = img.data[idx].to_double()
let g = img.data[idx + 1].to_double()
let b = img.data[idx + 2].to_double()
// 计算灰度值
let gray = 0.299 * r + 0.587 * g + 0.114 * b
// 饱和度增强
let new_r = gray + (r - gray) * sat
let new_g = gray + (g - gray) * sat
let new_b = gray + (b - gray) * sat
// 对比度增强
let final_r = (new_r - 128.0) * con + 128.0
let final_g = (new_g - 128.0) * con + 128.0
let final_b = (new_b - 128.0) * con + 128.0
out_data[idx] = final_r.min(255.0).max(0.0).to_int().to_byte()
out_data[idx + 1] = final_g.min(255.0).max(0.0).to_int().to_byte()
out_data[idx + 2] = final_b.min(255.0).max(0.0).to_int().to_byte()
// 复制其他通道
for c in 3..<channels {
out_data[idx + c] = img.data[idx + c]
}
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 自适应直方图均衡化(CLAHE 简化版)
pub fn clahe_enhance(
img : @types.Image,
clip_limit? : Double = 2.0,
tile_size? : Int = 8,
) -> @types.Image {
let clip = clip_limit
let _ = tile_size
let w = img.width
let h = img.height
let channels = img.channels
// 简化版:全局直方图均衡化
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for c in 0..<channels {
// 计算直方图
let histogram : Array[Int] = []
for _ in 0..<256 {
histogram.push(0)
}
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
histogram[img.data[idx + c].to_int()] += 1
}
}
// 裁剪直方图
let total_pixels = (w * h).to_double()
let max_count = (clip * total_pixels / 256.0).to_int()
let mut excess = 0
for i in 0..<256 {
if histogram[i] > max_count {
excess += histogram[i] - max_count
histogram[i] = max_count
}
}
// 重新分配超额像素
let redistribute = excess / 256
for i in 0..<256 {
histogram[i] += redistribute
}
// 计算累积分布函数(CDF)
let cdf : Array[Int] = []
let mut cumsum = 0
for i in 0..<256 {
cumsum += histogram[i]
cdf.push(cumsum)
}
// 查找表
let lut : Array[Byte] = []
let cdf_min = cdf[0]
let denominator = (w * h - cdf_min).max(1)
for i in 0..<256 {
let val = ((cdf[i] - cdf_min).to_double() /
denominator.to_double() *
255.0)
.to_int()
.min(255)
.max(0)
lut.push(val.to_byte())
}
// 应用查找表
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
out_data[idx + c] = lut[img.data[idx + c].to_int()]
}
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 白平衡校正(灰度世界算法)
pub fn white_balance(img : @types.Image) -> @types.Image {
let w = img.width
let h = img.height
let channels = img.channels
if channels < 3 {
return img
}
// 计算各通道平均值
let mut sum_r = 0.0
let mut sum_g = 0.0
let mut sum_b = 0.0
let total = (w * h).to_double()
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
sum_r += img.data[idx].to_double()
sum_g += img.data[idx + 1].to_double()
sum_b += img.data[idx + 2].to_double()
}
}
let avg_r = sum_r / total
let avg_g = sum_g / total
let avg_b = sum_b / total
let avg_gray = (avg_r + avg_g + avg_b) / 3.0
// 计算增益
let gain_r = avg_gray / avg_r.max(1.0)
let gain_g = avg_gray / avg_g.max(1.0)
let gain_b = avg_gray / avg_b.max(1.0)
// 应用增益
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
for y in 0..<h {
for x in 0..<w {
let idx = (y * w + x) * channels
out_data[idx] = (img.data[idx].to_double() * gain_r)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
out_data[idx + 1] = (img.data[idx + 1].to_double() * gain_g)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
out_data[idx + 2] = (img.data[idx + 2].to_double() * gain_b)
.min(255.0)
.max(0.0)
.to_int()
.to_byte()
for c in 3..<channels {
out_data[idx + c] = img.data[idx + c]
}
}
}
@types.Image::{
width: w,
height: h,
channels,
data: Bytes::from_array(out_data),
}
}
///|
/// 批量计算摄影增强
pub fn batch_photo_enhance(
images : Array[@types.Image],
method_type : String,
) -> Array[@types.Image] {
let result : Array[@types.Image] = []
for img in images {
let enhanced = match method_type {
"low_light" => low_light_enhance(img)
"deblur" => deblur_motion(img, kernel_size=15, angle=0.0)
"color" => color_enhance(img, saturation=1.3, contrast=1.1)
"clahe" => clahe_enhance(img, clip_limit=2.0, tile_size=8)
"white_balance" => white_balance(img)
_ => img
}
result.push(enhanced)
}
result
}