22
33using OffsetArrays, CatIndices
44
5- @compat abstract type AbstractBorder end
5+ abstract type AbstractBorder end
66
7- immutable NoPad{T} <: AbstractBorder
7+ struct NoPad{T} <: AbstractBorder
88 border:: T
99end
1010NoPad () = NoPad (nothing )
@@ -47,13 +47,13 @@ rather than
4747
4848 imfilter(img, kernel, Pad(:replicate))
4949"""
50- immutable Pad{N} <: AbstractBorder
50+ struct Pad{N} <: AbstractBorder
5151 style:: Symbol
5252 lo:: Dims{N} # number to extend by on the lower edge for each dimension
5353 hi:: Dims{N} # number to extend by on the upper edge for each dimension
5454end
5555
56- ( :: Type{ Pad{N}} ){N} (style, lo:: AbstractVector , hi:: AbstractVector ) =
56+ Pad {N} (style, lo:: AbstractVector , hi:: AbstractVector ) where {N} =
5757 Pad {N} (style, (lo... ), (hi... ))
5858
5959const valid_borders = (" replicate" , " circular" , " reflect" , " symmetric" )
@@ -92,8 +92,8 @@ Pad the input image by `lo` pixels at the lower edge, and `hi` pixels at the upp
9292"""
9393Pad (lo:: Dims , hi:: Dims ) = Pad (:replicate , lo, hi)
9494Pad (style:: Symbol , lo:: Tuple{} , hi:: Tuple{} ) = Pad {0} (style, lo, hi)
95- Pad {N} (style:: Symbol , lo:: Dims{N} , hi:: Tuple{} ) = Pad (style, lo, ntuple (d-> 0 ,Val{N}))
96- Pad {N} (style:: Symbol , lo:: Tuple{} , hi:: Dims{N} ) = Pad (style, ntuple (d-> 0 ,Val{N}), hi)
95+ Pad (style:: Symbol , lo:: Dims{N} , hi:: Tuple{} ) where {N} = Pad (style, lo, ntuple (d-> 0 ,Val{N}))
96+ Pad (style:: Symbol , lo:: Tuple{} , hi:: Dims{N} ) where {N} = Pad (style, ntuple (d-> 0 ,Val{N}), hi)
9797Pad (style:: Symbol , lo:: AbstractVector{Int} , hi:: AbstractVector{Int} ) = Pad (style, (lo... ,), (hi... ,))
9898
9999Pad (style:: Symbol , inds:: Indices ) = Pad (style, map (lo,inds), map (hi,inds))
@@ -118,13 +118,13 @@ function padfft(indk::AbstractUnitRange, l::Integer)
118118 range (first (indk), nextprod ([2 ,3 ], l+ lk)- l+ 1 )
119119end
120120
121- function padindices {_,N} (img:: AbstractArray{_,N} , border:: Pad )
121+ function padindices (img:: AbstractArray{_,N} , border:: Pad ) where {_,N}
122122 throw (ArgumentError (" $border lacks the proper padding sizes for an array with $(ndims (img)) dimensions" ))
123123end
124- function padindices {_,N} (img:: AbstractArray{_,N} , border:: Pad{N} )
124+ function padindices (img:: AbstractArray{_,N} , border:: Pad{N} ) where {_,N}
125125 _padindices (border, border. lo, indices (img), border. hi)
126126end
127- function padindices {P<:Pad} (img:: AbstractArray , :: Type{P} )
127+ function padindices (img:: AbstractArray , :: Type{P} ) where P <: Pad
128128 throw (ArgumentError (" must supply padding sizes to $P " ))
129129end
130130
@@ -144,15 +144,15 @@ add. `border` can be a `Pad`, `Fill`, or `Inner` object.
144144Optionally provide the element type `T` of `imgpadded`.
145145"""
146146padarray (img:: AbstractArray , border:: Pad ) = padarray (eltype (img), img, border)
147- function padarray {T} (:: Type{T} , img:: AbstractArray , border:: Pad )
147+ function padarray (:: Type{T} , img:: AbstractArray , border:: Pad ) where T
148148 inds = padindices (img, border)
149149 # like img[inds...] except that we can control the element type
150150 newinds = map (Base. indices1, inds)
151151 dest = similar (img, T, newinds)
152152 copydata! (dest, img, inds)
153153end
154154
155- padarray {P} (img, :: Type{P} ) = img[padindices (img, P)... ] # just to throw the nice error
155+ padarray (img, :: Type{P} ) where {P} = img[padindices (img, P)... ] # just to throw the nice error
156156
157157function copydata! (dest, img, inds)
158158 isempty (inds) && return dest
@@ -175,7 +175,7 @@ function copydata!(dest::OffsetArray, img, inds::Tuple{Vararg{OffsetArray}})
175175 dest
176176end
177177
178- Base. ndims {N} (:: Pad{N} ) = N
178+ Base. ndims (:: Pad{N} ) where {N} = N
179179
180180# Make these separate types because the dispatch almost surely needs to be different
181181"""
@@ -188,7 +188,7 @@ Indicate that edges are to be discarded in filtering, only the interior of the r
188188
189189 imfilter(img, kernel, Inner())
190190"""
191- immutable Inner{N} <: AbstractBorder
191+ struct Inner{N} <: AbstractBorder
192192 lo:: Dims{N}
193193 hi:: Dims{N}
194194end
@@ -210,20 +210,20 @@ As a consequence, filtering has the same behavior as
210210`nanmean`. Indeed, invalid pixels in `img` can be marked as `NaN` and
211211then they are effectively omitted from the filtered result.
212212"""
213- immutable NA{N} <: AbstractBorder
213+ struct NA{N} <: AbstractBorder
214214 lo:: Dims{N}
215215 hi:: Dims{N}
216216end
217217
218218for T in (:Inner , :NA )
219219 @eval begin
220220 (:: Type{$T} )(both:: Int... ) = $ T (both, both)
221- (:: Type{$T} ){N} (both:: Dims{N} ) = $ T (both, both)
221+ (:: Type{$T} )(both:: Dims{N} ) where {N} = $ T (both, both)
222222 (:: Type{$T} )(lo:: Tuple{} , hi:: Tuple{} ) = $ T {0} (lo, hi)
223- (:: Type{$T} ){N} (lo:: Dims{N} , hi:: Tuple{} ) = $ T {N} (lo, ntuple (d-> 0 ,Val{N}))
224- (:: Type{$T} ){N} (lo:: Tuple{} , hi:: Dims{N} ) = $ T {N} (ntuple (d-> 0 ,Val{N}), hi)
225- (:: Type{$T} ){N} (inds:: Indices{N} ) = $ T {N} (map (lo,inds), map (hi,inds))
226- (:: Type{$T{N}} ){N} (lo:: AbstractVector , hi:: AbstractVector ) = $ T {N} ((lo... ,), (hi... ,))
223+ (:: Type{$T} )(lo:: Dims{N} , hi:: Tuple{} ) where {N} = $ T {N} (lo, ntuple (d-> 0 ,Val{N}))
224+ (:: Type{$T} )(lo:: Tuple{} , hi:: Dims{N} ) where {N} = $ T {N} (ntuple (d-> 0 ,Val{N}), hi)
225+ (:: Type{$T} )(inds:: Indices{N} ) where {N} = $ T {N} (map (lo,inds), map (hi,inds))
226+ (:: Type{$T{N}} )(lo:: AbstractVector , hi:: AbstractVector ) where {N} = $ T {N} ((lo... ,), (hi... ,))
227227 (:: Type{$T} )(lo:: AbstractVector , hi:: AbstractVector ) = $ T ((lo... ,), (hi... ,)) # not inferrable
228228
229229 (p:: $T{0} )(kernel, img, :: Alg ) = p (kernel)
@@ -232,8 +232,8 @@ for T in (:Inner, :NA)
232232end
233233
234234padarray (img, border:: Inner ) = padarray (eltype (img), img, border)
235- padarray {T} (:: Type{T} , img:: AbstractArray{T} , border:: Inner ) = copy (img)
236- padarray {T} (:: Type{T} , img:: AbstractArray , border:: Inner ) = copy! (similar (Array{T}, indices (img)), img)
235+ padarray (:: Type{T} , img:: AbstractArray{T} , border:: Inner ) where {T} = copy (img)
236+ padarray (:: Type{T} , img:: AbstractArray , border:: Inner ) where {T} = copy! (similar (Array{T}, indices (img)), img)
237237
238238"""
239239 Fill(val)
@@ -247,20 +247,20 @@ Optionally supply the extent of the padding, see `Pad`.
247247
248248 imfilter(img, kernel, Fill(zero(eltype(img))))
249249"""
250- immutable Fill{T,N} <: AbstractBorder
250+ struct Fill{T,N} <: AbstractBorder
251251 value:: T
252252 lo:: Dims{N}
253253 hi:: Dims{N}
254254
255- ( :: Type{ Fill{T,N}} ){T,N} (value:: T ) = new {T,N} (value)
256- ( :: Type{ Fill{T,N}} ){T,N} (value:: T , lo:: Dims{N} , hi:: Dims{N} ) = new {T,N} (value, lo, hi)
255+ Fill {T,N} (value:: T ) where {T,N} = new {T,N} (value)
256+ Fill {T,N} (value:: T , lo:: Dims{N} , hi:: Dims{N} ) where {T,N} = new {T,N} (value, lo, hi)
257257end
258258
259- Fill {T} (value:: T ) = Fill {T,0} (value)
260- Fill {T,N} (value:: T , lo:: Dims{N} , hi:: Dims{N} ) = Fill {T,N} (value, lo, hi)
261- Fill {T,N} (value:: T , both:: Dims{N} ) = Fill {T,N} (value, both, both)
259+ Fill (value:: T ) where {T} = Fill {T,0} (value)
260+ Fill (value:: T , lo:: Dims{N} , hi:: Dims{N} ) where {T,N} = Fill {T,N} (value, lo, hi)
261+ Fill (value:: T , both:: Dims{N} ) where {T,N} = Fill {T,N} (value, both, both)
262262Fill (value, lo:: AbstractVector , hi:: AbstractVector ) = Fill (value, (lo... ,), (hi... ,))
263- Fill {T,N} (value:: T , inds:: Base.Indices{N} ) = Fill {T,N} (value, map (lo,inds), map (hi,inds))
263+ Fill (value:: T , inds:: Base.Indices{N} ) where {T,N} = Fill {T,N} (value, map (lo,inds), map (hi,inds))
264264Fill (value, kernel) = Fill (value, calculate_padding (kernel))
265265
266266(p:: Fill )(kernel) = Fill (p. value, kernel)
@@ -271,10 +271,10 @@ function (p::Fill)(kernel, img, ::FFT)
271271 Fill (p. value, newinds)
272272end
273273
274- function padarray {T} (:: Type{T} , img:: AbstractArray , border:: Fill )
274+ function padarray (:: Type{T} , img:: AbstractArray , border:: Fill ) where T
275275 throw (ArgumentError (" $border lacks the proper padding sizes for an array with $(ndims (img)) dimensions" ))
276276end
277- function padarray {T,S,_,N} (:: Type{T} , img:: AbstractArray{S,N} , f:: Fill{_,N} )
277+ function padarray (:: Type{T} , img:: AbstractArray{S,N} , f:: Fill{_,N} ) where {T,S,_,N}
278278 A = similar (arraytype (img, T), map ((l,r,h)-> first (r)- l: last (r)+ h, f. lo, indices (img), f. hi))
279279 try
280280 fill! (A, f. value)
@@ -365,12 +365,12 @@ accumulate_padding(inds::Indices) = inds
365365modrange (x, r:: AbstractUnitRange ) = mod (x- first (r), length (r))+ first (r)
366366modrange (A:: AbstractArray , r:: AbstractUnitRange ) = map (x-> modrange (x, r), A)
367367
368- arraytype {T} (A:: AbstractArray , :: Type{T} ) = Array{T} # fallback
368+ arraytype (A:: AbstractArray , :: Type{T} ) where {T} = Array{T} # fallback
369369arraytype (A:: BitArray , :: Type{Bool} ) = BitArray
370370
371371interior (A, kernel) = _interior (indices (A), indices (kernel))
372372interior (A, factkernel:: Tuple ) = _interior (indices (A), accumulate_padding (indices (factkernel[1 ]), tail (factkernel)... ))
373- function _interior {N} (indsA:: NTuple{N} , indsk)
373+ function _interior (indsA:: NTuple{N} , indsk) where N
374374 indskN = fill_to_length (indsk, 0 : 0 , Val{N})
375375 map (intersect, indsA, shrink (indsA, indsk))
376376end
@@ -404,8 +404,8 @@ shrink(inds::Indices, pad::Indices) = firsttype(map_copytail(shrink, inds, pad))
404404shrink (ind:: AbstractUnitRange , pad:: AbstractUnitRange ) = oftype (ind, first (ind)- first (pad): last (ind)- last (pad))
405405shrink (ind:: Base.OneTo , pad:: AbstractUnitRange ) = shrink (UnitRange (ind), pad)
406406
407- allocate_output {T} (:: Type{T} , img, kernel, border) = similar (img, T)
408- function allocate_output {T} (:: Type{T} , img, kernel, :: Inner{0} )
407+ allocate_output (:: Type{T} , img, kernel, border) where {T} = similar (img, T)
408+ function allocate_output (:: Type{T} , img, kernel, :: Inner{0} ) where T
409409 inds = interior (img, kernel)
410410 similar (img, T, inds)
411411end
0 commit comments