Skip to content

Commit

Permalink
force 1-based indexing for im_to_matlab output
Browse files Browse the repository at this point in the history
  • Loading branch information
johnnychen94 committed Apr 17, 2022
1 parent c630cda commit 2fc256a
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/ImageCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ using Reexport
@reexport using PaddedViews
using MappedArrays, Graphics
using OffsetArrays # for show.jl
using OffsetArrays: no_offset_view
using .ColorTypes: colorant_string
using Colors: Fractional
using MappedArrays: AbstractMultiMappedArray
Expand Down
12 changes: 6 additions & 6 deletions src/matlab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -211,34 +211,34 @@ See also: [`im_from_matlab`](@ref).
"""
function im_to_matlab end

im_to_matlab(X::AbstractArray{<:Number}) = X
im_to_matlab(X::AbstractArray{<:Number}) = no_offset_view(X)
im_to_matlab(img::AbstractArray{CT}) where {CT<:Colorant} = im_to_matlab(eltype(CT), img)

im_to_matlab(::Type{T}, img::AbstractArray{CT}) where {T,CT<:TransparentColor} =
im_to_matlab(T, of_eltype(base_color_type(CT), img))
im_to_matlab(::Type{T}, img::AbstractArray{<:Color}) where {T} =
im_to_matlab(T, of_eltype(RGB{T}, img))
im_to_matlab(::Type{T}, img::AbstractArray{<:Gray}) where {T} =
of_eltype(T, channelview(img))
no_offset_view(of_eltype(T, channelview(img)))

# for RGB, unroll the color channel in the last dimension
function im_to_matlab(::Type{T}, img::AbstractArray{<:RGB, N}) where {T, N}
v = of_eltype(T, channelview(img))
v = no_offset_view(of_eltype(T, channelview(img)))
perm = (ntuple(i->i+1, N)..., 1)
return PermutedDimsArray(v, perm)
end

# indexed image
function im_to_matlab(::Type{T}, img::IndirectArray{CT}) where {T<:Real, CT<:Colorant}
return img.index, im_to_matlab(T, img.values)
return no_offset_view(img.index), im_to_matlab(T, img.values)
end


if VERSION >= v"1.6.0-DEV.1083"
# this method allows `data === im_to_matlab(im_from_matlab(data))` for gray image
im_to_matlab(::Type{T}, img::Base.ReinterpretArray{CT,N,T,<:AbstractArray{T,N},true}) where {CT,N,T} =
img.parent
no_offset_view(img.parent)
else
im_to_matlab(::Type{T}, img::Base.ReinterpretArray{CT,N,T,<:AbstractArray{T,N}}) where {CT,N,T} =
img.parent
no_offset_view(img.parent)
end
40 changes: 40 additions & 0 deletions test/matlab.jl
Original file line number Diff line number Diff line change
Expand Up @@ -278,4 +278,44 @@
data = rand(UInt8, 4, 5)
img = im_from_matlab(data)
@test im_to_matlab(img) == data ./ 255

@testset "offset array" begin
# JuliaImages accepts arbitrary offsets thus there's no need to force 1-based indexing,
# MATLAB, on the other hand, generally requires 1-based indexing to properly work.

# Gray
x = rand(4, 5)
xo = OffsetArray(x, (-2, -3))
img = im_from_matlab(xo)
@test axes(img) == (-1:2, -2:2)
@test eltype(img) == Gray{Float64}

m_img = im_to_matlab(img)
@test axes(m_img) == (1:4, 1:5)
@test m_img == x

# RGB
x = rand(4, 5, 3)
xo = OffsetArray(x, (-2, -3, 0))
img = im_from_matlab(xo)
@test axes(img) == (-1:2, -2:2)
@test eltype(img) == RGB{Float64}

m_img = im_to_matlab(img)
@test axes(m_img) == (1:4, 1:5, 1:3)
@test m_img == x

# indexed image
index = rand(1:5, 4, 5)
index_offset = OffsetArray(index, (-1, -1))
values = rand(5, 3)
img = im_from_matlab(index_offset, values)
@test axes(img) == (0:3, 0:4)
@test eltype(img) == RGB{Float64}

m_index, m_values = im_to_matlab(img)
@test axes(m_index) == (1:4, 1:5)
@test m_index == index
@test m_values == values
end
end

0 comments on commit 2fc256a

Please sign in to comment.