Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Spout / OpenGL GL_RGBA to QImage

Spout / OpenGL GL_RGBA to QImage

Scheduled Pinned Locked Moved Solved General and Desktop
spoutopengl
10 Posts 4 Posters 1.8k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rtavakko
    wrote on last edited by rtavakko
    #1

    Hey guys,

    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

    1 Reply Last reply
    0
    • Kent-DorfmanK Offline
      Kent-DorfmanK Offline
      Kent-Dorfman
      wrote on last edited by
      #2

      the only two things that immediately come to mind, neither of which solidly explains what you are seeing:

      1. don't use malloc() in c++
      2. 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.
      1 Reply Last reply
      1
      • R Offline
        R Offline
        rtavakko
        wrote on last edited by
        #3

        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.

        1 Reply Last reply
        0
        • fcarneyF Offline
          fcarneyF Offline
          fcarney
          wrote on last edited by
          #4

          @rtavakko said in Spout / OpenGL GL_RGBA to QImage:

          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.

          C++ is a perfectly valid school of magic.

          R 1 Reply Last reply
          1
          • fcarneyF fcarney

            @rtavakko said in Spout / OpenGL GL_RGBA to QImage:

            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.

            R Offline
            R Offline
            rtavakko
            wrote on last edited by
            #5

            @fcarney Yes, I do see random colors on a mostly black / blank image. Then it sounds like malloc is not initializing memory correctly?

            fcarneyF 1 Reply Last reply
            0
            • R rtavakko

              @fcarney Yes, I do see random colors on a mostly black / blank image. Then it sounds like malloc is not initializing memory correctly?

              fcarneyF Offline
              fcarneyF Offline
              fcarney
              wrote on last edited by
              #6

              @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.

              C++ is a perfectly valid school of magic.

              R 1 Reply Last reply
              2
              • fcarneyF fcarney

                @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.

                R Offline
                R Offline
                rtavakko
                wrote on last edited by
                #7

                @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.

                jsulmJ 1 Reply Last reply
                0
                • R rtavakko

                  @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.

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  @rtavakko said in Spout / OpenGL GL_RGBA to QImage:

                  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...

                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply
                  0
                  • jsulmJ jsulm

                    @rtavakko said in Spout / OpenGL GL_RGBA to QImage:

                    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...

                    R Offline
                    R Offline
                    rtavakko
                    wrote on last edited by
                    #9

                    @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.

                    1 Reply Last reply
                    0
                    • R Offline
                      R Offline
                      rtavakko
                      wrote on last edited by rtavakko
                      #10

                      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.

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved