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. Another QImage::bits() question
QtWS25 Last Chance

Another QImage::bits() question

Scheduled Pinned Locked Moved Unsolved General and Desktop
8 Posts 3 Posters 413 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.
  • P Offline
    P Offline
    Perdrix
    wrote on 4 Sept 2024, 10:46 last edited by
    #1

    I have some image data that is held in a private format that I want to copy into a QImage. So I would create the QImage thus:

    std::make_shared<QImage>(width, hight, QImage::Format_RGB32);
    

    and then call bits() to get the buffer location to which I then copy and reformat the image data.

    To display the image I call QPixmap::fromImage() which I feed to an image viewer widget. That part I know works.

    Lets say that some processing is then done on the private image data and the revised data is copied to the location that bits() initially returned, and QPixmap::fromImage() is called to display the current revision. This is would be repeated until the user was happy with the revised image.

    Will that work as I expect, or does calling (e.g.) QPixmap::fromImage() result in my working on a different image buffer from the one I think I am using?

    Thanks
    David

    1 Reply Last reply
    0
    • C Offline
      C Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 4 Sept 2024, 10:48 last edited by
      #2

      A call to QImage::bits() always detaches. And you must not store this pointer somewhere to access it later on as it might get a dangling pointer.

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

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Perdrix
        wrote on 4 Sept 2024, 11:50 last edited by Perdrix 9 Apr 2024, 11:53
        #3

        The only changes I make to the QImage are by writing direct to the buffer. So what could cause that pointer to dangle?? I'd very much prefer not to incur the large cost of a deep copy every time round that process. That's why I proposed caching the result of bits() even though that's not normal practice.

        I didn't use the fixed buffer ctor as I didn't want to have to worry about the bookkeeping I'd need to do if I used that. If using QImage::Format_RGB32, is the buffer size always sizeof(QRGB) * width * height?

        C 1 Reply Last reply 4 Sept 2024, 12:01
        0
        • P Perdrix
          4 Sept 2024, 11:50

          The only changes I make to the QImage are by writing direct to the buffer. So what could cause that pointer to dangle?? I'd very much prefer not to incur the large cost of a deep copy every time round that process. That's why I proposed caching the result of bits() even though that's not normal practice.

          I didn't use the fixed buffer ctor as I didn't want to have to worry about the bookkeeping I'd need to do if I used that. If using QImage::Format_RGB32, is the buffer size always sizeof(QRGB) * width * height?

          C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 4 Sept 2024, 12:01 last edited by
          #4

          @Perdrix said in Another QImage::bits() question:

          QImage::Format_RGB32, is the buffer size always sizeof(QRGB) * width * height?

          No, it's 4 byte per pixel internally with alpha = 255

          Don't rely on any internals, it might work today but not tomorrow. And storing QImage::bits() pointer is such an internal thing.
          Also there is no reaons that QPixmap::fromImage() does not detach (e.g. due to other internal image format, a write access by accident (= calling a non-const function), a programming error or whatever).

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

          1 Reply Last reply
          1
          • P Offline
            P Offline
            Perdrix
            wrote on 4 Sept 2024, 12:32 last edited by Perdrix 9 Apr 2024, 12:33
            #5

            Err sizeof(QRgb) IS four, my question stands - is sizeof(Qrgb) * w * h correct

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 4 Sept 2024, 12:56 last edited by
              #6

              Yes, 4 bytes.

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

              1 Reply Last reply
              0
              • C Offline
                C Offline
                ChrisW67
                wrote on 4 Sept 2024, 22:25 last edited by
                #7

                You might find this similar thread with similar behind-Qimage's-back-manipulations useful: How to make QImage aware of updated memory buffer?

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Perdrix
                  wrote on 5 Sept 2024, 02:13 last edited by
                  #8

                  @ChrisW67 Nice one - I like the use of QVector as a buffer

                  1 Reply Last reply
                  0

                  3/8

                  4 Sept 2024, 11:50

                  topic:navigator.unread, 5
                  • Login

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