|
| 1 | +@testset "MATLAB" begin |
| 2 | + @testset "im_from_matlab" begin |
| 3 | + @testset "Gray" begin |
| 4 | + # Float64 |
| 5 | + data = rand(4, 5) |
| 6 | + img = @inferred im_from_matlab(data) |
| 7 | + @test eltype(img) == Gray{Float64} |
| 8 | + @test size(img) == (4, 5) |
| 9 | + @test channelview(img) == data |
| 10 | + |
| 11 | + # N0f8 |
| 12 | + data = rand(N0f8, 4, 5) |
| 13 | + img = @inferred im_from_matlab(data) |
| 14 | + mn, mx = extrema(img) |
| 15 | + @test eltype(img) == Gray{N0f8} |
| 16 | + @test size(img) == (4, 5) |
| 17 | + @test 0.0 <= mn <= mx <= 1.0 |
| 18 | + |
| 19 | + # UInt8 |
| 20 | + data = rand(UInt8, 4, 5) |
| 21 | + img = @inferred im_from_matlab(data) |
| 22 | + mn, mx = extrema(img) |
| 23 | + @test eltype(img) == Gray{N0f8} |
| 24 | + @test size(img) == (4, 5) |
| 25 | + @test 0.0 <= mn <= mx <= 1.0 |
| 26 | + |
| 27 | + # UInt16 |
| 28 | + data = rand(UInt16, 4, 5) |
| 29 | + img = @inferred im_from_matlab(data) |
| 30 | + mn, mx = extrema(img) |
| 31 | + @test eltype(img) == Gray{N0f16} |
| 32 | + @test size(img) == (4, 5) |
| 33 | + @test 0.0 <= mn <= mx <= 1.0 |
| 34 | + |
| 35 | + # Int16 -- MATLAB's im2double supports Int16 |
| 36 | + data = rand(Int16, 4, 5) |
| 37 | + img = @inferred im_from_matlab(data) |
| 38 | + mn, mx = extrema(img) |
| 39 | + @test eltype(img) == Gray{Float64} |
| 40 | + @test size(img) == (4, 5) |
| 41 | + @test 0.0 <= mn <= mx <= 1.0 |
| 42 | + data = Int16[-32768 0; 0 32767] |
| 43 | + @test isapprox([0.0 0.5; 0.5 1.0], @inferred im_from_matlab(data); atol=1e-4) |
| 44 | + |
| 45 | + # Int is ambiguious -- manual conversion is required but we provide some basic hints |
| 46 | + data = rand(1:255, 4, 5) |
| 47 | + msg = "Unrecognized element type $(Int), manual conversion to float point number or fixed point number is needed. For instance: `UInt8.(X)` or `X./255`" |
| 48 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 49 | + data = rand(256:65535, 4, 5) |
| 50 | + msg = "Unrecognized element type $(Int), manual conversion to float point number or fixed point number is needed. For instance: `UInt16.(X)` or `X./65535`" |
| 51 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 52 | + |
| 53 | + # vector |
| 54 | + data = rand(UInt8, 4) |
| 55 | + img = @inferred im_from_matlab(data) |
| 56 | + @test eltype(img) == Gray{N0f8} |
| 57 | + @test size(img) == (4,) |
| 58 | + end |
| 59 | + |
| 60 | + @testset "RGB" begin |
| 61 | + # Float64 |
| 62 | + data = rand(4, 5, 3) |
| 63 | + img = im_from_matlab(data) |
| 64 | + @test_broken @inferred im_from_matlab(data) |
| 65 | + @test_nowarn @inferred collect(im_from_matlab(data)) # type inference issue only occurs in lazy mode |
| 66 | + @test eltype(img) == RGB{Float64} |
| 67 | + @test size(img) == (4, 5) |
| 68 | + @test permutedims(channelview(img), (2, 3, 1)) == data |
| 69 | + |
| 70 | + # N0f8 |
| 71 | + data = rand(N0f8, 4, 5, 3) |
| 72 | + img = im_from_matlab(data) |
| 73 | + @test_broken @inferred im_from_matlab(data) |
| 74 | + @test_nowarn @inferred collect(im_from_matlab(data)) # type inference issue only occurs in lazy mode |
| 75 | + mn, mx = extrema(channelview(img)) |
| 76 | + @test eltype(img) == RGB{N0f8} |
| 77 | + @test size(img) == (4, 5) |
| 78 | + @test 0.0 <= mn <= mx <= 1.0 |
| 79 | + |
| 80 | + # UInt8 |
| 81 | + data = rand(UInt8, 4, 5, 3) |
| 82 | + img = im_from_matlab(data) |
| 83 | + @test_broken @inferred im_from_matlab(data) |
| 84 | + @test_nowarn @inferred collect(im_from_matlab(data)) # type inference issue only occurs in lazy mode |
| 85 | + mn, mx = extrema(channelview(img)) |
| 86 | + @test eltype(img) == RGB{N0f8} |
| 87 | + @test size(img) == (4, 5) |
| 88 | + @test 0.0 <= mn <= mx <= 1.0 |
| 89 | + |
| 90 | + # UInt16 |
| 91 | + data = rand(UInt16, 4, 5, 3) |
| 92 | + img = im_from_matlab(data) |
| 93 | + @test_broken @inferred im_from_matlab(data) |
| 94 | + @test_nowarn @inferred collect(im_from_matlab(data)) # type inference issue only occurs in lazy mode |
| 95 | + mn, mx = extrema(channelview(img)) |
| 96 | + @test eltype(img) == RGB{N0f16} |
| 97 | + @test size(img) == (4, 5) |
| 98 | + @test 0.0 <= mn <= mx <= 1.0 |
| 99 | + |
| 100 | + # Int16 -- MATLAB's im2double supports Int16 |
| 101 | + data = rand(Int16, 4, 5, 3) |
| 102 | + img = im_from_matlab(data) |
| 103 | + @test_broken @inferred im_from_matlab(data) |
| 104 | + @test_nowarn @inferred collect(im_from_matlab(data)) # type inference issue only occurs in lazy mode |
| 105 | + mn, mx = extrema(channelview(img)) |
| 106 | + @test eltype(img) == RGB{Float64} |
| 107 | + @test size(img) == (4, 5) |
| 108 | + @test 0.0 <= mn <= mx <= 1.0 |
| 109 | + |
| 110 | + # Int is ambiguious -- manual conversion is required but we provide some basic hints |
| 111 | + data = rand(1:255, 4, 5, 3) |
| 112 | + msg = "Unrecognized element type $(Int), manual conversion to float point number or fixed point number is needed. For instance: `UInt8.(X)` or `X./255`" |
| 113 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 114 | + data = rand(256:65535, 4, 5, 3) |
| 115 | + msg = "Unrecognized element type $(Int), manual conversion to float point number or fixed point number is needed. For instance: `UInt16.(X)` or `X./65535`" |
| 116 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 117 | + end |
| 118 | + |
| 119 | + data = rand(4, 4, 2) |
| 120 | + msg = "Unrecognized MATLAB image layout." |
| 121 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 122 | + |
| 123 | + data = rand(4, 4, 3, 1) |
| 124 | + msg = "Unrecognized MATLAB image layout." |
| 125 | + @test_throws ArgumentError(msg) im_from_matlab(data) |
| 126 | + end |
| 127 | +end |
0 commit comments