use crate::{
ConvertType, HoleFillMode, StreamType,
error::{OrbbecError, OrbbecErrorData},
frame::Frame,
stream::VideoStreamProfile,
sys::filter::OBFilter,
};
pub trait Filter<F: Frame>: AsRef<OBFilter> {
fn process(&self, frame: &F) -> Result<F, OrbbecError> {
self.as_ref()
.process(frame.as_ref())
.map(|f| F::from(f))
.map_err(OrbbecError::from)
}
}
pub struct DecimationFilter {
inner: OBFilter,
}
impl DecimationFilter {
pub fn new() -> Result<Self, OrbbecError> {
let filter = OBFilter::new(c"DecimationFilter")?.unwrap();
Ok(DecimationFilter { inner: filter })
}
pub fn set_factor(&mut self, factor: u8) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"decimate", factor as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for DecimationFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for DecimationFilter {}
pub struct FormatConvertFilter {
inner: OBFilter,
}
impl FormatConvertFilter {
pub fn new() -> Result<Self, OrbbecError> {
let filter = OBFilter::new(c"FormatConverter")?.unwrap();
Ok(FormatConvertFilter { inner: filter })
}
pub fn set_convert_type(&mut self, convert_type: ConvertType) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"convertType", convert_type as u32 as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for FormatConvertFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for FormatConvertFilter {}
pub struct HoleFillingFilter {
inner: OBFilter,
}
impl HoleFillingFilter {
pub fn new() -> Result<Self, OrbbecError> {
match OBFilter::new(c"HoleFillingFilter")? {
Some(f) => Ok(HoleFillingFilter { inner: f }),
None => {
let err_data = OrbbecErrorData {
message: "HoleFillingFilter is not available".to_string(),
function: "HoleFillingFilter::new".to_string(),
args: "".to_string(),
};
Err(OrbbecError::NotImplemented(err_data))
}
}
}
pub fn set_mode(&mut self, mode: HoleFillMode) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"hole_filling_mode", mode as u32 as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for HoleFillingFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for HoleFillingFilter {}
pub struct TemporalFilter {
inner: OBFilter,
}
impl TemporalFilter {
pub fn new() -> Result<Self, OrbbecError> {
match OBFilter::new(c"TemporalFilter")? {
Some(f) => Ok(TemporalFilter { inner: f }),
None => {
let err_data = OrbbecErrorData {
message: "TemporalFilter is not available".to_string(),
function: "TemporalFilter::new".to_string(),
args: "".to_string(),
};
Err(OrbbecError::NotImplemented(err_data))
}
}
}
pub fn set_threshold(&mut self, threshold: f32) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"diff_scale", threshold as f64)
.map_err(OrbbecError::from)
}
pub fn set_weight(&mut self, weight: f32) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"weight", weight as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for TemporalFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for TemporalFilter {}
pub struct SpatialFastFilter {
inner: OBFilter,
}
impl SpatialFastFilter {
pub fn new() -> Result<Self, OrbbecError> {
match OBFilter::new(c"SpatialFastFilter")? {
Some(f) => Ok(SpatialFastFilter { inner: f }),
None => {
let err_data = OrbbecErrorData {
message: "SpatialFastFilter is not available".to_string(),
function: "SpatialFastFilter::new".to_string(),
args: "".to_string(),
};
Err(OrbbecError::NotImplemented(err_data))
}
}
}
pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"radius", radius as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for SpatialFastFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for SpatialFastFilter {}
pub struct SpatialModerateFilter {
inner: OBFilter,
}
impl SpatialModerateFilter {
pub fn new() -> Result<Self, OrbbecError> {
match OBFilter::new(c"SpatialModerateFilter")? {
Some(f) => Ok(SpatialModerateFilter { inner: f }),
None => {
let err_data = OrbbecErrorData {
message: "SpatialModerateFilter is not available".to_string(),
function: "SpatialModerateFilter::new".to_string(),
args: "".to_string(),
};
Err(OrbbecError::NotImplemented(err_data))
}
}
}
pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"radius", radius as f64)
.map_err(OrbbecError::from)
}
pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"magnitude", magnitude as f64)
.map_err(OrbbecError::from)
}
pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"disp_diff", threshold as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for SpatialModerateFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for SpatialModerateFilter {}
pub struct SpatialAdvancedFilter {
inner: OBFilter,
}
impl SpatialAdvancedFilter {
pub fn new() -> Result<Self, OrbbecError> {
match OBFilter::new(c"SpatialAdvancedFilter")? {
Some(f) => Ok(SpatialAdvancedFilter { inner: f }),
None => {
let err_data = OrbbecErrorData {
message: "SpatialAdvancedFilter is not available".to_string(),
function: "SpatialAdvancedFilter::new".to_string(),
args: "".to_string(),
};
Err(OrbbecError::NotImplemented(err_data))
}
}
}
pub fn set_alpha(&mut self, alpha: f32) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"alpha", alpha as f64)
.map_err(OrbbecError::from)
}
pub fn set_threshold(&mut self, threshold: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"disp_diff", threshold as f64)
.map_err(OrbbecError::from)
}
pub fn set_radius(&mut self, radius: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"radius", radius as f64)
.map_err(OrbbecError::from)
}
pub fn set_magnitude(&mut self, magnitude: u8) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"magnitude", magnitude as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for SpatialAdvancedFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for SpatialAdvancedFilter {}
pub struct ThresholdFilter {
inner: OBFilter,
}
impl ThresholdFilter {
pub fn new() -> Result<Self, OrbbecError> {
let filter = OBFilter::new(c"ThresholdFilter")?.unwrap();
Ok(ThresholdFilter { inner: filter })
}
pub fn set_min_depth(&mut self, min_depth: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"min", min_depth as f64)
.map_err(OrbbecError::from)
}
pub fn set_max_depth(&mut self, max_depth: u16) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"max", max_depth as f64)
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for ThresholdFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for ThresholdFilter {}
pub struct AlignFilter {
inner: OBFilter,
}
impl AlignFilter {
pub fn new() -> Result<Self, OrbbecError> {
let filter = OBFilter::new(c"Align")?.unwrap();
Ok(AlignFilter { inner: filter })
}
pub fn set_align_to_stream_type(&mut self, stream_type: StreamType) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"AlignType", stream_type as u32 as f64)
.map_err(OrbbecError::from)
}
pub fn set_add_distortion(&mut self, enable: bool) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"TargetDistortion", if enable { 1.0 } else { 0.0 })
.map_err(OrbbecError::from)
}
pub fn set_gap_fill(&mut self, enable: bool) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"GapFillCopy", if enable { 1.0 } else { 0.0 })
.map_err(OrbbecError::from)
}
pub fn set_match_resolution(&mut self, enable: bool) -> Result<(), OrbbecError> {
self.inner
.set_config_value(c"MatchTargetRes", if enable { 1.0 } else { 0.0 })
.map_err(OrbbecError::from)
}
pub fn set_align_to_stream_profile(
&mut self,
profile: &VideoStreamProfile,
) -> Result<(), OrbbecError> {
self.inner
.set_align_to(profile.inner())
.map_err(OrbbecError::from)
}
}
impl AsRef<OBFilter> for AlignFilter {
fn as_ref(&self) -> &OBFilter {
&self.inner
}
}
impl<F: Frame> Filter<F> for AlignFilter {}