Skip to content

Commit b589d10

Browse files
authored
Merge pull request #289 from aotimme/decoder-inner
Add inner() method to decoder to provide access to raw reader
2 parents 3345328 + f4378ee commit b589d10

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

src/decoder/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,11 @@ impl<R: Read + Seek> Decoder<R> {
660660
}
661661
}
662662

663+
/// Returns a mutable reference to the stream being decoded.
664+
pub fn inner(&mut self) -> &mut R {
665+
self.value_reader.reader.inner()
666+
}
667+
663668
/// Reads a TIFF byte value
664669
#[inline]
665670
pub fn read_byte(&mut self) -> Result<u8, io::Error> {

tests/decode_images.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use tiff::decoder::{ifd, Decoder, DecodingResult};
44
use tiff::ColorType;
55

66
use std::fs::File;
7+
use std::io::{Read, Seek};
78
use std::path::PathBuf;
89

910
const TEST_IMAGE_DIR: &str = "./tests/images/";
@@ -231,6 +232,54 @@ fn test_tiled_rect_rgb_u8() {
231232
test_image_sum_u8("tiled-rect-rgb-u8.tif", ColorType::RGB(8), 62081032);
232233
}
233234

235+
#[test]
236+
fn test_inner_access() {
237+
let path = PathBuf::from(TEST_IMAGE_DIR).join("tiled-rect-rgb-u8.tif");
238+
let img_file = File::open(path).expect("Cannot find test image!");
239+
let mut decoder = Decoder::new(img_file).expect("Cannot create decoder");
240+
assert_eq!(decoder.colortype().unwrap(), ColorType::RGB(8));
241+
242+
let c = decoder
243+
.get_tag(tiff::tags::Tag::Compression)
244+
.unwrap()
245+
.into_u16()
246+
.unwrap();
247+
assert_eq!(c, tiff::tags::CompressionMethod::None.to_u16());
248+
249+
// Because the image is uncompressed, reading the first tile directly with the inner reader
250+
// should yield the same result as reading it with the decoder's read_chunk method.
251+
let first_offset = decoder
252+
.get_tag_u32_vec(tiff::tags::Tag::TileOffsets)
253+
.unwrap()
254+
.into_iter()
255+
.next()
256+
.unwrap();
257+
let first_byte_count = decoder
258+
.get_tag_u32_vec(tiff::tags::Tag::TileByteCounts)
259+
.unwrap()
260+
.into_iter()
261+
.next()
262+
.unwrap();
263+
decoder
264+
.inner()
265+
.seek(std::io::SeekFrom::Start(first_offset as u64))
266+
.expect("Cannot seek to tile offset");
267+
let mut buf = vec![0; first_byte_count as usize];
268+
decoder
269+
.inner()
270+
.read_exact(&mut buf)
271+
.expect("Cannot read tile data");
272+
let raw_sum: u64 = buf.into_iter().map(<u64>::from).sum();
273+
274+
match decoder.read_chunk(0).unwrap() {
275+
DecodingResult::U8(chunk) => {
276+
let sum: u64 = chunk.into_iter().map(<u64>::from).sum();
277+
assert_eq!(sum, raw_sum);
278+
}
279+
_ => panic!("Wrong bit depth"),
280+
}
281+
}
282+
234283
/* #[test]
235284
fn test_tiled_jpeg_rgb_u8() {
236285
test_image_sum_u8("tiled-jpeg-rgb-u8.tif", ColorType::RGB(8), 93031606);
@@ -292,6 +341,11 @@ fn test_tiled_incremental() {
292341

293342
#[test]
294343
fn test_planar_rgb_u8() {
344+
test_image_sum_u8("planar-rgb-u8.tif", ColorType::RGB(8), 15417630);
345+
}
346+
347+
#[test]
348+
fn test_read_planar_bands() {
295349
// gdal_translate tiled-rgb-u8.tif planar-rgb-u8.tif -co INTERLEAVE=BAND -co COMPRESS=LZW -co PROFILE=BASELINE
296350
let file = "planar-rgb-u8.tif";
297351
let expected_type = ColorType::RGB(8);
@@ -328,8 +382,6 @@ fn test_planar_rgb_u8() {
328382
}
329383
_ => panic!("Wrong bit depth"),
330384
}
331-
332-
test_image_sum_u8(file, ColorType::RGB(8), 15417630);
333385
}
334386

335387
#[test]

0 commit comments

Comments
 (0)