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?
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}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?
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}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?
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}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?
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}