Add Decoder::open, deprecate new

This commit is contained in:
A. Molzer committed 2026-03-06 09:31:23 +01:00
1 parent be9b3a04b9
commit bf79af8de4
16 files changed
+176 -108

No files matched your search

+37 -25
View File
@@ -857,6 +857,11 @@ impl TiffHeader {
}
impl<R: Read + Seek> Decoder<R> {
/// Create a decoder and attempt to parse the first image directory.
///
/// To open a file without parsing any directory yet (for instance to retain a [`Decoder`] on
/// errors in the directory), use [`TiffHeader::parse`] and [`TiffHeader::open`] instead..
#[deprecated = "Prefer `Decoder::open`. These methods will become aliases in a future major release."]
pub fn new(mut r: R) -> TiffResult<Decoder<R>> {
let header = TiffHeader::parse(&mut r)?;
let mut decoder = header.open(r);
@@ -1483,9 +1488,10 @@ impl<R: Read + Seek> Decoder<R> {
/// let mut result = DecodingResult::U8(vec![]);
///
/// let mut reader = /* */
/// # Decoder::new(std::io::Cursor::new(include_bytes!(concat!(
/// # Decoder::open(std::io::Cursor::new(include_bytes!(concat!(
/// # env!("CARGO_MANIFEST_DIR"), "/tests/images/tiled-gray-i1.tif"
/// # )))).unwrap();
/// # reader.next_image().unwrap();
/// let layout = reader.read_image_to_buffer(&mut result)?;
///
/// if result.as_buffer(0).as_bytes().len() < layout.complete_len {
@@ -1503,10 +1509,10 @@ impl<R: Read + Seek> Decoder<R> {
/// let mut result = DecodingResult::I8(vec![]);
///
/// let mut reader = /* */
/// # Decoder::new(std::io::Cursor::new(include_bytes!(concat!(
/// # Decoder::open(std::io::Cursor::new(include_bytes!(concat!(
/// # env!("CARGO_MANIFEST_DIR"), "/tests/images/tiled-gray-i1.tif"
/// # )))).unwrap();
///
/// # reader.next_image().unwrap();
/// reader.read_image_to_buffer(&mut result)?;
///
/// # Ok::<_, tiff::TiffError>(())
@@ -1589,7 +1595,7 @@ impl<R: Read + Seek> Decoder<R> {
/// This may be used with any directory, valid image or otherwise. When the decoder has not
/// read a directory yet (a state after [`TiffHeader::open`]) the decoder will represent an
/// empty directory with only the next pointer set.
pub fn image_ifd(&mut self) -> IfdDecoder<'_> {
pub fn current_ifd(&mut self) -> IfdDecoder<'_> {
IfdDecoder {
inner: tag_reader::TagReader {
decoder: &mut self.value_reader,
@@ -1611,7 +1617,9 @@ impl<R: Read + Seek> Decoder<R> {
///
/// # use std::io::Cursor;
/// # let mut data = Cursor::new(vec![0]);
/// let mut decoder = Decoder::new(&mut data).unwrap();
/// let mut decoder = Decoder::open(&mut data)?;
///
/// decoder.next_directory()?;
/// let sub_ifds = decoder.get_tag(Tag::SubIfd)?.into_ifd_vec()?;
///
/// for ifd in sub_ifds {
@@ -1634,13 +1642,13 @@ impl<R: Read + Seek> Decoder<R> {
/// Tries to retrieve a tag from the current image directory.
/// Return `Ok(None)` if the tag is not present.
pub fn find_tag(&mut self, tag: Tag) -> TiffResult<Option<ifd::Value>> {
self.image_ifd().find_tag(tag)
self.current_ifd().find_tag(tag)
}
/// Tries to retrieve a tag in the current image directory and convert it to the desired
/// unsigned type.
pub fn find_tag_unsigned<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<Option<T>> {
self.image_ifd().find_tag_unsigned(tag)
self.current_ifd().find_tag_unsigned(tag)
}
/// Tries to retrieve a vector of all a tag's values and convert them to the desired unsigned
@@ -1649,19 +1657,19 @@ impl<R: Read + Seek> Decoder<R> {
&mut self,
tag: Tag,
) -> TiffResult<Option<Vec<T>>> {
self.image_ifd().find_tag_unsigned_vec(tag)
self.current_ifd().find_tag_unsigned_vec(tag)
}
/// Tries to retrieve a tag from the current image directory and convert it to the desired
/// unsigned type. Returns an error if the tag is not present.
pub fn get_tag_unsigned<T: TryFrom<u64>>(&mut self, tag: Tag) -> TiffResult<T> {
self.image_ifd().get_tag_unsigned(tag)
self.current_ifd().get_tag_unsigned(tag)
}
/// Tries to retrieve a tag from the current image directory.
/// Returns an error if the tag is not present
pub fn get_tag(&mut self, tag: Tag) -> TiffResult<ifd::Value> {
self.image_ifd().get_tag(tag)
self.current_ifd().get_tag(tag)
}
pub fn get_tag_u32(&mut self, tag: Tag) -> TiffResult<u32> {
@@ -1716,7 +1724,7 @@ impl<R: Read + Seek> Decoder<R> {
}
pub fn tag_iter(&mut self) -> impl Iterator<Item = TiffResult<(Tag, ifd::Value)>> + '_ {
self.image_ifd().tag_iter()
self.current_ifd().tag_iter()
}
}
@@ -1972,9 +1980,11 @@ mod tests {
))
.unwrap();
let mut decoder = Decoder::new(file).unwrap();
let mut decoder = Decoder::open(file).unwrap();
let file_bo = decoder.byte_order();
let mut ifd = decoder.image_ifd();
decoder.next_image().unwrap();
let mut ifd = decoder.current_ifd();
{
let value = ifd
@@ -2041,25 +2051,26 @@ mod tests {
let (first_ptr, second_ptr, third_ptr);
{
let mut decoder = Decoder::new(&mut cursor).unwrap();
let mut decoder = Decoder::open(&mut cursor).unwrap();
decoder.next_image().unwrap();
first_ptr = decoder.ifd_pointer();
let first_ifd = decoder.image_ifd();
let first_ifd = decoder.current_ifd();
assert!(first_ifd.directory().get(Tag::ImageWidth).is_some());
assert!(decoder.more_images());
assert!(decoder.next_image().is_err());
let broken_ifd = decoder.image_ifd();
let broken_ifd = decoder.current_ifd();
assert!(broken_ifd.directory().get(Tag::ImageWidth).is_some());
assert!(decoder.next_directory().is_ok());
second_ptr = decoder.ifd_pointer();
let second_ifd = decoder.image_ifd();
let second_ifd = decoder.current_ifd();
assert!(second_ifd.directory().get(Tag::ImageWidth).is_none());
assert!(decoder.more_images());
assert!(decoder.next_image().is_ok());
third_ptr = decoder.ifd_pointer();
let third_ifd = decoder.image_ifd();
let third_ifd = decoder.current_ifd();
assert!(third_ifd.directory().get(Tag::ImageWidth).is_some());
assert!(!decoder.more_images());
@@ -2073,13 +2084,14 @@ mod tests {
cursor.set_position(0);
{
let mut seek_immediately = Decoder::new(&mut cursor).unwrap();
let mut seek_immediately = Decoder::open(&mut cursor).unwrap();
assert!(seek_immediately.seek_to_image(0).is_ok());
assert_eq!(seek_immediately.ifd_pointer(), first_ptr);
assert!(seek_immediately.seek_to_image(1).is_err());
assert!(seek_immediately.seek_to_image(2).is_ok());
assert_eq!(seek_immediately.ifd_pointer(), third_ptr);
assert!(seek_immediately
.image_ifd()
.current_ifd()
.directory()
.get(Tag::ImageWidth)
.is_some());
@@ -2088,11 +2100,11 @@ mod tests {
cursor.set_position(0);
{
let mut seek_immediately = Decoder::new(&mut cursor).unwrap();
let mut seek_immediately = Decoder::open(&mut cursor).unwrap();
assert!(seek_immediately.seek_to_directory(1).is_ok());
assert_eq!(seek_immediately.ifd_pointer(), second_ptr);
assert!(seek_immediately
.image_ifd()
.current_ifd()
.directory()
.get(Tag::ImageWidth)
.is_none());
@@ -2115,19 +2127,19 @@ mod tests {
let header = super::TiffHeader::parse(&mut cursor).unwrap();
let mut decoder = header.open(cursor);
let indicated_next = decoder.image_ifd().directory().next();
let indicated_next = decoder.current_ifd().directory().next();
assert!(decoder.more_images());
assert_eq!(decoder.ifd_pointer(), None);
assert!(decoder.next_directory().is_ok());
let first_ifd = decoder.image_ifd();
let first_ifd = decoder.current_ifd();
assert!(first_ifd.directory().get(Tag::ImageWidth).is_none());
assert!(decoder.more_images());
assert_eq!(decoder.ifd_pointer(), indicated_next);
assert!(decoder.next_image().is_ok());
let second_ifd = decoder.image_ifd();
let second_ifd = decoder.current_ifd();
assert!(second_ifd.directory().get(Tag::ImageWidth).is_some());
assert!(!decoder.more_images());
}