1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
//! This module provides useful traits that were deprecated in rust
// Note copied from the stdlib under MIT license
use num_traits::{Bounded, Num, NumCast};
use std::ops::{AddAssign};
use crate::color::{ColorType, Luma, LumaA, Rgb, Rgba, Bgr, Bgra};
/// Types which are safe to treat as an immutable byte slice in a pixel layout
/// for image encoding.
pub trait EncodableLayout: seals::EncodableLayout {
/// Get the bytes of this value.
fn as_bytes(&self) -> &[u8];
}
impl EncodableLayout for [u8] {
fn as_bytes(&self) -> &[u8] {
bytemuck::cast_slice(self)
}
}
impl EncodableLayout for [u16] {
fn as_bytes(&self) -> &[u8] {
bytemuck::cast_slice(self)
}
}
/// Primitive trait from old stdlib
pub trait Primitive: Copy + NumCast + Num + PartialOrd<Self> + Clone + Bounded {}
impl Primitive for usize {}
impl Primitive for u8 {}
impl Primitive for u16 {}
impl Primitive for u32 {}
impl Primitive for u64 {}
impl Primitive for isize {}
impl Primitive for i8 {}
impl Primitive for i16 {}
impl Primitive for i32 {}
impl Primitive for i64 {}
impl Primitive for f32 {}
impl Primitive for f64 {}
/// An Enlargable::Larger value should be enough to calculate
/// the sum (average) of a few hundred or thousand Enlargeable values.
pub trait Enlargeable: Sized + Bounded + NumCast {
type Larger: Primitive + AddAssign + 'static;
fn clamp_from(n: Self::Larger) -> Self {
// Note: Only unsigned value types supported.
if n > NumCast::from(Self::max_value()).unwrap() {
Self::max_value()
} else {
NumCast::from(n).unwrap()
}
}
}
impl Enlargeable for u8 {
type Larger = u32;
}
impl Enlargeable for u16 {
type Larger = u32;
}
impl Enlargeable for u32 {
type Larger = u64;
}
/// Linear interpolation without involving floating numbers.
pub trait Lerp: Bounded + NumCast {
type Ratio: Primitive;
fn lerp(a: Self, b: Self, ratio: Self::Ratio) -> Self {
let a = <Self::Ratio as NumCast>::from(a).unwrap();
let b = <Self::Ratio as NumCast>::from(b).unwrap();
let res = a + (b - a) * ratio;
if res > NumCast::from(Self::max_value()).unwrap() {
Self::max_value()
} else if res < NumCast::from(0).unwrap() {
NumCast::from(0).unwrap()
} else {
NumCast::from(res).unwrap()
}
}
}
impl Lerp for u8 {
type Ratio = f32;
}
impl Lerp for u16 {
type Ratio = f32;
}
impl Lerp for u32 {
type Ratio = f64;
}
/// A generalized pixel.
///
/// A pixel object is usually not used standalone but as a view into an image buffer.
pub trait Pixel: Copy + Clone {
/// The underlying subpixel type.
type Subpixel: Primitive;
/// The number of channels of this pixel type.
const CHANNEL_COUNT: u8;
/// Returns the number of channels of this pixel type.
#[deprecated(note="please use CHANNEL_COUNT associated constant")]
fn channel_count() -> u8 {
Self::CHANNEL_COUNT
}
/// Returns the components as a slice.
fn channels(&self) -> &[Self::Subpixel];
/// Returns the components as a mutable slice
fn channels_mut(&mut self) -> &mut [Self::Subpixel];
/// A string that can help to interpret the meaning each channel
/// See [gimp babl](http://gegl.org/babl/).
const COLOR_MODEL: &'static str;
/// Returns a string that can help to interpret the meaning each channel
/// See [gimp babl](http://gegl.org/babl/).
#[deprecated(note="please use COLOR_MODEL associated constant")]
fn color_model() -> &'static str {
Self::COLOR_MODEL
}
/// ColorType for this pixel format
const COLOR_TYPE: ColorType;
/// Returns the ColorType for this pixel format
#[deprecated(note="please use COLOR_TYPE associated constant")]
fn color_type() -> ColorType {
Self::COLOR_TYPE
}
/// Returns the channels of this pixel as a 4 tuple. If the pixel
/// has less than 4 channels the remainder is filled with the maximum value
///
/// TODO deprecate
fn channels4(
&self,
) -> (
Self::Subpixel,
Self::Subpixel,
Self::Subpixel,
Self::Subpixel,
);
/// Construct a pixel from the 4 channels a, b, c and d.
/// If the pixel does not contain 4 channels the extra are ignored.
///
/// TODO deprecate
fn from_channels(
a: Self::Subpixel,
b: Self::Subpixel,
c: Self::Subpixel,
d: Self::Subpixel,
) -> Self;
/// Returns a view into a slice.
///
/// Note: The slice length is not checked on creation. Thus the caller has to ensure
/// that the slice is long enough to prevent panics if the pixel is used later on.
fn from_slice(slice: &[Self::Subpixel]) -> &Self;
/// Returns mutable view into a mutable slice.
///
/// Note: The slice length is not checked on creation. Thus the caller has to ensure
/// that the slice is long enough to prevent panics if the pixel is used later on.
fn from_slice_mut(slice: &mut [Self::Subpixel]) -> &mut Self;
/// Convert this pixel to RGB
fn to_rgb(&self) -> Rgb<Self::Subpixel>;
/// Convert this pixel to RGB with an alpha channel
fn to_rgba(&self) -> Rgba<Self::Subpixel>;
/// Convert this pixel to luma
fn to_luma(&self) -> Luma<Self::Subpixel>;
/// Convert this pixel to luma with an alpha channel
fn to_luma_alpha(&self) -> LumaA<Self::Subpixel>;
/// Convert this pixel to BGR
fn to_bgr(&self) -> Bgr<Self::Subpixel>;
/// Convert this pixel to BGR with an alpha channel
fn to_bgra(&self) -> Bgra<Self::Subpixel>;
/// Apply the function ```f``` to each channel of this pixel.
fn map<F>(&self, f: F) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel;
/// Apply the function ```f``` to each channel of this pixel.
fn apply<F>(&mut self, f: F)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel;
/// Apply the function ```f``` to each channel except the alpha channel.
/// Apply the function ```g``` to the alpha channel.
fn map_with_alpha<F, G>(&self, f: F, g: G) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
G: FnMut(Self::Subpixel) -> Self::Subpixel;
/// Apply the function ```f``` to each channel except the alpha channel.
/// Apply the function ```g``` to the alpha channel. Works in-place.
fn apply_with_alpha<F, G>(&mut self, f: F, g: G)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
G: FnMut(Self::Subpixel) -> Self::Subpixel;
/// Apply the function ```f``` to each channel except the alpha channel.
fn map_without_alpha<F>(&self, f: F) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply_with_alpha(f, |x| x);
this
}
/// Apply the function ```f``` to each channel except the alpha channel.
/// Works in place.
fn apply_without_alpha<F>(&mut self, f: F)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
self.apply_with_alpha(f, |x| x);
}
/// Apply the function ```f``` to each channel of this pixel and
/// ```other``` pairwise.
fn map2<F>(&self, other: &Self, f: F) -> Self
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
/// Apply the function ```f``` to each channel of this pixel and
/// ```other``` pairwise. Works in-place.
fn apply2<F>(&mut self, other: &Self, f: F)
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
/// Invert this pixel
fn invert(&mut self);
/// Blend the color of a given pixel into ourself, taking into account alpha channels
fn blend(&mut self, other: &Self);
}
/// Private module for supertraits of sealed traits.
mod seals {
pub trait EncodableLayout {}
impl EncodableLayout for [u8] {}
impl EncodableLayout for [u16] {}
}