Skip to content

Commit 9819310

Browse files
committed
let im_from_matlab error on suspicious dimention
With some whitespace fixes
1 parent d46a180 commit 9819310

File tree

2 files changed

+24
-9
lines changed

2 files changed

+24
-9
lines changed

src/matlab.jl

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ im_from_matlab(X::AbstractArray) = throw(ArgumentError("Unrecognized MATLAB imag
5959

6060
# Step 2: storage type conversion
6161
function im_from_matlab(::Type{CT}, X::AbstractArray{T}) where {CT,T}
62-
if T<:Union{Normed, AbstractFloat}
62+
if T <: Union{Normed,AbstractFloat}
6363
return _im_from_matlab(CT, X)
6464
else
6565
msg = "Unrecognized element type $T, manual conversion to float point number or fixed point number is needed."
@@ -68,11 +68,11 @@ function im_from_matlab(::Type{CT}, X::AbstractArray{T}) where {CT,T}
6868
throw(ArgumentError(msg))
6969
end
7070
end
71-
im_from_matlab(::Type{CT}, X::AbstractArray{UInt8}) where CT = _im_from_matlab(CT, reinterpret(N0f8, X))
72-
im_from_matlab(::Type{CT}, X::AbstractArray{UInt16}) where CT = _im_from_matlab(CT, reinterpret(N0f16, X))
73-
function im_from_matlab(::Type{CT}, X::AbstractArray{Int16}) where CT
71+
im_from_matlab(::Type{CT}, X::AbstractArray{UInt8}) where {CT} = _im_from_matlab(CT, reinterpret(N0f8, X))
72+
im_from_matlab(::Type{CT}, X::AbstractArray{UInt16}) where {CT} = _im_from_matlab(CT, reinterpret(N0f16, X))
73+
function im_from_matlab(::Type{CT}, X::AbstractArray{Int16}) where {CT}
7474
# MALTAB compat
75-
_im2double(x) = (Float64(x)+Float64(32768))/Float64(65535)
75+
_im2double(x) = (Float64(x) + Float64(32768)) / Float64(65535)
7676
return _im_from_matlab(CT, mappedarray(_im2double, X))
7777
end
7878

@@ -88,22 +88,26 @@ function _matlab_type_hint(@nospecialize X)
8888
end
8989

9090
# Step 3: colorspace conversion
91-
_im_from_matlab(::Type{CT}, X::AbstractArray{CT}) where CT<:Colorant = X
91+
_im_from_matlab(::Type{CT}, X::AbstractArray{CT}) where {CT<:Colorant} = X
9292
@static if VERSION >= v"1.3"
9393
# use StructArray to inform that this is a struct of array layout
94-
function _im_from_matlab(::Type{CT}, X::AbstractArray{T,3}) where {CT<:Colorant, T<:Real}
94+
function _im_from_matlab(::Type{CT}, X::AbstractArray{T,3}) where {CT<:Colorant,T<:Real}
9595
_CT = isconcretetype(CT) ? CT : base_colorant_type(CT){T}
9696
# FIXME(johnnychen94): not type inferrable here
9797
return StructArray{_CT}(X; dims=3)
9898
end
9999
else
100-
function _im_from_matlab(::Type{CT}, X::AbstractArray{T,3}) where {CT<:Colorant, T<:Real}
100+
function _im_from_matlab(::Type{CT}, X::AbstractArray{T,3}) where {CT<:Colorant,T<:Real}
101101
_CT = isconcretetype(CT) ? CT : base_colorant_type(CT){T}
102102
# FIXME(johnnychen94): not type inferrable here
103103
return colorview(_CT, PermutedDimsArray(X, (3, 1, 2)))
104104
end
105105
end
106-
_im_from_matlab(::Type{CT}, X::AbstractArray{T}) where {CT<:Gray, T<:Real} = colorview(CT, X)
106+
function _im_from_matlab(::Type{CT}, X::AbstractArray{T}) where {CT<:Colorant,T<:Real}
107+
throw(ArgumentError("For $(ndims(X)) dimensional numerical array, manual conversion from MATLAB layout is required."))
108+
end
109+
_im_from_matlab(::Type{CT}, X::AbstractArray{T}) where {CT<:Gray,T<:Real} = colorview(CT, X)
110+
_im_from_matlab(::Type{CT}, X::AbstractArray{T,3}) where {CT<:Gray,T<:Real} = colorview(CT, X)
107111

108112

109113
"""

test/matlab.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,24 @@
116116
@test_throws ArgumentError(msg) im_from_matlab(data)
117117
end
118118

119+
@testset "Color3" begin
120+
img = Lab.(rand(RGB{Float64}, 4, 5))
121+
data = permutedims(channelview(img), (2, 3, 1))
122+
img1 = im_from_matlab(Lab, data)
123+
@test eltype(img1) == Lab{Float64}
124+
@test size(img1) == (4, 5)
125+
@test RGB.(img) RGB.(img1)
126+
end
127+
119128
data = rand(4, 4, 2)
120129
msg = "Unrecognized MATLAB image layout."
121130
@test_throws ArgumentError(msg) im_from_matlab(data)
122131

123132
data = rand(4, 4, 3, 1)
124133
msg = "Unrecognized MATLAB image layout."
125134
@test_throws ArgumentError(msg) im_from_matlab(data)
135+
msg = "For 4 dimensional numerical array, manual conversion from MATLAB layout is required."
136+
@test_throws ArgumentError(msg) im_from_matlab(RGB, data)
126137
end
127138

128139
@testset "im_to_matlab" begin

0 commit comments

Comments
 (0)