imagesize/formats/
tiff.rsuse crate::util::*;
use crate::{ImageResult, ImageSize};
use std::io::{BufRead, Cursor, Seek, SeekFrom};
pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
reader.seek(SeekFrom::Start(0))?;
let mut endian_marker = [0; 2];
reader.read_exact(&mut endian_marker)?;
let endianness = if &endian_marker[0..2] == b"II" {
Endian::Little
} else if &endian_marker[0..2] == b"MM" {
Endian::Big
} else {
return Err(
std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid TIFF header").into(),
);
};
reader.seek(SeekFrom::Start(4))?;
let ifd_offset = read_u32(reader, &endianness)?;
if ifd_offset == 0 {
return Err(
std::io::Error::new(std::io::ErrorKind::InvalidData, "Invalid IFD offset").into(),
);
}
reader.seek(SeekFrom::Start(ifd_offset.into()))?;
let ifd_count = read_u16(reader, &endianness)?;
let mut width = None;
let mut height = None;
for _ifd in 0..ifd_count {
let tag = read_u16(reader, &endianness)?;
let kind = read_u16(reader, &endianness)?;
let _count = read_u32(reader, &endianness)?;
let value_bytes = match kind {
1 | 2 | 6 | 7 => 1,
3 | 8 => 2,
4 | 9 | 11 | 13 => 4,
5 | 10 => 4 * 2,
12 | 16 | 17 | 18 => 8,
_ => {
return Err(std::io::Error::new(
std::io::ErrorKind::InvalidData,
"Invalid IFD type",
)
.into())
}
};
let mut value_buffer = [0; 4];
reader.read_exact(&mut value_buffer)?;
let mut r = Cursor::new(&value_buffer[..]);
let value = match value_bytes {
2 => Some(read_u16(&mut r, &endianness)? as u32),
4 => Some(read_u32(&mut r, &endianness)?),
_ => None,
};
if tag == 0x100 {
width = value;
} else if tag == 0x101 {
height = value;
}
if let (Some(width), Some(height)) = (width, height) {
return Ok(ImageSize {
width: width as usize,
height: height as usize,
});
}
}
Err(std::io::Error::new(std::io::ErrorKind::InvalidData, "No dimensions in IFD tags").into())
}
pub fn matches(header: &[u8]) -> bool {
header.starts_with(b"II\x2A\x00") || header.starts_with(b"MM\x00\x2A")
}