Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use super::{Blob, Extend};
pub enum ImageFormat {
/// 32-bit RGBA with 8-bit channels.
Rgba8 = 0,
/// 32-bit BGRA with 8-bit channels.
Bgra8 = 1,
// NOTICE: If a new value is added, be sure to update the bytemuck CheckedBitPattern impl.
}

Expand All @@ -22,7 +24,7 @@ impl ImageFormat {
#[must_use]
pub fn size_in_bytes(self, width: u32, height: u32) -> Option<usize> {
match self {
Self::Rgba8 => 4_usize
Self::Rgba8 | Self::Bgra8 => 4_usize
.checked_mul(width as usize)
.and_then(|x| x.checked_mul(height as usize)),
}
Expand Down
14 changes: 5 additions & 9 deletions src/impl_bytemuck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ unsafe impl bytemuck::checked::CheckedBitPattern for ImageFormat {
type Bits = u8;

fn is_valid_bit_pattern(bits: &u8) -> bool {
#![expect(
clippy::absurd_extreme_comparisons,
reason = "There is only one value."
)]
use bytemuck::Contiguous;
// Don't need to compare against MIN_VALUE as this is u8 and 0 is the MIN_VALUE.
*bits <= Self::MAX_VALUE
Expand All @@ -106,7 +102,7 @@ unsafe impl bytemuck::checked::CheckedBitPattern for ImageFormat {
unsafe impl bytemuck::Contiguous for ImageFormat {
type Int = u8;
const MIN_VALUE: u8 = Self::Rgba8 as u8;
const MAX_VALUE: u8 = Self::Rgba8 as u8;
const MAX_VALUE: u8 = Self::Bgra8 as u8;
}

// Safety: The enum is `repr(u8)` and has only fieldless variants.
Expand Down Expand Up @@ -174,6 +170,10 @@ mod tests {
Ok(&ImageFormat::Rgba8),
try_from_bytes::<ImageFormat>(valid_zero)
);
assert_eq!(
Ok(&ImageFormat::Bgra8),
try_from_bytes::<ImageFormat>(valid_one)
);
assert!(try_from_bytes::<ImageFormat>(invalid).is_err());

assert_eq!(
Expand Down Expand Up @@ -283,10 +283,6 @@ mod tests {
/// Tests that the [`Contiguous`] impl for [`ImageFormat`] is not trivially incorrect.
const _: () = {
let mut value = 0;
#[expect(
clippy::absurd_extreme_comparisons,
reason = "There is only one value."
)]
while value <= ImageFormat::MAX_VALUE {
// Safety: In a const context, therefore if this makes an invalid ImageFormat, that will be detected.
let it: ImageFormat = unsafe { ptr::read((&raw const value).cast()) };
Expand Down
Loading