Skip to content

Commit ab264e2

Browse files
authored
Don't use FFT with non-finite input (#205)
1 parent 16a1e5c commit ab264e2

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/imfilter.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ end
15351535
# allocated by the time this gets called.
15361536
function filter_algorithm(out, img, kernel::Union{ArrayType,Tuple{Vararg{ArrayType}}})
15371537
L = maxlen(kernel)
1538-
if L > 30 && eltype(img) <: Union{Number,Colorant} && !any(isnan,img)
1538+
if L > 30 && eltype(img) <: Union{Number,Colorant} && all(isfinite, img)
15391539
return FFT()
15401540
end
15411541
sz = map(length, calculate_padding(kernel))

test/nd.jl

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,9 +92,22 @@ Base.zero(::Type{WrappedFloat}) = WrappedFloat(0.0)
9292
@test all(x->x==0, imgf[first(inds):-2]) && all(x->x==0, imgf[2:last(inds)])
9393
end
9494

95-
# Input with NaN
96-
@test isnan.(imfilter([NaN;1:100],centered(ones(31)))) == ((0:100) .<= 15)
97-
95+
# Non-finite input
96+
for x in (NaN, Inf, -Inf)
97+
v = rand(100)
98+
i = rand(eachindex(v))
99+
w = [j == i ? x : v[j] for j in eachindex(v)]
100+
kern = centered(ones(31))
101+
# verify that non-finite values prevent use of FFT
102+
@test ImageFiltering.filter_algorithm(v, v, kern) == ImageFiltering.FFT()
103+
@test ImageFiltering.filter_algorithm(w, w, kern) == ImageFiltering.FIR()
104+
vf = imfilter(v, kern)
105+
wf = imfilter(w, kern)
106+
around_i = [abs(i-j) <= 15 for j in eachindex(v)]
107+
@test all(isequal(x), wf[around_i])
108+
@test wf[.!around_i] vf[.!around_i]
109+
end
110+
98111
# Issue #110
99112
img = reinterpret(WrappedFloat, rand(128))
100113
kern = centered(rand(31))

0 commit comments

Comments
 (0)