Merge pull request #326 from image-rs/encode-values-from-buffer

Encode values from buffer
This commit is contained in:
Aurelia Molzer authored and GitHub committed 2026-01-11 22:09:29 +00:00
commit 1d138a5568
2 files changed
+46 -15

No files matched your search

+42 -15
View File
@@ -291,21 +291,6 @@ impl<'a, W: 'a + Write + Seek, K: TiffKind> DirectoryEncoder<'a, W, K> {
})
}
/// Write a tag-value pair with prepared byte data.
///
/// Note that the library will _not_ attempt to verify that the data type or the count of the
/// buffered value is permissible for the given tag. The data will be associated with the tag
/// exactly as-is.
///
/// An error is returned if the byte order of the presented value does not match the byte order
/// of the underlying file (i.e. the [native byte order](`crate::tags::ByteOrder::native`)).
pub fn write_tag_value(&mut self, tag: Tag, value: &ValueBuffer) -> TiffResult<()> {
let entry = self.write_entry_buf(value)?;
self.directory.extend([(tag, entry)]);
Ok(())
}
/// Write a single ifd tag.
pub fn write_tag<T: TiffValue>(&mut self, tag: Tag, value: T) -> TiffResult<()> {
// Encodes the value if necessary. In the current bytes all integers are taken as native
@@ -330,6 +315,21 @@ impl<'a, W: 'a + Write + Seek, K: TiffKind> DirectoryEncoder<'a, W, K> {
Ok(())
}
/// Write a tag-value pair with prepared byte data.
///
/// Note that the library will _not_ attempt to verify that the data type or the count of the
/// buffered value is permissible for the given tag. The data will be associated with the tag
/// exactly as-is.
///
/// An error is returned if the byte order of the presented value does not match the byte order
/// of the underlying file (i.e. the [native byte order](`crate::tags::ByteOrder::native`)).
pub fn write_tag_buf(&mut self, tag: Tag, value: &ValueBuffer) -> TiffResult<()> {
let entry = self.write_entry_buf(value)?;
self.directory.extend([(tag, entry)]);
Ok(())
}
/// Write some data to the tiff file, the offset of the data is returned.
///
/// This could be used to write tiff strips. All of the data will be written into the file as a
@@ -396,6 +396,33 @@ impl<'a, W: 'a + Write + Seek, K: TiffKind> DirectoryEncoder<'a, W, K> {
)
}
/// Write a directory entry by its byte data.
///
/// The data must be pre-formatted to the byte order expected by the file.
///
/// Returns an [`Entry`]. If the value is short enough it will *not* be written in the file but
/// stored in the entry's offset field.
pub fn write_entry_bytes(&mut self, ty: Type, data: &[u8]) -> TiffResult<Entry> {
if data.len() % usize::from(ty.byte_len()) != 0 {
return Err(TiffError::UsageError(UsageError::MismatchedEntryLength {
ty,
found: data.len(),
}));
}
let count = data.len() / usize::from(ty.byte_len());
let count = u64::try_from(count)?;
Self::write_entry_inner(
self.writer,
DirectoryEntry {
data_type: ty,
count: K::convert_offset(count)?,
data: data.into(),
},
)
}
/// Insert previously written key-value entries.
///
/// It is the caller's responsibility to ensure that the data referred to by the entries is
+4
View File
@@ -5,6 +5,7 @@ use std::string;
use quick_error::quick_error;
use crate::decoder::ChunkType;
use crate::tags::Type;
use crate::tags::{
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, SampleFormat, Tag,
};
@@ -211,6 +212,9 @@ quick_error! {
ByteOrderMismatch {
display("attempted to use data with a byte order that does not match the required one")
}
MismatchedEntryLength {ty: Type, found: usize } {
display("the length of data is not a multiple of the size of the type {ty:?}, found {found}")
}
}
}