diff --git a/Cargo.toml b/Cargo.toml index 45d6236..ca29110 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -15,7 +15,7 @@ repository = "https://github.com/l1npengtul/nokhwa" default = [] input-uvc = ["uvc", "uvc/vendor", "ouroboros"] input-v4l = ["v4l"] -input-opencv = ["opencv"] +input-opencv = ["opencv", "opencv/clang-runtime"] output-wgpu = ["wgpu"] docs-only = ["input-uvc", "input-v4l", "input-opencv", "output-wgpu"] diff --git a/README.md b/README.md index 3a8ad70..ea30755 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ The default feature includes nothing. Anything starting with `input-*` is a feat Conversely, anything that starts with `output-*` controls a feature that controls the output of something (usually a frame from the camera) `output-*` features: - - `output-wgpu`: Copies a frame directly into a wgpu texture. + - `output-wgpu`: Enables the API to copy a frame directly into a `wgpu` texture. You many want to pick and choose to reduce bloat. diff --git a/src/backends/capture/uvc_backend.rs b/src/backends/capture/uvc_backend.rs index 1bca28f..3f262e3 100644 --- a/src/backends/capture/uvc_backend.rs +++ b/src/backends/capture/uvc_backend.rs @@ -465,9 +465,3 @@ impl<'a> CaptureBackendTrait for UVCCaptureDevice<'a> { Ok(()) } } - -// use default -#[cfg(feature = "output-wgpu")] -use crate::GpuCopyBackendTrait; -#[cfg(feature = "output-wgpu")] -impl<'a> GpuCopyBackendTrait for UVCCaptureDevice<'a> {} diff --git a/src/backends/capture/v4l2.rs b/src/backends/capture/v4l2.rs index a0ec2ec..0d1d275 100644 --- a/src/backends/capture/v4l2.rs +++ b/src/backends/capture/v4l2.rs @@ -414,9 +414,3 @@ impl<'a> CaptureBackendTrait for V4LCaptureDevice<'a> { Ok(()) } } - -// use default -#[cfg(feature = "output-wgpu")] -use crate::GpuCopyBackendTrait; -#[cfg(feature = "output-wgpu")] -impl<'a> GpuCopyBackendTrait for V4LCaptureDevice<'a> {} diff --git a/src/camera_traits.rs b/src/camera_traits.rs index b20f29b..1a21226 100644 --- a/src/camera_traits.rs +++ b/src/camera_traits.rs @@ -5,6 +5,12 @@ use crate::{ use image::{buffer::ConvertBuffer, ImageBuffer, Rgb, RgbaImage}; use std::{collections::HashMap, convert::TryFrom, num::NonZeroU32}; +#[cfg(feature = "output-wgpu")] +use wgpu::{ + Device as WgpuDevice, Extent3d, ImageCopyTexture, ImageDataLayout, Queue as WgpuQueue, + Texture as WgpuTexture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage, +}; + /// This trait is for any backend that allows you to grab and take frames from a camera. /// Many of the backends are **blocking**, if the camera is occupied the library will block while it waits for it to become availible. /// @@ -69,21 +75,21 @@ pub trait CaptureBackendTrait { /// # Errors /// If the backend fails to get the frame (e.g. already taken, busy, doesn't exist anymore), or [`open_stream()`](CaptureBackendTrait::open_stream()) has not been called yet, this will error. fn get_frame_raw(&mut self) -> Result, NokhwaError>; + /// The minimum buffer size needed to write the current frame. If `rgba` is true, it will instead return the minimum size of the RGBA buffer needed. + fn min_buffer_size(&self, rgba: bool) -> usize; + /// Directly writes the current frame(RGB24) into said `buffer`. If `convert_rgba` is true, the buffer written will be written as an RGBA frame instead of a RGB frame. Returns the amount of bytes written on successful capture. + /// # Errors + /// If the backend fails to get the frame (e.g. already taken, busy, doesn't exist anymore), or [`open_stream()`](CaptureBackendTrait::open_stream()) has not been called yet, this will error. + fn get_frame_buffer( + &mut self, + buffer: &mut [u8], + convert_rgba: bool, + ) -> Result; /// Will drop the stream. /// # Errors /// Please check the `Quirks` section of each backend. fn stop_stream(&mut self) -> Result<(), NokhwaError>; -} - -#[cfg(feature = "output-wgpu")] -use wgpu::{ - Device as WgpuDevice, Extent3d, ImageCopyTexture, ImageDataLayout, Queue as WgpuQueue, - Texture as WgpuTexture, TextureDescriptor, TextureDimension, TextureFormat, TextureUsage, -}; - -/// Trait that allows the user to copy directly into a Wgpu Texture -#[cfg(feature = "output-wgpu")] -pub trait GpuCopyBackendTrait: CaptureBackendTrait { + #[cfg(feature = "output-wgpu")] /// Directly copies a frame to a Wgpu texture. /// # Errors /// If the frame cannot be captured or the resolution is 0 on any axis, this will error.