Skip to content

Commit

Permalink
Merge pull request #149 from JuliaIO/bja/permutehorizontal
Browse files Browse the repository at this point in the history
add permute_horizontal arg to load()
  • Loading branch information
bjarthur committed Mar 8, 2019
2 parents 3c4e1c1 + d6a7b07 commit 40d725a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 21 deletions.
14 changes: 4 additions & 10 deletions src/ImageMagick.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const ufixedtype = Dict(10=>N6f10, 12=>N4f12, 14=>N2f14, 16=>N0f16)

readblob(data::Vector{UInt8}) = load_(data)

function load_(file::Union{AbstractString,IO,Vector{UInt8}}; ImageType=Array, extraprop="", extrapropertynames=nothing, view=false)
function load_(file::Union{AbstractString,IO,Vector{UInt8}}, permute_horizontal=true; ImageType=Array, extraprop="", extrapropertynames=nothing, view=false)
if ImageType != Array
error("this function now returns an Array, do not use ImageType keyword.")
end
Expand All @@ -140,7 +140,8 @@ function load_(file::Union{AbstractString,IO,Vector{UInt8}}; ImageType=Array, ex
exportimagepixels!(rawview(channelview(buf)), wand, cs, channelorder)

orient = getimageproperty(wand, "exif:Orientation", false)
oriented_buf = get(orientation_dict, orient, pd)(buf)
default = (A,ph) -> ph ? vertical_major(A) : identity(A)
oriented_buf = get(orientation_dict, orient, default)(buf, permute_horizontal)
view ? oriented_buf : collect(oriented_buf)
end

Expand All @@ -166,7 +167,7 @@ function image2wand(img, mapi=identity, quality=nothing, permute_horizontal=true
@warn "Mapping to the storage type failed; perhaps your data had out-of-range values?\nTry `map(clamp01nan, img)` to clamp values to a valid range."
rethrow()
end
permute_horizontal && (imgw = permutedims_horizontal(imgw))
permute_horizontal && (imgw = collect(vertical_major(imgw)))
if ndims(imgw) > 3
error("At most 3 dimensions are supported")
end
Expand Down Expand Up @@ -265,11 +266,4 @@ end
to_explicit(A::Array{T}) where {T<:Normed} = rawview(A)
to_explicit(A::Array{T}) where {T<:AbstractFloat} = to_explicit(convert(Array{N0f8}, A))

permutedims_horizontal(img::AbstractVector) = img
function permutedims_horizontal(img)
# Vertical-major is hard-coded here
p = [2;1;3:ndims(img)]
permutedims(img, p)
end

end # module
24 changes: 13 additions & 11 deletions src/libmagickwand.jl
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,19 @@ flip1(A) = view(A, reverse(axes(A,1)), ntuple(x->Colon(),ndims(A)-1)...)
flip2(A) = view(A, :, reverse(axes(A,2)), ntuple(x->Colon(),ndims(A)-2)...)
flip12(A) = view(A, reverse(axes(A,1)), reverse(axes(A,2)), ntuple(x->Colon(),ndims(A)-2)...)

pd(A) = PermutedDimsArray(A, [2;1;3:ndims(A)])

const orientation_dict = Dict(nothing => pd,
"1" => pd,
"2" => A->pd(flip1(A)),
"3" => A->pd(flip12(A)),
"4" => A->pd(flip2(A)),
"5" => identity,
"6" => flip2,
"7" => flip12,
"8" => flip1)
vertical_major(img::AbstractVector) = img
vertical_major(A) = PermutedDimsArray(A, [2;1;3:ndims(A)])

const orientation_dict = Dict(
nothing => (A,ph) -> ph ? vertical_major(A) : identity(A),
"1" => (A,ph) -> ph ? vertical_major(A) : identity(A),
"2" => (A,ph) -> ph ? vertical_major(flip1(A)) : flip1(A),
"3" => (A,ph) -> ph ? vertical_major(flip12(A)) : flip12(A),
"4" => (A,ph) -> ph ? vertical_major(flip2(A)) : flip2(A),
"5" => (A,ph) -> ph ? identity(A) : vertical_major(A),
"6" => (A,ph) -> ph ? flip2(A) : vertical_major(flip2(A)),
"7" => (A,ph) -> ph ? flip12(A) : vertical_major(flip12(A)),
"8" => (A,ph) -> ph ? flip1(A) : vertical_major(flip1(A)))

function nchannels(imtype::AbstractString, cs::AbstractString, havealpha = false)
n = 3
Expand Down
22 changes: 22 additions & 0 deletions test/constructed_images.jl
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,28 @@ mutable struct TestType end
imgr = ImageMagick.load(fn)
@test imgr == parent(img)
end

@testset "permute_horizontal" begin
Ar = [0x00 0xff; 0x00 0x00]
A = map(x->Gray(N0f8(x,0)), Ar)
fn = joinpath(workdir, "2d.tif")

ImageMagick.save(fn, A)
B = ImageMagick.load(fn)
@test A==B

ImageMagick.save(fn, A, false)
B = ImageMagick.load(fn, false)
@test A==B

ImageMagick.save(fn, A, true)
B = ImageMagick.load(fn, false)
@test A==transpose(B)

ImageMagick.save(fn, A, false)
B = ImageMagick.load(fn, true)
@test transpose(A)==B
end
end

nothing

0 comments on commit 40d725a

Please sign in to comment.