Skip to content

Commit 4f68504

Browse files
authored
handle multiple positions breaking unevenly across files (#105)
fix #104
1 parent befd686 commit 4f68504

File tree

5 files changed

+8779
-1
lines changed

5 files changed

+8779
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OMETIFF"
22
uuid = "2d0ec36b-e807-5756-994b-45af29551fcf"
33
authors = ["Tamas Nagy <[email protected]>"]
4-
version = "0.4.3"
4+
version = "0.4.4"
55

66
[deps]
77
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"

src/parsing.jl

+15
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ function ifdindex!(ifd_index::OrderedDict{Int, NTuple{4, Int}},
5656
#
5757
# TODO: This assumes a dense numbering of the indices, is this always true?
5858
prev_ifd = length(ifd_index) > 0 ? maximum(keys(ifd_index)) : 0
59+
prev_filepath = ""
5960
for tiffdata in tiffdatas
6061
try # if this tiffdata specifies the corresponding IFD
6162
ifd = parse(Int, tiffdata["IFD"]) + 1
@@ -73,8 +74,22 @@ function ifdindex!(ifd_index::OrderedDict{Int, NTuple{4, Int}},
7374
# if this file isn't one we've observed before, increment the offset
7475
if !in(filepath, keys(obs_filepaths))
7576
obs_filepaths[filepath] = prev_ifd
77+
# if this isn't the first file and we're switching files and we've
78+
# found an even bigger prev_ifd in the earlier file then
79+
# bump the offset, see https://github.com/tlnagy/OMETIFF.jl/issues/104
80+
elseif prev_filepath != "" && prev_filepath != filepath && obs_filepaths[filepath] < prev_ifd
81+
old_offset = obs_filepaths[filepath]
82+
δ = prev_ifd - old_offset
83+
# update the offset associated with this file to this new larger value
84+
obs_filepaths[filepath] = prev_ifd
85+
86+
# shift all the previously detected IFDs by δ
87+
shift!(ifd_files, δ, prev_ifd+1)
88+
shift!(ifd_index, δ, prev_ifd+1)
7689
end
7790

91+
prev_filepath = filepath
92+
7893
ifd += obs_filepaths[filepath]
7994

8095
ifd_files[ifd] = (make_uuid(uuid), filepath)

src/utils.jl

+44
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,47 @@ function to_symbol(input::String)
1010
Symbol(replace(fixed, r"^[\d]"=>s"_\g<0>"))
1111
end
1212

13+
"""
14+
shift!(d, δ, o)
15+
16+
Given an `OrderedDict` d, shifts all keys greater than or equal to `o` by `δ`.
17+
Preserves original order.
18+
19+
```jldoctest
20+
julia> d = OrderedDict(vcat(6 => 'F', 1:5 .=> 'A':'E'))
21+
OrderedDict{Int64, Char} with 6 entries:
22+
6 => 'F'
23+
1 => 'A'
24+
2 => 'B'
25+
3 => 'C'
26+
4 => 'D'
27+
5 => 'E'
28+
29+
julia> shift!(d, 1, 3)
30+
31+
julia> d
32+
OrderedDict{Int64, Char} with 6 entries:
33+
7 => 'F'
34+
1 => 'A'
35+
2 => 'B'
36+
4 => 'C'
37+
5 => 'D'
38+
6 => 'E'
39+
```
40+
"""
41+
function shift!(d::OrderedDict{Int, S}, δ::Int, o::Int) where S
42+
ks = Int[]
43+
vs = S[]
44+
shift = Bool[]
45+
46+
for k in keys(d)
47+
push!(ks, k)
48+
push!(vs, pop!(d, k))
49+
push!(shift, k >= o) # if shifting is needed for this item
50+
end
51+
52+
for (k,v,s) in zip(ks, vs, shift)
53+
# if shift needed, update key
54+
d[s ? k + δ : k] = v
55+
end
56+
end

0 commit comments

Comments
 (0)