How to prepare data for using in Images

With GMT.jl we can read or produce high quality images (like the one here) but the output is a MxNx2 uint8 array. And I would like to display them with Images.jl but can’t find a way to reformat the data.

I did try things around this

using FixedPointNumbers, Colors, ColorTypes
z=zeros(UInt8,2,2,3);
z[1,1,1] = 255; z[1,2,2] = 255; z[2,1,3] = 255;
imf8 = reinterpret(N0f8, z)
2×2×3 Array{FixedPointNumbers.Normed{UInt8,8},3}:
[:, :, 1] =
 1.0N0f8  0.0N0f8
 0.0N0f8  0.0N0f8

[:, :, 2] =
 0.0N0f8  1.0N0f8
 0.0N0f8  0.0N0f8

[:, :, 3] =
 0.0N0f8  0.0N0f8
 1.0N0f8  0.0N0f8

RGB24.(reshape(permutedims(imf8,[3 2 1])[:],2,6))
2×6 Array{ColorTypes.RGB24,2}:
 RGB24{N0f8}(1.0,1.0,1.0)  RGB24{N0f8}(0.0,0.0,0.0)  RGB24{N0f8}(1.0,1.0,1.0)  …  RGB24{N0f8}(1.0,1.0,1.0)  RGB24{N0f8}(0.0,0.0,0.0)
 RGB24{N0f8}(0.0,0.0,0.0)  RGB24{N0f8}(0.0,0.0,0.0)  RGB24{N0f8}(0.0,0.0,0.0)     RGB24{N0f8}(0.0,0.0,0.0)  RGB24{N0f8}(0.0,0.0,0.0)

but that’s not what it takes to create a color image (a 4 pixels Red,Green,Blue,Black image in this case).

How can I do it? This problem is clearly solved by the FileIO `load()`` function but I can’t find the code that loads an image from file and reformats the data.

Thanks.

Give ImageCore a try. I am not quite sure what color type you are looking for since you talk about 3 different bit-depths one way or the other, but play around with the following code and see if it gives you what you are looking for

julia> using ImageCore, ColorTypes

julia> z=zeros(UInt8,2,2,3);

julia> z[1,1,1] = 255; z[1,2,2] = 255; z[2,1,3] = 255;

julia> imf8 = normedview(z)
2×2×3 Array{N0f8,3}:
[:, :, 1] =
 1.0N0f8  0.0N0f8
 0.0N0f8  0.0N0f8

[:, :, 2] =
 0.0N0f8  1.0N0f8
 0.0N0f8  0.0N0f8

[:, :, 3] =
 0.0N0f8  0.0N0f8
 1.0N0f8  0.0N0f8

julia> colorview(RGB, permutedims(imf8,[3 2 1]))
2×2 Array{RGB{N0f8},2}:
 RGB{N0f8}(1.0,0.0,0.0)  RGB{N0f8}(0.0,0.0,1.0)
 RGB{N0f8}(0.0,1.0,0.0)  RGB{N0f8}(0.0,0.0,0.0)

Thanks, that’s what I was looking for. I see, however, that when data is RGB Band Interleaved to use it in Images one have to do a data copy (here materialized by the permutedims() call).
I wonder if this is inherent to all FileIO imports for formats that do not have the image stored as Pixel Interleaved.

Not quite. there is permuteddimsview in ImageCore which does not copy. You do sacrifice memory locality however.

Ah, nice but loosing memory locality is probably worst than a copy. Anyway, this should not be an issue for my intents because I’m supposed to be able to control the memory layout of the array that comes out from GMT so no call to permutedims should be needed. ‘Just’ have to work it out now.