Skip to content
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

eagerly convert to Array type before sending to IO backend #45

Merged
merged 3 commits into from
Oct 22, 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
6 changes: 4 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ImageShow"
uuid = "4e3cecfd-b093-5904-9786-8bbb286a6a31"
version = "0.3.2"
version = "0.3.3"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand All @@ -19,11 +19,13 @@ StackViews = "0.1.1"
julia = "1"

[extras]
AxisArrays = "39de3d68-74b9-583c-8d2d-e117c070f3a9"
ImageDistances = "51556ac3-7006-55f5-8cb3-34580c88182d"
ImageIO = "82e4d734-157c-48bb-816b-45c225c6df19"
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", "ImageIO", "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
# On performance: if the array type(e.g., OffsetArray) has efficient convert method to Array
# then this is almost a no-op
enforce_standard_dense_array(A::AbstractArray) = convert(Array, A)
enforce_standard_dense_array(A::DenseArray) = 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