Spout / OpenGL GL_RGBA to QImage
So I'm using the spout library to receive OpenGL texture data (GL_RGBA is the OpenGL format of the data) in the form of an unsigned char array. The library function takes an unsigned char* and updates the data it points to.
I'm having a difficult time creating a QImage using this pointer. RGBA8888 is the equivalent QImage format (this is confirmed by creating an empty QImage and passing its .bits() pointer to the library method).
This is pretty much what I do for the empty data:
unsigned char* data; data = (unsigned char*)malloc(4*width*height*sizeof(unsigned char));
If I understand correctly, this creates enough space for pixels of RGBA 8-8-8-8 format.
Once I pass the pointer to the library method, I try to recreate the QImage:
img = QImage((uchar*)(data),width,height,QImage::Format_RGBA8888);
Width and height are updated correctly prior to the data being updated so its not a size issue.
Any ideas of what I'm doing wrong? My 3 brain cells don't know what to do.
Thanks a bunch
the only two things that immediately come to mind, neither of which solidly explains what you are seeing:
- don't use malloc() in c++
- make sure that the data buffer remains valid during the lifetime of the qimage. deleting the data pointer while the qimage is alive will invalidate the qimage and is in error.
Thanks for the reply @Kent-Dorfman. I've used 'new' to allocate memory with the same results but I have read elsewhere also to stay away from malloc() C++. Is there a particular reason for this? Is it because its more of a C method which may interfere with newer versions of C++?
I don't delete the buffer any time, I do use realloc to update its size though. I'll try again using new to see what that does.
I've heard that in some cases you have to set up a 'color map' but it looks like that's for grayscale mainly.
Any ideas of what I'm doing wrong?
From your description I don't understand where you are failing. What exactly isn't working? The image is not displaying? What does it show? If the memory of the image was uninitialized it might show random colors for pixels or it might be black/blank.
@fcarney Yes, I do see random colors on a mostly black / blank image. Then it sounds like malloc is not initializing memory correctly?
@rtavakko Well, I don't think malloc initializes anything. However, this tells me that whatever is copying anything into that memory is copying uninitialized ram into that memory or is not copying, thus no image. What function is supposed to fill that memory? It sounds like it is creating the image just fine, with no data.
@fcarney Its a spout function that receives image data in the form of an OpenGL texture. I tried using 'new' to initialize the data array and it seems to be working good so far, I think before when I used 'new' I used just width*height for size which explains why I got crashes. Now with the correct size it seems ok. I'll post more details for future viewers shortly.
which explains why I got crashes
You should be more precise when asking a question. First you said you see blank/black image, now you say your app was crashing...
@jsulm Yes. I have had both. Mostly a blank image. I didn't really recall what caused what but it seems that memory was not being allocated correctly.
Just to sum up this thread for anyone else having this issue, this would be the correct way to do it:
QImage img; unsigned char* data; data = new unsigned char[4*width*height*sizeof(unsigned char)]; img = QImage((uchar*)(data),width,height,QImage::Format_RGBA8888);
Make sure you allocate the appropriate space needed. E.g. if image format is RGB, size of the data array will be 3 * width * height * sizeof(unsigned char) and the QImage format will be QImage::Format_RGB888.
Thanks all for your help.