1use crate::legacy::internal::pixel::{ComponentMap, ColorComponentMap, ComponentSlice};
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#[doc(hidden)]
9pub use crate::formats::bgra::Bgra as BGRA;
10
11#[doc(hidden)]
13pub use crate::formats::bgr::Bgr as BGR;
14
15#[doc(hidden)]
17pub use crate::formats::abgr::Abgr as ABGR;
18
19#[doc(hidden)]
21pub use crate::formats::argb::Argb as ARGB;
22
23#[doc(hidden)]
25pub use crate::formats::grb::Grb as GRB;
26
27pub type BGR8 = crate::formats::bgr::Bgr<u8>;
29
30pub type BGR16 = crate::formats::bgr::Bgr<u16>;
32
33pub type BGRA8 = crate::formats::bgra::Bgra<u8>;
35
36pub type ABGR8 = crate::formats::abgr::Abgr<u8>;
38
39pub type ARGB8 = crate::Argb<u8>;
41
42pub type BGRA16 = crate::formats::bgra::Bgra<u16>;
44
45pub type ABGR16 = crate::formats::abgr::Abgr<u16>;
47
48pub type ARGB16 = crate::Argb<u16>;
50
51pub type GRB8 = crate::formats::grb::Grb<u8>;
53
54#[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#[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#[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#[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 #[allow(deprecated)]
77 fn deref(&self) -> &T {
78 &self.0
79 }
80}
81
82impl<T: Copy> From<T> for Gray<T> {
83 #[inline(always)]
84 fn from(component: T) -> Self {
85 Self(component)
86 }
87}
88
89impl<T: Clone, A> GrayAlpha<T, A> {
90 #[inline(always)]
92 #[allow(deprecated)]
93 pub fn gray(&self) -> Gray<T> {
94 Gray(self.0.clone())
95 }
96}
97
98impl<T, A> GrayAlpha<T, A> {
99 #[inline(always)]
101 pub fn gray_mut(&mut self) -> &mut Gray<T> {
102 unsafe { &mut *(self as *mut Self).cast() }
103 }
104}
105
106impl<T: Copy, A: Clone> GrayAlpha<T, A> {
107 #[doc(hidden)]
109 #[deprecated(note = "use .with_alpha(a) instead; this will become a getter in the future")]
110 pub fn alpha(&self, a: A) -> Self {
111 self.with_alpha(a)
112 }
113
114 #[inline(always)]
116 #[allow(deprecated)]
117 pub fn with_alpha(&self, a: A) -> Self {
118 Self(self.0, a)
119 }
120
121 #[inline(always)]
123 #[allow(deprecated)]
124 pub fn map_alpha<F, B>(&self, f: F) -> GrayAlpha<T, B>
125 where F: FnOnce(A) -> B
126 {
127 GrayAlpha(self.0, f(self.1.clone()))
128 }
129
130 #[inline(always)]
132 #[allow(deprecated)]
133 pub fn map_gray<F, U, B>(&self, f: F) -> GrayAlpha<U, B>
134 where F: FnOnce(T) -> U, U: Clone, B: From<A> + Clone {
135 GrayAlpha(f(self.0), self.1.clone().into())
136 }
137}
138
139impl<T: Copy, B> ComponentMap<Gray<B>, T, B> for Gray<T> {
140 #[inline(always)]
141 #[allow(deprecated)]
142 fn map<F>(&self, mut f: F) -> Gray<B> where F: FnMut(T) -> B {
143 Gray(f(self.0))
144 }
145}
146
147impl<T: Copy, B> ColorComponentMap<Gray<B>, T, B> for Gray<T> {
148 #[inline(always)]
149 #[allow(deprecated)]
150 fn map_colors<F>(&self, mut f: F) -> Gray<B> where F: FnMut(T) -> B {
151 Gray(f(self.0))
152 }
153}
154
155impl<T: Copy, B> ComponentMap<GrayAlpha<B>, T, B> for GrayAlpha<T> {
156 #[inline(always)]
157 #[allow(deprecated)]
158 fn map<F>(&self, mut f: F) -> GrayAlpha<B>
159 where F: FnMut(T) -> B {
160 GrayAlpha(f(self.0), f(self.1))
161 }
162}
163
164impl<T: Copy, A: Copy, B> ColorComponentMap<GrayAlpha<B, A>, T, B> for GrayAlpha<T, A> {
165 #[inline(always)]
166 #[allow(deprecated)]
167 fn map_colors<F>(&self, mut f: F) -> GrayAlpha<B, A>
168 where F: FnMut(T) -> B {
169 GrayAlpha(f(self.0), self.1)
170 }
171}
172
173impl<T> ComponentSlice<T> for GrayAlpha<T> {
174 #[inline(always)]
175 fn as_slice(&self) -> &[T] {
176 unsafe {
177 slice::from_raw_parts((self as *const Self).cast::<T>(), 2)
178 }
179 }
180
181 #[inline(always)]
182 fn as_mut_slice(&mut self) -> &mut [T] {
183 unsafe {
184 slice::from_raw_parts_mut((self as *mut Self).cast::<T>(), 2)
185 }
186 }
187}
188
189impl<T> ComponentSlice<T> for [GrayAlpha<T>] {
190 #[inline]
191 fn as_slice(&self) -> &[T] {
192 unsafe {
193 slice::from_raw_parts(self.as_ptr().cast(), self.len() * 2)
194 }
195 }
196
197 #[inline]
198 fn as_mut_slice(&mut self) -> &mut [T] {
199 unsafe {
200 slice::from_raw_parts_mut(self.as_mut_ptr().cast::<T>(), self.len() * 2)
201 }
202 }
203}
204
205impl<T> ComponentSlice<T> for Gray<T> {
206 #[inline(always)]
207 #[allow(deprecated)]
208 fn as_slice(&self) -> &[T] {
209 slice::from_ref(&self.0)
210 }
211
212 #[inline(always)]
213 #[allow(deprecated)]
214 fn as_mut_slice(&mut self) -> &mut [T] {
215 slice::from_mut(&mut self.0)
216 }
217}
218
219impl<T> ComponentSlice<T> for [Gray<T>] {
220 #[inline]
221 fn as_slice(&self) -> &[T] {
222 unsafe {
223 slice::from_raw_parts(self.as_ptr().cast(), self.len())
224 }
225 }
226
227 #[inline]
228 fn as_mut_slice(&mut self) -> &mut [T] {
229 unsafe {
230 slice::from_raw_parts_mut(self.as_mut_ptr().cast::<T>(), self.len())
231 }
232 }
233}
234
235impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u8> {
237 #[inline(always)]
238 #[allow(deprecated)]
239 fn from(other: Gray<T>) -> Self {
240 Self(other.0, 0xFF)
241 }
242}
243
244impl<T: Copy> From<Gray<T>> for GrayAlpha<T, u16> {
246 #[inline(always)]
247 #[allow(deprecated)]
248 fn from(other: Gray<T>) -> Self {
249 Self(other.0, 0xFFFF)
250 }
251}
252
253#[test]
254#[allow(deprecated)]
255fn gray() {
256 let rgb: crate::RGB<_> = Gray(1).into();
257 assert_eq!(rgb.r, 1);
258 assert_eq!(rgb.g, 1);
259 assert_eq!(rgb.b, 1);
260
261 let rgba: crate::RGBA<_> = Gray(1u8).into();
262 assert_eq!(rgba.r, 1);
263 assert_eq!(rgba.g, 1);
264 assert_eq!(rgba.b, 1);
265 assert_eq!(rgba.a, 255);
266
267 let g: GRAY8 = 200.into();
268 let g = g.map(|c| c / 2);
269 #[cfg(not(feature = "unstable-experimental"))]
270 assert_eq!(110, *g + 10);
271 #[cfg(not(feature = "unstable-experimental"))]
272 assert_eq!(110, 10 + Gray(100).as_ref());
273
274 let ga: GRAYA8 = GrayAlpha(1, 2);
275 assert_eq!(ga.gray(), Gray::new(1));
276 let mut g2 = ga.clone();
277 *g2.gray_mut() = Gray(3);
278 assert_eq!(g2.map_gray(|g| g + 1), GRAYA8::new(4, 2));
279 assert_eq!(g2.map(|g| g + 1), GrayAlpha(4, 3));
280 assert_eq!(g2.0, 3);
281 assert_eq!(g2.as_slice(), &[3, 2]);
282 assert_eq!(g2.as_mut_slice(), &[3, 2]);
283 assert_eq!(g2.with_alpha(13), GrayAlpha(3, 13));
284 assert_eq!(g2.map_alpha(|x| x + 3), GrayAlpha(3, 5));
285
286 assert_eq!((&[Gray(1u16), Gray(2)][..]).as_slice(), &[1, 2]);
287 assert_eq!((&[GrayAlpha(1u16, 2), GrayAlpha(3, 4)][..]).as_slice(), &[1, 2, 3, 4]);
288
289 let rgba: crate::RGBA<_> = ga.into();
290 assert_eq!(rgba.r, 1);
291 assert_eq!(rgba.g, 1);
292 assert_eq!(rgba.b, 1);
293 assert_eq!(rgba.a, 2);
294
295 let ga: GRAYA16 = GrayAlpha(1, 2);
296 let rgba: crate::RGBA<u16, u16> = ga.into();
297 assert_eq!(rgba.r, 1);
298 assert_eq!(rgba.g, 1);
299 assert_eq!(rgba.b, 1);
300 assert_eq!(rgba.a, 2);
301}