# Convert Packed Image Format into Planar Format without Dependency

**URL:** https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784
**Category:** General Usage
**Tags:** question, images, data\_structures
**Created:** [June 24, 2023, 4:15pm UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784 "2023-06-24T16:15:15Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [June 24, 2023, 4:15pm UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/1 "2023-06-24T16:15:15Z")

</div>

I am loading images using `FileIO.jl`.

I want to convert the output into a Planar Format of the appropriate element type.  
I want to achieve this without any farther dependencies, so `channelview()` as in [Most idomatic way to convert an Images.jl Image to a 3-d array?](https://discourse.julialang.org/t/most-idomatic-way-to-convert-an-images-jl-image-to-a-3-d-array/7104) is not available.

The problem I have is figuring and supporting all the different formats of used the by the Julia Images.

I don’t mind solving it in a brute force using a loop but I need to solve the following 2 sub problems:

1. How to extract the number of channels given the image array.
2. How to map the elements into the regular element types (`UIntx` and `Floatx` usually).

I am struggling with finding a robust and global solution to this.

For instance:

```julia
using FileIO;

img001Url = "https://i.stack.imgur.com/WsSFV.png";
mT = load(download(img001Url));

```

The type of `mT` is `Matrix{RGBA{N0f8}}` so it has 4 channels (`RGBA -> 4`) and uses `UInt8` (`N0f8 -> UInt8`).  
But sometimes there are `Gray` and `N0f16` etc… So I wonder if there is something elegant to do here.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [June 26, 2023, 8:13am UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/2 "2023-06-26T08:13:31Z")

</div>

I got something like:

```julia
using ColorTypes;
using FileIO;

function ConvertJuliaImgArray001(mI :: Matrix{<: TransparentColor{C, T, N}}) where {C, T, N}
    
    numRows, numCols = size(mI);
    numChannels = N;
    dataType = T.types[1];

    if numChannels > 1
        mO = Array{dataType, 3}(undef, numRows, numCols, numChannels);
        for ii ∈ 1:numRows, jj ∈ 1:numCols
            for kk ∈ 1:numChannels
                mO[ii, jj, kk] = getfield(getfield(mI[ii, jj], kk), 1); #<! Accordign to ColorTypes data always in order RGBA
            end
        end
    else
        mO = Matrix{dataType}(undef, numRows, numCols);
        for ii ∈ 1:numRows, jj ∈ 1:numCols
            mO[ii, jj] = getfield(mI[ii, jj], 1);
        end
    end

    return mO;

end

```

Similarly I created for `mI :: Matrix{<: Color{T, N}}` and `mI :: Matrix{<: Color{T, 1}}`.  
It requires `ColorTypes.jl` yet since it is a dependency of `FileIO.jl` I am OK with this.

What I’m not sure is about `getfield()`. Is there anything more robust or that is the correct way to access the data?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 26, 2023, 8:34am UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/3 "2023-06-26T08:34:49Z")

</div>

I would be tempted to start with `reinterpret(dataType, mI)` and go from there.

Edit: Or actually `reinterpret(reshape, dataType, mI)`, which I just found out that it was added in Julia 1.6.

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [June 26, 2023, 5:37pm UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/4 "2023-06-26T17:37:26Z")

</div>

@GunnarFarneback , I think walking that path requires the function `channelview()`.  
Can you think a way to avoid it with the path you suggested?

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 26, 2023, 7:09pm UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/5 "2023-06-26T19:09:10Z")

</div>

In what way do you need `channelview` to make use of `reinterpret`? Is there some specific piece of information you’re missing?

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [June 27, 2023, 7:01am UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/6 "2023-06-27T07:01:54Z")

</div>

As far as I know `reshape()` can not change the order in memory, only the strides.  
Since images, by default in the Julia Images eco system, are in packed format (`R1G1B1R2G2B3...`) you can’t convert it into planar form (`R1R2R3...G1G2G3...B1B2B3...`).

If you have a code sample, I’d like to try.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [June 27, 2023, 7:18am UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/7 "2023-06-27T07:18:01Z")

</div>

I fail to see the difficulty so I guess I’m missing something. Do you want something significantly different from this?

```julia
x = rand(RGB{N0f16}, 4, 5)
y = permutedims(reinterpret(reshape, UInt16, x), (2, 3, 1))

```

---

<div class="post-metadata">

### Author: ![RoyiAvital](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/royiavital/32/571_2.png) [@RoyiAvital](https://discourse.julialang.org/u/RoyiAvital)
#### Post date: [July 3, 2023, 1:03pm UTC](https://discourse.julialang.org/t/convert-packed-image-format-into-planar-format-without-dependency/100784/8 "2023-07-03T13:03:01Z")

</div>

I tried it again and it worked.  
I am not sure why I was under the impression `channelview()` is required.

Thank You.
