From 6b8e5d6adca0d0f5ca513b21f35f869f4c1f0d6b Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 22 May 2026 23:52:11 +0200 Subject: [PATCH 1/2] Add some debug asserts to help with local reasoning --- src/codecs/avif/yuv.rs | 2 ++ src/utils/mod.rs | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/codecs/avif/yuv.rs b/src/codecs/avif/yuv.rs index 64f7e75977..7a91321157 100644 --- a/src/codecs/avif/yuv.rs +++ b/src/codecs/avif/yuv.rs @@ -172,6 +172,8 @@ pub(crate) struct YuvChromaRange { impl YuvIntensityRange { pub(crate) const fn get_yuv_range(self, depth: u32) -> YuvChromaRange { + debug_assert!((8..=16).contains(&depth)); + match self { YuvIntensityRange::Tv => YuvChromaRange { bias_y: 16 << (depth - 8), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index bdfdca90ac..1816be0794 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -2,12 +2,15 @@ use std::collections::TryReserveError; -/// Expand a buffer of packed 1, 2, or 4 bits integers into u8's. Assumes that -/// every `row_size` entries there are padding bits up to the next byte boundary. +/// Expand a buffer of packed 1, 2, or 4 bits integers into u8's. +/// +/// Assumes that: +/// 1. scanlines begin on byte boundaries, +/// 2. every `row_size` entries there are padding bits up to the next byte boundary. #[allow(dead_code)] // When no image formats that use it are enabled pub(crate) fn expand_bits(bit_depth: u8, row_size: u32, buf: &[u8]) -> Vec { - // Note: this conversion assumes that the scanlines begin on byte boundaries + debug_assert!(bit_depth == 1 || bit_depth == 2 || bit_depth == 4); let mask = (1u8 << bit_depth as usize) - 1; let scaling_factor = 255 / ((1 << bit_depth as usize) - 1); let bit_width = row_size * u32::from(bit_depth); From dd7f47988b468de003e16cc3a6d101bdca9fa865 Mon Sep 17 00:00:00 2001 From: RunDevelopment Date: Fri, 22 May 2026 23:57:25 +0200 Subject: [PATCH 2/2] const-compatible assert --- src/codecs/avif/yuv.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codecs/avif/yuv.rs b/src/codecs/avif/yuv.rs index 7a91321157..3f752d6d33 100644 --- a/src/codecs/avif/yuv.rs +++ b/src/codecs/avif/yuv.rs @@ -172,7 +172,7 @@ pub(crate) struct YuvChromaRange { impl YuvIntensityRange { pub(crate) const fn get_yuv_range(self, depth: u32) -> YuvChromaRange { - debug_assert!((8..=16).contains(&depth)); + debug_assert!(matches!(depth, 8..=16)); match self { YuvIntensityRange::Tv => YuvChromaRange {