Skip to main content

Image

Struct Image 

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

Safe owner for an Objective-C MPSImage.

Implementations§

Source§

impl Image

Source

pub fn new(device: &MetalDevice, descriptor: ImageDescriptor) -> Option<Self>

Allocate a lazily backed MPSImage on device.

Examples found in repository?
examples/01_blur_image.rs (line 11)
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}
Source

pub fn from_texture( texture: &MetalTexture, feature_channels: usize, ) -> Option<Self>

Wrap an existing Metal texture in an MPSImage.

Source

pub const fn as_ptr(&self) -> *mut c_void

Raw MPSImage pointer.

Source

pub fn width(&self) -> usize

Image width in pixels.

Source

pub fn height(&self) -> usize

Image height in pixels.

Source

pub fn feature_channels(&self) -> usize

Number of feature channels per pixel.

Source

pub fn number_of_images(&self) -> usize

Number of images stored in the backing texture array.

Source

pub fn pixel_size(&self) -> usize

Bytes between neighboring pixels in storage order.

Source

pub fn pixel_format(&self) -> usize

Underlying MTLPixelFormat raw value.

Source

pub fn whole_region(&self) -> ImageRegion

Convenience region covering the full first image.

Source

pub fn read_bytes( &self, dst: &mut [u8], data_layout: usize, bytes_per_row: usize, region: ImageRegion, params: ImageReadWriteParams, image_index: usize, ) -> Result<()>

Read bytes out of the image into a caller-provided buffer.

Source

pub fn write_bytes( &self, src: &[u8], data_layout: usize, bytes_per_row: usize, region: ImageRegion, params: ImageReadWriteParams, image_index: usize, ) -> Result<()>

Write bytes into the image from a caller-provided buffer.

Source

pub fn read_f32(&self) -> Result<Vec<f32>>

Read the first image slice as tightly packed float32 HWC data.

Examples found in repository?
examples/01_blur_image.rs (line 28)
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}
Source

pub fn write_f32(&self, data: &[f32]) -> Result<()>

Write tightly packed float32 HWC data into the first image slice.

Examples found in repository?
examples/01_blur_image.rs (line 17)
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 Drop for Image

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more
Source§

impl Send for Image

Source§

impl Sync for Image

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 = 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.