diff --git a/src/decoder/mod.rs b/src/decoder/mod.rs index f3e254c..b15a2f9 100644 --- a/src/decoder/mod.rs +++ b/src/decoder/mod.rs @@ -1865,3 +1865,70 @@ impl<'l> IfdDecoder<'l> { }) } } + +#[cfg(test)] +mod tests { + use super::Decoder; + use crate::{ + bytecast, + tags::{ByteOrder, Tag, ValueBuffer}, + }; + + #[test] + fn equivalence_of_tag_readers() { + let file = std::fs::File::open(concat!( + env!("CARGO_MANIFEST_DIR"), + "/tests/images/int8_rgb.tif" + )) + .unwrap(); + + let mut decoder = Decoder::new(file).unwrap(); + let file_bo = decoder.byte_order(); + let mut ifd = decoder.image_ifd(); + + { + let value = ifd + .find_tag(Tag::BitsPerSample) + .unwrap() + .expect("must have BitsPerSample"); + + let samples = value.into_u16_vec().unwrap(); + assert_eq!(samples.as_slice(), [8, 8, 8]); + } + + { + let mut value = ValueBuffer::from_value(&[0u16; 4]); + let _entry = ifd + .find_tag_buf(Tag::BitsPerSample, &mut value) + .unwrap() + .expect("must have BitsPerSample"); + + value.set_byte_order(ByteOrder::native()); + assert_eq!(value.as_bytes(), bytecast::u16_as_ne_bytes(&[8, 8, 8])); + } + + { + let mut by_bytes = [0u16; 4]; + let entry = ifd + .find_entry(Tag::BitsPerSample) + .expect("must have BitsPerSample"); + + let byte_len = ifd + .find_tag_bytes( + Tag::BitsPerSample, + bytecast::u16_as_ne_mut_bytes(&mut by_bytes), + 0, + ) + .unwrap() + .expect("must have BitsPerSample"); + assert_eq!(byte_len, 3 * std::mem::size_of::()); + + file_bo.convert( + entry.field_type(), + bytecast::u16_as_ne_mut_bytes(&mut by_bytes[..3]), + ByteOrder::native(), + ); + assert_eq!(&by_bytes[..3], &[8, 8, 8]); + } + } +} diff --git a/src/tags.rs b/src/tags.rs index 45f5ed9..a689dfa 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -432,9 +432,13 @@ impl ValueBuffer { } } + /// Internal method to change the type and count while re-interpreting the byte buffer. + /// + /// Should only be called after writing bytes to the internal buffer prepared with + /// `Self::prepare_length`. pub(crate) fn assume_type(&mut self, ty: Type, count: u64, bo: ByteOrder) { debug_assert!({ - ty.value_bytes(self.count) + ty.value_bytes(count) .is_ok_and(|n| n < self.bytes.len() as u64) });