Skip to content

Commit 9f6b00c

Browse files
committed
restrict: support 0-parameter colorant
1 parent 21d29ce commit 9f6b00c

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
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: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,5 +63,35 @@
6363
img = rand(RGBA{N0f8}, 11, 11)
6464
@test eltype(@inferred(restrict(img))) == RGBA{Float32}
6565
@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
6696
end
6797
end

0 commit comments

Comments
 (0)