CImg does not store pixels in the interleaved format


Took me hours before I found and read the documentation.

CImg stores pixels in a planer format (RRRR…..GGGG…..BBBB). For most tasks in CUDA, it’s much better to store the pixel values in the interleaved format (RGBRGBRGB……).

In order to do that, just call the permute_axes method of the CImg object:

CImg image("image.jpg");


After permutation, the width, height,  spectrum and depth values that are reported for CImg will all change.  To permute back (for displaying or saving) do this:

CImg result(dataPointer, spectrum, width, height, depth, false);


Where the values, are previously saved values (before doing any kind of permutation on the axes). This will undo any changes and now you can safely save the image or display it.

CImg instance from interleaved array (bitmap):

Now imagine you want to initialize a CImg object with an interleaved bitmap (say an OpenGL texture or what have you). In this case, you need to know the width and height of the image as well as the number of components. Also imagine that the spectrum is 1. To create a CImg object using this array you can do ( imageArray is the bitmap pointer):

cimg_library::CImg<unsigned char> result(imageArray, numChannels, imageWidth, imageHeight, 1, true);


1 comment

1 ping

    • Bill Tschumy on March 17, 2017 at 3:15 PM
    • Reply

    If I have a data buffer that has interleaved RGBA data, would I use the same:


    I don’t have a good mental model of what this string does. How is this converting from RGB (or RGBA) to planar data?


  1. […] input arrays (they need to be interleaved). If you are planning on using CImg with NPP, be sure to check this post out before attempting to do so. Failing to permute CImg image axes will result in wrong filtered values […]

Leave a Reply

Your email address will not be published.