Skip to main content

singe_npp/
lib.rs

1//! Safe NVIDIA Performance Primitives wrappers for image and signal APIs.
2//!
3//! This crate wraps NPP version queries, stream contexts, image and signal
4//! device memory, image processing pipelines, signal processing pipelines, typed
5//! image views, channel layouts, sizes, rectangles, and temporary workspaces.
6//!
7//! # Example
8//!
9//! ```no_run
10//! use singe_cuda::context::Context;
11//! use singe_npp::{
12//!     C3, ImagePipeline, ImageView, StreamContext, Workspace,
13//!     types::{ComparisonOperation, InterpolationMode, Size},
14//! };
15//!
16//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
17//! let ctx = Context::create()?;
18//! let stream = ctx.create_stream()?;
19//! let stream_context = StreamContext::create(&stream)?;
20//! let mut workspace = Workspace::create();
21//!
22//! let device_memory = todo!();
23//! let size = Size::new(640, 480);
24//! let source = ImageView::<u8, C3>::from_memory(&device_memory, size)?;
25//!
26//! let output = ImagePipeline::from_view(&stream_context, &mut workspace, source)
27//!     .resize(Size::new(256, 256), InterpolationMode::Lanczos)?
28//!     .rgb_to_gray()?
29//!     .threshold(128, ComparisonOperation::Less)?
30//!     .filter_sharpen()?
31//!     .finish()?;
32//! let image = output.copy_to_host_vec()?;
33//!
34//! println!("NPP version: {:?}", singe_npp::version()?);
35//! # Ok(())
36//! # }
37//! ```
38
39pub mod context;
40pub mod error;
41pub mod image;
42pub mod pipeline;
43pub mod signal;
44pub mod types;
45pub mod workspace;
46
47pub(crate) mod utility;
48
49#[cfg(feature = "testing")]
50pub mod testing;
51
52// TODO: refactor into mods
53pub use context::StreamContext;
54pub use error::{Error, Result};
55pub use image::{
56    AC4, AlphaIgnoredRgba, C1, C2, C3, C4, ChannelLayout, Channels2, Gray, Image, ImageView,
57    ImageViewMut, MaskView, MaskViewMut, Rgb, Rgba, SupportedImage,
58};
59pub use pipeline::{ImageBacking, ImagePipeline, SignalBacking, SignalPipeline, Workspace};
60pub use signal::{Signal, SignalView, SignalViewMut, SupportedSignal};
61
62use std::ptr;
63
64use singe_npp_sys as sys;
65
66/// Returns the loaded NPP library version.
67///
68/// # Errors
69///
70/// Returns an error if NPP returns a null version pointer.
71pub fn version() -> Result<Version> {
72    let raw = unsafe { sys::nppGetLibVersion() };
73    if raw.is_null() {
74        return Err(Error::NullHandle);
75    }
76
77    let raw = unsafe { ptr::read(raw) };
78    Ok(Version {
79        major: raw.major,
80        minor: raw.minor,
81        build: raw.build,
82    })
83}
84
85/// NPP library version components.
86#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
87pub struct Version {
88    /// Major version number.
89    pub major: i32,
90    /// Minor version number.
91    pub minor: i32,
92    /// Build number.
93    pub build: i32,
94}
95
96#[cfg(test)]
97mod tests {
98    use super::*;
99
100    #[test]
101    fn it_works() -> Result<()> {
102        let version = version()?;
103        assert_ne!(version.major, 0);
104        Ok(())
105    }
106}