image.vision\spatiotemporal\st_snapshot.mbt

89.7% · 35 / 39 points · 99 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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