From e90ec596431fa23212998ceea5aac842a0f7b9bc Mon Sep 17 00:00:00 2001 From: sagudev <16504129+sagudev@users.noreply.github.com> Date: Wed, 20 Aug 2025 15:06:36 +0200 Subject: [PATCH 1/2] Add `ImageFormat::Bgra8` Signed-off-by: sagudev <16504129+sagudev@users.noreply.github.com> --- src/image.rs | 4 +++- src/impl_bytemuck.rs | 14 +++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/image.rs b/src/image.rs index 7b43024..0e237a3 100644 --- a/src/image.rs +++ b/src/image.rs @@ -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. } @@ -22,7 +24,7 @@ impl ImageFormat { #[must_use] pub fn size_in_bytes(self, width: u32, height: u32) -> Option { 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)), } diff --git a/src/impl_bytemuck.rs b/src/impl_bytemuck.rs index e5ea2fd..00852ef 100644 --- a/src/impl_bytemuck.rs +++ b/src/impl_bytemuck.rs @@ -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 @@ -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. @@ -174,6 +170,10 @@ mod tests { Ok(&ImageFormat::Rgba8), try_from_bytes::(valid_zero) ); + assert_eq!( + Ok(&ImageFormat::Bgra8), + try_from_bytes::(valid_one) + ); assert!(try_from_bytes::(invalid).is_err()); assert_eq!( @@ -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()) }; From 1a9defc653e6acb108b158637489300c72468163 Mon Sep 17 00:00:00 2001 From: Sam <16504129+sagudev@users.noreply.github.com> Date: Fri, 29 Aug 2025 17:44:26 +0200 Subject: [PATCH 2/2] Update CHANGELOG --- CHANGELOG.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8f3d4f1..2c3847a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ This release has an [MSRV] of 1.82. ### Added - `Style` now impl `PartialEq`. ([#114][] by [@liferooter][]) +- Add `Bgra8` variant to `ImageFormat`. ([#120][] by [@sagudev][]) ## [0.4.0][] (2025-04-30) @@ -135,11 +136,13 @@ This release has an [MSRV] of 1.70. [#103]: https://github.com/linebender/peniko/pull/103 [#104]: https://github.com/linebender/peniko/pull/104 [#114]: https://github.com/linebender/peniko/pull/114 +[#120]: https://github.com/linebender/peniko/pull/120 [@dfrg]: https://github.com/dfrg [@DJMcNab]: https://github.com/DJMcNab [@liferooter]: https://github.com/liferooter [@ratmice]: https://github.com/ratmice +[@sagudev]: https://github.com/sagudev [@waywardmonkeys]: https://github.com/waywardmonkeys [Unreleased]: https://github.com/linebender/peniko/compare/v0.4.0...HEAD