image.vision\spatiotemporal\st_snapshot.mbt
89.7% · 35 / 39 points · 99 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 时空快照 spatiotemporal/st_snapshot - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 时序快照:取指定时间帧、截取子片段、帧抽取。 - 7
// Temporal snapshot: frame at time, subclip, frame extraction. - 8
- 9
///| - 10
/// 取指定时间的最近帧(不插值)。 - 11
/// 获取给定时间的最近帧(无插值) Get the nearest frame at the given time (no interpolation). - 12
pub fn st_snapshot(timage : TImage, time : Float) -> @types.Image { - 13●
if timage.frames.length() == 1 { - 14●
return timage.frames[0].image - 15
} - 16
let mut acc = 0.0F - 17●
for i in 0..<timage.frames.length() { - 18●
let frame = timage.frames[i] - 19
let frame_end = acc + frame.duration - 20●
if time < frame_end || i == timage.frames.length() - 1 { - 21●
return frame.image - 22
} - 23
acc = frame_end - 24
} - 25○
timage.frames[timage.frames.length() - 1].image - 26
} - 27
- 28
///| - 29
/// 截取子片段:从 start 到 end 时间区间的帧组成新 TImage。 - 30
/// 从起始到结束时间提取子片段 Extract a subclip from start to end time. - 31
pub fn st_subclip( - 32
timage : TImage, - 33
start : Float, - 34
end : Float, - 35
) -> TImage raise @types.LoadError { - 36●
if start >= end { - 37●
raise @types.LoadError::DecodeFailed("st_subclip: start 必须小于 end") - 38
} - 39
let mut acc = 0.0F - 40
let selected : Array[TFrame] = [] - 41●
for i in 0..<timage.frames.length() { - 42●
let frame = timage.frames[i] - 43
let frame_end = acc + frame.duration - 44
// 帧在 [start, end) 范围内 - 45
if frame_end > start && acc < end { - 46●
let new_ts = if acc > start { acc - start } else { 0.0F } - 47●
let frame_start_in_clip = if acc > start { acc } else { start } - 48●
let frame_end_in_clip = if frame_end < end { frame_end } else { end } - 49
let new_dur = frame_end_in_clip - frame_start_in_clip - 50●
let new_frame = TFrame::make(frame.image, new_ts, new_dur) - 51●
selected.push(new_frame) - 52
} - 53
acc = frame_end - 54
} - 55●
if selected.length() == 0 { - 56○
raise @types.LoadError::DecodeFailed("st_subclip: 时间范围内无帧") - 57
} - 58●
TImage::make(selected, timage.fps) - 59
} - 60
- 61
///| - 62
/// 抽取指定索引的帧。 - 63
/// 按索引提取特定帧 Extract a specific frame by index. - 64
pub fn st_extract_frame( - 65
timage : TImage, - 66
index : Int32, - 67
) -> @types.Image raise @types.LoadError { - 68●
if index < 0 || index >= timage.frames.length() { - 69●
raise @types.LoadError::DecodeFailed("st_extract_frame: 索引越界") - 70
} - 71
timage.frames[index].image - 72
} - 73
- 74
///| - 75
/// 从 GIF 动画创建时变图像。 - 76
/// 从 GIF 动画创建时变图像 Create a time-varying image from a GIF - 77
pub fn st_from_gif_animation( - 78
gif : @types.GifAnimation, - 79
) -> TImage raise @types.LoadError { - 80●
if gif.frames.length() == 0 { - 81○
raise @types.LoadError::DecodeFailed("st_from_gif_animation: GIF 无帧") - 82
} - 83
let frames : Array[TFrame] = [] - 84
let mut ts = 0.0F - 85●
for i in 0..<gif.frames.length() { - 86●
let dur = Float::from_int(gif.delays[i]) / 1000.0F - 87●
let frame = TFrame::make(gif.frames[i], ts, dur) - 88●
frames.push(frame) - 89
ts = ts + dur - 90
} - 91
// 推断 fps - 92
let total = ts - 93
let fps = if total > 0.0F { - 94●
Float::from_int(gif.frames.length()) / total - 95
} else { - 96○
1.0F - 97
} - 98●
TImage::make(frames, fps) - 99
}