Skip to content

Commit

Permalink
Merge pull request #45 from JuliaImages/jc/array
Browse files Browse the repository at this point in the history
eagerly convert to Array type before sending to IO backend
  • Loading branch information
johnnychen94 authored Oct 22, 2021
2 parents 953f8e6 + f5b4be0 commit 43fab62
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 5 deletions.
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

2 comments on commit 43fab62

@johnnychen94
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/47271

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.3.3 -m "<description of version>" 43fab62d2c094d93de070057ad7a1c095f1036e8
git push origin v0.3.3

Please sign in to comment.