diff --git a/REQUIRE b/REQUIRE index a1c420a..be4012e 100644 --- a/REQUIRE +++ b/REQUIRE @@ -11,3 +11,4 @@ FFTViews ComputationalResources DataStructures 0.4.6 TiledIteration +Compat 0.18 diff --git a/src/ImageFiltering.jl b/src/ImageFiltering.jl index 3c5ee63..aac8f34 100644 --- a/src/ImageFiltering.jl +++ b/src/ImageFiltering.jl @@ -4,13 +4,14 @@ module ImageFiltering using Colors, FixedPointNumbers, ImageCore, MappedArrays, FFTViews, OffsetArrays, StaticArrays, ComputationalResources, TiledIteration using ColorVectorSpace # for filtering RGB arrays +using Compat using Base: Indices, tail, fill_to_length, @pure, depwarn export Kernel, KernelFactors, Pad, Fill, Inner, NA, NoPad, Algorithm, imfilter, imfilter!, mapwindow, imgradients, padarray, centered, kernelfactors, reflect -typealias FixedColorant{T<:Normed} Colorant{T} -typealias StaticOffsetArray{T,N,A<:StaticArray} OffsetArray{T,N,A} -typealias OffsetVector{T} OffsetArray{T,1} +@compat FixedColorant{T<:Normed} = Colorant{T} +@compat StaticOffsetArray{T,N,A<:StaticArray} = OffsetArray{T,N,A} +@compat OffsetVector{T} = OffsetArray{T,1} # Needed for type-stability function Base.transpose{T}(A::StaticOffsetArray{T,2}) @@ -21,7 +22,8 @@ end module Algorithm # deliberately don't export these, but it's expected that they # will be used as Algorithm.FFT(), etc. - abstract Alg + using Compat + @compat abstract type Alg end "Filter using the Fast Fourier Transform" immutable FFT <: Alg end "Filter using a direct algorithm" immutable FIR <: Alg end "Cache-efficient filtering using tiles" immutable FIRTiled{N} <: Alg @@ -40,27 +42,27 @@ include("utils.jl") include("kernelfactors.jl") using .KernelFactors: TriggsSdika, IIRFilter, ReshapedOneD, iterdims, kernelfactors -typealias ReshapedVector{T,N,Npre,V<:AbstractVector} ReshapedOneD{T,N,Npre,V} -typealias ArrayType{T} Union{AbstractArray{T}, ReshapedVector{T}} -typealias ReshapedIIR{T,N,Npre,V<:IIRFilter} ReshapedOneD{T,N,Npre,V} -typealias AnyIIR{T} Union{IIRFilter{T}, ReshapedIIR{T}} -typealias ArrayLike{T} Union{ArrayType{T}, AnyIIR{T}} +@compat ReshapedVector{T,N,Npre,V<:AbstractVector} = ReshapedOneD{T,N,Npre,V} +@compat ArrayType{T} = Union{AbstractArray{T}, ReshapedVector{T}} +@compat ReshapedIIR{T,N,Npre,V<:IIRFilter} = ReshapedOneD{T,N,Npre,V} +@compat AnyIIR{T} = Union{IIRFilter{T}, ReshapedIIR{T}} +@compat ArrayLike{T} = Union{ArrayType{T}, AnyIIR{T}} include("kernel.jl") using .Kernel using .Kernel: Laplacian, reflect -typealias NDimKernel{N,K} Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}} +@compat NDimKernel{N,K} = Union{AbstractArray{K,N},ReshapedOneD{K,N},Laplacian{N}} include("border.jl") -typealias BorderSpec{T} Union{Pad{0}, Fill{T,0}, Inner{0}} -typealias BorderSpecNoNa{T} Union{Pad{0}, Fill{T,0}, Inner{0}} -typealias BorderSpecAny Union{BorderSpec,NA,NoPad} +@compat BorderSpec{T} = Union{Pad{0}, Fill{T,0}, Inner{0}} +@compat BorderSpecNoNa{T} = Union{Pad{0}, Fill{T,0}, Inner{0}} +const BorderSpecAny = Union{BorderSpec,NA,NoPad} include("deprecated.jl") -typealias ProcessedKernel Tuple +const ProcessedKernel = Tuple include("imfilter.jl") include("specialty.jl") diff --git a/src/border.jl b/src/border.jl index dc7b03e..3304cdf 100644 --- a/src/border.jl +++ b/src/border.jl @@ -2,7 +2,7 @@ using OffsetArrays, CatIndices -abstract AbstractBorder +@compat abstract type AbstractBorder end immutable NoPad{T} <: AbstractBorder border::T @@ -249,8 +249,8 @@ immutable Fill{T,N} <: AbstractBorder lo::Dims{N} hi::Dims{N} - Fill(value::T) = new(value) - Fill(value::T, lo::Dims{N}, hi::Dims{N}) = new(value, lo, hi) + (::Type{Fill{T,N}}){T,N}(value::T) = new{T,N}(value) + (::Type{Fill{T,N}}){T,N}(value::T, lo::Dims{N}, hi::Dims{N}) = new{T,N}(value, lo, hi) end Fill{T}(value::T) = Fill{T,0}(value) diff --git a/src/imfilter.jl b/src/imfilter.jl index 5d0d6f3..43dd6fe 100644 --- a/src/imfilter.jl +++ b/src/imfilter.jl @@ -1091,7 +1091,7 @@ function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N,Resha normalize_separable!(r, A, map(_vec, kernels), border) end -function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N}, border) +function normalize_separable!{N}(r::AbstractResource, A, kernels::NTuple{N,Any}, border) inds = indices(A) filtdims = ntuple(d->imfilter(r, similar(dims->ones(dims), inds[d]), _vec(kernels[d]), border), Val{N}) normalize_dims!(A, filtdims) diff --git a/src/kernel.jl b/src/kernel.jl index 29779af..0452102 100644 --- a/src/kernel.jl +++ b/src/kernel.jl @@ -192,7 +192,7 @@ immutable Laplacian{N} flags::NTuple{N,Bool} offsets::Vector{CartesianIndex{N}} - function Laplacian(flags::NTuple{N,Bool}) + function (::Type{Laplacian{N}}){N}(flags::NTuple{N,Bool}) offsets = Array{CartesianIndex{N}}(0) for i = 1:N if flags[i] @@ -200,7 +200,7 @@ immutable Laplacian{N} CartesianIndex{N}((ntuple(d->0, i-1)..., 1, ntuple(d->0, N-i)...))) end end - new(flags, offsets) + new{N}(flags, offsets) end end diff --git a/src/kernelfactors.jl b/src/kernelfactors.jl index 7eb67f8..b3453c0 100644 --- a/src/kernelfactors.jl +++ b/src/kernelfactors.jl @@ -5,7 +5,9 @@ using ..ImageFiltering: centered, dummyind import ..ImageFiltering: _reshape, _vec, nextendeddims using Base: tail, Indices, @pure, checkbounds_indices, throw_boundserror -abstract IIRFilter{T} +using Compat + +@compat abstract type IIRFilter{T} end Base.eltype{T}(kernel::IIRFilter{T}) = T @@ -26,9 +28,9 @@ ReshapedOneDs allow one to specify a "filtering dimension" for a immutable ReshapedOneD{T,N,Npre,V} # not <: AbstractArray{T,N} (more general, incl. IIR) data::V - function ReshapedOneD(data::V) + function (::Type{ReshapedOneD{T,N,Npre,V}}){T,N,Npre,V}(data::V) ndims(V) == 1 || throw(DimensionMismatch("must be one dimensional, got $(ndims(V))")) - new(data) + new{T,N,Npre,V}(data) end end @@ -73,17 +75,29 @@ import Base: == ==(A::ReshapedOneD, B::AbstractArray) = convert(AbstractArray, A) == B ==(A::AbstractArray, B::ReshapedOneD) = A == convert(AbstractArray, B) -import Base: .+, .-, .*, ./ -for op in (:+, :-, :*, :/) - opdot = Symbol(:., op) - @eval begin - ($opdot)(A::ReshapedOneD, B::ReshapedOneD) = broadcast($op, A, B) - @inline ($opdot)(As::ReshapedOneD...) = broadcast($op, As...) +if VERSION < v"0.6.0-dev.1839" + import Base: .+, .-, .*, ./ + for op in (:+, :-, :*, :/) + opdot = Symbol(:., op) + @eval begin + ($opdot)(A::ReshapedOneD, B::ReshapedOneD) = broadcast($op, A, B) + @inline ($opdot)(As::ReshapedOneD...) = broadcast($op, As...) + end + end + # for broadcasting + @pure Base.promote_eltype_op{S,T}(op, ::ReshapedOneD{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T) + @pure Base.promote_eltype_op{S,T}(op, ::Type{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T) +else + import Base: +, -, *, / + for op in (:+, :-, :*, :/) + @eval begin + ($op)(A::ReshapedOneD, B::ReshapedOneD) = + broadcast($op, convert(AbstractArray, A), convert(AbstractArray, B)) + @inline ($op)(As::ReshapedOneD...) = + broadcast($op, map(A->convert(AbstractArray, A), As)...) + end end end -# for broadcasting -@pure Base.promote_eltype_op{S,T}(op, ::ReshapedOneD{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T) -@pure Base.promote_eltype_op{S,T}(op, ::Type{S}, ::ReshapedOneD{T}) = Base.promote_op(op, S, T) if VERSION >= v"0.6.0-dev.693" Base.Broadcast.containertype{T<:ReshapedOneD}(::Type{T}) = Array end @@ -298,7 +312,8 @@ immutable TriggsSdika{T,k,l,L} <: IIRFilter{T} asum::T bsum::T - TriggsSdika(a, b, scale, M) = new(a, b, scale, M, sum(a), sum(b)) + (::Type{TriggsSdika{T,k,l,L}}){T,k,l,L}(a, b, scale, M) = + new{T,k,l,L}(a, b, scale, M, sum(a), sum(b)) end """ TriggsSdika(a, b, scale, M) @@ -414,7 +429,7 @@ appropriately along its "leading" dimensions; the dimensionality of each is """ kernelfactors{N}(factors::NTuple{N,AbstractVector}) = _kernelfactors((), ntuple(d->true,Val{N}), (), factors) -_kernelfactors(pre, post, out::NTuple, ::Tuple{}) = out +_kernelfactors(pre, post, out, ::Tuple{}) = out @inline function _kernelfactors(pre, post, out, factors) # L+M=N f = factors[1] diff --git a/src/utils.jl b/src/utils.jl index 5353c36..ec5a900 100644 --- a/src/utils.jl +++ b/src/utils.jl @@ -57,7 +57,7 @@ to_ranges(R::CartesianRange) = map((b,e)->b:e, R.start.I, R.stop.I) # ensure that overflow is detected, by ensuring that it doesn't happen # at intermediate stages of the computation accumfilter(pixelval, filterval) = pixelval * filterval -typealias SmallInts Union{UInt8,Int8,UInt16,Int16} +const SmallInts = Union{UInt8,Int8,UInt16,Int16} accumfilter(pixelval::SmallInts, filterval::SmallInts) = Int(pixelval)*Int(filterval) # advice: don't use FixedPoint for the kernel accumfilter(pixelval::N0f8, filterval::N0f8) = Float32(pixelval)*Float32(filterval) diff --git a/test/runtests.jl b/test/runtests.jl index a3e28eb..9edcf4b 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,9 +1,11 @@ using ImageFiltering, Base.Test -aif = detect_ambiguities(ImageFiltering, Kernel, KernelFactors, Base) -# Because StaticArrays has ambiguities with Base, we have to "subtract" these -asa = detect_ambiguities(StaticArrays, Base) -@test isempty(setdiff(aif, asa)) +if VERSION < v"0.6.0-dev" + aif = detect_ambiguities(ImageFiltering, Kernel, KernelFactors, Base) + # Because StaticArrays has ambiguities with Base, we have to "subtract" these + asa = detect_ambiguities(StaticArrays, Base) + @test isempty(setdiff(aif, asa)) +end include("border.jl") include("nd.jl") diff --git a/test/triggs.jl b/test/triggs.jl index f27b0e6..ef8e3e3 100644 --- a/test/triggs.jl +++ b/test/triggs.jl @@ -20,7 +20,7 @@ using Base.Test for (i,c) in enumerate(x) a = zeros(l) a[c] = 1 - af = exp(-((1:l)-c).^2/(2*σ^2))/(σ*√(2π)) + af = exp.(-((1:l)-c).^2/(2*σ^2))/(σ*√(2π)) @inferred(imfilter!(a, a, (kernel,), Fill(0))) @test norm(a-af) < 0.1*norm(af) # aσ[i][:,2n-1:2n] = [af a] @@ -46,11 +46,11 @@ using Base.Test y = (-3:3)' kernel = KernelFactors.IIRGaussian((σ, σ)) for img in (imgf, imgg, imgc) - imgcmp = img[3,4]*exp(-(x.^2 .+ y.^2)/(2*σ^2))/(σ^2*2*pi) + imgcmp = img[3,4]*exp.(-(x.^2 .+ y.^2)/(2*σ^2))/(σ^2*2*pi) border = Fill(zero(eltype(img))) img0 = copy(img) imgfilt = @inferred(imfilter(img, kernel, border)) - @test sumabs2(imgcmp - imgfilt) < 0.2^2*sumabs2(imgcmp) + @test sum(abs2, imgcmp - imgfilt) < 0.2^2*sum(abs2, imgcmp) @test img == img0 @test imgfilt != img end