Compare commits
8
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a6e8ed772d | ||
|
|
9b452e93b7 | ||
|
|
96d37bb087 | ||
|
|
12f16dbfdd | ||
|
|
a8abdd2484 | ||
|
|
934fde185a | ||
|
|
3302936b08 | ||
|
|
a5b9a57e09 |
No files matched your search
+2
-1
@@ -1,5 +1,6 @@
|
||||
|
||||
/target/
|
||||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
tests/benches/corpus/
|
||||
.idea
|
||||
.agentbridge
|
||||
+5
-3
@@ -1,8 +1,7 @@
|
||||
[package]
|
||||
name = "tiff"
|
||||
name = "ndbioimage-tiff"
|
||||
version = "0.11.3"
|
||||
edition = "2021"
|
||||
resolver = "2"
|
||||
|
||||
# note: when changed, also update test runner in `.github/workflows/rust.yml`
|
||||
rust-version = "1.88.0"
|
||||
@@ -16,6 +15,9 @@ categories = ["multimedia::images", "multimedia::encoding"]
|
||||
|
||||
exclude = ["tests/images/*", "tests/fuzz_images/*", "tests/**/*.tif"]
|
||||
|
||||
[lib]
|
||||
name = "tiff"
|
||||
|
||||
[dependencies]
|
||||
half = { version = "2.4.1" }
|
||||
quick-error = "2.0.1"
|
||||
@@ -24,7 +26,7 @@ quick-error = "2.0.1"
|
||||
fax34 = { package = "fax", version = "0.2.6", optional = true }
|
||||
flate2 = { version = "1.0.20", optional = true }
|
||||
weezl = { version = "0.2.1", optional = true }
|
||||
zstd = { version = "0.13", optional = true }
|
||||
zstd = { version = "0.14", optional = true }
|
||||
zrip-decode = { version = "0.8.2", features = ["paranoid"], optional = true }
|
||||
zune-jpeg = { version = "0.5.1", optional = true }
|
||||
image-webp = {version = "0.2.4", optional = true }
|
||||
|
||||
@@ -5,6 +5,8 @@
|
||||
|
||||
TIFF decoding and encoding library in pure Rust
|
||||
|
||||
Forked from https://github.com/image-rs/image-tiff. Changes: read images containing RGBPalette frames, ignoring colormaps.
|
||||
|
||||
## Supported
|
||||
|
||||
### Features
|
||||
|
||||
+42
-22
@@ -599,8 +599,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(2)
|
||||
.map(|ch| Short(u16::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Short(u16::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -609,8 +611,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(2)
|
||||
.map(|ch| SignedShort(i16::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| SignedShort(i16::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -619,8 +623,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(4)
|
||||
.map(|ch| Unsigned(u32::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Unsigned(u32::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -629,8 +635,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(4)
|
||||
.map(|ch| Signed(i32::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Signed(i32::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -639,8 +647,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(4)
|
||||
.map(|ch| Float(f32::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Float(f32::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -649,15 +659,17 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(8)
|
||||
.map(|ch| Double(f64::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<8>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Double(f64::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
Type::RATIONAL => {
|
||||
v = Self::vec_with_capacity(self.count, self.type_, limits)?;
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(bytes.chunks_exact(8).map(|ch| {
|
||||
v.extend(bytes.as_chunks::<8>().0.iter().map(|ch| {
|
||||
Rational(
|
||||
u32::from_ne_bytes(ch[..4].try_into().unwrap()),
|
||||
u32::from_ne_bytes(ch[4..].try_into().unwrap()),
|
||||
@@ -668,7 +680,7 @@ impl Entry {
|
||||
Type::SRATIONAL => {
|
||||
v = Self::vec_with_capacity(self.count, self.type_, limits)?;
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(bytes.chunks_exact(8).map(|ch| {
|
||||
v.extend(bytes.as_chunks::<8>().0.iter().map(|ch| {
|
||||
SRational(
|
||||
i32::from_ne_bytes(ch[..4].try_into().unwrap()),
|
||||
i32::from_ne_bytes(ch[4..].try_into().unwrap()),
|
||||
@@ -681,8 +693,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(8)
|
||||
.map(|ch| UnsignedBig(u64::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<8>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| UnsignedBig(u64::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -691,8 +705,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(8)
|
||||
.map(|ch| SignedBig(i64::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<8>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| SignedBig(i64::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -701,8 +717,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(4)
|
||||
.map(|ch| Ifd(u32::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| Ifd(u32::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
@@ -711,8 +729,10 @@ impl Entry {
|
||||
self.decode_values(self.count, self.type_, reader, |bytes| {
|
||||
v.extend(
|
||||
bytes
|
||||
.chunks_exact(8)
|
||||
.map(|ch| IfdBig(u64::from_ne_bytes(ch.try_into().unwrap()))),
|
||||
.as_chunks::<8>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|&ch| IfdBig(u64::from_ne_bytes(ch))),
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
+18
-7
@@ -36,6 +36,9 @@ impl TileAttributes {
|
||||
pub fn tiles_down(&self) -> usize {
|
||||
self.image_height.div_ceil(self.tile_length)
|
||||
}
|
||||
pub fn tiles_per_plane(&self) -> usize {
|
||||
self.tiles_across() * self.tiles_down()
|
||||
}
|
||||
fn padding_right(&self) -> usize {
|
||||
(self.tile_width - self.image_width % self.tile_width) % self.tile_width
|
||||
}
|
||||
@@ -176,7 +179,7 @@ impl Image {
|
||||
.map(Value::into_u16)
|
||||
.transpose()?
|
||||
.and_then(PhotometricInterpretation::from_u16)
|
||||
.ok_or(TiffUnsupportedError::UnknownInterpretation)?;
|
||||
.unwrap_or(PhotometricInterpretation::BlackIsZero);
|
||||
|
||||
// Try to parse both the compression method and the number, format, and bits of the included samples.
|
||||
// If they are not explicitly specified, those tags are reset to their default values and not carried from previous images.
|
||||
@@ -467,11 +470,11 @@ impl Image {
|
||||
}
|
||||
Some(map)
|
||||
}
|
||||
None => {
|
||||
return Err(TiffError::FormatError(
|
||||
TiffFormatError::RequiredTagNotFound(Tag::ColorMap),
|
||||
));
|
||||
}
|
||||
// A missing palette is not an error. The pixel values of an indexed image are just
|
||||
// indices into the palette, and the palette itself is only display information, so
|
||||
// the image still decodes. Callers observe the absence as `None` from
|
||||
// `Decoder::color_map`.
|
||||
None => None,
|
||||
}
|
||||
} else {
|
||||
None
|
||||
@@ -1016,7 +1019,15 @@ impl Image {
|
||||
}
|
||||
ChunkType::Tile => {
|
||||
let tile_attrs = self.tile_attributes.as_ref().unwrap();
|
||||
let (padding_right, padding_down) = tile_attrs.get_padding(chunk_index as usize);
|
||||
// The chunk index counts the tiles of all planes, but the tile grid repeats per
|
||||
// plane, so only the position within the plane decides the padding.
|
||||
let tiles_per_plane = tile_attrs.tiles_per_plane();
|
||||
let tile = if tiles_per_plane == 0 {
|
||||
0
|
||||
} else {
|
||||
chunk_index as usize % tiles_per_plane
|
||||
};
|
||||
let (padding_right, padding_down) = tile_attrs.get_padding(tile);
|
||||
|
||||
let tile_width = tile_attrs.tile_width - padding_right;
|
||||
let tile_length = tile_attrs.tile_length - padding_down;
|
||||
|
||||
+4
-4
@@ -294,8 +294,8 @@ fn one_byte_predict<const N: usize>(buf: &mut [u8]) {
|
||||
acc.copy_from_slice(&buf[..N]);
|
||||
|
||||
// Process N bytes per iteration. Each channel's prefix sum is independent.
|
||||
let mut chunks = buf[N..].chunks_exact_mut(N);
|
||||
for chunk in &mut chunks {
|
||||
let chunks = buf[N..].as_chunks_mut::<N>();
|
||||
for chunk in &mut chunks.0.iter_mut() {
|
||||
for c in 0..N {
|
||||
acc[c] = chunk[c].wrapping_add(acc[c]);
|
||||
chunk[c] = acc[c];
|
||||
@@ -303,7 +303,7 @@ fn one_byte_predict<const N: usize>(buf: &mut [u8]) {
|
||||
}
|
||||
|
||||
// Handle trailing bytes (when buf.len() - N is not a multiple of N).
|
||||
for (c, byte) in chunks.into_remainder().iter_mut().enumerate() {
|
||||
for (c, byte) in chunks.1.iter_mut().enumerate() {
|
||||
*byte = byte.wrapping_add(acc[c]);
|
||||
}
|
||||
}
|
||||
@@ -1288,7 +1288,7 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
/// Utility to create a reader like [`Self::read_entry`], from the current IFD.
|
||||
///
|
||||
/// Returns an `Ok(None)` if the tag is not found in the current directory. See
|
||||
/// [`Self::read_entry´] of which this is a simple wrapper.
|
||||
/// [`Self::read_entry`] of which this is a simple wrapper.
|
||||
pub fn read_tag(&mut self, tag: Tag) -> TiffResult<Option<EntryBytesReader<'_, R>>> {
|
||||
match self.current_ifd().find_entry(tag) {
|
||||
None => Ok(None),
|
||||
|
||||
+1
-1
@@ -171,7 +171,7 @@ mod tests {
|
||||
dir.get(Tag::Unknown(1))
|
||||
.expect("tag 1 should be present after this chain")
|
||||
.count(),
|
||||
u16::MAX.into()
|
||||
u64::from(u16::MAX)
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
+3
-7
@@ -617,26 +617,22 @@ impl ByteOrder {
|
||||
return;
|
||||
}
|
||||
|
||||
// FIXME: at MSRV 1.89 or higher use `slice::as_chunks_mut`.
|
||||
match cls {
|
||||
EndianBytes::One => {
|
||||
// No change needed
|
||||
}
|
||||
EndianBytes::Two => {
|
||||
for chunk in buffer.chunks_exact_mut(2) {
|
||||
let chunk: &mut [u8; 2] = chunk.try_into().unwrap();
|
||||
for chunk in buffer.as_chunks_mut::<2>().0 {
|
||||
*chunk = u16::from_be_bytes(*chunk).to_le_bytes();
|
||||
}
|
||||
}
|
||||
EndianBytes::Four => {
|
||||
for chunk in buffer.chunks_exact_mut(4) {
|
||||
let chunk: &mut [u8; 4] = chunk.try_into().unwrap();
|
||||
for chunk in buffer.as_chunks_mut::<4>().0 {
|
||||
*chunk = u32::from_be_bytes(*chunk).to_le_bytes();
|
||||
}
|
||||
}
|
||||
EndianBytes::Eight => {
|
||||
for chunk in buffer.chunks_exact_mut(8) {
|
||||
let chunk: &mut [u8; 8] = chunk.try_into().unwrap();
|
||||
for chunk in buffer.as_chunks_mut::<8>().0 {
|
||||
*chunk = u64::from_be_bytes(*chunk).to_le_bytes();
|
||||
}
|
||||
}
|
||||
|
||||
+47
-6
@@ -506,6 +506,47 @@ fn test_tiled_extra_samples_border_u8() {
|
||||
assert_eq!(data, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_tiled_planar_border_u16() {
|
||||
// A planar (separate) tiled image whose dimensions are not multiples of the tile size.
|
||||
// The tile grid repeats for every plane, but the chunk (tile) index counts all planes, so
|
||||
// the border padding of the second plane used to be looked up with the wrong tile index.
|
||||
// The bottom-right tile of plane 1 then claimed a full tile height, and the decoder ran
|
||||
// past the end of the output buffer.
|
||||
//
|
||||
// Note that `read_image` only decodes the first plane, so this has to go through
|
||||
// `read_image_to_buffer` to reach the tiles that were mis-sized.
|
||||
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-planar-2c-16b.tif");
|
||||
let img_file = File::open(path).expect("Cannot find test image!");
|
||||
let mut decoder = Decoder::open(img_file).expect("Cannot create decoder");
|
||||
decoder.next_image().expect("Cannot read image IFD");
|
||||
assert_eq!(decoder.dimensions().unwrap(), (30, 20));
|
||||
assert_eq!(
|
||||
decoder.colortype().unwrap(),
|
||||
ColorType::Multiband {
|
||||
bit_depth: 16,
|
||||
num_samples: 2
|
||||
}
|
||||
);
|
||||
|
||||
let mut buffer = DecodingSampleBuffer::U16(Vec::new());
|
||||
decoder.read_image_to_buffer(&mut buffer).unwrap();
|
||||
let DecodingSampleBuffer::U16(data) = buffer else {
|
||||
panic!("Wrong bit depth")
|
||||
};
|
||||
assert_eq!(data.len(), 2 * 20 * 30);
|
||||
|
||||
let mut expected = Vec::with_capacity(2 * 20 * 30);
|
||||
for plane in 0..2u16 {
|
||||
for y in 0..20u16 {
|
||||
for x in 0..30u16 {
|
||||
expected.push(1000 * plane + 10 * y + x);
|
||||
}
|
||||
}
|
||||
}
|
||||
assert_eq!(data, expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inner_access() {
|
||||
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-rect-rgb-u8.tif");
|
||||
@@ -898,22 +939,22 @@ fn test_image_bytes(
|
||||
}
|
||||
|
||||
fn byte_order_u16(bytes: &mut [u8]) {
|
||||
for chunk in bytes.chunks_exact_mut(2) {
|
||||
let n = u16::from_ne_bytes(chunk.try_into().unwrap());
|
||||
for chunk in bytes.as_chunks_mut::<2>().0 {
|
||||
let n = u16::from_ne_bytes(*chunk);
|
||||
chunk.copy_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn byte_order_u32(bytes: &mut [u8]) {
|
||||
for chunk in bytes.chunks_exact_mut(4) {
|
||||
let n = u32::from_ne_bytes(chunk.try_into().unwrap());
|
||||
for chunk in bytes.as_chunks_mut::<4>().0 {
|
||||
let n = u32::from_ne_bytes(*chunk);
|
||||
chunk.copy_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
fn byte_order_u64(bytes: &mut [u8]) {
|
||||
for chunk in bytes.chunks_exact_mut(8) {
|
||||
let n = u64::from_ne_bytes(chunk.try_into().unwrap());
|
||||
for chunk in bytes.as_chunks_mut::<8>().0 {
|
||||
let n = u64::from_ne_bytes(*chunk);
|
||||
chunk.copy_from_slice(&n.to_le_bytes());
|
||||
}
|
||||
}
|
||||
|
||||
Binary file not shown.
Reference in new issue
Block a user