add write api to capture directly gated by a feature

This commit is contained in:
l1npengtul
2021-06-02 00:00:23 +09:00
parent 30d91b518a
commit e7602b80c5
5 changed files with 19 additions and 25 deletions
+1 -1
View File
@@ -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"]
+1 -1
View File
@@ -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.
-6
View File
@@ -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> {}
-6
View File
@@ -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> {}
+17 -11
View File
@@ -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<Vec<u8>, 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<usize, NokhwaError>;
/// 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.