From 369dd33fd5644f5279f8e1d1c0c4aeeb3bf17c99 Mon Sep 17 00:00:00 2001 From: julianknodt Date: Wed, 19 Jul 2023 00:07:22 -0700 Subject: [PATCH] Switch to unsafe code instead of bytemuck --- src/codecs/hdr/decoder.rs | 18 ++++++++++++++++-- src/color.rs | 8 -------- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/codecs/hdr/decoder.rs b/src/codecs/hdr/decoder.rs index ff01e8cf54..dcd2e9bf93 100644 --- a/src/codecs/hdr/decoder.rs +++ b/src/codecs/hdr/decoder.rs @@ -484,7 +484,14 @@ impl HdrDecoder { pub fn read_image_ldr(self) -> ImageResult>> { let sz = (self.width * self.height) as usize; let mut buf = vec![Rgb([0; 3]); sz]; - self.read_image_ldr_buf(bytemuck::try_cast_slice_mut(buf.as_mut_slice()).unwrap())?; + // SAFETY: + // RGB is a loose wrapper around [T; 3], thus this + // cast is just flattening out the nesting. + let slice: &mut [u8] = unsafe { + let raw_ptr = buf.as_mut_ptr() as *mut u8; + std::slice::from_raw_parts_mut(raw_ptr, sz * 3) + }; + self.read_image_ldr_buf(slice)?; Ok(buf) } fn read_image_ldr_buf(self, buf: &mut [u8]) -> ImageResult<()> { @@ -497,7 +504,14 @@ impl HdrDecoder { pub fn read_image_hdr(self) -> ImageResult>> { let sz = (self.width * self.height) as usize; let mut buf = vec![Rgb([0.0f32; 3]); sz]; - self.read_image_hdr_buf(bytemuck::try_cast_slice_mut(buf.as_mut_slice()).unwrap())?; + // SAFETY: + // RGB is a loose wrapper around [T; 3], thus this + // cast is just flattening out the nesting. + let slice: &mut [f32] = unsafe { + let raw_ptr = buf.as_mut_ptr() as *mut f32; + std::slice::from_raw_parts_mut(raw_ptr, sz * 3) + }; + self.read_image_hdr_buf(slice)?; Ok(buf) } diff --git a/src/color.rs b/src/color.rs index 13814921b8..8426eb546e 100644 --- a/src/color.rs +++ b/src/color.rs @@ -349,14 +349,6 @@ impl From<[T; $channels]> for $ident { Self(c) } } - -unsafe impl bytemuck::Zeroable for $ident where T: bytemuck::Zeroable { - fn zeroed() -> Self { - Self(<[T; $channels] as bytemuck::Zeroable>::zeroed()) - } -} -unsafe impl bytemuck::Pod for $ident where T: bytemuck::Pod {} - )* // END Structure definitions }