From 170bd3697c4d7f04769c82565c1c40cd99f1ad76 Mon Sep 17 00:00:00 2001 From: "A. Molzer" <5550310+197g@users.noreply.github.com> Date: Sun, 2 Nov 2025 14:54:15 +0100 Subject: [PATCH] Provide tags in their byte encoding --- src/tags.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) diff --git a/src/tags.rs b/src/tags.rs index 8dc8716..ae10ed4 100644 --- a/src/tags.rs +++ b/src/tags.rs @@ -336,6 +336,62 @@ pub enum ExtraSamples(u16) { } } +/// A value represented as in-memory bytes with flexible byteorder. +pub struct ValueBuffer { + /// The raw bytes of the value. + bytes: Vec, + + /// The type of the value. + ty: Type, + + /// The byte order of the value. + byte_order: ByteOrder, +} + +impl ValueBuffer { + /// A value with a count of zero. + /// + /// The byte order is set to the native byte order of the platform. + pub fn empty(ty: Type) -> Self { + ValueBuffer { + bytes: vec![], + ty, + byte_order: ByteOrder::native(), + } + } + + pub fn byte_order(&self) -> ByteOrder { + self.byte_order + } + + pub fn tiff_type(&self) -> Type { + self.ty + } + + /// The count of items in the value. + pub fn count(&self) -> u64 { + (self.bytes.len() / usize::from(self.ty.byte_len())) as u64 + } + + /// View the underlying raw bytes of this value. + pub fn as_bytes(&self) -> &[u8] { + &self.bytes + } + + /// View the underlying mutable raw bytes of this value. + pub fn as_bytes_mut(&mut self) -> &mut [u8] { + &mut self.bytes + } + + /// Change the byte order of the value representation. + pub fn set_byte_order(&mut self, byte_order: ByteOrder) { + self.byte_order + .convert(self.ty, self.bytes.as_mut_slice(), byte_order); + + self.byte_order = byte_order; + } +} + /// Byte order of the TIFF file. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ByteOrder {