1use crate::{
3 ConvertType, HoleFillMode, StreamType,
4 error::{OrbbecError, OrbbecErrorData},
5 frame::Frame,
6 stream::VideoStreamProfile,
7 sys::filter::OBFilter,
8};
9
10pub trait Filter<F: Frame>: AsRef<OBFilter> {
12 fn process(&self, frame: &F) -> Result<F, OrbbecError> {
14 self.as_ref()
15 .process(frame.as_ref())
16 .map(|f| F::from(f))
17 .map_err(OrbbecError::from)
18 }
19}
20
21pub struct DecimationFilter {
25 inner: OBFilter,
26}
27
28impl DecimationFilter {
29 pub fn new() -> Result<Self, OrbbecError> {
31 let filter = OBFilter::new(c"DecimationFilter")?.unwrap();
34 Ok(DecimationFilter { inner: filter })
35 }
36
37 pub fn set_factor(&mut self, factor: u8) -> Result<(), OrbbecError> {
42 self.inner
43 .set_config_value(c"decimate", factor as f64)
44 .map_err(OrbbecError::from)
45 }
46}
47
48impl AsRef<OBFilter> for DecimationFilter {
49 fn as_ref(&self) -> &OBFilter {
50 &self.inner
51 }
52}
53
54impl<F: Frame> Filter<F> for DecimationFilter {}
55
56pub struct FormatConvertFilter {
60 inner: OBFilter,
61}
62
63impl FormatConvertFilter {
64 pub fn new() -> Result<Self, OrbbecError> {
66 let filter = OBFilter::new(c"FormatConverter")?.unwrap();
69 Ok(FormatConvertFilter { inner: filter })
70 }
71
72 pub fn set_convert_type(&mut self, convert_type: ConvertType) -> Result<(), OrbbecError> {
76 self.inner
77 .set_config_value(c"convertType", convert_type as u32 as f64)
78 .map_err(OrbbecError::from)
79 }
80}
81
82impl AsRef<OBFilter> for FormatConvertFilter {
83 fn as_ref(&self) -> &OBFilter {
84 &self.inner
85 }
86}
87
88impl<F: Frame> Filter<F> for FormatConvertFilter {}
89
90pub struct HoleFillingFilter {
94 inner: OBFilter,
95}
96
97impl HoleFillingFilter {
98 pub fn new() -> Result<Self, OrbbecError> {
100 match OBFilter::new(c"HoleFillingFilter")? {
101 Some(f) => Ok(HoleFillingFilter { inner: f }),
102 None => {
103 let err_data = OrbbecErrorData {
104 message: "HoleFillingFilter is not available".to_string(),
105 function: "HoleFillingFilter::new".to_string(),
106 args: "".to_string(),
107 };
108
109 Err(OrbbecError::NotImplemented(err_data))
110 }
111 }
112 }
113
114 pub fn set_mode(&mut self, mode: HoleFillMode) -> Result<(), OrbbecError> {
119 self.inner
120 .set_config_value(c"hole_filling_mode", mode as u32 as f64)
121 .map_err(OrbbecError::from)
122 }
123}
124
125impl AsRef<OBFilter> for HoleFillingFilter {
126 fn as_ref(&self) -> &OBFilter {
127 &self.inner
128 }
129}
130
131impl<F: Frame> Filter<F> for HoleFillingFilter {}
132
133pub struct TemporalFilter {
137 inner: OBFilter,
138}
139
140impl TemporalFilter {
141 pub fn new() -> Result<Self, OrbbecError> {
143 match OBFilter::new(c"TemporalFilter")? {
144 Some(f) => Ok(TemporalFilter { inner: f }),
145 None => {
146 let err_data = OrbbecErrorData {
147 message: "TemporalFilter is not available".to_string(),
148 function: "TemporalFilter::new".to_string(),
149 args: "".to_string(),
150 };
151
152 Err(OrbbecError::NotImplemented(err_data))
153 }
154 }
155 }
156
157 pub fn set_threshold(&mut self, threshold: f32) -> Result<(), OrbbecError> {
163 self.inner
164 .set_config_value(c"diff_scale", threshold as f64)
165 .map_err(OrbbecError::from)
166 }
167
168 pub fn set_weight(&mut self, weight: f32) -> Result<(), OrbbecError> {
174 self.inner
175 .set_config_value(c"weight", weight as f64)
176 .map_err(OrbbecError::from)
177 }
178}
179
180impl AsRef<OBFilter> for TemporalFilter {
181 fn as_ref(&self) -> &OBFilter {
182 &self.inner
183 }
184}
185
186impl<F: Frame> Filter<F> for TemporalFilter {}
187
188pub struct SpatialFastFilter {
193 inner: OBFilter,
194}
195
196impl SpatialFastFilter {
197 pub fn new() -> Result<Self, OrbbecError> {
199 match OBFilter::new(c"SpatialFastFilter")? {
200 Some(f) => Ok(SpatialFastFilter { inner: f }),
201 None => {
202 let err_data = OrbbecErrorData {
203 message: "SpatialFastFilter is not available".to_string(),
204 function: "SpatialFastFilter::new".to_string(),
205 args: "".to_string(),
206 };
207
208 Err(OrbbecError::NotImplemented(err_data))
209 }
210 }
211 }
212
213 pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
219 self.inner
220 .set_config_value(c"radius", radius as f64)
221 .map_err(OrbbecError::from)
222 }
223}
224
225impl AsRef<OBFilter> for SpatialFastFilter {
226 fn as_ref(&self) -> &OBFilter {
227 &self.inner
228 }
229}
230
231impl<F: Frame> Filter<F> for SpatialFastFilter {}
232
233pub struct SpatialModerateFilter {
238 inner: OBFilter,
239}
240
241impl SpatialModerateFilter {
242 pub fn new() -> Result<Self, OrbbecError> {
244 match OBFilter::new(c"SpatialModerateFilter")? {
245 Some(f) => Ok(SpatialModerateFilter { inner: f }),
246 None => {
247 let err_data = OrbbecErrorData {
248 message: "SpatialModerateFilter is not available".to_string(),
249 function: "SpatialModerateFilter::new".to_string(),
250 args: "".to_string(),
251 };
252
253 Err(OrbbecError::NotImplemented(err_data))
254 }
255 }
256 }
257
258 pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
264 self.inner
265 .set_config_value(c"radius", radius as f64)
266 .map_err(OrbbecError::from)
267 }
268
269 pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
275 self.inner
276 .set_config_value(c"magnitude", magnitude as f64)
277 .map_err(OrbbecError::from)
278 }
279
280 pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
286 self.inner
287 .set_config_value(c"disp_diff", threshold as f64)
288 .map_err(OrbbecError::from)
289 }
290}
291
292impl AsRef<OBFilter> for SpatialModerateFilter {
293 fn as_ref(&self) -> &OBFilter {
294 &self.inner
295 }
296}
297
298impl<F: Frame> Filter<F> for SpatialModerateFilter {}
299
300pub struct SpatialAdvancedFilter {
305 inner: OBFilter,
306}
307
308impl SpatialAdvancedFilter {
309 pub fn new() -> Result<Self, OrbbecError> {
311 match OBFilter::new(c"SpatialAdvancedFilter")? {
312 Some(f) => Ok(SpatialAdvancedFilter { inner: f }),
313 None => {
314 let err_data = OrbbecErrorData {
315 message: "SpatialAdvancedFilter is not available".to_string(),
316 function: "SpatialAdvancedFilter::new".to_string(),
317 args: "".to_string(),
318 };
319
320 Err(OrbbecError::NotImplemented(err_data))
321 }
322 }
323 }
324
325 pub fn set_alpha(&mut self, alpha: f32) -> Result<(), OrbbecError> {
331 self.inner
332 .set_config_value(c"alpha", alpha as f64)
333 .map_err(OrbbecError::from)
334 }
335
336 pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
342 self.inner
343 .set_config_value(c"disp_diff", threshold as f64)
344 .map_err(OrbbecError::from)
345 }
346
347 pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
353 self.inner
354 .set_config_value(c"radius", radius as f64)
355 .map_err(OrbbecError::from)
356 }
357
358 pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
364 self.inner
365 .set_config_value(c"magnitude", magnitude as f64)
366 .map_err(OrbbecError::from)
367 }
368}
369
370impl AsRef<OBFilter> for SpatialAdvancedFilter {
371 fn as_ref(&self) -> &OBFilter {
372 &self.inner
373 }
374}
375
376impl<F: Frame> Filter<F> for SpatialAdvancedFilter {}
377
378pub struct ThresholdFilter {
382 inner: OBFilter,
383}
384
385impl ThresholdFilter {
386 pub fn new() -> Result<Self, OrbbecError> {
388 let filter = OBFilter::new(c"ThresholdFilter")?.unwrap();
391 Ok(ThresholdFilter { inner: filter })
392 }
393
394 pub fn set_min_depth(&mut self, min_depth: u16) -> Result<(), OrbbecError> {
398 self.inner
399 .set_config_value(c"min", min_depth as f64)
400 .map_err(OrbbecError::from)
401 }
402
403 pub fn set_max_depth(&mut self, max_depth: u16) -> Result<(), OrbbecError> {
407 self.inner
408 .set_config_value(c"max", max_depth as f64)
409 .map_err(OrbbecError::from)
410 }
411}
412
413impl AsRef<OBFilter> for ThresholdFilter {
414 fn as_ref(&self) -> &OBFilter {
415 &self.inner
416 }
417}
418
419impl<F: Frame> Filter<F> for ThresholdFilter {}
420
421pub struct AlignFilter {
425 inner: OBFilter,
426}
427
428impl AlignFilter {
429 pub fn new() -> Result<Self, OrbbecError> {
432 let filter = OBFilter::new(c"Align")?.unwrap();
435 Ok(AlignFilter { inner: filter })
436 }
437
438 pub fn set_align_to_stream_type(&mut self, stream_type: StreamType) -> Result<(), OrbbecError> {
442 self.inner
443 .set_config_value(c"AlignType", stream_type as u32 as f64)
444 .map_err(OrbbecError::from)
445 }
446
447 pub fn set_add_distortion(&mut self, enable: bool) -> Result<(), OrbbecError> {
451 self.inner
452 .set_config_value(c"TargetDistortion", if enable { 1.0 } else { 0.0 })
453 .map_err(OrbbecError::from)
454 }
455
456 pub fn set_gap_fill(&mut self, enable: bool) -> Result<(), OrbbecError> {
460 self.inner
461 .set_config_value(c"GapFillCopy", if enable { 1.0 } else { 0.0 })
462 .map_err(OrbbecError::from)
463 }
464
465 pub fn set_match_resolution(&mut self, enable: bool) -> Result<(), OrbbecError> {
469 self.inner
470 .set_config_value(c"MatchTargetRes", if enable { 1.0 } else { 0.0 })
471 .map_err(OrbbecError::from)
472 }
473
474 pub fn set_align_to_stream_profile(
479 &mut self,
480 profile: &VideoStreamProfile,
481 ) -> Result<(), OrbbecError> {
482 self.inner
483 .set_align_to(profile.inner())
484 .map_err(OrbbecError::from)
485 }
486}
487
488impl AsRef<OBFilter> for AlignFilter {
489 fn as_ref(&self) -> &OBFilter {
490 &self.inner
491 }
492}
493
494impl<F: Frame> Filter<F> for AlignFilter {}