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

Auto Trait Implementations§

§

impl<T> Freeze for UninitImage<T>

§

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

§

impl<T> !Send for UninitImage<T>

§

impl<T> !Sync for UninitImage<T>

§

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

§

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.