Skip to content

Commit eff14e9

Browse files
authored
Merge pull request #3 from JuliaImages/jc/0color
`restrict`: support 0-parameter colorant
2 parents f56ef38 + 9f6b00c commit eff14e9

File tree

2 files changed

+96
-46
lines changed

2 files changed

+96
-46
lines changed

src/restrict.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ end
9696
_restrict_eltype(::Type{T}) where T = typeof(one(T)/4 + one(T)/2)
9797
_restrict_eltype(::Type{C}) where C<:Color = __restrict_eltype(RGB{eltype(C)})
9898
_restrict_eltype(::Type{C}) where C<:Colorant = __restrict_eltype(ARGB{eltype(C)})
99-
__restrict_eltype(::Type{C}) where C = base_colorant_type(C){promote_type(eltype(C), Float32)}
99+
function __restrict_eltype(::Type{C}) where C
100+
BT = base_colorant_type(C)
101+
isconcretetype(BT) && return floattype(BT)
102+
BT{_restrict_eltype(eltype(C))}
103+
end
100104

101105
function restrict!(out::AbstractArray{T,N}, A::AbstractArray, dim) where {T,N}
102106
if dim > N

test/restrict.jl

Lines changed: 91 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,97 @@
1-
@testset "restrict" begin
2-
A = reshape([UInt16(i) for i = 1:60], 4, 5, 3)
3-
B = restrict(A, (1,2))
4-
Btarget = cat( [ 0.96875 4.625 5.96875;
5-
2.875 10.5 12.875;
6-
1.90625 5.875 6.90625],
7-
[ 8.46875 14.625 13.46875;
1+
@testset "restrict" begin
2+
@testset "numerical test" begin
3+
A = reshape([UInt16(i) for i = 1:60], 4, 5, 3)
4+
B = restrict(A, (1,2))
5+
Btarget = cat([0.96875 4.625 5.96875;
6+
2.875 10.5 12.875;
7+
1.90625 5.875 6.90625],
8+
[8.46875 14.625 13.46875;
89
17.875 30.5 27.875;
9-
9.40625 15.875 14.40625],
10-
[ 15.96875 24.625 20.96875;
10+
9.40625 15.875 14.40625],
11+
[15.96875 24.625 20.96875;
1112
32.875 50.5 42.875;
1213
16.90625 25.875 21.90625], dims=3)
13-
@test B Btarget
14-
Argb = reinterpretc(RGB, reinterpret(N0f16, permutedims(A, (3,1,2))))
15-
B = restrict(Argb)
16-
Bf = permutedims(reinterpretc(eltype(eltype(B)), B), (2,3,1))
17-
# isapprox no longer lies, so atol is now serious
18-
@test isapprox(Bf, Btarget/reinterpret(one(N0f16)), atol=1e-10)
19-
Argba = reinterpretc(RGBA{N0f16}, reinterpret(N0f16, A))
20-
B = restrict(Argba)
21-
@test isapprox(reinterpretc(eltype(eltype(B)), B), restrict(A, (2,3))/reinterpret(one(N0f16)), atol=1e-10)
22-
A = reshape(1:60, 5, 4, 3)
23-
B = restrict(A, (1,2,3))
24-
@test cat( [ 2.6015625 8.71875 6.1171875;
25-
4.09375 12.875 8.78125;
26-
3.5390625 10.59375 7.0546875],
27-
[ 10.1015625 23.71875 13.6171875;
14+
@test B Btarget
15+
16+
Argb = reinterpretc(RGB, reinterpret(N0f16, permutedims(A, (3,1,2))))
17+
B = restrict(Argb)
18+
Bf = permutedims(reinterpretc(eltype(eltype(B)), B), (2,3,1))
19+
# isapprox no longer lies, so atol is now serious
20+
@test isapprox(Bf, Btarget/reinterpret(one(N0f16)), atol=1e-10)
21+
22+
Argba = reinterpretc(RGBA{N0f16}, reinterpret(N0f16, A))
23+
B = restrict(Argba)
24+
@test isapprox(reinterpretc(eltype(eltype(B)), B), restrict(A, (2,3))/reinterpret(one(N0f16)), atol=1e-10)
25+
26+
A = reshape(1:60, 5, 4, 3)
27+
B = restrict(A, (1,2,3))
28+
@test cat([2.6015625 8.71875 6.1171875;
29+
4.09375 12.875 8.78125;
30+
3.5390625 10.59375 7.0546875],
31+
[10.1015625 23.71875 13.6171875;
2832
14.09375 32.875 18.78125;
2933
11.0390625 25.59375 14.5546875], dims=3) B
30-
# Issue #395
31-
img1 = colorview(RGB, fill(0.9, 3, 5, 5))
32-
img2 = colorview(RGB, fill(N0f8(0.9), 3, 5, 5))
33-
@test isapprox(channelview(restrict(img1)), channelview(restrict(img2)), rtol=0.01)
34-
# Non-1 indices
35-
Ao = OffsetArray(A, (-2,1,0))
36-
@test parent(@inferred(restrict(Ao, 1))) == restrict(A, 1)
37-
@test parent(@inferred(restrict(Ao, 2))) == restrict(A, 2)
38-
@test parent(@inferred(restrict(Ao, (1,2)))) == restrict(A, (1,2))
39-
# Arrays-of-arrays
40-
a = Vector{Int}[[3,3,3], [2,1,7],[-11,4,2]]
41-
@test restrict(a) == Vector{Float64}[[2,3.5/2,6.5/2], [-5,4.5/2,5.5/2]]
42-
# Images issue #652
43-
img = testimage("cameraman")
44-
@test eltype(@inferred(restrict(img))) == Gray{Float32}
45-
img = testimage("mandrill")
46-
@test eltype(@inferred(restrict(img))) == RGB{Float32}
47-
@test eltype(@inferred(restrict(Lab.(img)))) == RGB{Float32}
48-
img = rand(RGBA{N0f8}, 11, 11)
49-
@test eltype(@inferred(restrict(img))) == RGBA{Float32}
50-
@test eltype(@inferred(restrict(LabA.(img)))) == ARGB{Float32}
34+
end
35+
36+
@testset "OffsetArray" begin
37+
A = rand(5, 4, 3)
38+
Ao = OffsetArray(A, (-2,1,0))
39+
@test parent(@inferred(restrict(Ao, 1))) == restrict(A, 1)
40+
@test parent(@inferred(restrict(Ao, 2))) == restrict(A, 2)
41+
@test parent(@inferred(restrict(Ao, (1,2)))) == restrict(A, (1,2))
42+
end
43+
44+
@testset "FixedPoint overflow" begin
45+
# issue https://github.com/JuliaImages/Images.jl/issues/395
46+
img1 = colorview(RGB, fill(0.9, 3, 5, 5))
47+
img2 = colorview(RGB, fill(N0f8(0.9), 3, 5, 5))
48+
@test isapprox(channelview(restrict(img1)), channelview(restrict(img2)), rtol=0.01)
49+
end
50+
51+
@testset "Array of arrays" begin
52+
a = Vector{Int}[[3,3,3], [2,1,7],[-11,4,2]]
53+
@test restrict(a) == Vector{Float64}[[2,3.5/2,6.5/2], [-5,4.5/2,5.5/2]]
54+
end
55+
56+
@testset "various colorant" begin
57+
# issue https://github.com/JuliaImages/Images.jl/issues/652
58+
img = testimage("cameraman")
59+
@test eltype(@inferred(restrict(img))) == Gray{Float32}
60+
img = testimage("mandrill")
61+
@test eltype(@inferred(restrict(img))) == RGB{Float32}
62+
@test eltype(@inferred(restrict(Lab.(img)))) == RGB{Float32}
63+
img = rand(RGBA{N0f8}, 11, 11)
64+
@test eltype(@inferred(restrict(img))) == RGBA{Float32}
65+
@test eltype(@inferred(restrict(LabA.(img)))) == ARGB{Float32}
66+
67+
ori = repeat(distinguishable_colors(10), inner=(1, 10))
68+
for T in (
69+
RGB, BGR, RGBX, XRGB,
70+
ARGB, RGBA,
71+
RGB24, ARGB32,
72+
)
73+
img = T.(ori)
74+
out = @inferred restrict(img)
75+
if T == RGB24
76+
@test eltype(out) == RGB{Float32}
77+
elseif T == ARGB32
78+
@test eltype(out) == ARGB{Float32}
79+
else
80+
@test eltype(out) <: T
81+
end
82+
ref = restrict(ori)
83+
@test ref RGB.(out)
84+
end
85+
for T in (Gray, AGray, GrayA, Gray24)
86+
img = T.(ori)
87+
out = @inferred restrict(img)
88+
if T == Gray24
89+
@test eltype(out) == Gray{Float32}
90+
else
91+
@test eltype(out) <: T
92+
end
93+
ref = restrict(Gray.(ori))
94+
@test ref Gray.(out)
95+
end
96+
end
5197
end

0 commit comments

Comments
 (0)