Skip to content

Commit

Permalink
handle multiple positions breaking unevenly across files (#105)
Browse files Browse the repository at this point in the history
fix #104
  • Loading branch information
tlnagy authored Jan 22, 2023
1 parent befd686 commit 4f68504
Show file tree
Hide file tree
Showing 5 changed files with 8,779 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "OMETIFF"
uuid = "2d0ec36b-e807-5756-994b-45af29551fcf"
authors = ["Tamas Nagy <tamas@tamasnagy.com>"]
version = "0.4.3"
version = "0.4.4"

[deps]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
Expand Down
15 changes: 15 additions & 0 deletions src/parsing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ function ifdindex!(ifd_index::OrderedDict{Int, NTuple{4, Int}},
#
# TODO: This assumes a dense numbering of the indices, is this always true?
prev_ifd = length(ifd_index) > 0 ? maximum(keys(ifd_index)) : 0
prev_filepath = ""
for tiffdata in tiffdatas
try # if this tiffdata specifies the corresponding IFD
ifd = parse(Int, tiffdata["IFD"]) + 1
Expand All @@ -73,8 +74,22 @@ function ifdindex!(ifd_index::OrderedDict{Int, NTuple{4, Int}},
# if this file isn't one we've observed before, increment the offset
if !in(filepath, keys(obs_filepaths))
obs_filepaths[filepath] = prev_ifd
# if this isn't the first file and we're switching files and we've
# found an even bigger prev_ifd in the earlier file then
# bump the offset, see /~https://github.com/tlnagy/OMETIFF.jl/issues/104
elseif prev_filepath != "" && prev_filepath != filepath && obs_filepaths[filepath] < prev_ifd
old_offset = obs_filepaths[filepath]
δ = prev_ifd - old_offset
# update the offset associated with this file to this new larger value
obs_filepaths[filepath] = prev_ifd

# shift all the previously detected IFDs by δ
shift!(ifd_files, δ, prev_ifd+1)
shift!(ifd_index, δ, prev_ifd+1)
end

prev_filepath = filepath

ifd += obs_filepaths[filepath]

ifd_files[ifd] = (make_uuid(uuid), filepath)
Expand Down
44 changes: 44 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,47 @@ function to_symbol(input::String)
Symbol(replace(fixed, r"^[\d]"=>s"_\g<0>"))
end

"""
shift!(d, δ, o)
Given an `OrderedDict` d, shifts all keys greater than or equal to `o` by `δ`.
Preserves original order.
```jldoctest
julia> d = OrderedDict(vcat(6 => 'F', 1:5 .=> 'A':'E'))
OrderedDict{Int64, Char} with 6 entries:
6 => 'F'
1 => 'A'
2 => 'B'
3 => 'C'
4 => 'D'
5 => 'E'
julia> shift!(d, 1, 3)
julia> d
OrderedDict{Int64, Char} with 6 entries:
7 => 'F'
1 => 'A'
2 => 'B'
4 => 'C'
5 => 'D'
6 => 'E'
```
"""
function shift!(d::OrderedDict{Int, S}, δ::Int, o::Int) where S
ks = Int[]
vs = S[]
shift = Bool[]

for k in keys(d)
push!(ks, k)
push!(vs, pop!(d, k))
push!(shift, k >= o) # if shifting is needed for this item
end

for (k,v,s) in zip(ks, vs, shift)
# if shift needed, update key
d[s ? k + δ : k] = v
end
end
Loading

2 comments on commit 4f68504

@tlnagy
Copy link
Owner Author

@tlnagy tlnagy commented on 4f68504 Jan 22, 2023

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/76149

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.4.4 -m "<description of version>" 4f685045442ce3ce2029d1044ba17ad272f77c5f
git push origin v0.4.4

Please sign in to comment.