# Changing color space of vtkAlgorithmOutput to BGR for vtkImageExport?

**URL:** https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832
**Category:** Support
**Created:** [May 20, 2021, 6:07pm UTC](https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832 "2021-05-20T18:07:03Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Nmlgc](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nmlgc/32/3435_2.png) [@Nmlgc](https://discourse.vtk.org/u/Nmlgc)
#### Post date: [May 20, 2021, 6:07pm UTC](https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832/1 "2021-05-20T18:07:03Z")

</div>

Hello,

I’m trying to export an image from a `vtkAlgorithmOutput` source to a C array in BGRA32 format. The export itself is working fine as long as `vtkImageMapToColors` is used to (apparently) ensure 8 bits per color component. Now, I only need to swap the red and blue channels in the exported image.  
(For context, I’m developing a Slicer extension, and the `input_image` is pulled from one of its slice views, at probably 16 bits per component.)

This is what I have so far:

```
struct BGRA32 {
    uint8_t b, g, r, a;
};
vtkAlgorithmOutput* input_image = /* ... */;
auto mapper = vtkSmartPointer<vtkImageMapToColors>::New();
auto exporter = vtkSmartPointer<vtkImageExport>::New();
std::vector<BGRA32> output_data;

mapper->SetInputConnection(input_image);
exporter->SetInputConnection(mapper->GetOutputPort());
exporter->GetDataDimensions(dims.data());

output_data.resize(exporter->GetDataMemorySize() / sizeof(BGRA32));
exporter->Export(output_data.data());

```

I can do a manual byte swap on the exported data afterwards:

```
for (auto& pixel : output_data) {
    std::swap(pixel.b, pixel.r);
}

```

But it’s rather slow to iterate over the whole image once again after the fact. Can VTK itself do this color space conversion somehow?

(Interestingly, none of the `SetOutputFormatTo*()` methods in `vtkImageMapToColors` seems to have any effect on the output, I’m always getting RGBA, even with `SetOutputFormatToLuminance()`, for example.)

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 20, 2021, 6:48pm UTC](https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832/2 "2021-05-20T18:48:16Z")

</div>

The vtkImageMapToColors class only performs a mapping if it is given a lookup table. In the absence of a lookup table, it just passes the input to the output with no modification. So this class is of no use for converting from one color format to another color format (unless the original color format is greyscale).

---

<div class="post-metadata">

### Author: ![Nmlgc](https://discourse.vtk.org/user_avatar/discourse.vtk.org/nmlgc/32/3435_2.png) [@Nmlgc](https://discourse.vtk.org/u/Nmlgc)
#### Post date: [May 21, 2021, 12:02pm UTC](https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832/3 "2021-05-21T12:02:15Z")

</div>

So what can I use instead?

---

<div class="post-metadata">

### Author: ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)
#### Post date: [May 21, 2021, 12:58pm UTC](https://discourse.vtk.org/t/changing-color-space-of-vtkalgorithmoutput-to-bgr-for-vtkimageexport/5832/4 "2021-05-21T12:58:03Z")

</div>

There is no direct support for BGRA in VTK and the only place RGBA to BGRA mappings occur are in a couple I/O classes where is done with similar efficiency and simplicity as your swap() code.

So to improve efficiency, I see some possible approaches, in order of complexity:

1. use vtkSMPTools to accelerate your current swap() approach via multithreading, or
2. derive a subclass of vtkImageMapToColors that performs a swap after the color mapping (thereby taking advantage of the multithreading already performed by vtkImageMapToColors), or
3. derive a subclass of vtkScalarsToColors (which is the base class of vtkLookupTable) that directly produces BGRA instead of RGBA.

The complexity of solution (3) is far greater than the complexity of (1) and (2), so even though it is a viable solution I would not recommend it.

Getting back to a comment that I gave in my previous reply, when you use vtkImageMapToColors, since you want it to always produce 4-bytes-per-color as output (regardless of input), you _must_ provide it with a LUT that is set up as follows:

```auto
auto mapper = vtkSmartPointer<vtkImageMapToColors>::New();
auto lut = vtkSmartPointer<vtkScalarsToColors>::New();

lut->SetVectorModeToRGBColors(); // how to deal with multi-component input
mapper->SetOutputFormatToRGBA(); // this is the default, of course
mapper->SetLookupTable(lut);

```

For data that isn’t in the range [0,255] it is also necessary to use `lut->SetRange(low, high)` so that the values will be scaled appropriately.
