| 1 | //! Extract pixel samples from a block of pixel bytes. |
| 2 | |
| 3 | use craterust-analyzer cargo exr 1.74.0 crate/::preluderust-analyzer cargo exr 1.74.0 prelude/::*; |
| 4 | use halfrust-analyzer cargo half 2.6.0 crate/::preluderust-analyzer cargo half 2.6.0 prelude/::HalfFloatSliceExtrust-analyzer cargo half 2.6.0 slice/HalfFloatSliceExt#; |
| 5 | |
| 6 | |
| 7 | /// A single red, green, blue, or alpha value. |
| 8 | #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy:, Clonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core clone/Clone:, Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:)] |
| 9 | pub enum Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 10 | |
| 11 | /// A 16-bit float sample. |
| 12 | F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(f16rust-analyzer cargo half 2.6.0 binary16/f16#), |
| 13 | |
| 14 | /// A 32-bit float sample. |
| 15 | F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(f32), |
| 16 | |
| 17 | /// An unsigned integer sample. |
| 18 | U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(u32) |
| 19 | } |
| 20 | |
| 21 | impl Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 22 | |
| 23 | /// Create a sample containing a 32-bit float. |
| 24 | pub fn f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]f32().(f32: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(f32) } |
| 25 | |
| 26 | /// Create a sample containing a 16-bit float. |
| 27 | pub fn f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]f16().(f16rust-analyzer cargo half 2.6.0 binary16/f16#: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(f16rust-analyzer cargo half 2.6.0 binary16/f16#) } |
| 28 | |
| 29 | /// Create a sample containing a 32-bit integer. |
| 30 | pub fn u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]u32().(u32: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(u32) } |
| 31 | |
| 32 | /// Convert the sample to an f16 value. This has lower precision than f32. |
| 33 | /// Note: An f32 can only represent integers up to `1024` as precise as a u32 could. |
| 34 | #[inline] |
| 35 | pub fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f16().(self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16# { |
| 36 | match self { |
| 37 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(sample) => sample, |
| 38 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(sample) => f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(sample), |
| 39 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(sample) => f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(sample as f32), |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | /// Convert the sample to an f32 value. |
| 44 | /// Note: An f32 can only represent integers up to `8388608` as precise as a u32 could. |
| 45 | #[inline] |
| 46 | pub fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f32().(self) -> f32 { |
| 47 | match self { |
| 48 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(sample) => sample, |
| 49 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(sample) => sample.to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().(), |
| 50 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(sample) => sample as f32, |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /// Convert the sample to a u32. Rounds floats to integers the same way that `3.1 as u32` does. |
| 55 | #[inline] |
| 56 | pub fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_u32().(self) -> u32 { |
| 57 | match self { |
| 58 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(sample) => sample.to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().() as u32, |
| 59 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(sample) => sample as u32, |
| 60 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(sample) => sample, |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /// Is this value not a number? |
| 65 | #[inline] |
| 66 | pub fn is_nanrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]is_nan().(self) -> bool { |
| 67 | match self { |
| 68 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(value) => value.is_nanrust-analyzer cargo half 2.6.0 binary16/impl#[f16]is_nan().(), |
| 69 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(value) => value.is_nanrust-analyzer cargo core https://github.com/rust-lang/rust/library/core f32/impl#[f32]is_nan().(), |
| 70 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(_) => false, |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | /// Is this value zero or negative zero? |
| 75 | #[inline] |
| 76 | pub fn is_zerorust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]is_zero().(&self) -> bool { |
| 77 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 78 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(value) => value rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().==rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq(). rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().f16rust-analyzer cargo half 2.6.0 binary16/f16#::ZEROrust-analyzer cargo half 2.6.0 binary16/impl#[f16]ZERO. || value rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().==rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq(). rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().f16rust-analyzer cargo half 2.6.0 binary16/f16#::NEG_ZEROrust-analyzer cargo half 2.6.0 binary16/impl#[f16]NEG_ZERO., |
| 79 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(value) => value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().0.0, |
| 80 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(value) => value rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().0, |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | impl PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq# for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 86 | fn eqrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`PartialEq<Self>`]eq().(&self, other: &Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`PartialEq<Self>`]) -> bool { |
| 87 | match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self { |
| 88 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(num) => num rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().==rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq(). rust-analyzer cargo half 2.6.0 binary16/impl#[f16][`PartialEq<Self>`]eq().other.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_f16().(), |
| 89 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(num) => num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[f32][`PartialEq<Self>`]eq().other.to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_f32().(), |
| 90 | Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(num) => num rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u32][`PartialEq<Self>`]eq().other.to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_u32().(), |
| 91 | } |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | // this is not recommended because it may hide whether a color is transparent or opaque and might be undesired for depth channels |
| 96 | impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 97 | fn defaultrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][Default]default().() -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][Default] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(0.0) } |
| 98 | } |
| 99 | |
| 100 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<f16rust-analyzer cargo half 2.6.0 binary16/f16#> for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f16>`]from().(f: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f16>`] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F16rust-analyzer cargo exr 1.74.0 block/samples/Sample#F16#(f) } } |
| 101 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<f32> for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f32>`]from().(f: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f32>`] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::F32rust-analyzer cargo exr 1.74.0 block/samples/Sample#F32#(f) } } |
| 102 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<u32> for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<u32>`]from().(f: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<u32>`] { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::U32rust-analyzer cargo exr 1.74.0 block/samples/Sample#U32#(f) } } |
| 103 | |
| 104 | impl<T> Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>> for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# where T: Intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#<Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#> + Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# { |
| 105 | #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<Option<T>>`]from().(num: Optionrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#<T>) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<Option<T>>`] { num.unwrap_or_defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/impl#[`Option<T>`]unwrap_or_default().().intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/Into#into().() } |
| 106 | } |
| 107 | |
| 108 | |
| 109 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#> for f16rust-analyzer cargo half 2.6.0 binary16/f16# { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][`From<Sample>`]from().(s: Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][`From<Sample>`] { s.to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f16().() } } |
| 110 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#> for f32 { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][`From<Sample>`]from().(s: Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][`From<Sample>`] { s.to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f32().() } } |
| 111 | impl Fromrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/From#<Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#> for u32 { #[inline] fn fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][`From<Sample>`]from().(s: Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][`From<Sample>`] { s.to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_u32().() } } |
| 112 | |
| 113 | |
| 114 | /// Create an arbitrary sample type from one of the defined sample types. |
| 115 | /// Should be compiled to a no-op where the file contains the predicted sample type. |
| 116 | /// The slice functions should be optimized into a `memcpy` where there is no conversion needed. |
| 117 | pub trait FromNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#: Sizedrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sized# + Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + 'static { |
| 118 | |
| 119 | /// Create this sample from a f16, trying to represent the same numerical value |
| 120 | fn from_f16rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f16().(value: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Self; |
| 121 | |
| 122 | /// Create this sample from a f32, trying to represent the same numerical value |
| 123 | fn from_f32rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f32().(value: f32) -> Self; |
| 124 | |
| 125 | /// Create this sample from a u32, trying to represent the same numerical value |
| 126 | fn from_u32rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_u32().(value: u32) -> Self; |
| 127 | |
| 128 | /// Convert all values from the slice into this type. |
| 129 | /// This function exists to allow the compiler to perform a vectorization optimization. |
| 130 | /// Note that this default implementation will **not** be vectorized by the compiler automatically. |
| 131 | /// For maximum performance you will need to override this function and implement it via |
| 132 | /// an explicit batched conversion such as [`convert_to_f32_slice`](https://docs.rs/half/2.3.1/half/slice/trait.HalfFloatSliceExt.html#tymethod.convert_to_f32_slice) |
| 133 | #[inline] |
| 134 | fn from_f16srust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f16s().(from: &[f16rust-analyzer cargo half 2.6.0 binary16/f16#], to: &mut [Self]) { |
| 135 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(from.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), to.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "slices must have the same length"); |
| 136 | for (from, to) in from.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(to.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) { |
| 137 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().to = Self::from_f16rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().from); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | /// Convert all values from the slice into this type. |
| 142 | /// This function exists to allow the compiler to perform a vectorization optimization. |
| 143 | /// Note that this default implementation will be vectorized by the compiler automatically. |
| 144 | #[inline] |
| 145 | fn from_f32srust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f32s().(from: &[f32], to: &mut [Self]) { |
| 146 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(from.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), to.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "slices must have the same length"); |
| 147 | for (from, to) in from.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(to.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) { |
| 148 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().to = Self::from_f32rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_f32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().from); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /// Convert all values from the slice into this type. |
| 153 | /// This function exists to allow the compiler to perform a vectorization optimization. |
| 154 | /// Note that this default implementation will be vectorized by the compiler automatically, |
| 155 | /// provided that the CPU supports the necessary conversion instructions. |
| 156 | /// For example, x86_64 lacks the instructions to convert `u32` to floats, |
| 157 | /// so this will inevitably be slow on x86_64. |
| 158 | #[inline] |
| 159 | fn from_u32srust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_u32s().(from: &[u32], to: &mut [Self]) { |
| 160 | assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(from.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), to.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), "slices must have the same length"); |
| 161 | for (from, to) in from.iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().ziprust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#zip().(to.iter_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter_mut().()) { |
| 162 | *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&mut T`][Deref]deref().to = Self::from_u32rust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample#from_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().from); |
| 163 | } |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | // TODO haven't i implemented this exact behaviour already somewhere else in this library...?? |
| 168 | impl FromNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample# for f32 { |
| 169 | #[inline] fn from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_f16().(value: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample] { value.to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().() } |
| 170 | #[inline] fn from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_f32().(value: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample] { value } |
| 171 | #[inline] fn from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_u32().(value: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample] { value as f32 } |
| 172 | |
| 173 | // f16 is a custom type |
| 174 | // so the compiler can not automatically vectorize the conversion |
| 175 | // that's why we need to specialize this function |
| 176 | #[inline] |
| 177 | fn from_f16srust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_f16s().(from: &[f16rust-analyzer cargo half 2.6.0 binary16/f16#], to: &mut [Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]]) { |
| 178 | from.convert_to_f32_slicerust-analyzer cargo half 2.6.0 slice/impl#[`[f16]`][HalfFloatSliceExt]convert_to_f32_slice().(to); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | impl FromNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample# for u32 { |
| 183 | #[inline] fn from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_f16().(value: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample] { value.to_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]to_f32().() as u32 } |
| 184 | #[inline] fn from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_f32().(value: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample] { value as u32 } |
| 185 | #[inline] fn from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_u32().(value: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample] { value } |
| 186 | } |
| 187 | |
| 188 | impl FromNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample# for f16rust-analyzer cargo half 2.6.0 binary16/f16# { |
| 189 | #[inline] fn from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_f16().(value: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample] { value } |
| 190 | #[inline] fn from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_f32().(value: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample] { f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(value) } |
| 191 | #[inline] fn from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_u32().(value: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample] { f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(value as f32) } |
| 192 | |
| 193 | // f16 is a custom type |
| 194 | // so the compiler can not automatically vectorize the conversion |
| 195 | // that's why we need to specialize this function |
| 196 | #[inline] |
| 197 | fn from_f32srust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_f32s().(from: &[f32], to: &mut [Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]]) { |
| 198 | to.convert_from_f32_slicerust-analyzer cargo half 2.6.0 slice/impl#[`[f16]`][HalfFloatSliceExt]convert_from_f32_slice().(from) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | impl FromNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/FromNativeSample# for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 203 | #[inline] fn from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]from_f16().(value: f16rust-analyzer cargo half 2.6.0 binary16/f16#) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample] { Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]::fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f16>`]from().(value) } |
| 204 | #[inline] fn from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]from_f32().(value: f32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample] { Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]::fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<f32>`]from().(value) } |
| 205 | #[inline] fn from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]from_u32().(value: u32) -> Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample] { Selfrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][FromNativeSample]::fromrust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][`From<u32>`]from().(value) } |
| 206 | } |
| 207 | |
| 208 | |
| 209 | /// Convert any type into one of the supported sample types. |
| 210 | /// Should be compiled to a no-op where the file contains the predicted sample type |
| 211 | pub trait IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample#: Copyrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Copy# + Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# + Syncrust-analyzer cargo core https://github.com/rust-lang/rust/library/core marker/Sync# + 'static { |
| 212 | |
| 213 | /// Convert this sample to an f16, trying to represent the same numerical value. |
| 214 | fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample#to_f16().(&self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16#; |
| 215 | |
| 216 | /// Convert this sample to an f32, trying to represent the same numerical value. |
| 217 | fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample#to_f32().(&self) -> f32; |
| 218 | |
| 219 | /// Convert this sample to an u16, trying to represent the same numerical value. |
| 220 | fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample#to_u32().(&self) -> u32; |
| 221 | } |
| 222 | |
| 223 | impl IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample# for f16rust-analyzer cargo half 2.6.0 binary16/f16# { |
| 224 | fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][IntoNativeSample]to_f16().(&self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16# { f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_f16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 225 | fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][IntoNativeSample]to_f32().(&self) -> f32 { f32::from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_f16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 226 | fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][IntoNativeSample]to_u32().(&self) -> u32 { u32::from_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_f16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 227 | } |
| 228 | |
| 229 | impl IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample# for f32 { |
| 230 | fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f16().(&self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16# { f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_f32rust-analyzer cargo half 2.6.0 binary16/impl#[f16]from_f32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 231 | fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_f32().(&self) -> f32 { f32::from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_f32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 232 | fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][IntoNativeSample]to_u32().(&self) -> u32 { u32::from_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_f32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 233 | } |
| 234 | |
| 235 | impl IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample# for u32 { |
| 236 | fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][IntoNativeSample]to_f16().(&self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16# { f16rust-analyzer cargo half 2.6.0 binary16/f16#::from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f16][FromNativeSample]from_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 237 | fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][IntoNativeSample]to_f32().(&self) -> f32 { f32::from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[f32][FromNativeSample]from_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 238 | fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][IntoNativeSample]to_u32().(&self) -> u32 { u32::from_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[u32][FromNativeSample]from_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 239 | } |
| 240 | |
| 241 | impl IntoNativeSamplerust-analyzer cargo exr 1.74.0 block/samples/IntoNativeSample# for Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample# { |
| 242 | fn to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_f16().(&self) -> f16rust-analyzer cargo half 2.6.0 binary16/f16# { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::to_f16rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f16().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 243 | fn to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_f32().(&self) -> f32 { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::to_f32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_f32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 244 | fn to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample][IntoNativeSample]to_u32().(&self) -> u32 { Samplerust-analyzer cargo exr 1.74.0 block/samples/Sample#::to_u32rust-analyzer cargo exr 1.74.0 block/samples/impl#[Sample]to_u32().(*rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self) } |
| 245 | } |
| 246 | |
| 247 | |
| 248 |