1+ use crate :: {
2+ Result ,
3+ filters:: traits:: { VideoData , VideoFilter } ,
4+ tracks:: video_frame_cache:: VideoImage ,
5+ } ;
6+ use image:: Rgba ;
7+
8+ #[ derive( Debug , Clone , serde:: Serialize , serde:: Deserialize ) ]
9+ pub struct BorderFilter {
10+ pub size : u32 , // border width in pixels
11+ pub color : [ u8 ; 4 ] , // RGBA color
12+ pub corner_radius : u32 , // rounded corner radius
13+ }
14+
15+ impl Default for BorderFilter {
16+ fn default ( ) -> Self {
17+ Self {
18+ size : 0 ,
19+ color : [ 0 , 0 , 0 , 255 ] ,
20+ corner_radius : 0 ,
21+ }
22+ }
23+ }
24+
25+ impl BorderFilter {
26+ pub const NAME : & ' static str = "border" ;
27+
28+ pub fn new ( size : u32 , color : [ u8 ; 4 ] , corner_radius : u32 ) -> Self {
29+ Self { size, color, corner_radius }
30+ }
31+
32+ fn draw_border ( & self , image : & mut image:: RgbaImage ) {
33+ let width = image. width ( ) ;
34+ let height = image. height ( ) ;
35+ let border_color = Rgba ( self . color ) ;
36+ let size = self . size . min ( width / 2 ) . min ( height / 2 ) ;
37+
38+ if size == 0 {
39+ return ;
40+ }
41+
42+ // Draw top border
43+ for y in 0 ..size {
44+ for x in 0 ..width {
45+ image. put_pixel ( x, y, border_color) ;
46+ }
47+ }
48+
49+ // Draw bottom border
50+ for y in ( height - size) ..height {
51+ for x in 0 ..width {
52+ image. put_pixel ( x, y, border_color) ;
53+ }
54+ }
55+
56+ // Draw left border
57+ for y in size..( height - size) {
58+ for x in 0 ..size {
59+ image. put_pixel ( x, y, border_color) ;
60+ }
61+ }
62+
63+ // Draw right border
64+ for y in size..( height - size) {
65+ for x in ( width - size) ..width {
66+ image. put_pixel ( x, y, border_color) ;
67+ }
68+ }
69+ }
70+ }
71+
72+ impl VideoFilter for BorderFilter {
73+ crate :: impl_default_video_filter!( BorderFilter ) ;
74+
75+ fn apply ( & self , data : & mut VideoData ) -> Result < ( ) > {
76+ for frame in & mut data. frames {
77+ if let VideoImage :: Image { buffer, .. } = frame {
78+ self . draw_border ( buffer) ;
79+ }
80+ }
81+ Ok ( ( ) )
82+ }
83+ }
0 commit comments