Skip to content

Commit

Permalink
use deplane_slow for unaligned arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
chrstphrbrns committed Jan 12, 2024
1 parent ea0995b commit 4e3bb5d
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
33 changes: 24 additions & 9 deletions src/ifds.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,10 +320,7 @@ function Base.read!(target::AbstractArray{T, N}, tf::TiffFile{O, S}, ifd::IFD{O}

if isplanar(ifd)
samplesv = vec(samples)
temp = deplane(samplesv, spp)
GC.@preserve samplesv temp target begin
memcpy(pointer(samplesv), pointer(temp), sizeof(target))
end
deplane!(samplesv, spp)
end
end

Expand Down Expand Up @@ -428,19 +425,37 @@ function reverse_prediction!(ifd::IFD, arr::AbstractArray{T,N}) where {T, N}
end
end
vw = view(reinterpret(UInt8, arr), start+1:start+columns)
deplane_simd!(buffer, vw, Val(sizeof(T)))
vw .= buffer
deplane!(buffer, vw, sizeof(T))
end

arr .= bswap.(arr)
end
end
end

function deplane(arr::AbstractVector{T}, n::Integer) where T
# {AAA...BBB...CCC...} => {ABCABCABC...}
function deplane!(arr::AbstractVector{T}, n::Integer) where T
out = Vector{T}(undef, length(arr))
deplane_simd!(out, arr, Val(n))
out
deplane!(out, arr, n)
end

# {AAA...BBB...CCC...} => {ABCABCABC...}
function deplane!(buffer::AbstractVector{T}, arr::AbstractVector{T}, n::Integer) where T
@assert length(buffer) == length(arr)
@assert length(arr) % n == 0

GC.@preserve arr buffer begin
if Int(pointer(arr)) & 0x3f > 0 || length(arr) < 64
# small or not 64-byte aligned
temp = deplane_slow(arr, n)
GC.@preserve temp begin
memcpy(pointer(arr), pointer(temp), sizeof(temp))
end
else
deplane_simd!(buffer, arr, Val(n))
memcpy(pointer(arr), pointer(buffer), sizeof(buffer))
end
end
end

# {AAA...BBB...CCC...} => {ABCABCABC...}
Expand Down
8 changes: 5 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ end

for typ in [Int8,UInt16,Float32]
for planes in 1:33
for size in 100:164
for size in 64:164
out = Vector{typ}(undef, size * planes)
a=reduce(vcat,[fill(typ(x),size) for x in 1:planes])
TiffImages.deplane_simd!(out, a, Val(planes))
Expand All @@ -245,9 +245,11 @@ end

for typ in [Int8,UInt16,Float32]
for planes in 1:33
for size in 100:164
for size in 1:164
a=reduce(vcat,[fill(typ(x),size) for x in 1:planes])
@test TiffImages.deplane(a, planes) == TiffImages.deplane_slow(a, planes)
b=copy(a)
TiffImages.deplane!(a, planes)
@test a == TiffImages.deplane_slow(b, planes)
end
end
end
Expand Down

0 comments on commit 4e3bb5d

Please sign in to comment.