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