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. Create QImage from an unsigned char array

Create QImage from an unsigned char array

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 9.1k 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.
  • V Offline
    V Offline
    Vick
    wrote on 24 Jun 2016, 10:01 last edited by
    #1

    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 !

    R J 2 Replies Last reply 24 Jun 2016, 10:08
    0
    • V Vick
      24 Jun 2016, 10:01

      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 !

      R Offline
      R Offline
      raven-worx
      Moderators
      wrote on 24 Jun 2016, 10:08 last edited by raven-worx
      #2

      @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

      M 1 Reply Last reply 24 Jun 2016, 10:11
      2
      • R raven-worx
        24 Jun 2016, 10:08

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

        M Offline
        M Offline
        mrjj
        Lifetime Qt Champion
        wrote on 24 Jun 2016, 10:11 last edited by mrjj
        #3

        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
        4
        • V Offline
          V Offline
          Vick
          wrote on 24 Jun 2016, 10:12 last edited by
          #4

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

          M R 2 Replies Last reply 24 Jun 2016, 10:14
          0
          • V Vick
            24 Jun 2016, 10:12

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

            M Offline
            M Offline
            mrjj
            Lifetime Qt Champion
            wrote on 24 Jun 2016, 10:14 last edited by mrjj
            #5

            @Vick
            the sample gives this:

            1 Reply Last reply
            2
            • P Offline
              P Offline
              Photatum
              wrote on 24 Jun 2016, 10:14 last edited by Photatum
              #6

              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
              2
              • V Vick
                24 Jun 2016, 10:12

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

                R Offline
                R Offline
                raven-worx
                Moderators
                wrote on 24 Jun 2016, 10:19 last edited by raven-worx
                #7

                @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
                2
                • V Vick
                  24 Jun 2016, 10:01

                  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 !

                  J Offline
                  J Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 24 Jun 2016, 10:23 last edited by
                  #8

                  @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

                  V 1 Reply Last reply 24 Jun 2016, 10:35
                  3
                  • V Offline
                    V Offline
                    Vick
                    wrote on 24 Jun 2016, 10:26 last edited by
                    #9

                    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
                    0
                    • J jsulm
                      24 Jun 2016, 10:23

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

                      V Offline
                      V Offline
                      Vick
                      wrote on 24 Jun 2016, 10:35 last edited by
                      #10

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

                      4/10

                      24 Jun 2016, 10:12

                      • Login

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