From 3559755b03ba768e19fd670c9c8145920093bc4e Mon Sep 17 00:00:00 2001 From: adrhill Date: Tue, 17 Sep 2024 14:44:29 +0200 Subject: [PATCH] Refactor color tests --- src/api/color.jl | 2 +- test/test_color.jl | 131 ++++++++++++++++++++++++++------------------- 2 files changed, 76 insertions(+), 57 deletions(-) diff --git a/src/api/color.jl b/src/api/color.jl index 8c1444c..501bafe 100644 --- a/src/api/color.jl +++ b/src/api/color.jl @@ -79,7 +79,7 @@ function _colordither( metric::DifferenceMetric=DEFAULT_METRIC, to_linear=false, kwargs..., -) where {T<:ColorLike} +) where {T<:GrayLike} return _colordither( eltype(cs), img, alg, cs; metric=metric, to_linear=to_linear, kwargs... ) diff --git a/test/test_color.jl b/test/test_color.jl index 767001f..78f12a7 100644 --- a/test/test_color.jl +++ b/test/test_color.jl @@ -1,5 +1,9 @@ using DitherPunk +using DitherPunk: DEFAULT_METHOD, AbstractDither using DitherPunk: ColorNotImplementedError +using Test + +using ColorSchemes using IndirectArrays using ImageBase using ReferenceTests @@ -20,39 +24,45 @@ img = testimage("fabio_color_256") img_gray = testimage("fabio_gray_256") # Run & test custom color palette dithering methods -algs = Dict( +const COLOR_ALGS = Dict( "FloydSteinberg" => @inferred(FloydSteinberg()), "ClosestColor" => @inferred(ClosestColor()), "Bayer" => @inferred(Bayer()), ) -for (name, alg) in algs - # Test custom color dithering on color images - local img2 = copy(img) - local d = @inferred dither(img2, alg, cs) - @test_reference "references/color/$(name).txt" d +@testset verbose = true "Binary dithering methods" begin + @testset "$(name)" for (name, alg) in COLOR_ALGS + # Test custom color dithering on color images + local img2 = copy(img) + local d = @inferred dither(img2, alg, cs) + @test_reference "references/color/$(name).txt" d - @test eltype(d) == eltype(img2) - @test img2 == img # image not modified + @test eltype(d) == eltype(img2) + @test img2 == img # image not modified - # Test custom color dithering on gray images - local img2_gray = copy(img_gray) - local d = @inferred dither(img2_gray, alg, cs) - @test_reference "references/color/$(name)_from_gray.txt" d + # Test custom color dithering on gray images + local img2_gray = copy(img_gray) + local d = @inferred dither(img2_gray, alg, cs) + @test_reference "references/color/$(name)_from_gray.txt" d - @test eltype(d) == eltype(cs) - @test img2_gray == img_gray # image not modified + @test eltype(d) == eltype(cs) + @test img2_gray == img_gray # image not modified + end end # Test error diffusion kwarg `clamp_error`: -d = @inferred dither(img, FloydSteinberg(), cs; clamp_error=false) -@test_reference "references/color/FloydSteinberg_clamp_error.txt" d -@test eltype(d) == eltype(img) +@testset "clamp_error" begin + d = @inferred dither(img, FloydSteinberg(), cs; clamp_error=false) + @test_reference "references/color/FloydSteinberg_clamp_error.txt" d + @test eltype(d) == eltype(img) +end ## Test API -# Test for argument errors on algorithms that don't support custom color palettes -for alg in [WhiteNoiseThreshold(), ConstantThreshold()] - @test_throws ColorNotImplementedError dither(img, alg, cs) +@testset "ColorNotImplementedError" begin + # Test for argument errors on algorithms that don't support custom color palettes + @testset "$(alg)" for alg in (WhiteNoiseThreshold(), ConstantThreshold()) + @test_throws ColorNotImplementedError dither(img, alg, cs) + end end img2 = copy(img) @@ -60,48 +70,57 @@ alg = FloydSteinberg() d = dither(img2, alg, cs) # Test setting output type -d2 = @inferred dither(HSV, img2, alg, cs) -@test d2 isa IndirectArray -@test eltype(d2) <: HSV -@test RGB{N0f8}.(d2) ≈ d -@test img2 == img # image not modified -d2default = @inferred dither(HSV, img2, cs) -@test d2 == d2default +@testset "Custom output type" begin + d2 = @inferred dither(HSV, img2, alg, cs) + @test d2 isa IndirectArray + @test eltype(d2) <: HSV + @test RGB{N0f8}.(d2) ≈ d + @test img2 == img # image not modified + d2default = @inferred dither(HSV, img2, cs) + @test d2 == d2default +end # Inplace modify output image -out = zeros(RGB{Float16}, size(img2)...) -d3 = @inferred dither!(out, img2, alg, cs) -@test out ≈ d # image updated in-place -@test d3 ≈ d -@test eltype(out) == RGB{Float16} -@test eltype(d3) == RGB{Float16} -@test img2 == img # image not modified -out = zeros(RGB{Float16}, size(img2)...) -d3default = @inferred dither!(out, img2, cs) -@test d2 == d2default +@testset "Inplace modify 3-arg" begin + out = zeros(RGB{Float16}, size(img2)...) + d3 = @inferred dither!(out, img2, alg, cs) + @test out ≈ d # image updated in-place + @test d3 ≈ d + @test eltype(out) == RGB{Float16} + @test eltype(d3) == RGB{Float16} + @test img2 == img # image not modified + out = zeros(RGB{Float16}, size(img2)...) + d3default = @inferred dither!(out, img2, cs) + @test d2 == d2default +end # Inplace modify image -d4 = @inferred dither!(img2, alg, cs) -@test d4 == d -@test img2 == d # image updated in-place -@test eltype(d4) == eltype(img) -@test eltype(img2) == eltype(img) -img2default = deepcopy(img) -d4default = @inferred dither!(img2default, cs) -@test img2default == d -@test d4 == d4default +@testset "Inplace modify 2-arg" begin + d4 = @inferred dither!(img2, alg, cs) + @test d4 == d + @test img2 == d # image updated in-place + @test eltype(d4) == eltype(img) + @test eltype(img2) == eltype(img) + img2default = deepcopy(img) + d4default = @inferred dither!(img2default, cs) + @test img2default == d + @test d4 == d4default +end ## Conditional dependencies # Test conditional dependency on ColorSchemes.jl -using ColorSchemes -d1 = @inferred dither(img, alg, ColorSchemes.jet) -d2 = @inferred dither(img, alg, ColorSchemes.jet.colors) -@test d1 == d2 -d3 = @inferred dither(img, ColorSchemes.jet) -d4 = @inferred dither(img, ColorSchemes.jet.colors) -@test d3 == d1 -@test d3 == d4 +@testset "Colorschemes.jl" begin + d1 = @inferred dither(img, alg, ColorSchemes.jet) + d2 = @inferred dither(img, alg, ColorSchemes.jet.colors) + @test d1 == d2 + d3 = @inferred dither(img, ColorSchemes.jet) + d4 = @inferred dither(img, ColorSchemes.jet.colors) + @test d3 == d1 + @test d3 == d4 +end # calls Clustering -d = @inferred dither(img, alg, 4) -d = @inferred dither(img, 4) +@testset "Automatic colorscheme" begin + @test_nowarn @inferred dither(img, DEFAULT_METHOD, 4) + @test_nowarn @inferred dither(img, 4) +end