image.vision\video\timelapse.mbt
77.5% · 55 / 71 points · 250 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 延时摄影 video/timelapse - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 时间 lapse — 采样/稳定/曝光 - 7
// Timelapse: sampling / stabilization / exposure - 8
// - 9
// Phase 17 v398: 视频处理大扩展 — 时间 lapse。 - 10
- 11
///| - 12
/// 曝光调整模式。 - 13
/// 曝光调整模式 Exposure adjustment mode. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum ExposureMode { - 18
Linear // 线性 - 19
Logarithmic // 对数 - 20
Gamma // 伽马校正 - 21
} - 22
- 23
///| - 24
pub extend ExposureMode with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend ExposureMode with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 时间 lapse 配置。 - 31
/// 延时摄影配置 Timelapse configuration. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct TimelapseConfig { - 34
sample_interval : Int32 // 采样间隔(每 N 帧取 1 帧) - 35
exposure_mode : ExposureMode - 36
exposure_compensation : Double // 曝光补偿 (-2.0 ~ +2.0) - 37
gamma : Double // 伽马值 (仅 Gamma 模式) - 38
stabilize : Bool // 是否启用稳定 - 39
} - 40
- 41
///| - 42
pub extend TimelapseConfig with @debug.Debug::{to_repr} - 43
- 44
///| - 45
pub extend TimelapseConfig with Eq::{not_equal, equal} - 46
- 47
///| - 48
/// 默认时间 lapse 配置。 - 49
/// 默认延时摄影配置 Default timelapse configuration. - 50
pub fn default_timelapse_config() -> TimelapseConfig { - 51●
{ - 52
sample_interval: 10, - 53
exposure_mode: Linear, - 54
exposure_compensation: 0.0, - 55
gamma: 1.0, - 56
stabilize: false, - 57
} - 58
} - 59
- 60
///| - 61
/// 按间隔采样帧。 - 62
/// 按间隔采样帧 Sample frames at interval. - 63
pub fn sample_frames( - 64
frames : Array[@types.Image], - 65
interval : Int32, - 66
) -> Array[@types.Image] { - 67●
if interval <= 0 { - 68●
return frames - 69
} - 70
let result : Array[@types.Image] = [] - 71●
let n = frames.length() - 72
let mut i = 0 - 73
while i < n { - 74●
result.push(frames[i]) - 75
i = i + interval - 76
} - 77
result - 78
} - 79
- 80
///| - 81
/// 线性曝光调整。 - 82
/// 线性曝光调整 Linear exposure adjustment. - 83
fn adjust_exposure_linear( - 84
img : @types.Image, - 85
compensation : Double, - 86
) -> @types.Image { - 87●
let w = img.width - 88
let h = img.height - 89
let ch = img.channels - 90
let data : Array[Byte] = [] - 91
// compensation: -2.0 ~ +2.0, 映射到 0.25x ~ 4x - 92●
let factor = @math.exp(compensation * 0.6931471805599453) - 93
for i in 0..<(w * h * ch) { - 94●
let val = img.data[i].to_int().to_double() * factor - 95●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 96
} - 97●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 98
} - 99
- 100
///| - 101
/// 对数曝光调整。 - 102
/// 对数曝光 Logarithmic exposure - 103
fn adjust_exposure_log( - 104
img : @types.Image, - 105
compensation : Double, - 106
) -> @types.Image { - 107○
let w = img.width - 108
let h = img.height - 109
let ch = img.channels - 110
let data : Array[Byte] = [] - 111
let shift = compensation * 50.0 - 112
for i in 0..<(w * h * ch) { - 113○
let val = img.data[i].to_int().to_double() - 114
// 对数映射: output = log(1 + val) * scale + shift - 115○
let log_val = @math.log10(1.0 + val) * 100.0 + shift - 116○
data.push(log_val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 117
} - 118○
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 119
} - 120
- 121
///| - 122
/// 伽马曝光调整。 - 123
/// 伽马曝光调整 Gamma exposure adjustment. - 124
fn adjust_exposure_gamma(img : @types.Image, gamma : Double) -> @types.Image { - 125●
let w = img.width - 126
let h = img.height - 127
let ch = img.channels - 128
let data : Array[Byte] = [] - 129◐
let g = if gamma > 0.0 { gamma } else { 1.0 } - 130
for i in 0..<(w * h * ch) { - 131●
let val = img.data[i].to_int().to_double() / 255.0 - 132●
let corrected = @math.pow(val, 1.0 / g) * 255.0 - 133●
data.push(corrected.clamp(min=0.0, max=255.0).to_int().to_byte()) - 134
} - 135●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 136
} - 137
- 138
///| - 139
/// 调整曝光。 - 140
/// 调整曝光 Adjust exposure. - 141
pub fn adjust_exposure( - 142
img : @types.Image, - 143
config : TimelapseConfig, - 144
) -> @types.Image { - 145●
match config.exposure_mode { - 146●
Linear => adjust_exposure_linear(img, config.exposure_compensation) - 147○
Logarithmic => adjust_exposure_log(img, config.exposure_compensation) - 148
Gamma => - 149●
adjust_exposure_gamma( - 150●
adjust_exposure_linear(img, config.exposure_compensation), - 151
config.gamma, - 152
) - 153
} - 154
} - 155
- 156
///| - 157
/// 曝光调整序列。 - 158
/// 调整序列曝光 Adjust exposure for sequence. - 159
pub fn adjust_exposure_sequence( - 160
frames : Array[@types.Image], - 161
config : TimelapseConfig, - 162
) -> Array[@types.Image] { - 163●
let result : Array[@types.Image] = [] - 164
for f in frames { - 165●
result.push(adjust_exposure(f, config)) - 166
} - 167
result - 168
} - 169
- 170
///| - 171
/// 创建时间 lapse。 - 172
/// 从帧创建延时摄影 Create timelapse from frames. - 173
pub fn create_timelapse( - 174
frames : Array[@types.Image], - 175
config : TimelapseConfig, - 176
) -> Array[@types.Image] { - 177
// 1. 采样 - 178●
let sampled = sample_frames(frames, config.sample_interval) - 179
// 2. 曝光调整 - 180●
let exposed = adjust_exposure_sequence(sampled, config) - 181
// 3. 稳定(可选) - 182
if config.stabilize { - 183○
let stab_config = default_stabilization_config() - 184○
let result = stabilize_video_sequence(exposed, stab_config) - 185
result.frames - 186
} else { - 187●
exposed - 188
} - 189
} - 190
- 191
///| - 192
/// 时间 lapse 流水线(采样 + 曝光 + 稳定 + 运动模糊)。 - 193
/// 延时摄影管线 Timelapse pipeline. - 194
pub fn timelapse_pipeline( - 195
frames : Array[@types.Image], - 196
config : TimelapseConfig, - 197
motion_blur_amount : Double, - 198
) -> Array[@types.Image] { - 199●
let timelapse = create_timelapse(frames, config) - 200
if motion_blur_amount > 0.0 { - 201●
let result : Array[@types.Image] = [] - 202
for frame in timelapse { - 203●
result.push(apply_motion_blur(frame, motion_blur_amount, 0)) - 204
} - 205
result - 206
} else { - 207●
timelapse - 208
} - 209
} - 210
- 211
///| - 212
/// 计算时间 lapse 帧数。 - 213
/// 计算延时摄影帧数 Compute timelapse frame count. - 214
pub fn compute_timelapse_count( - 215
frame_count : Int32, - 216
sample_interval : Int32, - 217
) -> Int32 { - 218●
if frame_count <= 0 || sample_interval <= 0 { - 219●
return 0 - 220
} - 221
(frame_count + sample_interval - 1) / sample_interval - 222
} - 223
- 224
///| - 225
/// 计算时间 lapse 时长。 - 226
/// 计算延时摄影时长 Compute timelapse duration. - 227
pub fn compute_timelapse_duration( - 228
frame_count : Int32, - 229
sample_interval : Int32, - 230
target_fps : Int32, - 231
) -> Double { - 232●
let tl_count = compute_timelapse_count(frame_count, sample_interval) - 233
if target_fps <= 0 { - 234○
return 0.0 - 235
} - 236●
tl_count.to_double() / target_fps.to_double() - 237
} - 238
- 239
///| - 240
/// 计算时间压缩比。 - 241
/// 计算时间压缩比 Compute time compression ratio. - 242
pub fn compute_time_compression( - 243
source_duration : Double, - 244
timelapse_duration : Double, - 245
) -> Double { - 246●
if timelapse_duration <= 0.0 { - 247●
return 0.0 - 248
} - 249
source_duration / timelapse_duration - 250
}