Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Fix lazy-loading for tiled images with multiple slices #179

Merged
merged 1 commit into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading