Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

    Solved Create QImage from an unsigned char array

    General and Desktop
    5
    10
    7611
    Loading More Posts
    • 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.
    • Vick
      Vick last edited by

      Hi,

      I would like to print a picture from an unsigned char array. I tried to use QImage but I don't think I use it properly, the GUI always shows me a grey picture.

      Here is my sample code :

      int main(int argc, char *argv[])
      {
          QApplication a(argc, argv);
      
          QLabel l;
          QImage i;
      
          unsigned char* data = (unsigned char*) malloc (1024*768*sizeof(unsigned char));
      
          for (int i = 0; i < 1024; i++)
          {
              for (int j = 0; j < 768; j++)
              {
                  data[768 * i + j] = 255;
              }
          }
      
          free(data);
      
          i = QImage(data, 768, 1024, QImage::Format_Indexed8);
          l.setPixmap(QPixmap::fromImage(i));
          l.show();
      
          return a.exec();
      }
      

      How would you do to get a correct picture ? Thanks for your help !

      raven-worx jsulm 2 Replies Last reply Reply Quote 0
      • raven-worx
        raven-worx Moderators @Vick last edited by raven-worx

        @Vick said:

        I tried to use QImage but I don't think I use it properly, the GUI always shows me a grey picture.

        You just fill the data with the value 255?! If i would have to guess it seems like it's grey, because you monitor needs some calibration maybe. I would expect it to be white.

        Also you mixed the height and width in this line.

        i = QImage(data, 768, 1024, QImage::Format_Indexed8);
        

        But it's not noticeable yet, since the data is all filled with the same value.

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        mrjj 1 Reply Last reply Reply Quote 2
        • mrjj
          mrjj Lifetime Qt Champion @raven-worx last edited by mrjj

          hi
          You might need a ColorTable?

          void MainWindow::paintEvent(QPaintEvent* e) {
            uchar imagio [500][500];
            for (int i = 0; i < 500; i++)
              for (int j = 0; j < 500; j++)
                imagio[i][j] = qrand() % 256;
            QVector<QRgb> colorTable;
            for (int i = 0; i < 256; i++)
              colorTable.push_back(QColor(qrand() % 256, qrand() % 256, qrand() % 256).rgb());
            QImage image((uchar*)imagio, 500, 500, 500, QImage::Format_Indexed8);
            image.setColorTable(colorTable);
            QPainter p(this);
            p.drawImage(QPoint(0, 0), image);
          }
          
          1 Reply Last reply Reply Quote 3
          • Vick
            Vick last edited by

            Yes, but I tried other values like 0, i and it does not change anything... Always getting this grey picture !

            mrjj raven-worx 2 Replies Last reply Reply Quote 0
            • mrjj
              mrjj Lifetime Qt Champion @Vick last edited by mrjj

              @Vick
              the sample gives this:

              1 Reply Last reply Reply Quote 2
              • P
                Photatum last edited by Photatum

                If you just want grayscale you can also use Format_Grayscale8 instead of Format_Indexed8.
                ...
                Just to expand my comment, like the other two said you need a color table for indexed format. Format_Grayscale8 would take care of that in this case and display the respective gray values.

                1 Reply Last reply Reply Quote 2
                • raven-worx
                  raven-worx Moderators @Vick last edited by raven-worx

                  @Vick said:

                  Yes, but I tried other values like 0, i and it does not change anything... Always getting this grey picture !

                  because @mrjj is correct.
                  The image format QImage::Format_Indexed8 needs a color map.
                  The way you want to use it is specifying the color for each pixel, so you need to use for example QImage::Format_ARGB32_Premultiplied.

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  1 Reply Last reply Reply Quote 2
                  • jsulm
                    jsulm Lifetime Qt Champion @Vick last edited by

                    @Vick You free the memory with your image data before you pass it to QImage. Why?

                    unsigned char* data = (unsigned char*) malloc (1024*768*sizeof(unsigned char));
                    
                        for (int i = 0; i < 1024; i++)
                        {
                            for (int j = 0; j < 768; j++)
                            {
                                data[768 * i + j] = 255;
                            }
                        }
                    
                        // Here you free the memory
                        free(data);
                    
                        i = QImage(data, 768, 1024, QImage::Format_Indexed8);
                    

                    In C++ you should not use malloc/free. Use new/delete[] instead. And in this case there is no need to allocate the memory on the heap.

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

                    Vick 1 Reply Last reply Reply Quote 3
                    • Vick
                      Vick last edited by

                      Hi all and many thanks for your replies. I think you pointed me on the fact that I should not use Format_Indexed8 to print only grayscale !

                      Thanks for the idea of ColorTable, I will use it when I need to display colors.

                      1 Reply Last reply Reply Quote 0
                      • Vick
                        Vick @jsulm last edited by

                        @jsulm

                        Thanks, it was a mistake to free the memory at this point ! Sorry for opening a thread for so little interest...

                        1 Reply Last reply Reply Quote 0
                        • First post
                          Last post