# Change memory layout of images

**URL:** <https://discourse.slicer.org/t/change-memory-layout-of-images/19217>\
**Category:** Development\
**Created:** [August 16, 2021, 10:07pm UTC](https://discourse.slicer.org/t/change-memory-layout-of-images/19217 "2021-08-16T22:07:09Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![keri](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/keri/32/11618_2.png) [@keri](https://discourse.slicer.org/u/keri)\
**Post date:** [August 16, 2021, 10:07pm UTC](https://discourse.slicer.org/t/change-memory-layout-of-images/19217/1 "2021-08-16T22:07:09Z")

</div>

I’m working now with 3D `vtkImageData` with scalar set. I fill `vtkFloatArray` from pointer of `Eigen::Matrix`:

```cpp
    vtkNew<vtkFloatArray> array;
    array->SetArray(TRACE->data(), TRACE->size(), 1);

```

The problem is that `vtkImageData` expects that the scalar array is filled in the order: `X-axis` then `Y-axis` and only then `Z-axis`.  
My eigen matrix is filled differently: at first `Z-axis` then `Y-axis` and then `X-axis` (or maybe after Z goes X and only then Y).

As I work with quite big data (few gigabytes or at minimum few hundreds of megabytes) I wanted to avoid copying data and somehow tell to my mrmlnode (or vtkimagedata) that the scalar data has some non-standard memory order.

Rotations will not help me there…  
Maybe you have some ideas?

---

<div class="post-metadata">

**Author:** ![lassoan](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.slicer.org/lassoan/32/13_2.png) [@lassoan](https://discourse.slicer.org/u/lassoan)\
**Post date:** [August 16, 2021, 10:40pm UTC](https://discourse.slicer.org/t/change-memory-layout-of-images/19217/2 "2021-08-16T22:40:08Z")

</div>

VTK core supports arbitrary memory layouts but I think only a fraction of filters support it, so you can only use special memory layouts for very specific operations.

This C vs Fortran memory order is typically just an annoyance, something that you need to keep in mind when you cross boundaries between libraries that use different conventions. For example, this issue comes up very often with ITK/VTK vs numpy, but I don’t recall any situation that required somebody to reallocate arrays.

If you just want to display the volume then you can apply a transform that reorders coordinate system axes. For example, the transformation matrix to convert from KJI to IJK is:

```auto
0,0,1,0
0,1,0,0
1,0,0,0
0,0,0,1

```

By the way, resampling of a volume with reordered axes is usually a very quick operation, so it should not be perceivable for users, unless you need to keep both representations in memory and synchronize them very frequently.
