Skip to main content

ImageDescriptor

Struct ImageDescriptor 

Source
pub struct ImageDescriptor {
    pub channel_format: usize,
    pub width: usize,
    pub height: usize,
    pub feature_channels: usize,
    pub number_of_images: usize,
    pub usage: usize,
    pub storage_mode: usize,
}
Expand description

Plain-Rust configuration for building a MPSImageDescriptor on the Swift side.

Fields§

§channel_format: usize§width: usize§height: usize§feature_channels: usize§number_of_images: usize§usage: usize§storage_mode: usize

Implementations§

Source§

impl ImageDescriptor

Source

pub const fn new( width: usize, height: usize, feature_channels: usize, channel_format: usize, ) -> Self

Create a single-image descriptor with sensible defaults for read/write image processing.

Examples found in repository?
examples/01_blur_image.rs (line 10)
4fn main() {
5    let device = MetalDevice::system_default().expect("no Metal device available");
6    let queue = device
7        .new_command_queue()
8        .expect("failed to create command queue");
9
10    let descriptor = ImageDescriptor::new(256, 256, 1, feature_channel_format::FLOAT32);
11    let src = Image::new(&device, descriptor).expect("failed to allocate source image");
12    let dst = Image::new(&device, descriptor).expect("failed to allocate destination image");
13
14    let mut impulse = vec![0.0_f32; 256 * 256];
15    let center_index = (256 / 2) * 256 + (256 / 2);
16    impulse[center_index] = 1.0;
17    src.write_f32(&impulse)
18        .expect("failed to upload source image");
19
20    let blur = ImageGaussianBlur::new(&device, 2.0).expect("failed to create gaussian blur");
21    let command_buffer = queue
22        .new_command_buffer()
23        .expect("failed to allocate command buffer");
24    blur.encode_image(&command_buffer, &src, &dst);
25    command_buffer.commit();
26    command_buffer.wait_until_completed();
27
28    let output = dst.read_f32().expect("failed to download blurred image");
29    let center_value = output[center_index];
30    let neighbor_value = output[center_index + 1];
31    let corner_value = output[0];
32    let total_energy: f32 = output.iter().sum();
33
34    assert!(
35        center_value < 1.0,
36        "center should have blurred away from impulse"
37    );
38    assert!(
39        neighbor_value > 0.0,
40        "neighbor should receive energy after blur"
41    );
42    assert!(
43        center_value > neighbor_value,
44        "center should remain strongest sample"
45    );
46    assert!(corner_value.abs() < 1.0e-6, "corners should stay at zero");
47    assert!(
48        (total_energy - 1.0).abs() < 1.0e-2,
49        "gaussian blur should preserve energy, got {total_energy}"
50    );
51
52    println!(
53        "blur smoke passed: center={center_value:.6} neighbor={neighbor_value:.6} sum={total_energy:.6}"
54    );
55}

Trait Implementations§

Source§

impl Clone for ImageDescriptor

Source§

fn clone(&self) -> ImageDescriptor

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ImageDescriptor

Source§

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

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

impl Copy for ImageDescriptor

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

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

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.