Skip to content

Commit e90ec59

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

2 files changed

Lines changed: 8 additions & 10 deletions

File tree

src/image.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ use super::{Blob, Extend};
1111
pub enum ImageFormat {
1212
/// 32-bit RGBA with 8-bit channels.
1313
Rgba8 = 0,
14+
/// 32-bit BGRA with 8-bit channels.
15+
Bgra8 = 1,
1416
// NOTICE: If a new value is added, be sure to update the bytemuck CheckedBitPattern impl.
1517
}
1618

@@ -22,7 +24,7 @@ impl ImageFormat {
2224
#[must_use]
2325
pub fn size_in_bytes(self, width: u32, height: u32) -> Option<usize> {
2426
match self {
25-
Self::Rgba8 => 4_usize
27+
Self::Rgba8 | Self::Bgra8 => 4_usize
2628
.checked_mul(width as usize)
2729
.and_then(|x| x.checked_mul(height as usize)),
2830
}

src/impl_bytemuck.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,6 @@ unsafe impl bytemuck::checked::CheckedBitPattern for ImageFormat {
9191
type Bits = u8;
9292

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

112108
// Safety: The enum is `repr(u8)` and has only fieldless variants.
@@ -174,6 +170,10 @@ mod tests {
174170
Ok(&ImageFormat::Rgba8),
175171
try_from_bytes::<ImageFormat>(valid_zero)
176172
);
173+
assert_eq!(
174+
Ok(&ImageFormat::Bgra8),
175+
try_from_bytes::<ImageFormat>(valid_one)
176+
);
177177
assert!(try_from_bytes::<ImageFormat>(invalid).is_err());
178178

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

0 commit comments

Comments
 (0)