Skip to content

Commit

Permalink
Merge pull request #6 from JuliaImages/teh/repeat
Browse files Browse the repository at this point in the history
Fix show for images with offset axes
  • Loading branch information
timholy authored Nov 14, 2018
2 parents dd8ccac + 3df9e66 commit f8db749
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 4 deletions.
1 change: 1 addition & 0 deletions REQUIRE
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Colors
ColorTypes 0.7.4
FixedPointNumbers
FileIO
OffsetArrays
2 changes: 1 addition & 1 deletion src/ImageShow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module ImageShow

using Requires
using FileIO
using ImageCore, Colors, FixedPointNumbers
using ImageCore, OffsetArrays, Colors, FixedPointNumbers

const _have_restrict=Ref(false)
function _use_restrict(val::Bool)
Expand Down
7 changes: 6 additions & 1 deletion src/showmime.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ 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
if !get(io, :full_fidelity, false)
while _length1(img) > maxpixels
img = _restrict1(img) # big images
Expand All @@ -42,6 +45,9 @@ function Base.show(io::IO, mime::MIME"image/png", img::AbstractMatrix{C};
end
end

Base.show(io::IO, mime::MIME"image/png", img::OffsetArray{C}; kwargs...) where C<:Colorant =
show(io, mime, parent(img); kwargs...)

# Not all colorspaces are supported by all backends, so reduce types to a minimum
csnormalize(c::AbstractGray) = Gray(c)
csnormalize(c::Color) = RGB(c)
Expand Down Expand Up @@ -129,4 +135,3 @@ end

_length1(A::AbstractArray) = length(eachindex(A))
_length1(A) = length(A)

1 change: 1 addition & 0 deletions test/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
@windows ImageMagick
@linux ImageMagick
@osx QuartzImageIO
PaddedViews
21 changes: 19 additions & 2 deletions test/writemime.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using ImageShow, Colors, FixedPointNumbers, FileIO
using ImageShow, Colors, FixedPointNumbers, FileIO, OffsetArrays, PaddedViews
import ImageCore: colorview, normedview
# We jump through some hoops so that this test script may work
# whether or not ImageTransformations (a fortiori Images) is loaded.
Expand All @@ -8,7 +8,7 @@ import ImageCore: colorview, normedview

using Test

workdir = joinpath(tempdir(), "Images")
const workdir = joinpath(tempdir(), "Images")
if !isdir(workdir)
mkdir(workdir)
end
Expand Down Expand Up @@ -157,6 +157,23 @@ end
@test !applicable(ImageShow._show_odd, io, MIME"text/html"(), flat_imgs)
@test !applicable(ImageShow._show_even, io, MIME"text/html"(), flat_imgs)
end
@testset "Non-1 indexing" begin
A = N0f8[0.01 0.99; 0.25 0.75]
Aoff = OffsetArray(A, 0:1, 2:3)
fn = joinpath(workdir, "oas.png")
open(fn, "w") do file
show(file, MIME("image/png"), Gray.(Aoff), minpixels=5, maxpixels=typemax(Int))
end
@test load(fn) == A[[1,1,2,2],[1,1,2,2]]
# Also test a type that doesn't have a specialization so as to trigger the generic
# fallback
A = PaddedView(0, reshape([1], 1, 1), (0:1, 1:2))
Ac = collect(A)
open(fn, "w") do file
show(file, MIME("image/png"), Gray.(A), minpixels=5, maxpixels=typemax(Int))
end
@test load(fn) == Ac[[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 f8db749

Please sign in to comment.