Skip to main content

ImageWorkspace

Struct ImageWorkspace 

Source
pub struct ImageWorkspace { /* private fields */ }
Expand description

Reusable conversion storage for still-image encoding.

Keeps one PNG row or JPEG final row, WebP’s complete converted image, and downscale sums at each sample depth. Capacity grows to the largest encode and remains retained until release or drop. Each concurrent worker needs its own workspace. Codec state and encoded output are not retained; lossy WebP still allocates its complete encoded result in the backend. Eligible RGBA input is borrowed without populating conversion scratch. Remembers the last successful output size for matching format settings, retaining no output memory; release resets that hint.

Implementations§

Source§

impl ImageWorkspace

Source

pub fn new() -> Self

Create an empty workspace without allocating.

Source

pub fn retained_bytes(&self) -> usize

Bytes retained in wrapper scratch, excluding source, codec and output.

Source

pub fn release(&mut self)

Release all retained scratch. The workspace remains usable.

Source

pub fn encode<F: VideoSource + ?Sized>( &mut self, frame: &F, options: &ImageOptions, ) -> Result<Vec<u8>>

Encode a frame into a newly allocated result, reusing conversion scratch.

Uses the same options, validation and errors as frame.encode_image. All frame types implementing crate::VideoSource are accepted.

Source

pub fn encode_to<F: VideoSource + ?Sized, W: Write>( &mut self, frame: &F, options: &ImageOptions, out: W, ) -> Result<()>

Encode into a writer, reusing conversion scratch. A cleared Vec<u8> also reuses output capacity. Both WebP formats buffer their complete encoded result before writing; PNG and JPEG write incrementally. Success means every byte was written and the writer was flushed. Validation happens before writing; a codec/writer failure may leave partial output.

use grafton_ndi::{PixelFormat, VideoFrame, image_encoding::{ImageWorkspace, ImageOptions, ImageFormat}};
let frame = VideoFrame::builder().resolution(2, 2)
    .pixel_format(PixelFormat::Bgra).data(vec![255; 16]).build()?;
let mut workspace = ImageWorkspace::new();
let mut output = Vec::new();
for _ in 0..3 {
    output.clear();
    workspace.encode_to(&frame, &ImageOptions::new(ImageFormat::WebpLossless), &mut output)?;
}
workspace.release();
Source

pub fn encode_data_url<F: VideoSource + ?Sized>( &mut self, frame: &F, options: &ImageOptions, ) -> Result<String>

Encode a frame as a base64 data URL, reusing conversion scratch.

Validation, format and color handling match Self::encode.

Trait Implementations§

Source§

impl Debug for ImageWorkspace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for ImageWorkspace

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.