Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions src/codecs/avif/yuv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ pub(crate) struct YuvChromaRange {

impl YuvIntensityRange {
pub(crate) const fn get_yuv_range(self, depth: u32) -> YuvChromaRange {
debug_assert!(matches!(depth, 8..=16));
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We seem to be checking the bit depth in all individual callers:

    assert!(
        (8..=16).contains(&BIT_DEPTH),
        "Invalid bit depth is provided"
    );

With this being an internal but supposedly local reasoning maybe we should use a simple wrapper type for it instead of u32 in this case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be tricky. Before this function, bit depth is passed around with const generics everywhere. So switching to a parameter might make perf worse. I can't test this though, since avif-native doesn't work on my machine (out of the box).


match self {
YuvIntensityRange::Tv => YuvChromaRange {
bias_y: 16 << (depth - 8),
Expand Down
9 changes: 6 additions & 3 deletions src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u8> {
// 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);
Expand Down
Loading