pub struct Image { /* private fields */ }Expand description
Safe owner for an Objective-C MPSImage.
Implementations§
Source§impl Image
impl Image
Sourcepub fn new(device: &MetalDevice, descriptor: ImageDescriptor) -> Option<Self>
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}Sourcepub fn from_texture(
texture: &MetalTexture,
feature_channels: usize,
) -> Option<Self>
pub fn from_texture( texture: &MetalTexture, feature_channels: usize, ) -> Option<Self>
Wrap an existing Metal texture in an MPSImage.
Sourcepub fn feature_channels(&self) -> usize
pub fn feature_channels(&self) -> usize
Number of feature channels per pixel.
Sourcepub fn number_of_images(&self) -> usize
pub fn number_of_images(&self) -> usize
Number of images stored in the backing texture array.
Sourcepub fn pixel_size(&self) -> usize
pub fn pixel_size(&self) -> usize
Bytes between neighboring pixels in storage order.
Sourcepub fn pixel_format(&self) -> usize
pub fn pixel_format(&self) -> usize
Underlying MTLPixelFormat raw value.
Sourcepub fn whole_region(&self) -> ImageRegion
pub fn whole_region(&self) -> ImageRegion
Convenience region covering the full first image.
Sourcepub fn read_bytes(
&self,
dst: &mut [u8],
data_layout: usize,
bytes_per_row: usize,
region: ImageRegion,
params: ImageReadWriteParams,
image_index: usize,
) -> Result<()>
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.
Sourcepub fn write_bytes(
&self,
src: &[u8],
data_layout: usize,
bytes_per_row: usize,
region: ImageRegion,
params: ImageReadWriteParams,
image_index: usize,
) -> Result<()>
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.
Sourcepub fn read_f32(&self) -> Result<Vec<f32>>
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}Sourcepub fn write_f32(&self, data: &[f32]) -> Result<()>
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§
Auto Trait Implementations§
impl Freeze for Image
impl RefUnwindSafe for Image
impl Unpin for Image
impl UnsafeUnpin for Image
impl UnwindSafe for Image
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more