Debian Codegraph @

src/legacy/internal/pixel.rs

raw symbols SCIP
1 /// Casting the struct to slices of its components
2 #[deprecated(note = "use `bytemuck::cast_slice()` instead")]
3 pub trait ComponentSlice<T> {
4 /// The components interpreted as an array, e.g. one `RGB` expands to 3 elements.
5 ///
6 /// It's implemented for individual pixels as well as slices of pixels.
7 #[deprecated(note = "use `bytemuck::cast_slice()` instead; or at least call it like ComponentSlice::as_slice(px)")]
8 fn as_slice(&self) -> &[T];
9
10 /// The components interpreted as a mutable array, e.g. one `RGB` expands to 3 elements.
11 ///
12 /// It's implemented for individual pixels as well as slices of pixels.
13 ///
14 /// If you get an error when calling this on an array, add `[..]`
15 ///
16 /// > use of unstable library feature 'array_methods'
17 ///
18 /// ```rust,ignore
19 /// arr[..].as_mut_slice()
20 /// ```
21 #[deprecated(note = "use `bytemuck::cast_slice_mut()` instead; or at least call it like ComponentSlice::as_mut_slice(px)")]
22 fn as_mut_slice(&mut self) -> &mut [T];
23 }
24
25 /// Use [`::bytemuck::cast_slice()`] instead.
26 ///
27 /// Casting a slice of `RGB/A` values to a slice of `u8`
28 ///
29 /// If instead of `RGB8` you use `RGB<MyCustomType>`, and you want to cast from/to that custom type,
30 /// implement the `Plain` trait for it:
31 ///
32 /// ```rust
33 /// # #[derive(Copy, Clone)]
34 /// # struct MyCustomType;
35 /// unsafe impl rgb::Pod for MyCustomType {}
36 /// unsafe impl rgb::Zeroable for MyCustomType {}
37 /// ```
38 ///
39 /// Plain types are not allowed to contain struct padding, booleans, chars, enums, references or pointers.
40 #[cfg(feature = "as-bytes")]
41 #[allow(deprecated)]
42 pub trait ComponentBytes<T: crate::Pod> where Self: ComponentSlice<T> {
43 /// The components interpreted as raw bytes, in machine's native endian. In `RGB` bytes of the red component are first.
44 #[inline]
45 fn as_bytes(&self) -> &[u8] {
46 assert_ne!(0, core::mem::size_of::<T>());
47 #[allow(deprecated)]
48 let slice = ComponentSlice::as_slice(self);
49 unsafe {
50 core::slice::from_raw_parts(slice.as_ptr().cast(), core::mem::size_of_val(slice))
51 }
52 }
53
54 /// The components interpreted as raw bytes, in machine's native endian. In `RGB` bytes of the red component are first.
55 #[inline]
56 fn as_bytes_mut(&mut self) -> &mut [u8] {
57 assert_ne!(0, core::mem::size_of::<T>());
58 #[allow(deprecated)]
59 let slice = ComponentSlice::as_mut_slice(self);
60 unsafe {
61 core::slice::from_raw_parts_mut(slice.as_mut_ptr().cast(), core::mem::size_of_val(slice))
62 }
63 }
64 }
65
66 /// Applying operation to every component
67 ///
68 /// ```rust
69 /// use rgb::prelude::*;
70 /// # let pixel = rgb::RGB::new(0u8,0,0);
71 /// let inverted = pixel.map(|c| 255 - c);
72 ///
73 /// // For simple math there are Add/Sub/Mul implementations:
74 /// let halved = pixel.map(|c| c / 2);
75 /// let doubled = pixel * 2;
76 /// ```
77 pub trait ComponentMap<DestPixel, SrcComponent, DestComponent> {
78 /// Convenience function (equivalent of `self.iter().map().collect()`) for applying the same formula to every component.
79 ///
80 /// Note that it returns the pixel directly, not an Interator.
81 fn map<Callback>(&self, f: Callback) -> DestPixel
82 where Callback: FnMut(SrcComponent) -> DestComponent;
83 }
84
85 /// Same as `ComponentMap`, but doesn't change the alpha channel (if there's any alpha).
86 ///
87 /// Import via `use rgb::prelude::*;` instead of directly.
88 pub trait ColorComponentMap<DestPixel, SrcComponent, DestComponent> {
89 /// Convenience function for applying the same formula to every rgb/gray component, but skipping the alpha component.
90 ///
91 /// Note that it returns the pixel directly, not an Interator.
92 #[doc(alias = "map_colors_same")]
93 fn map_colors<Callback>(&self, f: Callback) -> DestPixel
94 where Callback: FnMut(SrcComponent) -> DestComponent {
95 #[allow(deprecated)]
96 self.map_c(f)
97 }
98
99 /// Alias of `map_colors`
100 #[deprecated(note = "renamed to map_colors")]
101 fn map_c<Callback>(&self, f: Callback) -> DestPixel
102 where Callback: FnMut(SrcComponent) -> DestComponent {
103 self.map_colors(f)
104 }
105 }