Skip to main content

UninitImage

Struct UninitImage 

Source
pub struct UninitImage<T: ImageData>(/* private fields */);
Expand description

Represents an allocated Image whose image data has not yet been initialized.

Implementations§

Source§

impl<T: ImageData> UninitImage<T>

Source

pub fn new_2d( width: usize, height: usize, channels: usize, space: ColorSpace, interleaving: bool, ) -> UninitImage<T>

Construct a new uninitialized Image with the specified properties.

§Panics

This function will panic if UninitImage::try_new_2d() returns an error.

Examples found in repository?
examples/tests/test_images.rs (lines 33-39)
28fn test_create_bitmap_image() -> Image<bool> {
29    let width = 2;
30    let height = 2;
31    let channels = 1;
32
33    let mut image = UninitImage::<bool>::new_2d(
34        width,
35        height,
36        channels,
37        ColorSpace::Automatic,
38        false,
39    );
40
41    image.set(Pixel::D2([1, 1]), 1, false);
42    image.set(Pixel::D2([1, 2]), 1, true);
43    image.set(Pixel::D2([2, 1]), 1, true);
44    image.set(Pixel::D2([2, 2]), 1, false);
45
46    unsafe { image.assume_init() }
47}
48
49/// Create an image with four pixels, where the top left image is red, the top right
50/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
51/// gray.
52#[wll::export]
53fn test_create_color_rgb_u8_image() -> Image<u8> {
54    let width = 2;
55    let height = 2;
56    let channels = 3; // Red, green, and blue.
57
58    let mut image: UninitImage<u8> =
59        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
60
61    // Red, green, and blue channels indices.
62    const R: usize = 1;
63    const G: usize = 2;
64    const B: usize = 3;
65
66    // Set every pixel value to black. The image data is otherwise completely
67    // uninitialized memory, and can contain arbitrary values.
68    image.zero();
69
70    // Set the top left, top right, and bottom left pixels on only one color channel.
71    image.set(Pixel::D2([1, 1]), R, u8::MAX);
72    image.set(Pixel::D2([1, 2]), G, u8::MAX);
73    image.set(Pixel::D2([2, 1]), B, u8::MAX);
74
75    // Make this pixel white, by setting R, G, and B channels to ~80%.
76    image.set(Pixel::D2([2, 2]), R, 200);
77    image.set(Pixel::D2([2, 2]), G, 200);
78    image.set(Pixel::D2([2, 2]), B, 200);
79
80    unsafe { image.assume_init() }
81}
82
83/// Create an image with four pixels, where the top left image is red, the top right
84/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
85/// gray.
86#[wll::export]
87fn test_create_color_rgb_f32_image() -> Image<f32> {
88    let width = 2;
89    let height = 2;
90    let channels = 3; // Red, green, and blue.
91
92    let mut image: UninitImage<f32> =
93        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
94
95    // Red, green, and blue channels indices.
96    const R: usize = 1;
97    const G: usize = 2;
98    const B: usize = 3;
99
100    // Set every pixel value to black. The image data is otherwise completely
101    // uninitialized memory, and can contain arbitrary values.
102    image.zero();
103
104    // Set the top left, top right, and bottom left pixels on only one color channel.
105    image.set(Pixel::D2([1, 1]), R, 1.0);
106    image.set(Pixel::D2([1, 2]), G, 1.0);
107    image.set(Pixel::D2([2, 1]), B, 1.0);
108
109    // Make this pixel white, by setting R, G, and B channels to 80%.
110    image.set(Pixel::D2([2, 2]), R, 0.8);
111    image.set(Pixel::D2([2, 2]), G, 0.8);
112    image.set(Pixel::D2([2, 2]), B, 0.8);
113
114    unsafe { image.assume_init() }
115}
Source

pub fn try_new_2d( width: usize, height: usize, channels: usize, space: ColorSpace, interleaving: bool, ) -> Result<UninitImage<T>, i64>

Construct a new uninitialized 2D image.

Source

pub fn zero(&mut self)

Efficiently set every pixel value in this image to zero.

This fully initializes this image, albeit to a black image.

Examples found in repository?
examples/tests/test_images.rs (line 68)
53fn test_create_color_rgb_u8_image() -> Image<u8> {
54    let width = 2;
55    let height = 2;
56    let channels = 3; // Red, green, and blue.
57
58    let mut image: UninitImage<u8> =
59        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
60
61    // Red, green, and blue channels indices.
62    const R: usize = 1;
63    const G: usize = 2;
64    const B: usize = 3;
65
66    // Set every pixel value to black. The image data is otherwise completely
67    // uninitialized memory, and can contain arbitrary values.
68    image.zero();
69
70    // Set the top left, top right, and bottom left pixels on only one color channel.
71    image.set(Pixel::D2([1, 1]), R, u8::MAX);
72    image.set(Pixel::D2([1, 2]), G, u8::MAX);
73    image.set(Pixel::D2([2, 1]), B, u8::MAX);
74
75    // Make this pixel white, by setting R, G, and B channels to ~80%.
76    image.set(Pixel::D2([2, 2]), R, 200);
77    image.set(Pixel::D2([2, 2]), G, 200);
78    image.set(Pixel::D2([2, 2]), B, 200);
79
80    unsafe { image.assume_init() }
81}
82
83/// Create an image with four pixels, where the top left image is red, the top right
84/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
85/// gray.
86#[wll::export]
87fn test_create_color_rgb_f32_image() -> Image<f32> {
88    let width = 2;
89    let height = 2;
90    let channels = 3; // Red, green, and blue.
91
92    let mut image: UninitImage<f32> =
93        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
94
95    // Red, green, and blue channels indices.
96    const R: usize = 1;
97    const G: usize = 2;
98    const B: usize = 3;
99
100    // Set every pixel value to black. The image data is otherwise completely
101    // uninitialized memory, and can contain arbitrary values.
102    image.zero();
103
104    // Set the top left, top right, and bottom left pixels on only one color channel.
105    image.set(Pixel::D2([1, 1]), R, 1.0);
106    image.set(Pixel::D2([1, 2]), G, 1.0);
107    image.set(Pixel::D2([2, 1]), B, 1.0);
108
109    // Make this pixel white, by setting R, G, and B channels to 80%.
110    image.set(Pixel::D2([2, 2]), R, 0.8);
111    image.set(Pixel::D2([2, 2]), G, 0.8);
112    image.set(Pixel::D2([2, 2]), B, 0.8);
113
114    unsafe { image.assume_init() }
115}
Source

pub fn set(&mut self, pixel: Pixel, channel: usize, value: T)

Set the value of the specified pixel and channel.

§Panics

This function will panic if the underlying call to ImageData::setter() fails. This can happen if the specified pixel or channel does not exist.

Examples found in repository?
examples/tests/test_images.rs (line 41)
28fn test_create_bitmap_image() -> Image<bool> {
29    let width = 2;
30    let height = 2;
31    let channels = 1;
32
33    let mut image = UninitImage::<bool>::new_2d(
34        width,
35        height,
36        channels,
37        ColorSpace::Automatic,
38        false,
39    );
40
41    image.set(Pixel::D2([1, 1]), 1, false);
42    image.set(Pixel::D2([1, 2]), 1, true);
43    image.set(Pixel::D2([2, 1]), 1, true);
44    image.set(Pixel::D2([2, 2]), 1, false);
45
46    unsafe { image.assume_init() }
47}
48
49/// Create an image with four pixels, where the top left image is red, the top right
50/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
51/// gray.
52#[wll::export]
53fn test_create_color_rgb_u8_image() -> Image<u8> {
54    let width = 2;
55    let height = 2;
56    let channels = 3; // Red, green, and blue.
57
58    let mut image: UninitImage<u8> =
59        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
60
61    // Red, green, and blue channels indices.
62    const R: usize = 1;
63    const G: usize = 2;
64    const B: usize = 3;
65
66    // Set every pixel value to black. The image data is otherwise completely
67    // uninitialized memory, and can contain arbitrary values.
68    image.zero();
69
70    // Set the top left, top right, and bottom left pixels on only one color channel.
71    image.set(Pixel::D2([1, 1]), R, u8::MAX);
72    image.set(Pixel::D2([1, 2]), G, u8::MAX);
73    image.set(Pixel::D2([2, 1]), B, u8::MAX);
74
75    // Make this pixel white, by setting R, G, and B channels to ~80%.
76    image.set(Pixel::D2([2, 2]), R, 200);
77    image.set(Pixel::D2([2, 2]), G, 200);
78    image.set(Pixel::D2([2, 2]), B, 200);
79
80    unsafe { image.assume_init() }
81}
82
83/// Create an image with four pixels, where the top left image is red, the top right
84/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
85/// gray.
86#[wll::export]
87fn test_create_color_rgb_f32_image() -> Image<f32> {
88    let width = 2;
89    let height = 2;
90    let channels = 3; // Red, green, and blue.
91
92    let mut image: UninitImage<f32> =
93        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
94
95    // Red, green, and blue channels indices.
96    const R: usize = 1;
97    const G: usize = 2;
98    const B: usize = 3;
99
100    // Set every pixel value to black. The image data is otherwise completely
101    // uninitialized memory, and can contain arbitrary values.
102    image.zero();
103
104    // Set the top left, top right, and bottom left pixels on only one color channel.
105    image.set(Pixel::D2([1, 1]), R, 1.0);
106    image.set(Pixel::D2([1, 2]), G, 1.0);
107    image.set(Pixel::D2([2, 1]), B, 1.0);
108
109    // Make this pixel white, by setting R, G, and B channels to 80%.
110    image.set(Pixel::D2([2, 2]), R, 0.8);
111    image.set(Pixel::D2([2, 2]), G, 0.8);
112    image.set(Pixel::D2([2, 2]), B, 0.8);
113
114    unsafe { image.assume_init() }
115}
Source

pub unsafe fn assume_init(self) -> Image<T>

Assume that the data in this image has been initialized.

Use UninitImage::zero() to quickly ensure that every pixel value has been initialized.

Use UninitImage::set() to set individual pixel/channel values.

§Safety

This function must only be called once all elements of this image have been initialized. It is undefined behavior to construct an Image without first initializing the data array. In practice, uninitialized values will be essentially random, causing the resulting image to appear different each time it is created.

Examples found in repository?
examples/tests/test_images.rs (line 46)
28fn test_create_bitmap_image() -> Image<bool> {
29    let width = 2;
30    let height = 2;
31    let channels = 1;
32
33    let mut image = UninitImage::<bool>::new_2d(
34        width,
35        height,
36        channels,
37        ColorSpace::Automatic,
38        false,
39    );
40
41    image.set(Pixel::D2([1, 1]), 1, false);
42    image.set(Pixel::D2([1, 2]), 1, true);
43    image.set(Pixel::D2([2, 1]), 1, true);
44    image.set(Pixel::D2([2, 2]), 1, false);
45
46    unsafe { image.assume_init() }
47}
48
49/// Create an image with four pixels, where the top left image is red, the top right
50/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
51/// gray.
52#[wll::export]
53fn test_create_color_rgb_u8_image() -> Image<u8> {
54    let width = 2;
55    let height = 2;
56    let channels = 3; // Red, green, and blue.
57
58    let mut image: UninitImage<u8> =
59        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
60
61    // Red, green, and blue channels indices.
62    const R: usize = 1;
63    const G: usize = 2;
64    const B: usize = 3;
65
66    // Set every pixel value to black. The image data is otherwise completely
67    // uninitialized memory, and can contain arbitrary values.
68    image.zero();
69
70    // Set the top left, top right, and bottom left pixels on only one color channel.
71    image.set(Pixel::D2([1, 1]), R, u8::MAX);
72    image.set(Pixel::D2([1, 2]), G, u8::MAX);
73    image.set(Pixel::D2([2, 1]), B, u8::MAX);
74
75    // Make this pixel white, by setting R, G, and B channels to ~80%.
76    image.set(Pixel::D2([2, 2]), R, 200);
77    image.set(Pixel::D2([2, 2]), G, 200);
78    image.set(Pixel::D2([2, 2]), B, 200);
79
80    unsafe { image.assume_init() }
81}
82
83/// Create an image with four pixels, where the top left image is red, the top right
84/// pixel is green, the bottom left pixel is blue, and the bottom right pixel is light
85/// gray.
86#[wll::export]
87fn test_create_color_rgb_f32_image() -> Image<f32> {
88    let width = 2;
89    let height = 2;
90    let channels = 3; // Red, green, and blue.
91
92    let mut image: UninitImage<f32> =
93        UninitImage::new_2d(width, height, channels, ColorSpace::RGB, false);
94
95    // Red, green, and blue channels indices.
96    const R: usize = 1;
97    const G: usize = 2;
98    const B: usize = 3;
99
100    // Set every pixel value to black. The image data is otherwise completely
101    // uninitialized memory, and can contain arbitrary values.
102    image.zero();
103
104    // Set the top left, top right, and bottom left pixels on only one color channel.
105    image.set(Pixel::D2([1, 1]), R, 1.0);
106    image.set(Pixel::D2([1, 2]), G, 1.0);
107    image.set(Pixel::D2([2, 1]), B, 1.0);
108
109    // Make this pixel white, by setting R, G, and B channels to 80%.
110    image.set(Pixel::D2([2, 2]), R, 0.8);
111    image.set(Pixel::D2([2, 2]), G, 0.8);
112    image.set(Pixel::D2([2, 2]), B, 0.8);
113
114    unsafe { image.assume_init() }
115}

Auto Trait Implementations§

§

impl<T> !Send for UninitImage<T>

§

impl<T> !Sync for UninitImage<T>

§

impl<T> Freeze for UninitImage<T>

§

impl<T> RefUnwindSafe for UninitImage<T>
where T: RefUnwindSafe,

§

impl<T> Unpin for UninitImage<T>
where T: Unpin,

§

impl<T> UnsafeUnpin for UninitImage<T>

§

impl<T> UnwindSafe for UninitImage<T>
where T: UnwindSafe,

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.