diff --git a/reference/Project.toml b/reference/Project.toml index af7ebde..ddf33a1 100644 --- a/reference/Project.toml +++ b/reference/Project.toml @@ -1,5 +1,6 @@ [deps] CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" +Colors = "5ae59095-9a9b-59fe-a467-6f913c188581" FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549" LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f" MathTeXEngine = "0a4f8689-d25c-4efe-a92b-7142dfc1aa53" diff --git a/reference/compare.jl b/reference/compare.jl index 9a6ce92..cebdecb 100644 --- a/reference/compare.jl +++ b/reference/compare.jl @@ -1,4 +1,5 @@ using CairoMakie +using Colors using FileIO using MathTeXEngine using Tar @@ -7,7 +8,7 @@ using TOML include("references.jl") -function download_refimages(tag = "refimages-v2") +function download_refimages(tag = "refimages-v3") url = "https://github.com/Kolaru/MathTeXEngine.jl/releases/download/$tag/reference_images.tar" images_tar = joinpath(@__DIR__, "reference_images.tar") images = joinpath(@__DIR__, "reference_images") @@ -37,12 +38,15 @@ function image_paths(references) return paths end +n_bad_pixels(img1, img2) = count(>(0.5), image_difference(img1, img2)) +image_difference(img1, img2) = colordiff.(img1, img2) ./ 100 + @testset "Reference images" begin @info "Reference test started" @info "Downloading reference images" reference_images = download_refimages() - @inof "Reference images downloaded in $reference_images" + @info "Reference images downloaded in $reference_images" @info "Generating comparison images" comparison_images = joinpath(@__DIR__, "comparison_images") @@ -59,22 +63,37 @@ end for image_path in image_paths(REFERENCES) @info "Comparing $image_path" - refimg = load(joinpath(reference_images, image_path)) - img = load(joinpath(comparison_images, image_path)) + refimg = rotr90(load(joinpath(reference_images, image_path))) + img = rotr90(load(joinpath(comparison_images, image_path))) + + if (failed = n_bad_pixels(img, refimg) >= 10) + @info "Saving the reference comparison for '$image_path' (image difference $(n_bad_pixels(img, refimg)))" + fig = Figure(size = (3*size(img, 1), size(img, 2))) + Label(fig[1, 1], "Reference $(size(refimg))", tellwidth=false) + axref = Axis(fig[2, 1], aspect = DataAspect()) + hidedecorations!(axref) + image!(axref, refimg) + + Label(fig[1, 2], "Current $(size(img))", tellwidth=false) + axcurrent = Axis(fig[2, 2], aspect = DataAspect()) + hidedecorations!(axcurrent) + image!(axcurrent, img) - if img != refimg - @info "Saving the reference comparison for '$image_path'." - fig = Figure() - fig[1, 1] = Label(fig, "Reference", tellwidth=false) - axref = fig[2, 1] = Axis(fig, aspect=DataAspect()) - image!(axref, rotr90(refimg)) + if size(img) == size(refimg) + Label(fig[1, 3], "Difference ($(n_bad_pixels(img, refimg)) bad pixels). Blue: reference. Orange: current.", tellwidth=false) + axdiff = Axis(fig[2, 3], aspect = DataAspect()) + hidedecorations!(axdiff) + diff = Gray.(img) - Gray.(refimg) + overlay = RGB.(Gray.(refimg), Gray.(img)./2 .+ Gray.(refimg)./2, Gray.(img)) - fig[1, 2] = Label(fig, "Current", tellwidth=false) - axcurrent = fig[2, 2] = Axis(fig, aspect=DataAspect()) - image!(axcurrent, rotr90(img)) + image!(axdiff, overlay) + end - save(joinpath(path, image_path), fig) + comparison_path = joinpath(path, image_path) + mkpath(dirname(comparison_path)) + @info "Saving the reference plot at $comparison_path" + save(comparison_path, fig) end - @test img == refimg + @test !failed end end diff --git a/reference/references.jl b/reference/references.jl index 0a0a018..585be57 100644 --- a/reference/references.jl +++ b/reference/references.jl @@ -20,7 +20,7 @@ const SUPPORTED_FONTS = [ ] function generate(destination_folder, references = REFERENCES, fonts = SUPPORTED_FONTS) - @info "Generating reference images in folder $destination_folder" + @info "Generating images in folder $destination_folder" path = mkpath(destination_folder) failures = Dict()