|
124 | 124 | msg = "Unrecognized MATLAB image layout."
|
125 | 125 | @test_throws ArgumentError(msg) im_from_matlab(data)
|
126 | 126 | end
|
| 127 | + |
| 128 | + @testset "im_to_matlab" begin |
| 129 | + @testset "Gray" begin |
| 130 | + img = rand(Gray{N0f8}, 4, 5) |
| 131 | + data = @inferred im_to_matlab(img) |
| 132 | + @test eltype(data) == N0f8 |
| 133 | + @test size(data) == (4, 5) |
| 134 | + @test img == data |
| 135 | + data = @inferred im_to_matlab(Float64, img) |
| 136 | + @test eltype(data) == Float64 |
| 137 | + @test img == data |
| 138 | + |
| 139 | + img = rand(Gray{Float64}, 4, 5) |
| 140 | + data = @inferred im_to_matlab(img) |
| 141 | + @test eltype(data) == Float64 |
| 142 | + @test size(data) == (4, 5) |
| 143 | + @test img == data |
| 144 | + |
| 145 | + img = rand(UInt8, 4, 5) |
| 146 | + @test img === @inferred im_to_matlab(img) |
| 147 | + |
| 148 | + img = rand(Gray{Float64}, 4) |
| 149 | + data = @inferred im_to_matlab(img) |
| 150 | + @test eltype(data) == Float64 |
| 151 | + @test size(data) == (4, ) |
| 152 | + end |
| 153 | + |
| 154 | + @testset "RGB" begin |
| 155 | + img = rand(RGB{N0f8}, 4, 5) |
| 156 | + data = @inferred im_to_matlab(img) |
| 157 | + @test eltype(data) == N0f8 |
| 158 | + @test size(data) == (4, 5, 3) |
| 159 | + @test permutedims(channelview(img), (2, 3, 1)) == data |
| 160 | + data = @inferred im_to_matlab(Float64, img) |
| 161 | + @test eltype(data) == Float64 |
| 162 | + @test size(data) == (4, 5, 3) |
| 163 | + @test permutedims(channelview(img), (2, 3, 1)) == data |
| 164 | + |
| 165 | + img = rand(RGB{Float64}, 4, 5) |
| 166 | + data = @inferred im_to_matlab(img) |
| 167 | + @test eltype(data) == Float64 |
| 168 | + @test size(data) == (4, 5, 3) |
| 169 | + @test permutedims(channelview(img), (2, 3, 1)) == data |
| 170 | + |
| 171 | + img = rand(UInt8, 4, 5, 3) |
| 172 | + @test img === @inferred im_to_matlab(img) |
| 173 | + |
| 174 | + img = rand(RGB{Float64}, 4) |
| 175 | + data = @inferred im_to_matlab(img) |
| 176 | + @test eltype(data) == Float64 |
| 177 | + @test size(data) == (4, 1, 3) # oh yes, we add one extra dimension for RGB but not for Gray |
| 178 | + |
| 179 | + img = rand(RGB{Float64}, 2, 3, 4) |
| 180 | + msg = "For 3 dimensional color image, manual conversion to MATLAB layout is required." |
| 181 | + @test_throws ArgumentError(msg) im_to_matlab(img) |
| 182 | + end |
| 183 | + |
| 184 | + @testset "Color3" begin |
| 185 | + img = Lab.(rand(RGB, 4, 5)) |
| 186 | + @test @inferred(im_to_matlab(img)) ≈ @inferred(im_to_matlab(RGB.(img))) |
| 187 | + end |
| 188 | + @testset "transparent" begin |
| 189 | + img = rand(AGray, 4, 5) |
| 190 | + @test @inferred(im_to_matlab(img)) == @inferred(im_to_matlab(Gray.(img))) |
| 191 | + img = rand(RGBA, 4, 5) |
| 192 | + @test @inferred(im_to_matlab(img)) == @inferred(im_to_matlab(RGB.(img))) |
| 193 | + end |
| 194 | + end |
| 195 | + |
| 196 | + # test `im_from_matlab` and `im_to_matlab` are inverses of each other. |
| 197 | + data = rand(4, 5) |
| 198 | + @test data === im_to_matlab(im_from_matlab(data)) |
| 199 | + # For RGB, ideally we would want to ensure this === equality, but it's not possible at the moment. |
| 200 | + data = rand(4, 5, 3) |
| 201 | + @test data == im_to_matlab(im_from_matlab(data)) |
| 202 | + # the output range are always in [0, 1]; in this case they're not inverse of each other. |
| 203 | + data = rand(UInt8, 4, 5) |
| 204 | + img = im_from_matlab(data) |
| 205 | + @test im_to_matlab(img) == data ./ 255 |
127 | 206 | end
|
0 commit comments