Skip to content

Commit

Permalink
fix lazy-loading for tiled images with multiple slices (#179)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstphrbrns authored Dec 16, 2024
1 parent 301bea1 commit 8cd9370
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

2 comments on commit 8cd9370

@tlnagy
Copy link
Owner

@tlnagy tlnagy commented on 8cd9370 Dec 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/121502

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.11.2 -m "<description of version>" 8cd9370fb5b2e53854c6aaeb6b7c44f79b470709
git push origin v0.11.2

Please # to comment.