From fa9cdfe918c67304579cab3a620cc50dc854f371 Mon Sep 17 00:00:00 2001 From: Johnny Chen Date: Sat, 22 May 2021 18:30:08 +0800 Subject: [PATCH] imresize: handle 0-parameter colorant types --- src/resizing.jl | 6 +++++- test/resizing.jl | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/src/resizing.jl b/src/resizing.jl index d8a17b8..928e73a 100644 --- a/src/resizing.jl +++ b/src/resizing.jl @@ -351,7 +351,11 @@ end # changes correspond to integer ratios. We mimic ratio arithmetic # without actually using Rational (which risks promoting to a # Rational type, too slow for image processing). -imresize_type(c::Colorant) = base_colorant_type(c){eltype(imresize_type(Gray(c)))} +function imresize_type(c::Colorant) + CT = base_colorant_type(c) + isconcretetype(CT) && return CT # special 0-parameter colorant types: ARGB32, etc + CT{eltype(imresize_type(Gray(c)))} +end imresize_type(c::Gray) = Gray{imresize_type(gray(c))} imresize_type(c::FixedPoint) = typeof(c) imresize_type(c) = typeof((c*1)/1) diff --git a/test/resizing.jl b/test/resizing.jl index e452470..f43c1cf 100644 --- a/test/resizing.jl +++ b/test/resizing.jl @@ -181,4 +181,26 @@ end @test OffsetArrays.no_offset_view(out) == imresize(img, (128, 128), method=Lanczos4OpenCV()) end end + + @testset "special RGB/Gray types (#97)" begin + ori = repeat(distinguishable_colors(10), inner=(1, 10)) + for T in ( + RGB, BGR, RGBX, XRGB, + ARGB, RGBA, + RGB24, ARGB32, + ) + img = T.(ori) + out = @inferred imresize(img, ratio=2) + @test eltype(out) <: T + ref = imresize(ori, ratio=2) + @test ref ≈ RGB.(out) + end + for T in (Gray, AGray, GrayA, Gray24) + img = T.(ori) + out = @inferred imresize(img, ratio=2) + @test eltype(out) <: T + ref = imresize(Gray.(ori), ratio=2) + @test ref ≈ Gray.(out) + end + end end