Skip to main content

philips_isyntax_rs/
pixel_engine.rs

1//! This module contains all functions related to Philips ISyntax SDK
2//! Results of theses functions should only depend on the SDK and not ISyntax file
3//!
4
5use crate::{ContainerName, Facade, ImageType, PhilipsEngine, Result, bindings::ffi};
6use cxx::let_cxx_string;
7use rand::Rng;
8use std::path::Path;
9
10impl PhilipsEngine {
11    /// Create a new instance of PhilipsEngine
12    pub fn new() -> Self {
13        PhilipsEngine { inner: ffi::new_() }
14    }
15
16    /// Create a new instance of Facade
17    /// A Facade is a reference to a Philips Engine internal object
18    /// This facade is a handle to a file
19    /// May fail if the fail cannot be opened
20    pub fn facade<P: AsRef<Path>>(
21        &self,
22        filename: P,
23        container: &ContainerName,
24    ) -> Result<Facade<'_>> {
25        self.facade_with_cache_file(filename, container, "")
26    }
27
28    /// Create a new instance of Facade
29    /// A Facade is a reference to a Philips Engine internal object
30    /// This facade is a handle to a file
31    /// May fail if the fail cannot be opened
32    pub fn facade_with_cache_file<P: AsRef<Path>, R: AsRef<Path>>(
33        &self,
34        filename: P,
35        container: &ContainerName,
36        cache_filename: R,
37    ) -> Result<Facade<'_>> {
38        let facade_id = rand::rng().random::<u64>().to_string();
39        let_cxx_string!(facade_id = facade_id);
40        let facade = Facade {
41            inner: self.inner.facade(&facade_id)?,
42            _lifetime: Default::default(),
43        };
44        facade.open_with_cache_file(filename, container, cache_filename)?;
45        Ok(facade)
46    }
47
48    /// Returns the SDK PixelEngine version
49    pub fn sdk_version(&self) -> Result<String> {
50        Ok(self.inner.sdkVersion().to_str()?.to_string())
51    }
52
53    /// Returns all containers supported by the SDK PixelEngine
54    pub fn containers(&self) -> impl Iterator<Item = &str> {
55        self.inner
56            .containers()
57            .iter()
58            .filter_map(|cxx_str| cxx_str.to_str().ok())
59    }
60
61    /// Returns the version of a container
62    pub fn container_version(&self, container: &str) -> Result<&str> {
63        let_cxx_string!(container = container);
64        Ok(self.inner.containerVersion(&container)?.to_str()?)
65    }
66
67    /// Returns all compressors supported by the SDK PixelEngine
68    pub fn compressors(&self) -> impl Iterator<Item = &str> {
69        self.inner
70            .compressors()
71            .iter()
72            .filter_map(|cxx_str| cxx_str.to_str().ok())
73    }
74
75    /// Returns all pixel_transforms supported by the SDK PixelEngine
76    pub fn pixel_transforms(&self) -> impl Iterator<Item = &str> {
77        self.inner
78            .pixelTransforms()
79            .iter()
80            .filter_map(|cxx_str| cxx_str.to_str().ok())
81    }
82
83    /// Returns all colorspace_transforms supported by the SDK PixelEngine
84    pub fn colorspace_transforms(&self) -> impl Iterator<Item = &str> {
85        self.inner
86            .colorspaceTransforms()
87            .iter()
88            .filter_map(|cxx_str| cxx_str.to_str().ok())
89    }
90
91    /// Returns all quality_presets supported by the SDK PixelEngine
92    pub fn quality_presets(&self) -> impl Iterator<Item = &str> {
93        self.inner
94            .qualityPresets()
95            .iter()
96            .filter_map(|cxx_str| cxx_str.to_str().ok())
97    }
98
99    /// Returns all supported_filters supported by the SDK PixelEngine
100    /// filters can be added to the pipeline using addFilter
101    pub fn supported_filters(&self) -> impl Iterator<Item = &str> {
102        self.inner
103            .supportedFilters()
104            .iter()
105            .filter_map(|cxx_str| cxx_str.to_str().ok())
106    }
107}
108
109impl Default for PhilipsEngine {
110    fn default() -> Self {
111        Self::new()
112    }
113}
114
115impl ImageType {
116    pub fn as_str(&self) -> &str {
117        match &self {
118            Self::WSI => "WSI",
119            Self::MacroImage => "MACROIMAGE",
120            Self::LabelImage => "LABELIMAGE",
121        }
122    }
123}
124
125impl AsRef<[u8]> for ImageType {
126    fn as_ref(&self) -> &[u8] {
127        self.as_str().as_bytes()
128    }
129}