Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
99a7b8cba7 | ||
|
|
418ea182c3 | ||
|
|
529fa64e13 | ||
|
|
3d6b94b65a | ||
|
|
a51b23091e | ||
|
|
ac65bd8477 | ||
|
|
1bd6293857 | ||
|
|
87341f9075 |
No files matched your search
+17
@@ -1,3 +1,20 @@
|
||||
# Version 0.11.2
|
||||
|
||||
Changes:
|
||||
- Planar files can now be read with `Decoder::read_image_to_buffer` if the
|
||||
limits allow for such a large buffer size. Previously only the first plane
|
||||
was read and the decoder will attempt a fallback to this behavior if the
|
||||
limits do not allow all planes to be allocated.
|
||||
|
||||
Fixes:
|
||||
- An off-by-one error in a `debug_assert` of `ValueBuffer` caused its use in
|
||||
`IfdDecoder::find_tag_buf` to panic outside release mode when the buffer
|
||||
exactly matched the required size.
|
||||
|
||||
Additions:
|
||||
- The methods `ColorType::{num_samples, bit_depth}` are now public. This
|
||||
promises that we only have homogeneous color depths in this major version.
|
||||
|
||||
# Version 0.11.1
|
||||
|
||||
Fixes:
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "tiff"
|
||||
version = "0.11.1"
|
||||
version = "0.11.2"
|
||||
edition = "2021"
|
||||
resolver = "2"
|
||||
|
||||
|
||||
+104
-1
@@ -37,6 +37,7 @@ fn debug_planes(
|
||||
(width, height): (u32, u32),
|
||||
) {
|
||||
let (depth, mut tupltype) = match colortype {
|
||||
// Note: we will expand, so this is in fact not BLACKANDWHITE
|
||||
tiff::ColorType::Gray(_) => (1, "GRAYSCALE"),
|
||||
tiff::ColorType::RGB(_) => (3, "RGB"),
|
||||
tiff::ColorType::RGBA(_) => (4, "RGB_ALPHA"),
|
||||
@@ -49,8 +50,57 @@ fn debug_planes(
|
||||
}
|
||||
};
|
||||
|
||||
// Number of samples in a line and number of sample lines in the image, depending on whether
|
||||
// they are planar or not. This determines the layout of the image buffer when expanding bit
|
||||
// packed samples to a full byte (we support power-of-two sample sizes, positive and negative,
|
||||
// but only those).
|
||||
let (swidth, sheight) = if layout.planes > 1 {
|
||||
(
|
||||
u64::from(width) * u64::from(colortype.num_samples()),
|
||||
u64::from(height),
|
||||
)
|
||||
} else {
|
||||
(
|
||||
u64::from(width),
|
||||
u64::from(height) * u64::from(colortype.num_samples()),
|
||||
)
|
||||
};
|
||||
|
||||
let mut fallback_buffer;
|
||||
let maxval = match data.as_buffer(0) {
|
||||
DecodingBuffer::U8(_) => u64::from(u8::MAX),
|
||||
// `U8` is also our representation of bit-packed color. PAM uses the smallest number of
|
||||
// *bytes* needed to represent a sample but we must also adhere to maxval. We could explode
|
||||
// them here (e.g. represent 1-bit as `{0, 1}`) but we'd also set `maxval` accordingly and
|
||||
// this requires tupltype to be `BLACKANDWHITE` so introducing a bunch of complexity. If
|
||||
// you want to do that in your real code, go ahead.
|
||||
DecodingBuffer::U8(buf) => match colortype.bit_depth() {
|
||||
8 => u64::from(u8::MAX),
|
||||
4 => {
|
||||
fallback_buffer = vec![0; (swidth * sheight) as usize];
|
||||
expand_4bit(buf, swidth, &mut fallback_buffer);
|
||||
*data = DecodingResult::U8(fallback_buffer);
|
||||
u64::from(u8::MAX)
|
||||
}
|
||||
2 => {
|
||||
fallback_buffer = vec![0; (swidth * sheight) as usize];
|
||||
expand_2bit(buf, swidth, &mut fallback_buffer);
|
||||
*data = DecodingResult::U8(fallback_buffer);
|
||||
u64::from(u8::MAX)
|
||||
}
|
||||
1 => {
|
||||
fallback_buffer = vec![0; (swidth * sheight) as usize];
|
||||
expand_1bit(buf, swidth, &mut fallback_buffer);
|
||||
*data = DecodingResult::U8(fallback_buffer);
|
||||
u64::from(u8::MAX)
|
||||
}
|
||||
_ => {
|
||||
eprintln!(
|
||||
"Unsupported bit depth for U8 buffer: {}",
|
||||
colortype.bit_depth()
|
||||
);
|
||||
return;
|
||||
}
|
||||
},
|
||||
DecodingBuffer::U16(_) => u64::from(u16::MAX),
|
||||
DecodingBuffer::U32(_) => u64::from(u32::MAX),
|
||||
DecodingBuffer::U64(_) => u64::MAX,
|
||||
@@ -101,3 +151,56 @@ fn debug_planes(
|
||||
.expect("Failed to write PAM data");
|
||||
}
|
||||
}
|
||||
|
||||
/// Put exactly the ith `SCALE`-bit chunk of `val` into the least significant bits.
|
||||
#[inline(always)]
|
||||
fn bits_extract<const SCALE: u8>(val: u8, i: u8) -> u8 {
|
||||
// Shift left to discard high bits, then right to bring desired bits down. This way also
|
||||
// conveniently avoids all cases of overflow.
|
||||
(val << (i * SCALE)) >> (8 - SCALE)
|
||||
}
|
||||
|
||||
fn expand_1bit(from: &[u8], samples_per_padded: u64, into: &mut [u8]) {
|
||||
let stride = samples_per_padded.div_ceil(8);
|
||||
|
||||
let from_row = from.chunks_exact(stride as usize);
|
||||
let into_row = into.chunks_exact_mut(samples_per_padded as usize);
|
||||
|
||||
for (from_row, into_row) in from_row.zip(into_row) {
|
||||
for (into, &from) in into_row.chunks_mut(8).zip(from_row) {
|
||||
let data =
|
||||
core::array::from_fn::<u8, 8, _>(|i| 0xff * bits_extract::<1>(from, i as u8));
|
||||
into.copy_from_slice(&data[..into.len()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_2bit(from: &[u8], samples_per_padded: u64, into: &mut [u8]) {
|
||||
let stride = samples_per_padded.div_ceil(4);
|
||||
|
||||
let from_row = from.chunks_exact(stride as usize);
|
||||
let into_row = into.chunks_exact_mut(samples_per_padded as usize);
|
||||
|
||||
for (from_row, into_row) in from_row.zip(into_row) {
|
||||
for (into, &from) in into_row.chunks_mut(4).zip(from_row) {
|
||||
let data =
|
||||
core::array::from_fn::<u8, 4, _>(|i| 0xaa * bits_extract::<2>(from, i as u8));
|
||||
into.copy_from_slice(&data[..into.len()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn expand_4bit(from: &[u8], samples_per_padded: u64, into: &mut [u8]) {
|
||||
let stride = samples_per_padded.div_ceil(2);
|
||||
|
||||
let from_row = from.chunks_exact(stride as usize);
|
||||
let into_row = into.chunks_exact_mut(samples_per_padded as usize);
|
||||
|
||||
for (from_row, into_row) in from_row.zip(into_row) {
|
||||
for (into, &from) in into_row.chunks_mut(2).zip(from_row) {
|
||||
let data =
|
||||
core::array::from_fn::<u8, 2, _>(|i| 0x88 * bits_extract::<4>(from, i as u8));
|
||||
into.copy_from_slice(&data[..into.len()]);
|
||||
}
|
||||
}
|
||||
}
|
||||
+42
-8
@@ -1396,14 +1396,16 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
/// # Bugs
|
||||
///
|
||||
/// When the image is stored as a planar configuration, this method will currently only read
|
||||
/// the first sample's plane. This will be fixed in a future major version of `tiff`.
|
||||
/// the first sample's plane. This will be fixed in a future major version of `tiff`. To read
|
||||
/// multiple planes, [`Self::read_image_to_buffer`] can be used instead.
|
||||
///
|
||||
/// # Intent to deprecate
|
||||
///
|
||||
/// Use `DecodingResult::resize_to` and `read_image_bytes` instead where possible, preserving
|
||||
/// the buffer across multiple calls. This old method will likely keep its bugged planar
|
||||
/// behavior until it is fully replaced, to ensure that existing code will not run into
|
||||
/// unexpectedly large allocations that will error on limits instead.
|
||||
/// Use [`Self::read_image_to_buffer`] or a combination of [`DecodingResult::resize_to`] and
|
||||
/// [`Self::read_image_bytes`] instead where possible, preserving the buffer across multiple
|
||||
/// calls. This old method will likely keep its bugged planar behavior until it is fully
|
||||
/// replaced, to ensure that existing code will not run into unexpectedly large allocations
|
||||
/// that will error on limits instead.
|
||||
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
|
||||
let readout = self.image().readout_for_image()?;
|
||||
|
||||
@@ -1422,6 +1424,30 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
/// configured limits, and then pass it to the underlying method. This is essentially a
|
||||
/// type-safe wrapper around the raw [`Self::read_image_bytes`] method.
|
||||
///
|
||||
/// ## Planar behavior
|
||||
///
|
||||
/// If the image is stored as a planar configuration, an attempt is made to resize the buffer
|
||||
/// to hold all planes. If that does not fit then only the first plane is read. Check the
|
||||
/// buffer size against [`BufferLayoutPreference::complete_len`] to ensure that all planes were
|
||||
/// read:
|
||||
///
|
||||
/// ```
|
||||
/// use tiff::decoder::{Decoder, DecodingResult};
|
||||
/// let mut result = DecodingResult::U8(vec![]);
|
||||
///
|
||||
/// let mut reader = /* */
|
||||
/// # Decoder::new(std::io::Cursor::new(include_bytes!(concat!(
|
||||
/// # env!("CARGO_MANIFEST_DIR"), "/tests/images/tiled-gray-i1.tif"
|
||||
/// # )))).unwrap();
|
||||
/// let layout = reader.read_image_to_buffer(&mut result)?;
|
||||
///
|
||||
/// if result.as_buffer(0).as_bytes().len() < layout.complete_len {
|
||||
/// println!("Only the first plane was read");
|
||||
/// }
|
||||
///
|
||||
/// # Ok::<_, tiff::TiffError>(())
|
||||
/// ```
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
/// ```
|
||||
@@ -1443,12 +1469,20 @@ impl<R: Read + Seek> Decoder<R> {
|
||||
result: &mut DecodingResult,
|
||||
) -> TiffResult<BufferLayoutPreference> {
|
||||
let readout = self.image().readout_for_image()?;
|
||||
|
||||
let planes = readout.to_plane_layout()?;
|
||||
let layout = BufferLayoutPreference::from_planes(&planes);
|
||||
|
||||
let extent = readout.result_extent_for_planes(0..1)?;
|
||||
let num_planes = if planes.total_bytes <= self.value_reader.limits.decoding_buffer_size {
|
||||
planes.plane_offsets.len() as u16
|
||||
} else {
|
||||
1
|
||||
};
|
||||
|
||||
let layout = BufferLayoutPreference::from_planes(&planes);
|
||||
let extent = readout.result_extent_for_planes(0..num_planes)?;
|
||||
// Compatibility: if this extent is too large our configured limits we will fall back to
|
||||
// reading only the first plane.
|
||||
result.resize_to_extent(extent, &self.value_reader.limits)?;
|
||||
|
||||
self.read_image_bytes(result.as_buffer(0).as_bytes_mut())?;
|
||||
|
||||
Ok(layout)
|
||||
|
||||
+4
-2
@@ -51,7 +51,8 @@ pub enum ColorType {
|
||||
Multiband { bit_depth: u8, num_samples: u16 },
|
||||
}
|
||||
impl ColorType {
|
||||
fn bit_depth(&self) -> u8 {
|
||||
/// The number of bits used to store each sample.
|
||||
pub fn bit_depth(&self) -> u8 {
|
||||
match *self {
|
||||
ColorType::Gray(b)
|
||||
| ColorType::RGB(b)
|
||||
@@ -66,7 +67,8 @@ impl ColorType {
|
||||
}
|
||||
}
|
||||
|
||||
fn num_samples(&self) -> u16 {
|
||||
/// The number of samples in each representation of a single color.
|
||||
pub fn num_samples(&self) -> u16 {
|
||||
match *self {
|
||||
ColorType::Gray(_) => 1,
|
||||
ColorType::RGB(_) => 3,
|
||||
|
||||
+2
-2
@@ -390,7 +390,7 @@ impl ValueBuffer {
|
||||
debug_assert!({
|
||||
self.ty
|
||||
.value_bytes(self.count)
|
||||
.is_ok_and(|n| n < self.bytes.len() as u64)
|
||||
.is_ok_and(|n| n <= self.bytes.len() as u64)
|
||||
});
|
||||
|
||||
self.count
|
||||
@@ -439,7 +439,7 @@ impl ValueBuffer {
|
||||
pub(crate) fn assume_type(&mut self, ty: Type, count: u64, bo: ByteOrder) {
|
||||
debug_assert!({
|
||||
ty.value_bytes(count)
|
||||
.is_ok_and(|n| n < self.bytes.len() as u64)
|
||||
.is_ok_and(|n| n <= self.bytes.len() as u64)
|
||||
});
|
||||
|
||||
self.byte_order = bo;
|
||||
|
||||
+48
-1
@@ -1,4 +1,4 @@
|
||||
use tiff::decoder::{Decoder, TiffCodingUnit};
|
||||
use tiff::decoder::{Decoder, DecodingResult, Limits, TiffCodingUnit};
|
||||
|
||||
use std::fs::File;
|
||||
use std::path::PathBuf;
|
||||
@@ -74,3 +74,50 @@ fn read_first_coding_unit() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn read_planes_by_limits() {
|
||||
let path = PathBuf::from(TEST_IMAGE_DIR).join("planar-rgb-u8.tif");
|
||||
let file = File::open(path).unwrap();
|
||||
let io = std::io::BufReader::new(file);
|
||||
|
||||
let mut limits = Limits::default();
|
||||
let mut buffer = DecodingResult::U8(vec![]);
|
||||
|
||||
// Make room for 1 plane.
|
||||
let mut tif = Decoder::new(io).unwrap();
|
||||
tif = tif.with_limits({
|
||||
limits.decoding_buffer_size = 374 * 499;
|
||||
limits.clone()
|
||||
});
|
||||
|
||||
tif.read_image_to_buffer(&mut buffer).unwrap();
|
||||
assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499);
|
||||
|
||||
// Make room for 2 planes.
|
||||
tif = tif.with_limits({
|
||||
limits.decoding_buffer_size = 374 * 499 * 2;
|
||||
limits.clone()
|
||||
});
|
||||
|
||||
tif.read_image_to_buffer(&mut buffer).unwrap();
|
||||
assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499);
|
||||
|
||||
// Make room for 3 planes.
|
||||
tif = tif.with_limits({
|
||||
limits.decoding_buffer_size = 374 * 499 * 3;
|
||||
limits.clone()
|
||||
});
|
||||
|
||||
tif.read_image_to_buffer(&mut buffer).unwrap();
|
||||
assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499 * 3);
|
||||
|
||||
// Make room for 4 planes?
|
||||
tif = tif.with_limits({
|
||||
limits.decoding_buffer_size = 374 * 499 * 4;
|
||||
limits.clone()
|
||||
});
|
||||
|
||||
tif.read_image_to_buffer(&mut buffer).unwrap();
|
||||
assert_eq!(buffer.as_buffer(0).byte_len(), 374 * 499 * 3);
|
||||
}
|
||||
Reference in new issue
Block a user