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"); image.permute_axes("cxyz");
IMPORTANT:
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); result.permute_axes("yzcx");
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); result.permute_axes("yzcx");
1 comment
1 ping
If I have a data buffer that has interleaved RGBA data, would I use the same:
result.permute_axes(“yzcx”);
I don’t have a good mental model of what this string does. How is this converting from RGB (or RGBA) to planar data?
Thanks,
Bill
[…] 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 […]