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
Forum Updated to NodeBB v4.3 + New Features

Create QImage from an unsigned char array

Scheduled Pinned Locked Moved Solved General and Desktop
10 Posts 5 Posters 9.1k Views 1 Watching
  • 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.
  • VickV Offline
    VickV Offline
    Vick
    wrote on 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 !

    raven-worxR jsulmJ 2 Replies Last reply
    0
    • VickV Vick

      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-worxR Offline
      raven-worxR Offline
      raven-worx
      Moderators
      wrote on 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

      mrjjM 1 Reply Last reply
      2
      • raven-worxR 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.

        mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on 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
        • VickV Offline
          VickV Offline
          Vick
          wrote on last edited by
          #4

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

          mrjjM raven-worxR 2 Replies Last reply
          0
          • VickV Vick

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

            mrjjM Offline
            mrjjM Offline
            mrjj
            Lifetime Qt Champion
            wrote on last edited by mrjj
            #5

            @Vick
            the sample gives this:

            1 Reply Last reply
            2
            • P Offline
              P Offline
              Photatum
              wrote on 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
              • VickV Vick

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

                raven-worxR Offline
                raven-worxR Offline
                raven-worx
                Moderators
                wrote on 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
                • VickV Vick

                  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 !

                  jsulmJ Offline
                  jsulmJ Offline
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

                  VickV 1 Reply Last reply
                  3
                  • VickV Offline
                    VickV Offline
                    Vick
                    wrote on 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
                    • jsulmJ jsulm

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

                      VickV Offline
                      VickV Offline
                      Vick
                      wrote on 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

                      • Login

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