Use bulk byte order conversion for tag reads

This commit is contained in:
A. Molzer committed 2025-11-04 12:54:53 +01:00
1 parent 19ac30ac3f
commit 99b3c03ce3
3 files changed
+260 -99

No files matched your search

+14 -25
View File
@@ -535,32 +535,16 @@ fn invert_colors(
/// Fix endianness. If `byte_order` matches the host, then conversion is a no-op.
fn fix_endianness(buf: &mut [u8], byte_order: ByteOrder, bit_depth: u8) {
match byte_order {
ByteOrder::LittleEndian => match bit_depth {
0..=8 => {}
9..=16 => buf.chunks_exact_mut(2).for_each(|v| {
v.copy_from_slice(&u16::from_le_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
17..=32 => buf.chunks_exact_mut(4).for_each(|v| {
v.copy_from_slice(&u32::from_le_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
_ => buf.chunks_exact_mut(8).for_each(|v| {
v.copy_from_slice(&u64::from_le_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
},
ByteOrder::BigEndian => match bit_depth {
0..=8 => {}
9..=16 => buf.chunks_exact_mut(2).for_each(|v| {
v.copy_from_slice(&u16::from_be_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
17..=32 => buf.chunks_exact_mut(4).for_each(|v| {
v.copy_from_slice(&u32::from_be_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
_ => buf.chunks_exact_mut(8).for_each(|v| {
v.copy_from_slice(&u64::from_be_bytes((*v).try_into().unwrap()).to_ne_bytes())
}),
},
let host = ByteOrder::native();
let class = match bit_depth {
0..=8 => crate::tags::ByteOrderClass::One,
9..=16 => crate::tags::ByteOrderClass::Two,
17..=32 => crate::tags::ByteOrderClass::Four,
_ => crate::tags::ByteOrderClass::Eight,
};
host.convert_class(class, buf, byte_order);
}
impl<R: Read + Seek> Decoder<R> {
@@ -747,6 +731,11 @@ impl<R: Read + Seek> Decoder<R> {
}
/// Returns the byte_order of the file.
///
/// # Usage
///
/// This is only relevant to interpreting raw bytes read from tags. The image decoding methods
/// will correct to the host byte order automatically.
pub fn byte_order(&self) -> ByteOrder {
self.value_reader.reader.byte_order
}