Skip to content

Commit

Permalink
eagerly convert Array type before sending to IO backend
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 committed Oct 22, 2021
1 parent 953f8e6 commit 5183c93
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ StackViews = "0.1.1"
julia = "1"

[extras]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
ImageDistances = "51556ac3-7006-55f5-8cb3-34580c88182d"
ImageMagick = "6218d12a-5da1-5696-b52f-db25d2ecc6d1"
Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
TestImages = "5e47fb64-e119-507b-a336-dd2b206d9990"

[targets]
test = ["ImageDistances", "ImageMagick", "Suppressor", "Test", "TestImages"]
test = ["AxisArrays", "ImageDistances", "ImageMagick", "Suppressor", "Test", "TestImages"]
11 changes: 8 additions & 3 deletions src/showmime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,7 @@ function Base.show(io::IO, mime::MIME"image/png", img::AbstractMatrix{C};
minpixels=10^4, maxpixels=10^6,
# Jupyter seemingly can't handle 16-bit colors:
mapi=x->mapc(N0f8, clamp01nan(csnormalize(x)))) where C<:Colorant
if Base.has_offset_axes(img)
img = collect(img)
end
img = enforce_standard_dense_array(img)
if !get(io, :full_fidelity, false)
while _length1(img) > maxpixels
img = restrict(img) # big images
Expand All @@ -54,6 +52,13 @@ csnormalize(c::AbstractGray) = Gray(c)
csnormalize(c::Color) = RGB(c)
csnormalize(c::Colorant) = RGBA(c)

# Unless we have PNG IO backend that works on generic array types, we have to eagerly
# convert it to dense array types
# The performance of `convert` depends on the concrete array type `typeof(A)`
enforce_standard_dense_array(A::AbstractArray) = convert(Array, A)
enforce_standard_dense_array(A::DenseArray) = A
enforce_standard_dense_array(A::OffsetArray) = parent(A)

const ColorantMatrix{T<:Colorant} = AbstractMatrix{T}

function _show_odd(io::IO, m::MIME"text/html", imgs::AbstractArray{T, 1}) where T<:ColorantMatrix
Expand Down
10 changes: 10 additions & 0 deletions test/writemime.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using ImageShow, ImageCore, FileIO, OffsetArrays
using ImageCore: PaddedViews
import ImageBase: restrict
import AxisArrays: AxisArray

using Test

Expand Down Expand Up @@ -143,6 +144,15 @@ end
end
@test load(fn) == Ac[[1,1,2,2],[1,1,2,2]]
end
@testset "Array types that may not recognized by IO backends" begin
A = N0f8[0.01 0.99; 0.25 0.75]
A_axis = AxisArray(A)
fn = joinpath(workdir, "axis.png")
open(fn, "w") do file
show(file, MIME("image/png"), Gray.(A_axis), minpixels=5, maxpixels=typemax(Int))
end
@test load(fn) == A[[1,1,2,2],[1,1,2,2]]
end
end
try
# if this fails, it is not our fault
Expand Down

0 comments on commit 5183c93

Please sign in to comment.