Skip to content

Commit fbc621c

Browse files
authored
Merge pull request #30 from JuliaImages/teh/non1_inputs
Fix padding for inputs with non-1 indices
2 parents 2e3156d + 350236a commit fbc621c

File tree

3 files changed

+22
-5
lines changed

3 files changed

+22
-5
lines changed

src/border.jl

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -295,16 +295,17 @@ Generate an index-vector to be used for padding. `inds` specifies the image indi
295295
"""
296296
function padindex(border::Pad, lo::Integer, inds::AbstractUnitRange, hi::Integer)
297297
if border.style == :replicate
298-
return vcat(fill(first(inds), lo), PinIndices(inds), fill(last(inds), hi))
298+
indsnew = vcat(fill(first(inds), lo), inds, fill(last(inds), hi))
299+
OffsetArray(indsnew, first(inds)-lo:last(inds)+hi)
299300
elseif border.style == :circular
300301
return modrange(extend(lo, inds, hi), inds)
301302
elseif border.style == :symmetric
302-
I = [inds; reverse(inds)]
303-
r = modrange(extend(lo, inds, hi), 1:2*length(inds))
303+
I = OffsetArray([inds; reverse(inds)], (0:2*length(inds)-1)+first(inds))
304+
r = modrange(extend(lo, inds, hi), indices(I, 1))
304305
return I[r]
305306
elseif border.style == :reflect
306-
I = [inds; last(inds)-1:-1:first(inds)+1]
307-
return I[modrange(extend(lo, inds, hi), 1:2*length(inds)-2)]
307+
I = OffsetArray([inds; last(inds)-1:-1:first(inds)+1], (0:2*length(inds)-3)+first(inds))
308+
return I[modrange(extend(lo, inds, hi), indices(I, 1))]
308309
else
309310
error("border style $(border.style) unrecognized")
310311
end

test/2d.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ end
228228
b = @inferred(imfilter!(CPU1(Algorithm.FIR()), ret, a, (), NoPad()))
229229
@test b == a
230230
@test !(b === a)
231+
# OffsetArrays
232+
img = OffsetArray(rand(RGB{N0f8}, 80, 100), (-5, 3))
233+
imgf = imfilter(img, Kernel.gaussian((3,3)))
234+
@test indices(imgf) == indices(img)
231235
end
232236

233237
nothing

test/nd.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ using Base.Test
6969
img[1] = img[8] = 0
7070
out = imfilter!(CPU1(Algorithm.FFT()), similar(img, Float64), img, kern, NoPad())
7171
@test out imfilter(img, kern)
72+
73+
# Inputs that have non-1 indices
74+
img = OffsetArray(zeros(11), -5:5)
75+
img[0] = 1
76+
for border in ("replicate", "circular", "symmetric", "reflect",
77+
Fill(zero(eltype(img))), Inner(1), NA())
78+
imgf = imfilter(img, centered([0.25, 0.5, 0.25]), border)
79+
@test imgf[-1] == imgf[1] == 0.25
80+
@test imgf[0] == 0.5
81+
inds = indices(imgf,1)
82+
@test all(x->x==0, imgf[first(inds):-2]) && all(x->x==0, imgf[2:last(inds)])
83+
end
7284
end
7385

7486
@testset "2d widening" begin

0 commit comments

Comments
 (0)