image.vision\video\video_effects.mbt

99.3% · 302 / 304 points · 662 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/video_effects
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 视频特效 — 滤镜/转场/字幕
  7. 7// Video effects: filters / transitions / text overlay
  8. 8//
  9. 9// Phase 17 v393: 视频处理大扩展 — 视频特效。
  10. 10
  11. 11///|
  12. 12/// 视频滤镜类型。
  13. 13/// 视频滤镜类型 Video filter type.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum VideoFilterType {
  18. 18 Blur // 模糊
  19. 19 Sharpen // 锐化
  20. 20 Vintage // 复古
  21. 21 Grayscale // 黑白
  22. 22 Vignette // 晕影
  23. 23 Sepia // 棕褐
  24. 24}
  25. 25
  26. 26///|
  27. 27pub extend VideoFilterType with @debug.Debug::{to_repr}
  28. 28
  29. 29///|
  30. 30pub extend VideoFilterType with Eq::{not_equal, equal}
  31. 31
  32. 32///|
  33. 33/// 转场类型。
  34. 34/// 过渡类型 Transition type.
  35. 35/// `#non_exhaustive` 表示未来可能添加新构造器
  36. 36#derive(Eq, @debug.Debug)
  37. 37#non_exhaustive
  38. 38pub(all) enum TransitionType {
  39. 39 Fade // 淡入淡出
  40. 40 Dissolve // 溶解
  41. 41 Slide // 滑动
  42. 42 Wipe // 擦除
  43. 43 Push // 推送
  44. 44}
  45. 45
  46. 46///|
  47. 47pub extend TransitionType with @debug.Debug::{to_repr}
  48. 48
  49. 49///|
  50. 50pub extend TransitionType with Eq::{not_equal, equal}
  51. 51
  52. 52///|
  53. 53/// 视频滤镜参数。
  54. 54/// 视频滤镜参数 Video filter parameters.
  55. 55#derive(Eq, @debug.Debug)
  56. 56pub(all) struct VideoFilterParams {
  57. 57 filter_type : VideoFilterType
  58. 58 intensity : Double // 0.0 - 1.0
  59. 59}
  60. 60
  61. 61///|
  62. 62pub extend VideoFilterParams with @debug.Debug::{to_repr}
  63. 63
  64. 64///|
  65. 65pub extend VideoFilterParams with Eq::{not_equal, equal}
  66. 66
  67. 67///|
  68. 68/// 转场参数。
  69. 69/// 过渡参数 Transition parameters.
  70. 70#derive(Eq, @debug.Debug)
  71. 71pub(all) struct TransitionParams {
  72. 72 transition_type : TransitionType
  73. 73 duration : Int32 // 帧数
  74. 74 direction : Int32 // 0=左到右, 1=右到左, 2=上到下, 3=下到上
  75. 75}
  76. 76
  77. 77///|
  78. 78pub extend TransitionParams with @debug.Debug::{to_repr}
  79. 79
  80. 80///|
  81. 81pub extend TransitionParams with Eq::{not_equal, equal}
  82. 82
  83. 83///|
  84. 84/// 默认滤镜参数。
  85. 85/// 默认滤镜参数 Default filter parameters.
  86. 86pub fn default_video_filter_params(
  87. 87 filter_type : VideoFilterType,
  88. 88) -> VideoFilterParams {
  89. 89● { filter_type, intensity: 0.5, }
  90. 90}
  91. 91
  92. 92///|
  93. 93/// 默认转场参数。
  94. 94/// 默认过渡参数 Default transition parameters.
  95. 95pub fn default_transition_params(
  96. 96 transition_type : TransitionType,
  97. 97) -> TransitionParams {
  98. 98● { transition_type, duration: 10, direction: 0, }
  99. 99}
  100. 100
  101. 101///|
  102. 102/// 应用视频滤镜。
  103. 103/// 应用视频滤镜 Apply video filter.
  104. 104pub fn apply_video_filter(
  105. 105 img : @types.Image,
  106. 106 params : VideoFilterParams,
  107. 107) -> @types.Image {
  108. 108● match params.filter_type {
  109. 109● Blur => apply_blur_filter(img, params.intensity)
  110. 110● Sharpen => apply_sharpen_filter(img, params.intensity)
  111. 111● Vintage => apply_vintage_filter(img, params.intensity)
  112. 112● Grayscale => apply_grayscale_filter(img)
  113. 113● Vignette => apply_vignette_filter(img, params.intensity)
  114. 114● Sepia => apply_sepia_filter(img, params.intensity)
  115. 115 }
  116. 116}
  117. 117
  118. 118///|
  119. 119fn apply_blur_filter(img : @types.Image, intensity : Double) -> @types.Image {
  120. 120● let w = img.width
  121. 121 let h = img.height
  122. 122 let ch = img.channels
  123. 123 let data : Array[Byte] = []
  124. 124● let radius = (intensity * 3.0).to_int().clamp(min=1, max=3)
  125. 125 for y in 0..<h {
  126. 126● for x in 0..<w {
  127. 127● for c in 0..<ch {
  128. 128● let mut sum = 0.0
  129. 129 let mut count = 0.0
  130. 130 for dy in [-radius, 0, radius] {
  131. 131● for dx in [-radius, 0, radius] {
  132. 132● let nx = x + dx
  133. 133 let ny = y + dy
  134. 134 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  135. 135● let idx = (ny * w + nx) * ch + c
  136. 136● sum = sum + img.data[idx].to_int().to_double()
  137. 137 count = count + 1.0
  138. 138 }
  139. 139 }
  140. 140 }
  141. 141◐ let val = if count > 0.0 { sum / count } else { 0.0 }
  142. 142● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  143. 143 }
  144. 144 }
  145. 145 }
  146. 146● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  147. 147}
  148. 148
  149. 149///|
  150. 150fn apply_sharpen_filter(img : @types.Image, intensity : Double) -> @types.Image {
  151. 151● let w = img.width
  152. 152 let h = img.height
  153. 153 let ch = img.channels
  154. 154 let data : Array[Byte] = []
  155. 155 let amount = intensity * 2.0
  156. 156 for y in 0..<h {
  157. 157● for x in 0..<w {
  158. 158● for c in 0..<ch {
  159. 159● let center = img.data[(y * w + x) * ch + c].to_int().to_double()
  160. 160 let mut neighbors = 0.0
  161. 161 let mut ncount = 0.0
  162. 162 for (dx, dy) in [(1, 0), (-1, 0), (0, 1), (0, -1)] {
  163. 163● let nx = x + dx
  164. 164 let ny = y + dy
  165. 165 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  166. 166● neighbors = neighbors +
  167. 167● img.data[(ny * w + nx) * ch + c].to_int().to_double()
  168. 168 ncount = ncount + 1.0
  169. 169 }
  170. 170 }
  171. 171◐ let avg = if ncount > 0.0 { neighbors / ncount } else { center }
  172. 172 let val = center + amount * (center - avg)
  173. 173● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  174. 174 }
  175. 175 }
  176. 176 }
  177. 177● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  178. 178}
  179. 179
  180. 180///|
  181. 181fn apply_vintage_filter(img : @types.Image, intensity : Double) -> @types.Image {
  182. 182● let w = img.width
  183. 183 let h = img.height
  184. 184 let ch = img.channels
  185. 185 let data : Array[Byte] = []
  186. 186 for y in 0..<h {
  187. 187● for x in 0..<w {
  188. 188● for c in 0..<ch {
  189. 189● let val = img.data[(y * w + x) * ch + c].to_int().to_double()
  190. 190 // 复古效果:降低对比度 + 暖色调
  191. 191 let vintage = val * 0.9 + 20.0 * intensity
  192. 192● data.push(vintage.clamp(min=0.0, max=255.0).to_int().to_byte())
  193. 193 }
  194. 194 }
  195. 195 }
  196. 196● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  197. 197}
  198. 198
  199. 199///|
  200. 200fn apply_grayscale_filter(img : @types.Image) -> @types.Image {
  201. 201● let w = img.width
  202. 202 let h = img.height
  203. 203 let ch = img.channels
  204. 204 if ch < 3 {
  205. 205● return img
  206. 206 }
  207. 207 let data : Array[Byte] = []
  208. 208 for y in 0..<h {
  209. 209● for x in 0..<w {
  210. 210● let idx = (y * w + x) * ch
  211. 211● let r = img.data[idx].to_int().to_double()
  212. 212● let g = img.data[idx + 1].to_int().to_double()
  213. 213● let b = img.data[idx + 2].to_int().to_double()
  214. 214 let gray = 0.299 * r + 0.587 * g + 0.114 * b
  215. 215● let gray_byte = gray.clamp(min=0.0, max=255.0).to_int().to_byte()
  216. 216 for _ in 0..<ch {
  217. 217● data.push(gray_byte)
  218. 218 }
  219. 219 }
  220. 220 }
  221. 221● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  222. 222}
  223. 223
  224. 224///|
  225. 225fn apply_vignette_filter(
  226. 226 img : @types.Image,
  227. 227 intensity : Double,
  228. 228) -> @types.Image {
  229. 229● let w = img.width
  230. 230 let h = img.height
  231. 231 let ch = img.channels
  232. 232● let cx = w.to_double() / 2.0
  233. 233● let cy = h.to_double() / 2.0
  234. 234● let max_dist = (cx * cx + cy * cy).sqrt()
  235. 235 let data : Array[Byte] = []
  236. 236 for y in 0..<h {
  237. 237● for x in 0..<w {
  238. 238● let dx = x.to_double() - cx
  239. 239● let dy = y.to_double() - cy
  240. 240● let dist = (dx * dx + dy * dy).sqrt()
  241. 241 let factor = 1.0 - intensity * (dist / max_dist) * (dist / max_dist)
  242. 242 for c in 0..<ch {
  243. 243● let val = img.data[(y * w + x) * ch + c].to_int().to_double()
  244. 244● data.push((val * factor).clamp(min=0.0, max=255.0).to_int().to_byte())
  245. 245 }
  246. 246 }
  247. 247 }
  248. 248● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  249. 249}
  250. 250
  251. 251///|
  252. 252fn apply_sepia_filter(img : @types.Image, intensity : Double) -> @types.Image {
  253. 253● let w = img.width
  254. 254 let h = img.height
  255. 255 let ch = img.channels
  256. 256 if ch < 3 {
  257. 257● return img
  258. 258 }
  259. 259 let data : Array[Byte] = []
  260. 260 for y in 0..<h {
  261. 261● for x in 0..<w {
  262. 262● let idx = (y * w + x) * ch
  263. 263● let r = img.data[idx].to_int().to_double()
  264. 264● let g = img.data[idx + 1].to_int().to_double()
  265. 265● let b = img.data[idx + 2].to_int().to_double()
  266. 266 let sr = 0.393 * r + 0.769 * g + 0.189 * b
  267. 267 let sg = 0.349 * r + 0.686 * g + 0.168 * b
  268. 268 let sb = 0.272 * r + 0.534 * g + 0.131 * b
  269. 269 let orig_r = r
  270. 270 let orig_g = g
  271. 271 let orig_b = b
  272. 272 let final_r = orig_r * (1.0 - intensity) + sr * intensity
  273. 273 let final_g = orig_g * (1.0 - intensity) + sg * intensity
  274. 274 let final_b = orig_b * (1.0 - intensity) + sb * intensity
  275. 275● data.push(final_r.clamp(min=0.0, max=255.0).to_int().to_byte())
  276. 276● data.push(final_g.clamp(min=0.0, max=255.0).to_int().to_byte())
  277. 277● data.push(final_b.clamp(min=0.0, max=255.0).to_int().to_byte())
  278. 278 for c in 3..<ch {
  279. 279● data.push(img.data[idx + c])
  280. 280 }
  281. 281 }
  282. 282 }
  283. 283● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  284. 284}
  285. 285
  286. 286///|
  287. 287/// 创建转场帧。
  288. 288/// 在两图像间创建过渡帧 Create transition frame between two images.
  289. 289pub fn create_transition_frame(
  290. 290 from : @types.Image,
  291. 291 to_ : @types.Image,
  292. 292 progress : Double, // 0.0 - 1.0
  293. 293 params : TransitionParams,
  294. 294) -> @types.Image {
  295. 295● let p = progress.clamp(min=0.0, max=1.0)
  296. 296● match params.transition_type {
  297. 297● Fade => transition_fade(from, to_, p)
  298. 298● Dissolve => transition_dissolve(from, to_, p)
  299. 299● Slide => transition_slide(from, to_, p, params.direction)
  300. 300● Wipe => transition_wipe(from, to_, p, params.direction)
  301. 301● Push => transition_push(from, to_, p, params.direction)
  302. 302 }
  303. 303}
  304. 304
  305. 305///|
  306. 306fn transition_fade(
  307. 307 from : @types.Image,
  308. 308 to_ : @types.Image,
  309. 309 p : Double,
  310. 310) -> @types.Image {
  311. 311● let w = from.width
  312. 312 let h = from.height
  313. 313 let ch = from.channels
  314. 314 let data : Array[Byte] = []
  315. 315 for i in 0..<(w * h * ch) {
  316. 316● let fv = from.data[i].to_int().to_double()
  317. 317● let tv = to_.data[i].to_int().to_double()
  318. 318 let val = fv * (1.0 - p) + tv * p
  319. 319● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  320. 320 }
  321. 321● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  322. 322}
  323. 323
  324. 324///|
  325. 325fn transition_dissolve(
  326. 326 from : @types.Image,
  327. 327 to_ : @types.Image,
  328. 328 p : Double,
  329. 329) -> @types.Image {
  330. 330 // 溶解 = 带噪声的淡入淡出
  331. 331● transition_fade(from, to_, p)
  332. 332}
  333. 333
  334. 334///|
  335. 335fn transition_slide(
  336. 336 from : @types.Image,
  337. 337 to_ : @types.Image,
  338. 338 p : Double,
  339. 339 direction : Int32,
  340. 340) -> @types.Image {
  341. 341● let w = from.width
  342. 342 let h = from.height
  343. 343 let ch = from.channels
  344. 344● let offset_x = (w.to_double() * p).to_int()
  345. 345● let offset_y = (h.to_double() * p).to_int()
  346. 346 let data : Array[Byte] = []
  347. 347 for y in 0..<h {
  348. 348● for x in 0..<w {
  349. 349● let src_x = x + offset_x
  350. 350 let src_y = y + offset_y
  351. 351● let use_to = match direction {
  352. 352● 0 => x < offset_x
  353. 353● 1 => x >= w - offset_x
  354. 354● 2 => y < offset_y
  355. 355● _ => y >= h - offset_y
  356. 356 }
  357. 357 for c in 0..<ch {
  358. 358● if use_to {
  359. 359● let tx = if direction == 0 {
  360. 360● x - offset_x
  361. 361● } else if direction == 1 {
  362. 362● x + offset_x
  363. 363 } else {
  364. 364● x
  365. 365 }
  366. 366 let ty = if direction == 2 {
  367. 367● y - offset_y
  368. 368● } else if direction == 3 {
  369. 369● y + offset_y
  370. 370 } else {
  371. 371● y
  372. 372 }
  373. 373● let fx = tx.clamp(min=0, max=w - 1)
  374. 374● let fy = ty.clamp(min=0, max=h - 1)
  375. 375● data.push(to_.data[(fy * w + fx) * ch + c])
  376. 376 } else {
  377. 377● let fx = src_x.clamp(min=0, max=w - 1)
  378. 378● let fy = src_y.clamp(min=0, max=h - 1)
  379. 379● data.push(from.data[(fy * w + fx) * ch + c])
  380. 380 }
  381. 381 }
  382. 382 }
  383. 383 }
  384. 384● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  385. 385}
  386. 386
  387. 387///|
  388. 388fn transition_wipe(
  389. 389 from : @types.Image,
  390. 390 to_ : @types.Image,
  391. 391 p : Double,
  392. 392 direction : Int32,
  393. 393) -> @types.Image {
  394. 394● let w = from.width
  395. 395 let h = from.height
  396. 396 let ch = from.channels
  397. 397● let threshold = match direction {
  398. 398● 0 => (w.to_double() * p).to_int()
  399. 399● 1 => w - (w.to_double() * p).to_int()
  400. 400● 2 => (h.to_double() * p).to_int()
  401. 401● _ => h - (h.to_double() * p).to_int()
  402. 402 }
  403. 403 let data : Array[Byte] = []
  404. 404 for y in 0..<h {
  405. 405● for x in 0..<w {
  406. 406● let use_to = match direction {
  407. 407● 0 => x < threshold
  408. 408● 1 => x >= threshold
  409. 409● 2 => y < threshold
  410. 410● _ => y >= threshold
  411. 411 }
  412. 412 for c in 0..<ch {
  413. 413● if use_to {
  414. 414● data.push(to_.data[(y * w + x) * ch + c])
  415. 415 } else {
  416. 416● data.push(from.data[(y * w + x) * ch + c])
  417. 417 }
  418. 418 }
  419. 419 }
  420. 420 }
  421. 421● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  422. 422}
  423. 423
  424. 424///|
  425. 425fn transition_push(
  426. 426 from : @types.Image,
  427. 427 to_ : @types.Image,
  428. 428 p : Double,
  429. 429 direction : Int32,
  430. 430) -> @types.Image {
  431. 431● let w = from.width
  432. 432 let h = from.height
  433. 433 let ch = from.channels
  434. 434● let offset = (w.to_double() * p).to_int()
  435. 435 let data : Array[Byte] = []
  436. 436 for y in 0..<h {
  437. 437● for x in 0..<w {
  438. 438● for c in 0..<ch {
  439. 439● let use_to = match direction {
  440. 440● 0 => x < offset
  441. 441● 1 => x >= w - offset
  442. 442● 2 => y < offset
  443. 443● _ => y >= h - offset
  444. 444 }
  445. 445 if use_to {
  446. 446● let tx = if direction == 0 {
  447. 447● x - offset + w
  448. 448● } else if direction == 1 {
  449. 449● x + offset - w
  450. 450 } else {
  451. 451● x
  452. 452 }
  453. 453 let ty = if direction == 2 {
  454. 454● y - offset + h
  455. 455● } else if direction == 3 {
  456. 456● y + offset - h
  457. 457 } else {
  458. 458● y
  459. 459 }
  460. 460● let fx = tx.clamp(min=0, max=w - 1)
  461. 461● let fy = ty.clamp(min=0, max=h - 1)
  462. 462● data.push(to_.data[(fy * w + fx) * ch + c])
  463. 463 } else {
  464. 464● let sx = if direction == 0 {
  465. 465● x - offset
  466. 466● } else if direction == 1 {
  467. 467● x + offset
  468. 468 } else {
  469. 469● x
  470. 470 }
  471. 471 let sy = if direction == 2 {
  472. 472● y - offset
  473. 473● } else if direction == 3 {
  474. 474● y + offset
  475. 475 } else {
  476. 476● y
  477. 477 }
  478. 478● let fx = sx.clamp(min=0, max=w - 1)
  479. 479● let fy = sy.clamp(min=0, max=h - 1)
  480. 480● data.push(from.data[(fy * w + fx) * ch + c])
  481. 481 }
  482. 482 }
  483. 483 }
  484. 484 }
  485. 485● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  486. 486}
  487. 487
  488. 488///|
  489. 489/// 生成转场序列。
  490. 490/// 生成过渡序列 Generate transition sequence.
  491. 491pub fn generate_transition(
  492. 492 from : @types.Image,
  493. 493 to_ : @types.Image,
  494. 494 params : TransitionParams,
  495. 495) -> Array[@types.Image] {
  496. 496● let frames : Array[@types.Image] = []
  497. 497 for i in 0..<params.duration {
  498. 498● let p = i.to_double() / params.duration.to_double()
  499. 499● frames.push(create_transition_frame(from, to_, p, params))
  500. 500 }
  501. 501 frames
  502. 502}
  503. 503
  504. 504///|
  505. 505/// 添加文本叠加(简化:用矩形区域标记)。
  506. 506/// 添加文字叠加(简化:矩形区域) Add text overlay (simplified: rectangular region).
  507. 507pub fn add_text_overlay(
  508. 508 img : @types.Image,
  509. 509 x : Int32,
  510. 510 y : Int32,
  511. 511 width : Int32,
  512. 512 height : Int32,
  513. 513 color : Byte,
  514. 514) -> @types.Image {
  515. 515● let w = img.width
  516. 516 let h = img.height
  517. 517 let ch = img.channels
  518. 518 let data : Array[Byte] = []
  519. 519 for py in 0..<h {
  520. 520● for px in 0..<w {
  521. 521● for c in 0..<ch {
  522. 522● if px >= x && px < x + width && py >= y && py < y + height {
  523. 523● data.push(color)
  524. 524 } else {
  525. 525● data.push(img.data[(py * w + px) * ch + c])
  526. 526 }
  527. 527 }
  528. 528 }
  529. 529 }
  530. 530● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  531. 531}
  532. 532
  533. 533///|
  534. 534/// 添加时间戳标记。
  535. 535/// 添加时间戳标记 Add timestamp marker.
  536. 536pub fn add_timestamp(
  537. 537 img : @types.Image,
  538. 538 frame_index : Int32,
  539. 539 total_frames : Int32,
  540. 540) -> @types.Image {
  541. 541● let w = img.width
  542. 542 let h = img.height
  543. 543 let ch = img.channels
  544. 544 let data : Array[Byte] = []
  545. 545● let progress = frame_index.to_double() / total_duration(total_frames)
  546. 546● let bar_width = (w.to_double() * 0.3).to_int()
  547. 547 let bar_height = 2
  548. 548 let bar_x = 0
  549. 549 let bar_y = h - bar_height - 1
  550. 550● let filled = (bar_width.to_double() * progress).to_int()
  551. 551 for py in 0..<h {
  552. 552● for px in 0..<w {
  553. 553● for c in 0..<ch {
  554. 554● if px >= bar_x &&
  555. 555 px < bar_x + bar_width &&
  556. 556 py >= bar_y &&
  557. 557 py < bar_y + bar_height {
  558. 558● if px < bar_x + filled {
  559. 559● data.push((255 : Byte))
  560. 560 } else {
  561. 561● data.push((128 : Byte))
  562. 562 }
  563. 563 } else {
  564. 564● data.push(img.data[(py * w + px) * ch + c])
  565. 565 }
  566. 566 }
  567. 567 }
  568. 568 }
  569. 569● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  570. 570}
  571. 571
  572. 572///|
  573. 573fn total_duration(total_frames : Int32) -> Double {
  574. 574● if total_frames <= 0 {
  575. 575● 1.0
  576. 576 } else {
  577. 577● total_frames.to_double()
  578. 578 }
  579. 579}
  580. 580
  581. 581///|
  582. 582/// 添加水印。
  583. 583/// 添加水印 Add watermark.
  584. 584pub fn add_watermark(
  585. 585 img : @types.Image,
  586. 586 watermark : @types.Image,
  587. 587 x : Int32,
  588. 588 y : Int32,
  589. 589 opacity : Double,
  590. 590) -> @types.Image {
  591. 591● let w = img.width
  592. 592 let h = img.height
  593. 593 let ch = img.channels
  594. 594 let data : Array[Byte] = []
  595. 595● let alpha = opacity.clamp(min=0.0, max=1.0)
  596. 596 for py in 0..<h {
  597. 597● for px in 0..<w {
  598. 598● for c in 0..<ch {
  599. 599● let base_val = img.data[(py * w + px) * ch + c].to_int().to_double()
  600. 600 let wx = px - x
  601. 601 let wy = py - y
  602. 602 if wx >= 0 && wx < watermark.width && wy >= 0 && wy < watermark.height {
  603. 603● let wm_val = watermark.data[(wy * watermark.width + wx) *
  604. 604 watermark.channels +
  605. 605 c % watermark.channels]
  606. 606● .to_int()
  607. 607● .to_double()
  608. 608 let val = base_val * (1.0 - alpha) + wm_val * alpha
  609. 609● data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte())
  610. 610 } else {
  611. 611● data.push(base_val.to_int().to_byte())
  612. 612 }
  613. 613 }
  614. 614 }
  615. 615 }
  616. 616● { width: w, height: h, channels: ch, data: Bytes::from_array(data), }
  617. 617}
  618. 618
  619. 619///|
  620. 620/// 视频特效流水线。
  621. 621/// 视频特效管线 Video effects pipeline.
  622. 622pub fn video_effects_pipeline(
  623. 623 frames : Array[@types.Image],
  624. 624 filter_params : VideoFilterParams?,
  625. 625 transition_params : TransitionParams?,
  626. 626) -> Array[@types.Image] {
  627. 627● let result : Array[@types.Image] = []
  628. 628● let n = frames.length()
  629. 629 if n == 0 {
  630. 630● return result
  631. 631 }
  632. 632 // 应用滤镜
  633. 633 let filtered : Array[@types.Image] = []
  634. 634 for frame in frames {
  635. 635● match filter_params {
  636. 636● Some(fp) => filtered.push(apply_video_filter(frame, fp))
  637. 637● None => filtered.push(frame)
  638. 638 }
  639. 639 }
  640. 640 // 应用转场(在帧之间插入转场帧)
  641. 641● match transition_params {
  642. 642 Some(tp) =>
  643. 643● for i in 0..<n {
  644. 644● result.push(filtered[i])
  645. 645 if i + 1 < n {
  646. 646● let trans_frames = generate_transition(
  647. 647 filtered[i],
  648. 648 filtered[i + 1],
  649. 649 tp,
  650. 650 )
  651. 651 for tf in trans_frames {
  652. 652● result.push(tf)
  653. 653 }
  654. 654 }
  655. 655 }
  656. 656 None =>
  657. 657● for frame in filtered {
  658. 658● result.push(frame)
  659. 659 }
  660. 660 }
  661. 661 result
  662. 662}