rgb/legacy/
alt.rs

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