image.vision\video\timelapse.mbt

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