diff --git a/src/encoder/mod.rs b/src/encoder/mod.rs index ed28b8e..522f1d9 100644 --- a/src/encoder/mod.rs +++ b/src/encoder/mod.rs @@ -28,6 +28,8 @@ use self::compression::Compression as Comp; use self::compression::*; use self::writer::*; +pub use self::writer::TiffWriter; + /// Type of prediction to prepare the image with. /// /// Image data can be very unpredictable, and thus very hard to compress. Predictors are simple @@ -185,6 +187,14 @@ impl TiffEncoder { self } + /// Get a mutable reference to a writer for raw values. + /// + /// This can be used to write custom data sections to the underlying writer that may or may not + /// be referenced in the IFDs of the tiff structure. + pub fn get_mut(&mut self) -> &mut TiffWriter { + &mut self.writer + } + /// Create a [`DirectoryEncoder`] to encode an ifd directory. #[deprecated = "`image_directory` replaced the old behavior and clarifies the intent"] #[doc(hidden)] diff --git a/src/encoder/writer.rs b/src/encoder/writer.rs index 7299282..a0b3d8b 100644 --- a/src/encoder/writer.rs +++ b/src/encoder/writer.rs @@ -40,6 +40,7 @@ pub fn write_bigtiff_header(writer: &mut TiffWriter) -> TiffResult< Ok(()) } +/// A writer that tracks offset and can compress. pub struct TiffWriter { writer: W, offset: u64, @@ -61,6 +62,7 @@ impl TiffWriter { self.compressor = compressor; } + /// Unset the compression. pub fn reset_compression(&mut self) { self.compressor = Compressor::default(); } @@ -73,6 +75,7 @@ impl TiffWriter { self.offset() - core::mem::size_of::() as u64 } + /// Get the number of bytes written by the last write operation. pub fn last_written(&self) -> u64 { self.byte_count } @@ -183,6 +186,19 @@ impl TiffWriter { } } +/// Write uncompressed bytes to the raw underlying file. +impl std::io::Write for TiffWriter { + fn write(&mut self, buf: &[u8]) -> io::Result { + let n = self.writer.write(buf)?; + self.offset += n as u64; + Ok(n) + } + + fn flush(&mut self) -> io::Result<()> { + self.writer.flush() + } +} + impl TiffWriter { pub fn goto_offset(&mut self, offset: u64) -> Result<(), io::Error> { self.offset = offset;