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

Save QImage from BYTE buffer segfaults ?

Scheduled Pinned Locked Moved Solved General and Desktop
qimagebmpbytes
43 Posts 8 Posters 12.1k Views 4 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.
  • Kent-DorfmanK Kent-Dorfman

    FWIW, you cannot make the absolute assumption that buf will be 32bit word aligned. If I had to guess, that would be my guess as to why it crashes sometimes. The Qt docs ae specific about the need for buf to be 32bit word aligned.

    R Offline
    R Offline
    R-P-H
    wrote on last edited by
    #5

    @Kent-Dorfman said in Save QImage from BYTE buffer segfaults ?:

    FWIW, you cannot make the absolute assumption that buf will be 32bit word aligned. If I had to guess, that would be my guess as to why it crashes sometimes. The Qt docs ae specific about the need for buf to be 32bit word aligned.

    Yes I saw that in the documentation. I assumed that if buffer is being populated the same way each time and that if it works sometimes, then it should be fine.

    Does Qt have any other way to get around this besides using another image processing library to save the image ?

    Christian EhrlicherC 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #6

      What are you using to fill that buffer ?
      Where do these data come from ?

      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
      0
      • SGaistS SGaist

        What are you using to fill that buffer ?
        Where do these data come from ?

        R Offline
        R Offline
        R-P-H
        wrote on last edited by
        #7

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

        What are you using to fill that buffer ?
        Where do these data come from ?

        It's populated using an API call which finishes successfully. So I can only interrogate the validity of the data after its completed.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #8

          That's a bit short on details. An API call can be a method from a library your are using or a remote call to a REST service.

          What kind of image are you getting ?

          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
          0
          • R R-P-H

            @Kent-Dorfman said in Save QImage from BYTE buffer segfaults ?:

            FWIW, you cannot make the absolute assumption that buf will be 32bit word aligned. If I had to guess, that would be my guess as to why it crashes sometimes. The Qt docs ae specific about the need for buf to be 32bit word aligned.

            Yes I saw that in the documentation. I assumed that if buffer is being populated the same way each time and that if it works sometimes, then it should be fine.

            Does Qt have any other way to get around this besides using another image processing library to save the image ?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #9

            @R-P-H said in Save QImage from BYTE buffer segfaults ?:

            I assumed that if buffer is being populated the same way each time and that if it works sometimes, then it should be fine.

            Even if it does it would depend on your image size (or better: bytes per line). If it's a multiple of 4 it does not crash, if it's something else the alignment is wrong.

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            R 1 Reply Last reply
            2
            • SGaistS SGaist

              That's a bit short on details. An API call can be a method from a library your are using or a remote call to a REST service.

              What kind of image are you getting ?

              R Offline
              R Offline
              R-P-H
              wrote on last edited by
              #10

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

              That's a bit short on details. An API call can be a method from a library your are using or a remote call to a REST service.

              What kind of image are you getting ?

              It's from a library. The API reference is very short on details. It's a 256 gray-level image.

              1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @R-P-H said in Save QImage from BYTE buffer segfaults ?:

                I assumed that if buffer is being populated the same way each time and that if it works sometimes, then it should be fine.

                Even if it does it would depend on your image size (or better: bytes per line). If it's a multiple of 4 it does not crash, if it's something else the alignment is wrong.

                R Offline
                R Offline
                R-P-H
                wrote on last edited by
                #11

                @Christian-Ehrlicher said in Save QImage from BYTE buffer segfaults ?:

                @R-P-H said in Save QImage from BYTE buffer segfaults ?:

                I assumed that if buffer is being populated the same way each time and that if it works sometimes, then it should be fine.

                Even if it does it would depend on your image size (or better: bytes per line). If it's a multiple of 4 it does not crash, if it's something else the alignment is wrong.

                The image size is always the same (260 x 300 px).

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  And one byte per pixel ?

                  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
                  0
                  • SGaistS SGaist

                    And one byte per pixel ?

                    R Offline
                    R Offline
                    R-P-H
                    wrote on last edited by
                    #13

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

                    And one byte per pixel ?

                    That is correct yes.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Can you get the stack trace of the crash ?

                      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
                      0
                      • SGaistS SGaist

                        Can you get the stack trace of the crash ?

                        R Offline
                        R Offline
                        R-P-H
                        wrote on last edited by
                        #15

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

                        Can you get the stack trace of the crash ?

                        I'm testing the application on another machine using the executable. I don't think I'd be able to get it without QT or another IDE right ?

                        1 Reply Last reply
                        0
                        • SGaistS Offline
                          SGaistS Offline
                          SGaist
                          Lifetime Qt Champion
                          wrote on last edited by
                          #16

                          Windows machine ?

                          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
                          0
                          • SGaistS SGaist

                            Windows machine ?

                            R Offline
                            R Offline
                            R-P-H
                            wrote on last edited by
                            #17

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

                            Windows machine ?

                            Yes it is. Windows 7 and also Windows 10.

                            1 Reply Last reply
                            0
                            • mranger90M Offline
                              mranger90M Offline
                              mranger90
                              wrote on last edited by
                              #18

                              try:
                              BYTE *buf = new BYTE[imWidth * imHeight];

                              R 1 Reply Last reply
                              1
                              • mranger90M mranger90

                                try:
                                BYTE *buf = new BYTE[imWidth * imHeight];

                                R Offline
                                R Offline
                                R-P-H
                                wrote on last edited by
                                #19

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

                                try:
                                BYTE *buf = new BYTE[imWidth * imHeight];

                                Hi, it is not crashing anymore, however the image seems to be coming out completely black. I will confirm tomorrow if the image is supposed to be completely black or not...

                                1 Reply Last reply
                                0
                                • J Offline
                                  J Offline
                                  Jarek B
                                  wrote on last edited by
                                  #20

                                  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 1 Reply Last reply
                                  2
                                  • J Jarek B

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

                                    mranger90M J 2 Replies Last reply
                                    0
                                    • R R-P-H

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

                                      mranger90M Offline
                                      mranger90M Offline
                                      mranger90
                                      wrote on last edited by mranger90
                                      #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
                                      0
                                      • mranger90M mranger90

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

                                        mranger90M 1 Reply Last reply
                                        0
                                        • R R-P-H

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

                                          mranger90M Offline
                                          mranger90M Offline
                                          mranger90
                                          wrote on 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
                                          0

                                          • Login

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