Debian Codegraph @

.cargo/vendor/rgb/src/legacy/alt.rs

raw
1 use crate::legacy::internal::pixel::{ComponentMap, ColorComponentMap, ComponentSlice};
2 use core::slice;
3
4 pub use crate::formats::gray::Gray_v08 as Gray;
5 pub use crate::formats::gray_alpha::GrayAlpha_v08 as GrayAlpha;
6
7 /// Renamed to `Bgra`
8 #[doc(hidden)]
9 pub use crate::formats::bgra::Bgra as BGRA;
10
11 /// Renamed to `Bgr`
12 #[doc(hidden)]
13 pub use crate::formats::bgr::Bgr as BGR;
14
15 /// Renamed to `Abgr`
16 #[doc(hidden)]
17 pub use crate::formats::abgr::Abgr as ABGR;
18
19 /// Renamed to `Argb`
20 #[doc(hidden)]
21 pub use crate::formats::argb::Argb as ARGB;
22
23 /// Renamed to `Grb`
24 #[doc(hidden)]
25 pub use crate::formats::grb::Grb as GRB;
26
27 /// 8-bit BGR
28 pub type BGR8 = crate::formats::bgr::Bgr<u8>;
29
30 /// 16-bit BGR in machine's native endian
31 pub type BGR16 = crate::formats::bgr::Bgr<u16>;
32
33 /// 8-bit BGRA
34 pub type BGRA8 = crate::formats::bgra::Bgra<u8>;
35
36 /// 8-bit ABGR, alpha is first. 0 = transparent, 255 = opaque.
37 pub type ABGR8 = crate::formats::abgr::Abgr<u8>;
38
39 /// 8-bit ARGB, alpha is first. 0 = transparent, 255 = opaque.
40 pub type ARGB8 = crate::Argb<u8>;
41
42 /// 16-bit BGR in machine's native endian
43 pub type BGRA16 = crate::formats::bgra::Bgra<u16>;
44
45 /// 16-bit ABGR in machine's native endian. 0 = transparent, 65535 = opaque.
46 pub type ABGR16 = crate::formats::abgr::Abgr<u16>;
47
48 /// 16-bit ARGB in machine's native endian. 0 = transparent, 65535 = opaque.
49 pub type ARGB16 = crate::Argb<u16>;
50
51 /// 8-bit GRB
52 pub 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)")]
56 pub 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)")]
60 pub 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)")]
64 pub 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)")]
68 pub type GRAYA16 = GrayAlpha<u16>;
69
70
71 #[cfg(not(feature = "unstable-experimental"))]
72 impl<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
82 impl<T: Copy> From<T> for Gray<T> {
83 #[inline(always)]
84 fn from(component: T) -> Self {
85 Self(component)
86 }
87 }
88
89 impl<T: Clone, A> GrayAlpha<T, A> {
90 /// Copy `Gray` component out of the `GrayAlpha` struct
91 #[inline(always)]
92 #[allow(deprecated)]
93 pub fn gray(&self) -> Gray<T> {
94 Gray(self.0.clone())
95 }
96 }
97
98 impl<T, A> GrayAlpha<T, A> {
99 /// Provide a mutable view of only `Gray` component (leaving out alpha).
100 #[inline(always)]
101 pub fn gray_mut(&mut self) -> &mut Gray<T> {
102 unsafe { &mut *(self as *mut Self).cast() }
103 }
104 }
105
106 impl<T: Copy, A: Clone> GrayAlpha<T, A> {
107 /// Create a new `GrayAlpha` with the new alpha value, but same gray value
108 #[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 /// Create a new `GrayAlpha` with the new alpha value, but same gray value
115 #[inline(always)]
116 #[allow(deprecated)]
117 pub fn with_alpha(&self, a: A) -> Self {
118 Self(self.0, a)
119 }
120
121 /// Create a new `GrayAlpha` with a new alpha value created by the callback.
122 #[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 /// Create new `GrayAlpha` with the same alpha value, but different `Gray` value
131 #[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
139 impl<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
147 impl<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
155 impl<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
164 impl<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
173 impl<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
189 impl<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
205 impl<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
219 impl<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
235 /// Assumes 255 is opaque
236 impl<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
244 /// Assumes 65535 is opaque
245 impl<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)]
255 fn 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 }