Skip to content

Commit acd7696

Browse files
committed
fixed cursor handling
1 parent 3433196 commit acd7696

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

src/ifd.rs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use num_enum::TryFromPrimitive;
77

88
use crate::error::{AsyncTiffError, AsyncTiffResult};
99
use crate::geo::{GeoKeyDirectory, GeoKeyTag};
10-
use crate::reader::{AsyncCursor, AsyncFileReader};
10+
use crate::reader::{AsyncCursor, AsyncFileReader, EndianAwareReader};
1111
use crate::tiff::tags::{
1212
CompressionMethod, PhotometricInterpretation, PlanarConfiguration, Predictor, ResolutionUnit,
1313
SampleFormat, Tag, Type,
@@ -839,7 +839,7 @@ impl ImageFileDirectory {
839839

840840
/// Read a single tag from the cursor
841841
async fn read_tag(cursor: &mut AsyncCursor, bigtiff: bool) -> AsyncTiffResult<(Tag, Value)> {
842-
let start_cursor_position = cursor.position();
842+
// let start_cursor_position = cursor.position();
843843

844844
let tag_name = Tag::from_u16_exhaustive(cursor.read_u16().await?);
845845

@@ -855,9 +855,9 @@ async fn read_tag(cursor: &mut AsyncCursor, bigtiff: bool) -> AsyncTiffResult<(T
855855

856856
let tag_value = read_tag_value(cursor, tag_type, count, bigtiff).await?;
857857

858-
// TODO: better handle management of cursor state
859-
let ifd_entry_size = if bigtiff { 20 } else { 12 };
860-
cursor.seek(start_cursor_position + ifd_entry_size);
858+
// TODO: better handle management of cursor state <- should be done now
859+
// let ifd_entry_size = if bigtiff { 20 } else { 12 };
860+
// cursor.seek(start_cursor_position + ifd_entry_size);
861861

862862
Ok((tag_name, tag_value))
863863
}
@@ -885,16 +885,24 @@ async fn read_tag_value(
885885
// prefetch all tag data
886886
let mut data = if (bigtiff && value_byte_length <= 8) || value_byte_length <= 4 {
887887
// value fits in offset field
888-
cursor.read(value_byte_length).await?
888+
let res = cursor.read(value_byte_length).await?;
889+
if bigtiff {
890+
cursor.advance(8-value_byte_length);
891+
} else {
892+
cursor.advance(4-value_byte_length);
893+
}
894+
res
889895
} else {
890896
// Seek cursor
891897
let offset = if bigtiff {
892898
cursor.read_u64().await?
893899
} else {
894900
cursor.read_u32().await?.into()
895901
};
896-
cursor.seek(offset);
897-
cursor.read(value_byte_length).await?
902+
let reader = cursor.reader().get_bytes(offset..offset+value_byte_length).await?.reader();
903+
EndianAwareReader::new(reader, cursor.endianness())
904+
// cursor.seek(offset);
905+
// cursor.read(value_byte_length).await?
898906
};
899907
// Case 2: there is one value.
900908
if count == 1 {

src/reader.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,9 @@ pub(crate) struct EndianAwareReader {
418418
}
419419

420420
impl EndianAwareReader {
421+
pub(crate) fn new(reader: Reader<Bytes>, endianness: Endianness) -> Self {
422+
Self { reader, endianness }
423+
}
421424
/// Read a u8 from the cursor, advancing the internal state by 1 byte.
422425
pub(crate) fn read_u8(&mut self) -> AsyncTiffResult<u8> {
423426
Ok(self.reader.read_u8()?)

0 commit comments

Comments
 (0)