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>
impl<T: ImageData> UninitImage<T>
Sourcepub fn new_2d(
width: usize,
height: usize,
channels: usize,
space: ColorSpace,
interleaving: bool,
) -> UninitImage<T>
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?
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}
Sourcepub fn try_new_2d(
width: usize,
height: usize,
channels: usize,
space: ColorSpace,
interleaving: bool,
) -> Result<UninitImage<T>, i64>
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.
Sourcepub fn zero(&mut self)
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?
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}
Sourcepub fn set(&mut self, pixel: Pixel, channel: usize, value: T)
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?
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}
Sourcepub unsafe fn assume_init(self) -> Image<T>
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?
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}