image.vision\spatiotemporal\primitives.mbt

95.5% · 21 / 22 points · 174 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/primitives
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 时空原语:TInterval / TFrame / TImage / TVideo。
  7. 7// Spatiotemporal primitives: time interval, temporal frame, time-varying image, video.
  8. 8//
  9. 9// 核心思想(GeoVerse):时间不是属性标签,是原生第 3 维。
  10. 10// 静态图像 = 单帧 TImage;动画/视频 = 多帧 TImage。
  11. 11
  12. 12///|
  13. 13/// 时间区间 [start, end),单位秒。
  14. 14/// 时间区间 [start Time interval [start
  15. 15#derive(Eq, @debug.Debug)
  16. 16pub(all) struct TInterval {
  17. 17 start : Float
  18. 18 end : Float
  19. 19}
  20. 20
  21. 21///|
  22. 22pub extend TInterval with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25pub extend TInterval with Eq::{not_equal, equal}
  26. 26
  27. 27///|
  28. 28/// 创建时间区间,要求 end >= start。
  29. 29/// 创建时间区间;要求 end >= start Create a time interval; requires end >= start.
  30. 30pub fn TInterval::make(
  31. 31 start : Float,
  32. 32 end : Float,
  33. 33) -> TInterval raise @types.LoadError {
  34. 34● if end < start {
  35. 35● raise @types.LoadError::DecodeFailed(
  36. 36 "TInterval::make: end 必须大于等于 start",
  37. 37 )
  38. 38 }
  39. 39 { start, end, }
  40. 40}
  41. 41
  42. 42///|
  43. 43/// 时间区间长度。
  44. 44/// 时间区间时长 Duration of the time interval.
  45. 45pub fn TInterval::duration(self : TInterval) -> Float {
  46. 46● self.end - self.start
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 时空帧:单帧图像 + 时间戳 + 持续时间。
  51. 51/// 时间帧:带时间戳和时长的单帧图像 Temporal frame: a single image with timestamp and duration.
  52. 52#derive(Eq, @debug.Debug)
  53. 53pub(all) struct TFrame {
  54. 54 image : @types.Image
  55. 55 timestamp : Float
  56. 56 duration : Float
  57. 57}
  58. 58
  59. 59///|
  60. 60pub extend TFrame with @debug.Debug::{to_repr}
  61. 61
  62. 62///|
  63. 63pub extend TFrame with Eq::{not_equal, equal}
  64. 64
  65. 65///|
  66. 66/// 创建时空帧。
  67. 67/// 创建时间帧 Create a temporal frame.
  68. 68pub fn TFrame::make(
  69. 69 image : @types.Image,
  70. 70 timestamp : Float,
  71. 71 duration : Float,
  72. 72) -> TFrame raise @types.LoadError {
  73. 73● if duration < 0.0F {
  74. 74● raise @types.LoadError::DecodeFailed("TFrame::make: duration 必须非负")
  75. 75 }
  76. 76 { image, timestamp, duration, }
  77. 77}
  78. 78
  79. 79///|
  80. 80/// 时变图像:多帧序列 + 维度 + 帧率。
  81. 81/// 时变图像:带尺寸和 fps 的多帧序列 Time-varying image: multi-frame sequence with dimensions and fps.
  82. 82#derive(Eq, @debug.Debug)
  83. 83pub(all) struct TImage {
  84. 84 frames : Array[TFrame]
  85. 85 width : Int32
  86. 86 height : Int32
  87. 87 channels : Int32
  88. 88 fps : Float
  89. 89}
  90. 90
  91. 91///|
  92. 92pub extend TImage with @debug.Debug::{to_repr}
  93. 93
  94. 94///|
  95. 95pub extend TImage with Eq::{not_equal, equal}
  96. 96
  97. 97///|
  98. 98/// 创建时变图像,所有帧尺寸必须一致。
  99. 99/// 创建时变图像;所有帧须有相同尺寸 Create a time-varying image; all frames must have the same dimensions.
  100. 100pub fn TImage::make(
  101. 101 frames : Array[TFrame],
  102. 102 fps : Float,
  103. 103) -> TImage raise @types.LoadError {
  104. 104● if frames.length() == 0 {
  105. 105● raise @types.LoadError::DecodeFailed("TImage::make: 帧序列不能为空")
  106. 106 }
  107. 107 if fps <= 0.0F {
  108. 108● raise @types.LoadError::DecodeFailed("TImage::make: fps 必须大于0")
  109. 109 }
  110. 110 let first = frames[0]
  111. 111 let w = first.image.width
  112. 112 let h = first.image.height
  113. 113 let ch = first.image.channels
  114. 114● for i in 1..<frames.length() {
  115. 115● let f = frames[i]
  116. 116 if f.image.width != w || f.image.height != h || f.image.channels != ch {
  117. 117○ raise @types.LoadError::DecodeFailed(
  118. 118 "TImage::make: 所有帧尺寸和通道数必须一致",
  119. 119 )
  120. 120 }
  121. 121 }
  122. 122 { frames, width: w, height: h, channels: ch, fps, }
  123. 123}
  124. 124
  125. 125///|
  126. 126/// 时变图像的帧数。
  127. 127/// 时变图像帧数 Number of frames in the time-varying image.
  128. 128pub fn TImage::frame_count(self : TImage) -> Int32 {
  129. 129● self.frames.length()
  130. 130}
  131. 131
  132. 132///|
  133. 133/// 时变图像总时长(秒)。
  134. 134/// 时变图像总时长(秒) Total duration of the time-varying image (seconds).
  135. 135pub fn TImage::total_duration(self : TImage) -> Float {
  136. 136● let mut sum = 0.0F
  137. 137● for i in 0..<self.frames.length() {
  138. 138● sum = sum + self.frames[i].duration
  139. 139 }
  140. 140 sum
  141. 141}
  142. 142
  143. 143///|
  144. 144/// 从单帧静态图像创建时变图像。
  145. 145/// 创建时变图像;所有帧须有相同尺寸 Create a time-varying image from a single static image frame.
  146. 146pub fn TImage::from_static(
  147. 147 image : @types.Image,
  148. 148) -> TImage raise @types.LoadError {
  149. 149● let frame = TFrame::make(image, 0.0F, 0.0F)
  150. 150● TImage::make([frame], 1.0F)
  151. 151}
  152. 152
  153. 153///|
  154. 154/// 时空视频:时变图像 + 元数据。
  155. 155/// 时空视频:带元数据的时变图像 Spatiotemporal video: time-varying image with metadata.
  156. 156#derive(Eq, @debug.Debug)
  157. 157pub(all) struct TVideo {
  158. 158 timage : TImage
  159. 159 codec_name : String
  160. 160 total_duration : Float
  161. 161}
  162. 162
  163. 163///|
  164. 164pub extend TVideo with @debug.Debug::{to_repr}
  165. 165
  166. 166///|
  167. 167pub extend TVideo with Eq::{not_equal, equal}
  168. 168
  169. 169///|
  170. 170/// 从时变图像创建视频。
  171. 171/// 从时变图像创建视频 Create a video from a time-varying image.
  172. 172pub fn TVideo::from_timage(timage : TImage, codec_name : String) -> TVideo {
  173. 173● { timage, codec_name, total_duration: timage.total_duration(), }
  174. 174}