rgb/legacy/
alt.rs

1use crate::legacy::internal::pixel::{ComponentMap, ColorComponentMap};
2#[allow(deprecated)]
3use crate::legacy::internal::pixel::ComponentSlice;
4use core::slice;
5
6pub use crate::formats::gray::Gray_v08 as Gray;
7pub use crate::formats::gray_alpha::GrayAlpha_v08 as GrayAlpha;
8
9/// Renamed to `Bgra`
10#[doc(hidden)]
11pub use crate::formats::bgra::Bgra as BGRA;
12
13/// Renamed to `Bgr`
14#[doc(hidden)]
15pub use crate::formats::bgr::Bgr as BGR;
16
17/// Renamed to `Abgr`
18#[doc(hidden)]
19pub use crate::formats::abgr::Abgr as ABGR;
20
21/// Renamed to `Argb`
22#[doc(hidden)]
23pub use crate::formats::argb::Argb as ARGB;
24
25/// Renamed to `Grb`
26#[doc(hidden)]
27pub use crate::formats::grb::Grb as GRB;
28
29/// 8-bit BGR
30pub type BGR8 = crate::formats::bgr::Bgr<u8>;
31
32/// 16-bit BGR in machine's native endian
33pub type BGR16 = crate::formats::bgr::Bgr<u16>;
34
35/// 8-bit BGRA
36pub type BGRA8 = crate::formats::bgra::Bgra<u8>;
37
38/// 8-bit ABGR, alpha is first. 0 = transparent, 255 = opaque.
39pub type ABGR8 = crate::formats::abgr::Abgr<u8>;
40
41/// 8-bit ARGB, alpha is first. 0 = transparent, 255 = opaque.
42pub type ARGB8 = crate::Argb<u8>;
43
44/// 16-bit BGR in machine's native endian
45pub type BGRA16 = crate::formats::bgra::Bgra<u16>;
46
47/// 16-bit ABGR in machine's native endian. 0 = transparent, 65535 = opaque.
48pub type ABGR16 = crate::formats::abgr::Abgr<u16>;
49
50/// 16-bit ARGB in machine's native endian. 0 = transparent, 65535 = opaque.
51pub type ARGB16 = crate::Argb<u16>;
52
53/// 8-bit GRB
54pub type GRB8 = crate::formats::grb::Grb<u8>;
55
56/// 8-bit gray
57#[deprecated(note = "Refer to ::rgb::alt::Gray<u8> directly (this type alias will change in the next major version)")]
58pub type GRAY8 = Gray<u8>;
59
60/// 16-bit gray in machine's native endian
61#[deprecated(note = "Refer to ::rgb::alt::Gray<u16> directly (this type alias will change in the next major version)")]
62pub type GRAY16 = Gray<u16>;
63
64/// 8-bit gray with alpha in machine's native endian
65#[deprecated(note = "Refer to ::rgb::alt::GrayAlpha<u8> directly (this type alias will change in the next major version)")]
66pub type GRAYA8 = GrayAlpha<u8>;
67
68/// 16-bit gray with alpha in machine's native endian
69#[deprecated(note = "Refer to ::rgb::alt::GrayAlpha<u16> directly (this type alias will change in the next major version)")]
70pub type GRAYA16 = GrayAlpha<u16>;
71
72
73#[cfg(not(feature = "unstable-experimental"))]
74impl<T> core::ops::Deref for Gray<T> {
75    type Target = T;
76
77    #[inline(always)]
78    #[allow(deprecated)]
79    fn deref(&self) -> &T {
80        &self.0
81    }
82}
83
84impl<T: Copy> From<T> for Gray<T> {
85    #[inline(always)]
86    fn from(component: T) -> Self {
87        Self(component)
88    }
89}
90
91impl<T: Clone, A> GrayAlpha<T, A> {
92    /// Copy `Gray` component out of the `GrayAlpha` struct
93    #[inline(always)]
94    #[allow(deprecated)]
95    pub fn gray(&self) -> Gray<T> {
96        Gray(self.0.clone())
97    }
98}
99
100impl<T, A> GrayAlpha<T, A> {
101    /// Provide a mutable view of only `Gray` component (leaving out alpha).
102    #[inline(always)]
103    pub fn gray_mut(&mut self) -> &mut Gray<T> {
104        unsafe { &mut *(self as *mut Self).cast() }
105    }
106}
107
108impl<T: Copy, A: Clone> GrayAlpha<T, A> {
109    /// Create a new `GrayAlpha` with the new alpha value, but same gray value
110    #[doc(hidden)]
111    #[deprecated(note = "use .with_alpha(a) instead; this will become a getter in the future")]
112    pub fn alpha(&self, a: A) -> Self {
113        self.with_alpha(a)
114    }
115
116    /// Create a new `GrayAlpha` with the new alpha value, but same gray value
117    #[inline(always)]
118    #[allow(deprecated)]
119    pub fn with_alpha(&self, a: A) -> Self {
120        Self(self.0, a)
121    }
122
123    /// Create a new `GrayAlpha` with a new alpha value created by the callback.
124    #[inline(always)]
125    #[allow(deprecated)]
126    pub fn map_alpha<F, B>(&self, f: F) -> GrayAlpha<T, B>
127        where F: FnOnce(A) -> B
128    {
129        GrayAlpha(self.0, f(self.1.clone()))
130    }
131
132    /// Create new `GrayAlpha` with the same alpha value, but different `Gray` value
133    #[inline(always)]
134    #[allow(deprecated)]
135    pub fn map_gray<F, U, B>(&self, f: F) -> GrayAlpha<U, B>
136        where F: FnOnce(T) -> U, U: Clone, B: From<A> + Clone {
137        GrayAlpha(f(self.0), self.1.clone().into())
138    }
139}
140
141impl<T: Copy, B> ComponentMap<Gray<B>, T, B> for Gray<T> {
142    #[inline(always)]
143    #[allow(deprecated)]
144    fn map<F>(&self, mut f: F) -> Gray<B> where F: FnMut(T) -> B {
145        Gray(f(self.0))
146    }
147}
148
149impl<T: Copy, B> ColorComponentMap<Gray<B>, T, B> for Gray<T> {
150    #[inline(always)]
151    #[allow(deprecated)]
152    fn map_colors<F>(&self, mut f: F) -> Gray<B> where F: FnMut(T) -> B {
153        Gray(f(self.0))
154    }
155}
156
157impl<T: Copy, B> ComponentMap<GrayAlpha<B>, T, B> for GrayAlpha<T> {
158    #[inline(always)]
159    #[allow(deprecated)]
160    fn map<F>(&self, mut f: F) -> GrayAlpha<B>
161    where F: FnMut(T) -> B {
162        GrayAlpha(f(self.0), f(self.1))
163    }
164}
165
166impl<T: Copy, A: Copy, B> ColorComponentMap<GrayAlpha<B, A>, T, B> for GrayAlpha<T, A> {
167    #[inline(always)]
168    #[allow(deprecated)]
169    fn map_colors<F>(&self, mut f: F) -> GrayAlpha<B, A>
170    where F: FnMut(T) -> B {
171        GrayAlpha(f(self.0), self.1)
172    }
173}
174
175#[allow(deprecated)]
176impl<T> ComponentSlice<T> for GrayAlpha<T> {
177    #[inline(always)]
178    fn as_slice(&self) -> &[T] {
179        unsafe {
180            slice::from_raw_parts((self as *const Self).cast::<T>(), 2)
181        }
182    }
183
184    #[inline(always)]
185    fn as_mut_slice(&mut self) -> &mut [T] {
186        unsafe {
187            slice::from_raw_parts_mut((self as *mut Self).cast::<T>(), 2)
188        }
189    }
190}
191
192#[allow(deprecated)]
193impl<T> ComponentSlice<T> for [GrayAlpha<T>] {
194    #[inline]
195    fn as_slice(&self) -> &[T] {
196        unsafe {
197            slice::from_raw_parts(self.as_ptr().cast(), self.len() * 2)
198        }
199    }
200
201    #[inline]
202    fn as_mut_slice(&mut self) -> &mut [T] {
203        unsafe {
204            slice::from_raw_parts_mut(self.as_mut_ptr().cast::<T>(), self.len() * 2)
205        }
206    }
207}
208
209#[allow(deprecated)]
210impl<T> ComponentSlice<T> for Gray<T> {
211    #[inline(always)]
212    #[allow(deprecated)]
213    fn as_slice(&self) -> &[T] {
214        slice::from_ref(&self.0)
215    }
216
217    #[inline(always)]
218    #[allow(deprecated)]
219    fn as_mut_slice(&mut self) -> &mut [T] {
220        slice::from_mut(&mut self.0)
221    }
222}
223
224#[allow(deprecated)]
225impl<T> ComponentSlice<T> for [Gray<T>] {
226    #[inline]
227    fn as_slice(&self) -> &[T] {
228        unsafe {
229            slice::from_raw_parts(self.as_ptr().cast(), self.len())
230        }
231    }
232
233    #[inline]
234    fn as_mut_slice(&mut self) -> &mut [T] {
235        unsafe {
236            slice::from_raw_parts_mut(self.as_mut_ptr().cast::<T>(), self.len())
237        }
238    }
239}
240
241/// Assumes 255 is opaque
242impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u8> {
243    #[inline(always)]
244    #[allow(deprecated)]
245    fn from(other: Gray<T>) -> Self {
246        Self(other.0, 0xFF)
247    }
248}
249
250/// Assumes 65535 is opaque
251impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u16> {
252    #[inline(always)]
253    #[allow(deprecated)]
254    fn from(other: Gray<T>) -> Self {
255        Self(other.0, 0xFFFF)
256    }
257}