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. Save QImage from BYTE buffer segfaults ?

Save QImage from BYTE buffer segfaults ?

Scheduled Pinned Locked Moved Solved General and Desktop
qimagebmpbytes
43 Posts 8 Posters 11.3k 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.
  • J Jarek B
    8 Jul 2020, 16:32

    From the documentation
    https://doc.qt.io/qt-5/qimage.html#QImage-4
    data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

    So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.

    R Offline
    R Offline
    R-P-H
    wrote on 8 Jul 2020, 16:48 last edited by
    #21

    @Jarek-B said in Save QImage from BYTE buffer segfaults ?:

    From the documentation
    https://doc.qt.io/qt-5/qimage.html#QImage-4
    data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

    So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.

    Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?

    M J 2 Replies Last reply 8 Jul 2020, 17:00
    0
    • R R-P-H
      8 Jul 2020, 16:48

      @Jarek-B said in Save QImage from BYTE buffer segfaults ?:

      From the documentation
      https://doc.qt.io/qt-5/qimage.html#QImage-4
      data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

      So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.

      Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?

      M Offline
      M Offline
      mranger90
      wrote on 8 Jul 2020, 17:00 last edited by mranger90 7 Aug 2020, 17:01
      #22

      @R-P-H Of course it's black, the data never gets initialized.
      Also, the reason why it doesn't crash is because:
      BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
      BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

      R 2 Replies Last reply 8 Jul 2020, 17:08
      0
      • M mranger90
        8 Jul 2020, 17:00

        @R-P-H Of course it's black, the data never gets initialized.
        Also, the reason why it doesn't crash is because:
        BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
        BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

        R Offline
        R Offline
        R-P-H
        wrote on 8 Jul 2020, 17:08 last edited by
        #23

        @mranger90 said in Save QImage from BYTE buffer segfaults ?:

        @R-P-H Of course it's black, the data never gets initialized.
        Also, the reason why it doesn't crash is because:
        BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
        BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

        I don't quite understand what you're saying to be honest. Where are you saying the issue is ?

        M 1 Reply Last reply 8 Jul 2020, 17:27
        0
        • R R-P-H
          8 Jul 2020, 17:08

          @mranger90 said in Save QImage from BYTE buffer segfaults ?:

          @R-P-H Of course it's black, the data never gets initialized.
          Also, the reason why it doesn't crash is because:
          BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
          BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

          I don't quite understand what you're saying to be honest. Where are you saying the issue is ?

          M Offline
          M Offline
          mranger90
          wrote on 8 Jul 2020, 17:27 last edited by
          #24

          @R-P-H here is the amended code

          // allocates a buffer of imWidth * imHeight bytes, which  depending on the compiler 
          // settings will either be initialized to zero, or may contain garbage
          BYTE *buf = new BYTE[imWidth * imHeight];
          
          // creates a QImage based on the data in the buffer, 
          QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8);
          
          // saves the data
          img.save("image.bmp", "BMP");
          delete [] buf;
          
          

          The point is that you never put actual image data into the buffer.

          R 1 Reply Last reply 8 Jul 2020, 17:56
          0
          • M mranger90
            8 Jul 2020, 17:27

            @R-P-H here is the amended code

            // allocates a buffer of imWidth * imHeight bytes, which  depending on the compiler 
            // settings will either be initialized to zero, or may contain garbage
            BYTE *buf = new BYTE[imWidth * imHeight];
            
            // creates a QImage based on the data in the buffer, 
            QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8);
            
            // saves the data
            img.save("image.bmp", "BMP");
            delete [] buf;
            
            

            The point is that you never put actual image data into the buffer.

            R Offline
            R Offline
            R-P-H
            wrote on 8 Jul 2020, 17:56 last edited by R-P-H 7 Aug 2020, 17:59
            #25

            @mranger90 said in Save QImage from BYTE buffer segfaults ?:

            @R-P-H here is the amended code

            // allocates a buffer of imWidth * imHeight bytes, which  depending on the compiler 
            // settings will either be initialized to zero, or may contain garbage
            BYTE *buf = new BYTE[imWidth * imHeight];
            
            // creates a QImage based on the data in the buffer, 
            QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8);
            
            // saves the data
            img.save("image.bmp", "BMP");
            delete [] buf;
            
            

            The point is that you never put actual image data into the buffer.

            This is already what I did according to your first post. The buffer IS populated, I just didn't show that API call. I will modify my original post to make this clear...

            So the result is a completely black image. This may be normal but I will have to check with the API provider.

            EDIT: I have edited my original post.

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 8 Jul 2020, 20:48 last edited by
              #26

              Is that some private library you can't share ? Or would it be possible to have more information about that API ?

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              R 1 Reply Last reply 9 Jul 2020, 09:46
              0
              • S SGaist
                8 Jul 2020, 20:48

                Is that some private library you can't share ? Or would it be possible to have more information about that API ?

                R Offline
                R Offline
                R-P-H
                wrote on 9 Jul 2020, 09:46 last edited by
                #27

                @SGaist said in Save QImage from BYTE buffer segfaults ?:

                Is that some private library you can't share ? Or would it be possible to have more information about that API ?

                Your assumption is correct. There really aren't any additional details on using this API function. It simply takes as input a reference to the buffer and a unique id of the capturing device. It then populates the buffer in place. The example in the documentation initializes the buffer the same way as shown in my original post.

                If we are sure the error is on the API side, then they will need to fix that.

                I made the assumption that the error was in saving the image to disk.

                1 Reply Last reply
                0
                • S Offline
                  S Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on 9 Jul 2020, 11:15 last edited by
                  #28

                  Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?

                  Is it the Windows BYTE type ?

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  R 1 Reply Last reply 9 Jul 2020, 15:40
                  0
                  • R R-P-H
                    8 Jul 2020, 16:48

                    @Jarek-B said in Save QImage from BYTE buffer segfaults ?:

                    From the documentation
                    https://doc.qt.io/qt-5/qimage.html#QImage-4
                    data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.

                    So (as far as I understand) that is not enough to have memory buffer aligned to 32 bit address, also each line have to be aligned to 32 bit address. So if you have image that is 260x300 pixels, a whole line will take 288 bytes. So for whole image you should have 288*300 bytes which is more than you have provided.

                    Hi, interesting...I have not seen any examples like that. How are you arriving at 288 bytes ? Are you suggesting I make the buffer 288x300 bytes instead ?

                    J Offline
                    J Offline
                    Jarek B
                    wrote on 9 Jul 2020, 11:51 last edited by Jarek B 7 Sept 2020, 11:52
                    #29

                    @R-P-H

                    Just round up space that you need for one line

                    280 / 32 = 8.75
                    9 * 32 = 288

                    R 1 Reply Last reply 9 Jul 2020, 15:37
                    0
                    • J Jarek B
                      9 Jul 2020, 11:51

                      @R-P-H

                      Just round up space that you need for one line

                      280 / 32 = 8.75
                      9 * 32 = 288

                      R Offline
                      R Offline
                      R-P-H
                      wrote on 9 Jul 2020, 15:37 last edited by
                      #30

                      @Jarek-B said in Save QImage from BYTE buffer segfaults ?:

                      @R-P-H

                      Just round up space that you need for one line

                      280 / 32 = 8.75
                      9 * 32 = 288

                      I tried: BYTE *buf = new BYTE(288 * 300); Result is exactly the same. Still crashes on occasion.

                      1 Reply Last reply
                      0
                      • S SGaist
                        9 Jul 2020, 11:15

                        Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?

                        Is it the Windows BYTE type ?

                        R Offline
                        R Offline
                        R-P-H
                        wrote on 9 Jul 2020, 15:40 last edited by
                        #31

                        @SGaist said in Save QImage from BYTE buffer segfaults ?:

                        Then are you sure their BYTE type is not something custom ? Do they also show how to access the data ?

                        Is it the Windows BYTE type ?

                        They do not show how to access the data afterwards.

                        It's defined as typedef unsigned char BYTE; from windows.h.

                        1 Reply Last reply
                        0
                        • M mranger90
                          8 Jul 2020, 17:00

                          @R-P-H Of course it's black, the data never gets initialized.
                          Also, the reason why it doesn't crash is because:
                          BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
                          BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

                          R Offline
                          R Offline
                          R-P-H
                          wrote on 9 Jul 2020, 15:41 last edited by
                          #32

                          @mranger90 said in Save QImage from BYTE buffer segfaults ?:

                          @R-P-H Of course it's black, the data never gets initialized.
                          Also, the reason why it doesn't crash is because:
                          BYTE *buf = new BYTE(x * y); // allocates a single byte, initialized to the value of (x * y)
                          BYTE *buf = new BYTE[x * y]; // allocates an array of bytes whose size is x * y

                          The image is always black and it's not supposed to be, so this doesn't work.

                          1 Reply Last reply
                          0
                          • hskoglundH Online
                            hskoglundH Online
                            hskoglund
                            wrote on 9 Jul 2020, 15:44 last edited by hskoglund 7 Sept 2020, 15:51
                            #33

                            Hi, you could try explicitly specify the number of bytes per line:
                            Edit: also, it could be that your deleting the buffer before the QImage is deleted:

                            BYTE *buf = new BYTE(imWidth * imHeight);
                            //Populate buf with data using API call here (not shown)
                            {
                                QImage img(buf, imWidth, imHeight, imWidth, QImage::Format_Grayscale8);
                                img.save("image.bmp", "BMP");
                            }
                            delete [] buf;
                            
                            R 1 Reply Last reply 9 Jul 2020, 16:20
                            0
                            • hskoglundH hskoglund
                              9 Jul 2020, 15:44

                              Hi, you could try explicitly specify the number of bytes per line:
                              Edit: also, it could be that your deleting the buffer before the QImage is deleted:

                              BYTE *buf = new BYTE(imWidth * imHeight);
                              //Populate buf with data using API call here (not shown)
                              {
                                  QImage img(buf, imWidth, imHeight, imWidth, QImage::Format_Grayscale8);
                                  img.save("image.bmp", "BMP");
                              }
                              delete [] buf;
                              
                              R Offline
                              R Offline
                              R-P-H
                              wrote on 9 Jul 2020, 16:20 last edited by
                              #34

                              @hskoglund said in Save QImage from BYTE buffer segfaults ?:

                              Hi, you could try explicitly specify the number of bytes per line:
                              Edit: also, it could be that your deleting the buffer before the QImage is deleted:

                              BYTE *buf = new BYTE(imWidth * imHeight);
                              //Populate buf with data using API call here (not shown)
                              {
                                  QImage img(buf, imWidth, imHeight, imWidth, QImage::Format_Grayscale8);
                                  img.save("image.bmp", "BMP");
                              }
                              delete [] buf;
                              

                              Hi, thanks for the suggestion. I tried it and result is the same...still crashes on occasion.

                              I also tried with removing the delete [] buf line and still the same.

                              B 1 Reply Last reply 9 Jul 2020, 16:35
                              0
                              • R R-P-H
                                9 Jul 2020, 16:20

                                @hskoglund said in Save QImage from BYTE buffer segfaults ?:

                                Hi, you could try explicitly specify the number of bytes per line:
                                Edit: also, it could be that your deleting the buffer before the QImage is deleted:

                                BYTE *buf = new BYTE(imWidth * imHeight);
                                //Populate buf with data using API call here (not shown)
                                {
                                    QImage img(buf, imWidth, imHeight, imWidth, QImage::Format_Grayscale8);
                                    img.save("image.bmp", "BMP");
                                }
                                delete [] buf;
                                

                                Hi, thanks for the suggestion. I tried it and result is the same...still crashes on occasion.

                                I also tried with removing the delete [] buf line and still the same.

                                B Offline
                                B Offline
                                Bonnie
                                wrote on 9 Jul 2020, 16:35 last edited by
                                #35

                                @R-P-H
                                If your BYTE means unsigned char, then the expression of new BYTE(imWidth * imHeight) is definitely wrong.
                                As @mranger90 says, that really means you allocate a buf of only one byte size, whose value is (imWidth * imHeight).
                                So I won't be surprised that it crashes.

                                R 1 Reply Last reply 9 Jul 2020, 16:48
                                0
                                • B Bonnie
                                  9 Jul 2020, 16:35

                                  @R-P-H
                                  If your BYTE means unsigned char, then the expression of new BYTE(imWidth * imHeight) is definitely wrong.
                                  As @mranger90 says, that really means you allocate a buf of only one byte size, whose value is (imWidth * imHeight).
                                  So I won't be surprised that it crashes.

                                  R Offline
                                  R Offline
                                  R-P-H
                                  wrote on 9 Jul 2020, 16:48 last edited by
                                  #36

                                  @Bonnie said in Save QImage from BYTE buffer segfaults ?:

                                  @R-P-H
                                  If your BYTE means unsigned char, then the expression of new BYTE(imWidth * imHeight) is definitely wrong.
                                  As @mranger90 says, that really means you allocate a buf of only one byte size, whose value is (imWidth * imHeight).
                                  So I won't be surprised that it crashes.

                                  I tried with new BYTE[imWidth * imHeight] as well but the image is always black.

                                  B 1 Reply Last reply 9 Jul 2020, 16:59
                                  0
                                  • R R-P-H
                                    9 Jul 2020, 16:48

                                    @Bonnie said in Save QImage from BYTE buffer segfaults ?:

                                    @R-P-H
                                    If your BYTE means unsigned char, then the expression of new BYTE(imWidth * imHeight) is definitely wrong.
                                    As @mranger90 says, that really means you allocate a buf of only one byte size, whose value is (imWidth * imHeight).
                                    So I won't be surprised that it crashes.

                                    I tried with new BYTE[imWidth * imHeight] as well but the image is always black.

                                    B Offline
                                    B Offline
                                    Bonnie
                                    wrote on 9 Jul 2020, 16:59 last edited by Bonnie 7 Sept 2020, 17:12
                                    #37

                                    @R-P-H
                                    Right, this is another problem.
                                    How can you be sure your populated data is not all black?
                                    I will suggest you do some tests without Qt classes.

                                    BYTE *buf = new BYTE[imWidth * imHeight];
                                    //Populate buf with data using API call here (not shown)
                                    bool AllZero = true;
                                    for(int i = 0; i < imWidth * imHeight; i++) {
                                        if(buf[i] != 0)
                                            AllZero = false;
                                    }
                                    qDebug() << "AllZero :" << AllZero;
                                    delete [] buf;
                                    

                                    Or you can just print all of the data to see whether it is all zero(black).

                                    R 1 Reply Last reply 9 Jul 2020, 21:09
                                    0
                                    • B Bonnie
                                      9 Jul 2020, 16:59

                                      @R-P-H
                                      Right, this is another problem.
                                      How can you be sure your populated data is not all black?
                                      I will suggest you do some tests without Qt classes.

                                      BYTE *buf = new BYTE[imWidth * imHeight];
                                      //Populate buf with data using API call here (not shown)
                                      bool AllZero = true;
                                      for(int i = 0; i < imWidth * imHeight; i++) {
                                          if(buf[i] != 0)
                                              AllZero = false;
                                      }
                                      qDebug() << "AllZero :" << AllZero;
                                      delete [] buf;
                                      

                                      Or you can just print all of the data to see whether it is all zero(black).

                                      R Offline
                                      R Offline
                                      R-P-H
                                      wrote on 9 Jul 2020, 21:09 last edited by R-P-H 7 Sept 2020, 22:38
                                      #38

                                      @Bonnie said in Save QImage from BYTE buffer segfaults ?:

                                      @R-P-H
                                      Right, this is another problem.
                                      How can you be sure your populated data is not all black?
                                      I will suggest you do some tests without Qt classes.

                                      BYTE *buf = new BYTE[imWidth * imHeight];
                                      //Populate buf with data using API call here (not shown)
                                      bool AllZero = true;
                                      for(int i = 0; i < imWidth * imHeight; i++) {
                                          if(buf[i] != 0)
                                              AllZero = false;
                                      }
                                      qDebug() << "AllZero :" << AllZero;
                                      delete [] buf;
                                      

                                      Or you can just print all of the data to see whether it is all zero(black).

                                      Yes it's all black everytime.

                                      1 Reply Last reply
                                      0
                                      • R Offline
                                        R Offline
                                        R-P-H
                                        wrote on 9 Jul 2020, 23:09 last edited by
                                        #39

                                        I would like to try save the buffer to a
                                        BMP image using only standard C++ just to check that it's not a problem with using QImage. But I haven't been able to get that working yet...

                                        1 Reply Last reply
                                        0
                                        • hskoglundH Online
                                          hskoglundH Online
                                          hskoglund
                                          wrote on 9 Jul 2020, 23:49 last edited by
                                          #40

                                          QImage is quite reliable, for example if I add just 2 for loops to change the pixels to something not black:

                                          int imWidth = 260;
                                          int imHeight = 300;
                                          
                                          typedef unsigned char BYTE;
                                          BYTE *buf = new BYTE[imWidth * imHeight];
                                          
                                          for (int y = 0; (y < imHeight); ++y)
                                              for (int x = 0; (x < imWidth); ++x)
                                                  buf[x + y * imWidth] = x*x + y*y;
                                          
                                          QImage img(buf, imWidth, imHeight, QImage::Format_Grayscale8);
                                          
                                          img.save("image.bmp", "BMP");
                                          delete [] buf;
                                          

                                          I get:
                                          Screenshot 2020-07-10 at 01.49.14.png

                                          R 1 Reply Last reply 10 Jul 2020, 08:33
                                          1

                                          21/43

                                          8 Jul 2020, 16:48

                                          • Login

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