From 0eb499311b4fc3b823dea7b1f4fa999438b6caf8 Mon Sep 17 00:00:00 2001 From: JohnnyChen Date: Thu, 2 Dec 2021 02:15:47 +0800 Subject: [PATCH] unwrap rawview(::channelview) with one reshape reinterpret --- src/ImageCore.jl | 5 +++++ test/show.jl | 7 +++++-- test/views.jl | 13 +++++++++++++ 3 files changed, 23 insertions(+), 2 deletions(-) diff --git a/src/ImageCore.jl b/src/ImageCore.jl index f835c9c..a9caa4a 100644 --- a/src/ImageCore.jl +++ b/src/ImageCore.jl @@ -115,6 +115,11 @@ rawview(a::AbstractArray{T}) where {T<:FixedPoint} = mappedarray(reinterpret, y- rawview(a::Array{T}) where {T<:FixedPoint} = reinterpret(FixedPointNumbers.rawtype(T), a) rawview(a::AbstractArray{T}) where {T<:Real} = a +if VERSION >= v"1.6.0-DEV.1083" + # does not use the slow `mappedarray` fallback for, typically, `rawview(channelview(img))` + rawview(a::Base.ReinterpretArray{T}) where {T<:FixedPoint} = reinterpret(reshape, FixedPointNumbers.rawtype(T), parent(a)) +end + """ normedview([T], img::AbstractArray{Unsigned}) diff --git a/test/show.jl b/test/show.jl index c521b16..a997b7f 100644 --- a/test/show.jl +++ b/test/show.jl @@ -35,8 +35,11 @@ RGB_str = typestring(RGB) rgb8 = rand(RGB{N0f8}, 3, 5) c = rawview(channelview(rgb8)) str = summary(c) - @test occursin("3×3×5 rawview(reinterpret($(rrstr)", str) && occursin("N0f8", str) && - occursin("::Array{RGB{N0f8},$(rrdim(3))}", str) && occursin("with eltype UInt8", str) + if VERSION >= v"1.6.0-DEV.1083" + @test str == "3×3×5 reinterpret(reshape, UInt8, ::Array{RGB{N0f8},2}) with eltype UInt8" + else + @test str == "3×3×5 rawview(reinterpret(N0f8, ::Array{RGB{N0f8},3})) with eltype UInt8" + end @test summary(rgb8) == "3×5 Array{RGB{N0f8},2} with eltype $(RGB_str){$(N0f8_str)}" rand8 = rand(UInt8, 3, 5) d = normedview(PermutedDimsArray(rand8, (2,1))) diff --git a/test/views.jl b/test/views.jl index b488eb2..b3d4fd6 100644 --- a/test/views.jl +++ b/test/views.jl @@ -18,6 +18,19 @@ using PaddedViews: filltype v[2,2] = 0x0f @test a[2,2].i == 0x0f @test rawview(v) === v + + if VERSION >= v"1.6.0-DEV.1083" + @testset "rawview(::AbstractArray)" begin + a = rand(RGB{N0f8}, 5, 5) + X1 = rawview(channelview(a)) + X2 = rawview(collect(channelview(a))) + @test !isa(parent(X1), Base.ReinterpretArray) + @test typeof(X1) != typeof(X2) + @test axes(X1) == axes(X2) + @test eltype(X1) == eltype(X2) + @test X1 == X2 + end + end end @testset "normedview" begin