Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

png: Make bytemuck optional #138

Merged
merged 1 commit into from
Sep 13, 2023
Merged
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
1 change: 0 additions & 1 deletion zune-png/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ default = ["sse", "std"]
zune-core = { path = "../zune-core", version = "0.2" }
log = "0.4.17"
zune-inflate = { path = "../zune-inflate", version = "0.2", default-features = false, features = ["zlib"] }
bytemuck = { version = "1.13.1", default-features = false }

[dev-dependencies]
nanorand = { version = "0.7.0", default-features = false, features = ["wyrand"] } # testing purposes.
Expand Down
7 changes: 2 additions & 5 deletions zune-png/src/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use crate::filters::de_filter::{
use crate::options::default_chunk_handler;
use crate::utils::{
add_alpha, convert_be_to_target_endian_u16, expand_bits_to_byte, expand_palette, expand_trns,
is_le
is_le, convert_u16_to_u8_slice
};

/// A palette entry.
Expand Down Expand Up @@ -777,11 +777,8 @@ impl<T: ZReaderTrait> PngDecoder<T> {
{
&mut out_u8
} else {
let (a, b, c) = bytemuck::pod_align_to_mut::<u16, u8>(&mut out_u16);
let b = convert_u16_to_u8_slice(&mut out_u16);

// a and c should be empty since we do not expect slop bytes on either edge
assert!(a.is_empty());
assert!(c.is_empty());
assert_eq!(b.len(), new_len * 2); // length should be twice that of u8
b
};
Expand Down
11 changes: 11 additions & 0 deletions zune-png/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,3 +350,14 @@ pub(crate) fn add_alpha(input: &[u8], output: &mut [u8], colorspace: PngColor, d
(a, b) => panic!("Unknown combination of depth {a:?} and color type for expand alpha {b:?}")
}
}

pub fn convert_u16_to_u8_slice(slice: &mut [u16]) -> &mut [u8] {
// Converting a u16 slice to a u8 slice is always correct because
// the alignment of the target is smaller.
unsafe {
std::slice::from_raw_parts_mut(
slice.as_ptr() as *mut u8,
slice.len().checked_mul(2).unwrap(),
)
}
}