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. Dump QImage raw pixel data into `std::vector<char>`
Forum Updated to NodeBB v4.3 + New Features

Dump QImage raw pixel data into `std::vector<char>`

Scheduled Pinned Locked Moved Solved General and Desktop
qimagevector
48 Posts 5 Posters 24.2k 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.
  • C Offline
    C Offline
    Christian Ehrlicher
    Lifetime Qt Champion
    wrote on 5 Dec 2020, 22:02 last edited by
    #33

    @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

    std::vector<char> &fb

    Is this used in another thread?

    Otherwise I don't see anything obvious.

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

    A 1 Reply Last reply 5 Dec 2020, 22:17
    0
    • C Christian Ehrlicher
      5 Dec 2020, 22:02

      @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

      std::vector<char> &fb

      Is this used in another thread?

      Otherwise I don't see anything obvious.

      A Offline
      A Offline
      abmyii
      wrote on 5 Dec 2020, 22:17 last edited by
      #34

      @Christian-Ehrlicher I think the while loop is in another thread and VNC is accessing fb.data() throughout execution.

      C 1 Reply Last reply 5 Dec 2020, 22:35
      0
      • A abmyii
        5 Dec 2020, 22:17

        @Christian-Ehrlicher I think the while loop is in another thread and VNC is accessing fb.data() throughout execution.

        C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 5 Dec 2020, 22:35 last edited by
        #35

        @abmyii So you have your answer. Concurrent access to a memory region from two different threads.

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

        A 1 Reply Last reply 5 Dec 2020, 23:02
        1
        • C Christian Ehrlicher
          5 Dec 2020, 22:35

          @abmyii So you have your answer. Concurrent access to a memory region from two different threads.

          A Offline
          A Offline
          abmyii
          wrote on 5 Dec 2020, 23:02 last edited by abmyii 12 May 2020, 23:24
          #36

          @Christian-Ehrlicher Ah I see, that makes perfect sense now! Is there a simple solution to this? I tried the following which didn't work:

          std::vector<char> temp_fb(frame_size_bytes, 0);
          std::memcpy(temp_fb.data(), (const char *)image.constBits(), image.byteCount());
          fb = temp_fb
          
          1 Reply Last reply
          0
          • C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 6 Dec 2020, 08:09 last edited by
            #37

            Use mutexes or copy the data - I would suggest reading a little bit on how threading works. E.g. https://doc.qt.io/qt-5/threads-synchronizing.html - c++ uses the same mechanisms

            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
            2
            • A Offline
              A Offline
              abmyii
              wrote on 6 Dec 2020, 12:34 last edited by
              #38

              Whoops, I've been saying malloc (which is apparently thread-safe) all along when in fact the actual function I'm using is memcpy... Does that change things and/or can I use malloc instead?

              J 1 Reply Last reply 6 Dec 2020, 13:22
              0
              • A abmyii
                6 Dec 2020, 12:34

                Whoops, I've been saying malloc (which is apparently thread-safe) all along when in fact the actual function I'm using is memcpy... Does that change things and/or can I use malloc instead?

                J Offline
                J Offline
                JonB
                wrote on 6 Dec 2020, 13:22 last edited by
                #39

                @abmyii
                Hence what @Christian-Ehrlicher has been saying above. malloc and memcpy do totally different things, it makes no sense to ask whether one can be used in place of the other....

                A 1 Reply Last reply 6 Dec 2020, 14:49
                0
                • J JonB
                  6 Dec 2020, 13:22

                  @abmyii
                  Hence what @Christian-Ehrlicher has been saying above. malloc and memcpy do totally different things, it makes no sense to ask whether one can be used in place of the other....

                  A Offline
                  A Offline
                  abmyii
                  wrote on 6 Dec 2020, 14:49 last edited by abmyii 12 Jun 2020, 14:50
                  #40

                  @JonB I understand - I got confused due to my unfamiliarity with the functions but I never asked if I could use malloc instead of memcpy - I asked if I could use push_back instead of malloc (sic).

                  J 1 Reply Last reply 6 Dec 2020, 15:04
                  0
                  • A abmyii
                    6 Dec 2020, 14:49

                    @JonB I understand - I got confused due to my unfamiliarity with the functions but I never asked if I could use malloc instead of memcpy - I asked if I could use push_back instead of malloc (sic).

                    J Offline
                    J Offline
                    JonB
                    wrote on 6 Dec 2020, 15:04 last edited by JonB 12 Jun 2020, 15:06
                    #41

                    @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

                    I never asked if I could use malloc instead of memcpy [sic.]

                    in fact the actual function I'm using is memcpy... Does that change things and/or can I use malloc instead? [*sic.]

                    If you say so! I'll leave others to untangle, who clearly understand what you are saying better than I :) Don't worry about explaining to me.

                    A 1 Reply Last reply 6 Dec 2020, 15:09
                    0
                    • J JonB
                      6 Dec 2020, 15:04

                      @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

                      I never asked if I could use malloc instead of memcpy [sic.]

                      in fact the actual function I'm using is memcpy... Does that change things and/or can I use malloc instead? [*sic.]

                      If you say so! I'll leave others to untangle, who clearly understand what you are saying better than I :) Don't worry about explaining to me.

                      A Offline
                      A Offline
                      abmyii
                      wrote on 6 Dec 2020, 15:09 last edited by abmyii 12 Jun 2020, 15:10
                      #42

                      @JonB OK, you got me! Too much to keep track of...

                      I tried this and it worked, but is quite a lot slower than memcpy:

                      std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);
                      fb = vectorBuffer;
                      
                      J C 2 Replies Last reply 6 Dec 2020, 15:20
                      0
                      • A abmyii
                        6 Dec 2020, 15:09

                        @JonB OK, you got me! Too much to keep track of...

                        I tried this and it worked, but is quite a lot slower than memcpy:

                        std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);
                        fb = vectorBuffer;
                        
                        J Offline
                        J Offline
                        JonB
                        wrote on 6 Dec 2020, 15:20 last edited by
                        #43

                        @abmyii
                        I am lost in the complexities of your questions :) But I would suggest heeding @Christian-Ehrlicher's advice, he is usually right! He last offered:

                        Use mutexes or copy the data

                        So why not pursue that?

                        A 1 Reply Last reply 6 Dec 2020, 15:56
                        0
                        • A abmyii
                          6 Dec 2020, 15:09

                          @JonB OK, you got me! Too much to keep track of...

                          I tried this and it worked, but is quite a lot slower than memcpy:

                          std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);
                          fb = vectorBuffer;
                          
                          C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 6 Dec 2020, 15:29 last edited by
                          #44

                          @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

                          std::vector<unsigned char> vectorBuffer(charBuffer, charBuffer + length);
                          fb = vectorBuffer;

                          This does two memcpy for no good reason. Since I don't know what libvncserver function you use you're on your own. Read the documentation of them and also read what's needed for threadsafe access.

                          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
                          • J JonB
                            6 Dec 2020, 15:20

                            @abmyii
                            I am lost in the complexities of your questions :) But I would suggest heeding @Christian-Ehrlicher's advice, he is usually right! He last offered:

                            Use mutexes or copy the data

                            So why not pursue that?

                            A Offline
                            A Offline
                            abmyii
                            wrote on 6 Dec 2020, 15:56 last edited by
                            #45

                            @JonB Haha, you think you are the only one?!

                            I am planning to try the mutex idea - I haven't had a chance to look it up and figure out how to do it yet. A basic QMutex lock/unlock didn't fix it (though I probably applied it wrong)...

                            @Christian-Ehrlicher Ah, I see. What "libvncserver function" do you mean?

                            1 Reply Last reply
                            0
                            • C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 6 Dec 2020, 16:09 last edited by
                              #46

                              @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

                              What "libvncserver function" do you mean?

                              You are using libvncserver, not me.

                              Giving up here now.

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

                              A 1 Reply Last reply 6 Dec 2020, 21:37
                              0
                              • C Christian Ehrlicher
                                6 Dec 2020, 16:09

                                @abmyii said in Dump QImage raw pixel data into &#x60;std::vector<char>&#x60;:

                                What "libvncserver function" do you mean?

                                You are using libvncserver, not me.

                                Giving up here now.

                                A Offline
                                A Offline
                                abmyii
                                wrote on 6 Dec 2020, 21:37 last edited by abmyii 12 Jun 2020, 21:39
                                #47

                                Going to mark this thread as closed since it seems the issue lies with VNC, not Qt. Thank you all for your suggestions and helping me to isolate the problem.

                                @Christian-Ehrlicher Sorry for stressing you out - I thought you were referring to a specific function for some reason. I really need to slow down when reading and comprehend rather than just having a quick look and then replying which is causing confusion on all sides. Thank you again for your patience and assistance throughout.

                                1 Reply Last reply
                                1
                                • H Offline
                                  H Offline
                                  heatblazer
                                  wrote on 17 Jul 2024, 10:06 last edited by
                                  #48

                                  Hi I've done something recently I've needed so my approach was:

                                  total_len = qimg.width() * qimg.height();
                                   std::vector<unsigned int> data;
                                   data.reserve(total_len);
                                   for(int i=0; i < qimg.height(); i++)
                                       for(int j=0; j < qimg.width(); j++){
                                           data.push_back(qimg.pixel(j, i));
                                       }
                                  

                                  I needed it as an rgb pixels in order to do some image manipulations, but I believe you can also take the uchar data.

                                  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