wgpu fix & inital decoder infra

This commit is contained in:
l1npengtul
2023-07-02 09:21:39 +09:00
parent cab61c5750
commit e0c0796b48
8 changed files with 81 additions and 14 deletions
+11 -14
View File
@@ -16,6 +16,8 @@
use crate::{frame_format::SourceFrameFormat, types::Resolution};
use bytes::Bytes;
use image::ImageBuffer;
use crate::error::NokhwaError;
/// A buffer returned by a camera to accommodate custom decoding.
/// Contains information of Resolution, the buffer's [`FrameFormat`], and the buffer.
@@ -152,6 +154,9 @@ impl Buffer {
}
}
#[cfg(feature = "wgpu-types")]
use wgpu::{Extent3d, TextureDescriptor, TextureDimension, TextureFormat, TextureUsages, ImageCopyTexture, TextureAspect, ImageDataLayout};
#[cfg(feature = "wgpu-types")]
impl Buffer {
#[cfg_attr(feature = "docs-features", doc(cfg(feature = "wgpu-types")))]
@@ -160,12 +165,10 @@ impl Buffer {
/// If the frame cannot be captured or the resolution is 0 on any axis, this will error.
fn frame_texture<'a>(
&mut self,
device: &WgpuDevice,
queue: &WgpuQueue,
device: &wgpu::Device,
queue: &wgpu::Queue,
label: Option<&'a str>,
) -> Result<WgpuTexture, NokhwaError> {
use crate::pixel_format::RgbAFormat;
use std::num::NonZeroU32;
) -> Result<wgpu::Texture, NokhwaError> {
let frame = self.frame()?.decode_image::<RgbAFormat>()?;
let texture_size = Extent3d {
@@ -182,17 +185,11 @@ impl Buffer {
dimension: TextureDimension::D2,
format: TextureFormat::Rgba8UnormSrgb,
usage: TextureUsages::TEXTURE_BINDING | TextureUsages::COPY_DST,
view_formats: &[TextureFormat::Rgba8UnormSrgb],
});
let width_nonzero = match NonZeroU32::try_from(4 * frame.width()) {
Ok(w) => Some(w),
Err(why) => return Err(NokhwaError::ReadFrameError(why.to_string())),
};
let height_nonzero = match NonZeroU32::try_from(frame.height()) {
Ok(h) => Some(h),
Err(why) => return Err(NokhwaError::ReadFrameError(why.to_string())),
};
let width_nonzero = 4 * frame.width();
let height_nonzero = frame.height();
queue.write_texture(
ImageCopyTexture {
+33
View File
@@ -0,0 +1,33 @@
use std::ops::Deref;
use image::{ImageBuffer, Pixel};
use serde::de::Error;
use crate::buffer::Buffer;
use crate::frame_format::{SourceFrameFormat};
/// Trait to define a struct that can decode a [`Buffer`]
pub trait Decoder {
/// Formats that the decoder can decode.
const ALLOWED_FORMATS: &'static [SourceFrameFormat];
/// Output pixel type (e.g. [`Rgb<u8>`](image::Rgb))
type Pixel: Pixel;
/// Container for [`Self::Pixel`] - must have the same [`Pixel::Subpixel`]
type Container: Deref<Target = [Pixel::Subpixel]>;
/// Error that the decoder will output (use [`NokhwaError`] if you're not sure)
type Error: Error;
/// Decode function.
fn decode(&mut self, buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
}
/// Decoder that can be used statically (struct contains no state)
///
/// This is useful for times that a simple function is all that is required.
pub trait StaticDecoder: Decoder {
fn decode_static(buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
}
/// Decoder that does not change its internal state.
pub trait IdemptDecoder: Decoder {
fn decode_nm(buffer: Buffer) -> Result<ImageBuffer<Self::Pixel, Self::Container>, Self::Error>;
}
+1
View File
@@ -25,3 +25,4 @@ pub mod format_filter;
pub mod frame_format;
pub mod traits;
pub mod types;
pub mod decoder;