How to "convert" an ofTexture to `const unsigned char* data` for ImGui?


I am stuck trying to feed a method that is an ImGui widget (image inspector).
The method to feed the image must receive a texture,
but as a pointer: (memory position of the first pixel and the size)

const unsigned char*

The thing is that I am trying with many workarounds to convert/cast the ofTexture, ofImage or even ofPixels to something like the “raw data”…

I am looking into the OF examples, like gl\textureBufferInstancedExample and others, also ofBuffer and ofBufferObject and ofTexture methods but no success.

If I don’t misunderstand, C++ apps/classes commonly passes a pointer to the image to be drawn, analyzed or whatever.

Here’s some code:


ofxImGui::Gui gui;

ofTexture textureSource;
GLuint textureSourceID;

float width = 200;
float height = 200;



// setup
textureSourceID = gui.loadTexture(textureSource, "image.jpg");

// draw image
ImGui::Image((ImTextureID)(uintptr_t)textureSourceID, ImVec2(width, height));

// -> ??
// how to get pointer, to cast or to convert into:
const unsigned char* data = ...
ImageInspect::inspect(w, h, data, mouseUVCoord, displayedTextureSize);


// custom image widget	
inline void inspect(const int width,
	const int height,
	const unsigned char* const data,
	ImVec2 mouseUVCoord,
	ImVec2 displayedTextureSize)

That’s the widget that I am trying to use:

Image of imgInspect

What I want to do is to get the color of the image pixels.

Any help around?

Hey @moebiussurfing! ofPixels::getData() will return a const unsigned char* type. Would something like the following work OK?
in pseudocode:

    ofTexture textureSource;
    ofPixels pixels;
    // when you need the pointer:
    const unsigned char* data = pixels.getData();
    ImageInspect::inspect(w, h, data, mouseUVCoord, displayedTextureSize);

Thanks a lot, it worked.

1 Like

… BTW, the only bad side is that must correlate the pixel format with the widget.
In this case the widget seams that works only with RGBA instead of RGB.
So the image/ofPixels/ofTexture it should be converted before to that format…
or to modify the widget to remove alpha.

1 Like