Skip to content

Commit 2d90179

Browse files
committed
Soften deprecation of Decoder::read_image
1 parent 5a3b113 commit 2d90179

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

src/decoder/mod.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,13 @@ impl<R: Read + Seek> Decoder<R> {
13961396
///
13971397
/// When the image is stored as a planar configuration, this method will currently only read
13981398
/// the first sample's plane. This will be fixed in a future major version of `tiff`.
1399-
#[deprecated = "Use `DecodingResult::resize_to` and `read_image_bytes` instead. This old method will keep its bugged planar behavior until it is removed."]
1399+
///
1400+
/// # Intent to deprecate
1401+
///
1402+
/// Use `DecodingResult::resize_to` and `read_image_bytes` instead where possible, preserving
1403+
/// the buffer across multiple calls. This old method will likely keep its bugged planar
1404+
/// behavior until it is fully replaced, to ensure that existing code will not run into
1405+
/// unexpectedly large allocations that will error on limits instead.
14001406
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
14011407
let readout = self.image().readout_for_image()?;
14021408

src/encoder/writer.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ fn encoder_on_short_writes() {
221221
};
222222

223223
let imgdata = vec![42u8; 100 * 100];
224+
let mut data = crate::decoder::DecodingResult::U8(vec![]);
225+
224226
for comp in [
225227
super::Compression::Uncompressed,
226228
super::Compression::Packbits,
@@ -240,7 +242,7 @@ fn encoder_on_short_writes() {
240242

241243
write.inner.set_position(0);
242244
let mut tiff_reader = crate::decoder::Decoder::new(&mut write.inner).unwrap();
243-
let mut data = tiff_reader.read_image().unwrap();
245+
let _layout = tiff_reader.read_image_to_buffer(&mut data).unwrap();
244246

245247
assert!(
246248
matches!(&data, crate::decoder::DecodingResult::U8(v) if *v == imgdata),

tests/encode_images.rs

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ fn encode_decode() {
8181
decoder.get_tag(Tag::Artist).unwrap(),
8282
ifd::Value::Ascii("Image-tiff".into())
8383
);
84-
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
84+
85+
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
86+
let _layout = decoder.read_image_to_buffer(&mut buffer).unwrap();
87+
if let tiff::decoder::DecodingResult::U8(img_res) = buffer {
8588
assert_eq!(image_data, img_res);
8689
} else {
8790
panic!("Wrong data type");
@@ -161,7 +164,9 @@ fn encode_decode_big() {
161164
decoder.get_tag(Tag::Artist).unwrap(),
162165
ifd::Value::Ascii("Image-tiff".into())
163166
);
164-
if let DecodingResult::U8(img_res) = decoder.read_image().unwrap() {
167+
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
168+
let _layout = decoder.read_image_to_buffer(&mut buffer).unwrap();
169+
if let DecodingResult::U8(img_res) = buffer {
165170
assert_eq!(image_data, img_res);
166171
} else {
167172
panic!("Wrong data type");
@@ -245,7 +250,10 @@ macro_rules! test_roundtrip {
245250
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
246251
assert_eq!(decoder.colortype().unwrap(), expected_type);
247252

248-
let image_data = match decoder.read_image().unwrap() {
253+
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
254+
decoder.read_image_to_buffer(&mut buffer).unwrap();
255+
256+
let image_data = match buffer {
249257
DecodingResult::$buffer(res) => res,
250258
_ => panic!("Wrong data type"),
251259
};
@@ -259,9 +267,11 @@ macro_rules! test_roundtrip {
259267
}
260268
file.seek(SeekFrom::Start(0)).unwrap();
261269
{
270+
let mut buffer = tiff::decoder::DecodingResult::U8(vec![]);
262271
let mut decoder = Decoder::new(&mut file).unwrap();
263-
if let DecodingResult::$buffer(img_res) = decoder.read_image().unwrap() {
264-
assert_eq!(image_data, img_res);
272+
decoder.read_image_to_buffer(&mut buffer).unwrap();
273+
if let DecodingResult::$buffer(img_res) = buffer {
274+
assert_eq!(*image_data, img_res);
265275
} else {
266276
panic!("Wrong data type");
267277
}

0 commit comments

Comments
 (0)