| 1 | |
| 2 | //! Provides a predefined pixel storage. |
| 3 | //! Currently only contains a simple flattened vector storage. |
| 4 | //! Use the functions `create_pixel_vec::<YourPixelTuple>` and |
| 5 | //! `set_pixel_in_vec::<YourPixelTuple>` for reading a predefined pixel vector. |
| 6 | //! Use the function `PixelVec::new` to create a pixel vector which can be written to a file. |
| 7 | |
| 8 | use superrust-analyzer cargo exr 1.74.0 image/::*; |
| 9 | |
| 10 | /// Store all samples in a single array. |
| 11 | /// All samples will be converted to the type `T`. |
| 12 | /// This supports all the sample types, `f16`, `f32`, and `u32`. |
| 13 | /// |
| 14 | /// The flattened vector contains all rows one after another. |
| 15 | /// In each row, for each pixel, its red, green, blue, and then alpha |
| 16 | /// samples are stored one after another. |
| 17 | /// |
| 18 | /// Use `PixelVec.compute_pixel_index(position)` |
| 19 | /// to compute the flat index of a specific pixel. |
| 20 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/Eq:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:)] |
| 21 | pub struct PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<T> { |
| 22 | |
| 23 | /// The resolution of this layer. |
| 24 | pub resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution.: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, |
| 25 | |
| 26 | /// The flattened vector contains all rows one after another. |
| 27 | /// In each row, for each pixel, its red, green, blue, and then alpha |
| 28 | /// samples are stored one after another. |
| 29 | /// |
| 30 | /// Use `Flattened::compute_pixel_index(image, position)` |
| 31 | /// to compute the flat index of a specific pixel. |
| 32 | pub pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels.: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<T>, |
| 33 | } |
| 34 | |
| 35 | impl<Pixel> PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<Pixel> { |
| 36 | |
| 37 | /// Create a new flattened pixel storage, filled with default pixels. |
| 38 | /// Accepts a `Channels` parameter, which is not used, so that it can be passed as a function pointer instead of calling it. |
| 39 | pub fn constructorrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]constructor().<Channels>(resolution: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, _: &Channels) -> Selfrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`] where Pixel: Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# { |
| 40 | PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec# { resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution., pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels.: vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![Pixel::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#default().(); resolution.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().()] } |
| 41 | } |
| 42 | |
| 43 | /// Examine a pixel of a `PixelVec<T>` image. |
| 44 | /// Can usually be used as a function reference instead of calling it directly. |
| 45 | #[inline] |
| 46 | pub fn get_pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]get_pixel().(&self, position: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> &Pixel where Pixel: Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# { |
| 47 | &self.pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().self.compute_pixel_indexrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]compute_pixel_index().(position)]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index(). |
| 48 | } |
| 49 | |
| 50 | /// Update a pixel of a `PixelVec<T>` image. |
| 51 | /// Can usually be used as a function reference instead of calling it directly. |
| 52 | #[inline] |
| 53 | pub fn set_pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]set_pixel().(&mut self, position: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>, pixel: Pixel) { |
| 54 | let index = self.compute_pixel_indexrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]compute_pixel_index().(position); |
| 55 | self.pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels.[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut().index]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`IndexMut<I>`]index_mut(). = pixel; |
| 56 | } |
| 57 | |
| 58 | /// Create a new flattened pixel storage, checking the length of the provided pixels vector. |
| 59 | pub fn newrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]new().(resolution: impl Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>>, pixels: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<Pixel>) -> Selfrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`] { |
| 60 | let size = resolution.intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().(); |
| 61 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(size.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().(), pixels.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().(), "expected {} samples, but vector length is {}", size.arearust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]area().(), pixels.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()); |
| 62 | Selfrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`] { resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution.: size, pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels. } |
| 63 | } |
| 64 | |
| 65 | /// Compute the flat index of a specific pixel. Returns a range of either 3 or 4 samples. |
| 66 | /// The computed index can be used with `PixelVec.samples[index]`. |
| 67 | /// Panics for invalid sample coordinates. |
| 68 | #[inline] |
| 69 | pub fn compute_pixel_indexrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]compute_pixel_index().(&self, position: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> usize { |
| 70 | position.flat_index_for_sizerust-analyzer cargo exr 1.74.0 math/impl#[`Vec2<T>`]flat_index_for_size().(self.resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution.) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | use craterust-analyzer cargo exr 1.74.0 crate/::imagerust-analyzer cargo exr 1.74.0 image/::validate_resultsrust-analyzer cargo exr 1.74.0 image/validate_results/::{ValidateResultrust-analyzer cargo exr 1.74.0 image/validate_results/ValidateResult#, ValidationResultrust-analyzer cargo exr 1.74.0 image/validate_results/ValidationResult#}; |
| 75 | |
| 76 | impl<Px> ValidateResultrust-analyzer cargo exr 1.74.0 image/validate_results/ValidateResult# for PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<Px> where Px: ValidateResultrust-analyzer cargo exr 1.74.0 image/validate_results/ValidateResult# { |
| 77 | fn validate_resultrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Px>`][ValidateResult]validate_result().(&self, other: &Selfrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Px>`][ValidateResult], options: ValidationOptionsrust-analyzer cargo exr 1.74.0 image/validate_results/ValidationOptions#, location: impl Fnrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/Fn#() -> Stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/String#) -> ValidationResultrust-analyzer cargo exr 1.74.0 image/validate_results/ValidationResult# { |
| 78 | if self.resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq#ne().other.resolutionrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#resolution. { Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(location() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().+rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add()." > resolution") } |
| 79 | else { self.pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels..as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().().validate_resultrust-analyzer cargo exr 1.74.0 image/validate_results/impl#[`&[T]`][ValidateResult]validate_result().(&other.pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels..as_slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_slice().(), options, || location() rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add().+rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add(). rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[String][`Add<&str>`]add()." > pixels") } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | impl<Px> GetPixelrust-analyzer cargo exr 1.74.0 image/write/channels/GetPixel# for PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<Px> where Px: Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# { |
| 84 | type Pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Px>`][GetPixel]Pixel# = Px; |
| 85 | fn get_pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Px>`][GetPixel]get_pixel().(&self, position: Vec2rust-analyzer cargo exr 1.74.0 math/Vec2#<usize>) -> Selfrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Px>`][GetPixel]::Pixelrust-analyzer cargo exr 1.74.0 image/write/channels/GetPixel#Pixel# { |
| 86 | self.get_pixelrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<Pixel>`]get_pixel().(position).clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone#clone().() |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::*; |
| 91 | |
| 92 | impl<T> Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Debug# for PixelVecrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#<T> { |
| 93 | #[inline] fn fmtrust-analyzer cargo exr 1.74.0 image/pixel_vec/impl#[`PixelVec<T>`][Debug]fmt().(&self, formatter: &mut Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#<'_>) -> stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# { |
| 94 | writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(formatter, "[{}; {}]", stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::anyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/::type_namerust-analyzer cargo core https://github.com/rust-lang/rust/library/core any/type_name().::<T>(), self.pixelsrust-analyzer cargo exr 1.74.0 image/pixel_vec/PixelVec#pixels..lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().()) |
| 95 | } |
| 96 | } |
| 97 |