Skip to content

Commit

Permalink
fix lazy-loading for tiled images with multiple slices
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstphrbrns committed Dec 13, 2024
1 parent 301bea1 commit 180e42c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 8 additions & 3 deletions src/types/lazy.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ mutable struct LazyBufferedTIFF{T <: ColorOrTuple, O <: Unsigned, AA <: Abstract
"""
An internal cache to fill reading from disk
"""
working_cache::AA

"""
An internal cache to fill with fully transformed data
"""
cache::AA

"""
Expand All @@ -57,7 +62,7 @@ mutable struct LazyBufferedTIFF{T <: ColorOrTuple, O <: Unsigned, AA <: Abstract
readonly::Bool

function LazyBufferedTIFF(file::TiffFile{O}, ifds::Vector{IFD{O}}, dims, cache::AA, cache_index::Int, last_ifd_offset::O, readonly::Bool) where {O, AA <: AbstractArray}
new{eltype(cache), O, typeof(cache)}(file, ifds, dims, cache, cache_index, last_ifd_offset, readonly)
new{eltype(cache), O, typeof(cache)}(file, ifds, dims, cache, cache, cache_index, last_ifd_offset, readonly)
end
end

Expand Down Expand Up @@ -124,9 +129,9 @@ function Base.getindex(A::LazyBufferedTIFF{T, O, AA}, i1::Int, i2::Int, i::Int)
A.file.io = getstream(format"TIFF", open(path), path)
end

read!(A.cache, A.file, ifd)
read!(A.working_cache, A.file, ifd)

A.cache = transform(A.cache, ifd)
A.cache = transform(A.working_cache, ifd)

A.cache_index = i
end
Expand Down
10 changes: 8 additions & 2 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -222,8 +222,14 @@ end
@testset "Tiled" begin
uncompressed = get_example("shapes_uncompressed.tif")
compressed_tiled = get_example("shapes_lzw_tiled.tif")
@test TiffImages.load(uncompressed) == TiffImages.load(compressed_tiled)
@test TiffImages.load(uncompressed)[:] == TiffImages.load(compressed_tiled; lazyio=true)[:]
compressed_tiled_multi = get_example("shapes_tiled_multi.tif")
uncompressed_loaded = TiffImages.load(uncompressed)
compressed_tiled_multi_loaded = TiffImages.load(compressed_tiled_multi; lazyio=true)
@test uncompressed_loaded == TiffImages.load(compressed_tiled)
@test uncompressed_loaded[:] == TiffImages.load(compressed_tiled; lazyio=true)[:]
@test compressed_tiled_multi_loaded[:,:,1] == uncompressed_loaded
@test compressed_tiled_multi_loaded[:,:,2] == uncompressed_loaded
@test compressed_tiled_multi_loaded[:,:,3] == uncompressed_loaded
end

@testset "Planar" begin
Expand Down

0 comments on commit 180e42c

Please sign in to comment.