Skip to content

Commit 01e99cd

Browse files
authored
Cut allocations in padindex (#277)
* Cut allocations in `padindex` * Bump to v0.7.11
1 parent e92b73d commit 01e99cd

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ImageFiltering"
22
uuid = "6a3955dd-da59-5b1f-98d4-e7296123deb5"
33
author = ["Tim Holy <[email protected]>", "Jan Weidner <[email protected]>"]
4-
version = "0.7.10"
4+
version = "0.7.11"
55

66
[deps]
77
CatIndices = "aafaddc9-749c-510e-ac4f-586e18779b91"

src/border.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,16 +563,26 @@ Generate an index-vector to be used for padding. `inds` specifies the image axes
563563
"""
564564
function padindex(border::Pad, lo::Int, inds::UnitRange{Int}, hi::Int)
565565
if border.style == :replicate
566-
indsnew = Int[fill(first(inds), lo); inds; fill(last(inds), hi)]
567-
OffsetArray(indsnew, first(inds)-lo:last(inds)+hi)
566+
indsnew = OffsetArray{Int}(undef, first(inds)-lo:last(inds)+hi)
567+
offview = OffsetArrays.no_offset_view(indsnew)
568+
offview[1:lo] .= first(inds)
569+
offview[lo .+ eachindex(inds)] .= inds
570+
offview[lo + length(inds) + 1:end] .= last(inds)
571+
return indsnew
568572
elseif border.style == :circular
569573
return modrange(extend(lo, inds, hi), inds)
570574
elseif border.style == :symmetric
571-
I = OffsetArray(Int[inds; reverse(inds)], (0:2*length(inds)-1) .+ first(inds))
575+
I = OffsetArray{Int}(undef, (0:2*length(inds)-1) .+ first(inds))
576+
offview = OffsetArrays.no_offset_view(I)
577+
offview[eachindex(inds)] .= inds
578+
offview[end:-1:length(inds) + 1] .= inds
572579
r = modrange(extend(lo, inds, hi), axes(I, 1))
573580
return I[r]
574581
elseif border.style == :reflect
575-
I = OffsetArray(Int[inds; last(inds)-1:-1:first(inds)+1], (0:2*length(inds)-3) .+ first(inds))
582+
I = OffsetArray{Int}(undef, (0:2*length(inds)-3) .+ first(inds))
583+
offview = OffsetArrays.no_offset_view(I)
584+
offview[eachindex(inds)] .= inds
585+
offview[length(inds) + 1:end] .= last(inds)-1:-1:first(inds)+1
576586
return I[modrange(extend(lo, inds, hi), axes(I, 1))]
577587
else
578588
error("border style $(border.style) unrecognized")

0 commit comments

Comments
 (0)