Qt / C ++ - converting raw binary data and displaying it as images (i.e. QImage)

I have a C ++ Open GL application that displays an animation display and grabs the contents of the framebuffer using glReadPixels (), which is then stored as a 1D char array.

I can get the contents of the buffer and store it into a char array like below:

char * values = (char *) malloc(width * height * 4 * sizeof(char));
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, values);


Then I send this raw data over the network using a socket, which is not a problem for me. I have a Qt Desktop client (which communicates with an OpenGL application using a socket)

This client program can successfully receive the raw binary data sent by the Open GL application.

How can I render an image from this binary data in my client application? Does Qt have a built-in function / library for drawing an image from binary data (which is generated from glReadPixels)?

Do I need to encode / decode this raw binary data as base64 before sending it over sockets? Why or why not?


source to share

1 answer

QImage has a nice constructor that accepts an existing buffer, width, height, and pixel format . This looks like a great place to start.

And if you're creating a QImage on the server, you can use Qt's built-in streaming to send the QImage to your clients (QDataStreams can wrap almost any I / O device, including network sockets), so you don't have to do any encoding and decoding yourself - Qt will take care of everything for you.

Hope it helps!



All Articles