Skip to content

Commit 4e76d9c

Browse files
committed
Add ImageAlphaType
Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com>
1 parent 6b00f65 commit 4e76d9c

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

src/image.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,17 @@ impl ImageFormat {
2929
}
3030
}
3131

32+
/// Handling of alpha channel
33+
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
34+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
35+
#[repr(u8)]
36+
pub enum ImageAlphaType {
37+
/// Image has separate alpha channel (also called straight/unpremultiplied alpha).
38+
Alpha = 1,
39+
/// Image has colors with premultiplied alpha
40+
AlphaPremultiplied = 0,
41+
}
42+
3243
/// Defines the desired quality for sampling an image.
3344
#[derive(Copy, Clone, PartialEq, Eq, Default, Debug)]
3445
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
@@ -58,6 +69,8 @@ pub struct ImageData {
5869
pub data: Blob<u8>,
5970
/// Pixel format of the image.
6071
pub format: ImageFormat,
72+
/// Encoding of alpha in the image pixels
73+
pub alpha_type: ImageAlphaType,
6174
/// Width of the image.
6275
pub width: u32,
6376
/// Height of the image.

src/impl_bytemuck.rs

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
#![allow(unsafe_code, reason = "unsafe is required for bytemuck unsafe impls")]
55

6-
use crate::{Compose, Extend, Fill, ImageFormat, ImageQuality, Mix};
6+
use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix};
77

88
// Safety: The enum is `repr(u8)` and has only fieldless variants.
99
unsafe impl bytemuck::NoUninit for Compose {}
@@ -109,6 +109,31 @@ unsafe impl bytemuck::Contiguous for ImageFormat {
109109
const MAX_VALUE: u8 = Self::Rgba8 as u8;
110110
}
111111

112+
// Safety: The enum is `repr(u8)` and has only fieldless variants.
113+
unsafe impl bytemuck::NoUninit for ImageAlphaType {}
114+
115+
// Safety: The enum is `repr(u8)` and `0` is a valid value.
116+
unsafe impl bytemuck::Zeroable for ImageAlphaType {}
117+
118+
// Safety: The enum is `repr(u8)`.
119+
unsafe impl bytemuck::checked::CheckedBitPattern for ImageAlphaType {
120+
type Bits = u8;
121+
122+
fn is_valid_bit_pattern(bits: &u8) -> bool {
123+
use bytemuck::Contiguous;
124+
// Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
125+
*bits <= Self::MAX_VALUE
126+
}
127+
}
128+
129+
// Safety: The enum is `repr(u8)`. All values are `u8` and fall within
130+
// the min and max values.
131+
unsafe impl bytemuck::Contiguous for ImageAlphaType {
132+
type Int = u8;
133+
const MIN_VALUE: u8 = Self::Alpha as u8;
134+
const MAX_VALUE: u8 = Self::AlphaPremultiplied as u8;
135+
}
136+
112137
// Safety: The enum is `repr(u8)` and has only fieldless variants.
113138
unsafe impl bytemuck::NoUninit for ImageQuality {}
114139

@@ -151,7 +176,7 @@ unsafe impl bytemuck::checked::CheckedBitPattern for Mix {
151176

152177
#[cfg(test)]
153178
mod tests {
154-
use crate::{Compose, Extend, Fill, ImageFormat, ImageQuality, Mix};
179+
use crate::{Compose, Extend, Fill, ImageAlphaType, ImageFormat, ImageQuality, Mix};
155180
use bytemuck::{checked::try_from_bytes, Contiguous, Zeroable};
156181
use core::ptr;
157182

src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ pub use gradient::{
5252
RadialGradientPosition, SweepGradientPosition,
5353
};
5454
pub use image::{
55-
ImageBrush, ImageBrushRef, ImageData, ImageFormat, ImageQuality, ImageRenderParams,
55+
ImageAlphaType, ImageBrush, ImageBrushRef, ImageData, ImageFormat, ImageQuality,
56+
ImageRenderParams,
5657
};
5758
pub use style::{Fill, Style, StyleRef};
5859

0 commit comments

Comments
 (0)