Skip to content

Support anything that can save as PNG #77

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Feb 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ ImageCore = "0.8.1"
ImageInTerminal = "0.3, 0.4"
ImageMagick = "0.7, 1"
ImageTransformations = "0.8"
Plots = "1.4.3"
TestImages = "0.6, 1"
julia = "1"

Expand All @@ -31,7 +32,8 @@ CSVFiles = "5d742f6a-9f54-50ce-8119-2520741973ca"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
ImageTransformations = "02fcd773-0e25-5acc-982a-7f6622650795"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"

[targets]
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "TestImages"]
test = ["CSVFiles", "DataFrames", "ImageMagick", "ImageTransformations", "Plots", "TestImages"]
10 changes: 10 additions & 0 deletions src/fileio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@ function _convert(
return join(strs,'\n')
end

# PNG (Including images as arrays of colorants, and things like Plots.jl plots etc)
function _convert(::Type{<:DataFormat{:PNG}}, data)::AbstractArray{<:Colorant}
mktempdir() do dir
filename = File{DataFormat{:PNG}}(joinpath(dir, "inconversion.png"))
savefile(filename, data)
load(filename)
end
end
_convert(::Type{<:DataFormat{:PNG}}, img::AbstractArray{<:Colorant}; kw...) = img

# SHA256
_convert(::Type{DataFormat{:SHA256}}, x; kw...) = bytes2hex(sha256(string(x)))
function _convert(::Type{DataFormat{:SHA256}}, img::AbstractArray{<:Colorant}; kw...)
Expand Down
9 changes: 5 additions & 4 deletions src/test_reference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,20 @@ function test_reference(
path = file.filename
dir, filename = splitdir(path)

actual = _convert(F, raw_actual; kw...)

# infer the default rendermode here
# since `nothing` is always passed to this method from
# test_reference(filename::AbstractString, raw_actual; kw...)
if rendermode === nothing
rendermode = default_rendermode(F, raw_actual)
rendermode = default_rendermode(F, actual)
end

actual = _convert(F, raw_actual; kw...)
# preprocessing when reference file doesn't exists
if !isfile(path)
@info("Reference file for \"$filename\" does not exist. It will be created")
# TODO: move encoding out from render
render(rendermode, raw_actual)
render(rendermode, actual)

mkpath(dir)
savefile(file, actual)
Expand All @@ -122,7 +123,7 @@ function test_reference(
end

# file exists
reference = loadfile(T, file)
reference = loadfile(typeof(actual), file)

if equiv === nothing
# generally, `reference` and `actual` are of the same type after preprocessing
Expand Down
Binary file added test/references/heatmap.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added test/references/scatter.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Test
using ImageInTerminal, TestImages, ImageCore, ImageTransformations
using Plots
using Random

if isinteractive()
Expand Down Expand Up @@ -120,6 +121,14 @@ end
@test_throws Exception @test_reference "references/camera.png" camera # unequal size
end

@testset "Plots as PNG images" begin
# Test disabled on linux because: https://github.com/JuliaPlots/Plots.jl/issues/2127
if !Sys.islinux()
@test_reference "references/heatmap.png" heatmap([1 0; 0 1]) by=psnr_equality(15)
@test_reference "references/scatter.png" scatter([(0,0),(1,0),(0,1),(1,1)], ms=8)
end
end

using DataFrames, CSVFiles
@testset "DataFrame as CSV" begin
@test_reference "references/dataframe.csv" DataFrame(v1=[1,2,3], v2=["a","b","c"])
Expand Down