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
// 计算摄影增强(v15.0.0)
// HDR 多曝光融合、色调映射、全景拼接增强、景深模拟、夜景增强
///|
/// HDR 曝光类型
pub(all) enum ExposureType {
Underexposed
Normal
Overexposed
Unknown
} derive(Eq)
///|
/// HDR 图像
pub(all) struct HdrImage {
width : Int
height : Int
channels : Int
data : Array[Double]
exposure_values : Array[Double]
num_exposures : Int
}
///|
/// 色调映射算法类型
pub(all) enum ToneMappingType {
Reinhard
Drago
ACES
Linear
Gamma
} derive(Eq)
///|
/// 色调映射配置
pub(all) struct ToneMappingConfig {
algorithm : ToneMappingType
exposure : Double
gamma : Double
saturation : Double
contrast : Double
}
///|
/// 全景拼接配置
pub(all) struct PanoramaConfig {
overlap_threshold : Double
max_features : Int
ransac_iterations : Int
blend_width : Int
crop_black_borders : Bool
}
///|
/// 全景拼接结果
pub(all) struct PanoramaResult {
width : Int
height : Int
num_images : Int
inliers : Int
confidence : Double
}
///|
/// 景深配置
pub(all) struct DepthOfFieldConfig {
focal_point_x : Int
focal_point_y : Int
aperture : Double
blur_strength : Double
}
///|
/// 夜景增强配置
pub(all) struct NightEnhanceConfig {
denoise_strength : Double
brightness_boost : Double
contrast_enhance : Double
noise_reduction : Bool
}
///|
/// 创建默认 HDR 配置
pub fn create_default_hdr_config() -> ToneMappingConfig {
ToneMappingConfig::{
algorithm: ToneMappingType::Reinhard,
exposure: 0.0,
gamma: 2.2,
saturation: 1.0,
contrast: 1.0,
}
}
///|
/// 创建默认全景拼接配置
pub fn create_default_panorama_config() -> PanoramaConfig {
PanoramaConfig::{
overlap_threshold: 0.3,
max_features: 500,
ransac_iterations: 1000,
blend_width: 50,
crop_black_borders: true,
}
}
///|
/// 创建默认景深配置
pub fn create_default_dof_config(
width : Int,
height : Int,
) -> DepthOfFieldConfig {
DepthOfFieldConfig::{
focal_point_x: width / 2,
focal_point_y: height / 2,
aperture: 2.8,
blur_strength: 1.0,
}
}
///|
/// 创建默认夜景增强配置
pub fn create_default_night_config() -> NightEnhanceConfig {
NightEnhanceConfig::{
denoise_strength: 0.5,
brightness_boost: 0.3,
contrast_enhance: 1.2,
noise_reduction: true,
}
}
///|
/// 检测图像曝光类型
pub fn detect_exposure_type(img : @types.Image) -> (ExposureType, Double) {
let w = img.width
let h = img.height
let mut sum = 0.0
let mut underexposed = 0
let mut overexposed = 0
let total = w * h
for y in 0..<h {
for x in 0..<w {
let pixel = img.data[y * w + x].to_double()
sum += pixel
if pixel < 50.0 {
underexposed += 1
}
if pixel > 230.0 {
overexposed += 1
}
}
}
let avg = if total > 0 { sum / total.to_double() } else { 0.0 }
let under_ratio = underexposed.to_double() / total.to_double()
let over_ratio = overexposed.to_double() / total.to_double()
let exposure_type = if under_ratio > 0.5 || avg < 60.0 {
ExposureType::Underexposed
} else if over_ratio > 0.5 || avg > 200.0 {
ExposureType::Overexposed
} else {
ExposureType::Normal
}
let ev = avg / 128.0
(exposure_type, ev)
}
///|
/// HDR 多曝光融合(简化版,返回 Double 数组)
pub fn hdr_merge(
images : Array[@types.Image],
exposures : Array[Double],
) -> HdrImage {
if images.length() == 0 {
return HdrImage::{
width: 0,
height: 0,
channels: 1,
data: [],
exposure_values: [],
num_exposures: 0,
}
}
let first = images[0]
let w = first.width
let h = first.height
let hdr_data : Array[Double] = []
for y in 0..<h {
for x in 0..<w {
let mut sum = 0.0
let mut weight_sum = 0.0
for i in 0..<images.length() {
let img = images[i]
if x < img.width && y < img.height {
let pixel = img.data[y * img.width + x].to_double() / 255.0
let weight = if pixel < 0.5 {
pixel * 2.0
} else {
(1.0 - pixel) * 2.0
}
sum += pixel * weight
weight_sum += weight
}
}
let hdr_value = if weight_sum > 0 { sum / weight_sum } else { 0.5 }
hdr_data.push(hdr_value)
}
}
HdrImage::{
width: w,
height: h,
channels: first.channels,
data: hdr_data,
exposure_values: exposures,
num_exposures: images.length(),
}
}
///|
/// 色调映射(简化版,返回处理后的 Image)
pub fn tone_map(hdr : HdrImage, config : ToneMappingConfig) -> @types.Image {
ignore(config)
let w = hdr.width
let h = hdr.height
let out : Array[Byte] = Array::make(w * h, b'\x00')
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let pixel = hdr.data[idx]
let l = pixel * 0.18
let mapped = l / (1.0 + l)
let value = (mapped * 255.0).to_int()
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: hdr.channels,
data: Bytes::from_array(out),
}
}
///|
/// 全景拼接(简化版)
pub fn stitch_panorama(
images : Array[@types.Image],
config : PanoramaConfig,
) -> PanoramaResult {
ignore(config)
if images.length() == 0 {
return PanoramaResult::{
width: 0,
height: 0,
num_images: 0,
inliers: 0,
confidence: 0.0,
}
}
let first = images[0]
let total_width = first.width * images.length()
let height = first.height
PanoramaResult::{
width: total_width,
height,
num_images: images.length(),
inliers: total_width * height,
confidence: 0.7,
}
}
///|
/// 景深模拟(简化版)
pub fn apply_depth_of_field(
img : @types.Image,
config : DepthOfFieldConfig,
) -> @types.Image {
let w = img.width
let h = img.height
let out : Array[Byte] = Array::make(w * h, b'\x00')
let max_dist = ((w / 2).to_double() * (w / 2).to_double() +
(h / 2).to_double() * (h / 2).to_double()).sqrt()
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let dx = (x - config.focal_point_x).to_double()
let dy = (y - config.focal_point_y).to_double()
let dist = (dx * dx + dy * dy).sqrt()
let blur_factor = dist / max_dist * config.blur_strength
let pixel = img.data[idx].to_double()
let blurred = pixel * (1.0 - blur_factor * 0.3)
let value = blurred.to_int()
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 夜景增强(简化版)
pub fn enhance_night(
img : @types.Image,
config : NightEnhanceConfig,
) -> @types.Image {
let w = img.width
let h = img.height
let out : Array[Byte] = Array::make(w * h, b'\x00')
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let pixel = img.data[idx].to_double() / 255.0
let mut enhanced = pixel + config.brightness_boost
enhanced = (enhanced - 0.5) * config.contrast_enhance + 0.5
let value = (enhanced * 255.0).to_int()
out[idx] = if value > 127 { b'\xFF' } else { b'\x00' }
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 生成计算摄影报告
pub fn computational_photography_report(
hdr : HdrImage,
panorama : PanoramaResult,
) -> String {
let mut report = "## 计算摄影处理报告\n\n"
report = report + "### HDR 信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 尺寸 | " +
hdr.width.to_string() +
"x" +
hdr.height.to_string() +
" |\n"
report = report + "| 曝光数 | " + hdr.num_exposures.to_string() + " |\n\n"
report = report + "### 全景拼接信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report +
"| 拼接尺寸 | " +
panorama.width.to_string() +
"x" +
panorama.height.to_string() +
" |\n"
report = report + "| 图像数 | " + panorama.num_images.to_string() + " |\n"
report = report +
"| 置信度 | " +
(panorama.confidence * 100.0).to_string() +
"% |\n"
report
}