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. QImage, parsing raw char from image to get alpha?
Forum Updated to NodeBB v4.3 + New Features

QImage, parsing raw char from image to get alpha?

Scheduled Pinned Locked Moved Unsolved General and Desktop
qimageqopengl
14 Posts 4 Posters 2.3k 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.
  • D Offline
    D Offline
    Dariusz
    wrote on 11 May 2019, 16:50 last edited by
    #1

    Hey

    I'm doing some openGl rendering and on finish render I would like to get some data out of the image.

    The QOpenGLFramebufferObject.toImage() returns a Format_ARGB32_Premultiplied and I use this for format > fboFormat.setAttachment(QOpenGLFramebufferObject::Depth);

    Here is the function I'm trying to run but sadly no luck...

        for (int he = 0; he < height; ++he) {
            for (int wi = 0; wi < width; ++wi) {
                pixelOffset = imageWidth * he + (wi * 4); /// get item 0 every 4 items for alpha, since its ARGB, if RGBA mode then ```imageWidth * he + (wi * 4) +4 ```
                alpha = temp[pixelOffset];//qAlpha(img.pixel(wi, he)); /// img.pixel work but slow due to different access function
                im[heightOffset][widthOffset] = alpha;
                widthOffset++;
            }
            widthOffset = 0;
            heightOffset++;
        }
    

    Can any1 help ?
    TIA

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mranger90
      wrote on 11 May 2019, 19:34 last edited by
      #2

      What is temp[] ? is it a byte array or a pixel array ?

      D 1 Reply Last reply 12 May 2019, 01:03
      0
      • M mranger90
        11 May 2019, 19:34

        What is temp[] ? is it a byte array or a pixel array ?

        D Offline
        D Offline
        Dariusz
        wrote on 12 May 2019, 01:03 last edited by
        #3

        @mranger90 Oh sorry, missed that!

        uchar *temp = (uchar *) img.constBits();

        1 Reply Last reply
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 12 May 2019, 07:58 last edited by
          #4

          @Dariusz said in QImage, parsing raw char from image to get alpha?:

          run but sadly no luck...

          What does this mean?

          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
          • D Offline
            D Offline
            Dariusz
            wrote on 12 May 2019, 10:15 last edited by
            #5

            That this

            uchar *temp = (uchar *) img.constBits();
            
            ...
                        pixelOffset = imageWidth * he + (wi * 4);
                        alpha = temp[pixelOffset];//qAlpha(img.pixel(wi, he)); /// img.pixel work but slow due to different access function
            

            does not return alpha int. as far as I understand img.constBits() return 1d array, 4 values equal to a color, esentially A-R-G-B, so every 4th item is the alpha value and pixelOffset uses/calculates indexOffset in to the 1d array from 2d array of height*width of image.

            The loop function in 1st post shows it.

            I don't understand why the value I get from the loop above is not alpha.

            1 Reply Last reply
            0
            • C Offline
              C Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 12 May 2019, 10:48 last edited by
              #6

              Your offset calculation is wrong, every pixel has 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

              D 1 Reply Last reply 12 May 2019, 11:33
              0
              • C Christian Ehrlicher
                12 May 2019, 10:48

                Your offset calculation is wrong, every pixel has 4 bytes.

                D Offline
                D Offline
                Dariusz
                wrote on 12 May 2019, 11:33 last edited by
                #7

                @Christian-Ehrlicher said in QImage, parsing raw char from image to get alpha?:

                Your offset calculation is wrong, every pixel has 4 bytes.

                Hmm did I not address that in (wi*4) ? You say bytes but isn't uchar *temp an array of ints? I'm lost o.o

                1 Reply Last reply
                0
                • C Offline
                  C Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 12 May 2019, 12:03 last edited by
                  #8

                  @Dariusz said in QImage, parsing raw char from image to get alpha?:

                  uchar *

                  I don't see int here... only (unsigned) char

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

                  D 1 Reply Last reply 12 May 2019, 12:24
                  1
                  • C Christian Ehrlicher
                    12 May 2019, 12:03

                    @Dariusz said in QImage, parsing raw char from image to get alpha?:

                    uchar *

                    I don't see int here... only (unsigned) char

                    D Offline
                    D Offline
                    Dariusz
                    wrote on 12 May 2019, 12:24 last edited by Dariusz 5 Dec 2019, 12:25
                    #9

                    @Christian-Ehrlicher Yeah, sorry just used to ints.

                    So the:
                    uchar *temp = (uchar *) img.constBits();
                    Should be something like
                    temp = [alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue, alpha, red, green, blue ]
                    Each value being char. 0-255 - not quite sure how to deal with 32 bit+ arrays tho... are they then int *temp ? constBits return only chars... I'm lost I never dig in to 16/32+ image formats...

                    so if I were to do temp[0] / temp[4],temp[8] then they all should be alphas?

                    So what am I missing in
                    imageWidth * imageHeightIndex + (imageWidthIndex *4)
                    Which if gives correct numbers would result for 300x400 image (w x h)

                    300 * 0 + (0*4) = 0
                    300 * 0 + (1*4) = 4
                    300 * 0 + (2*4) = 8
                    

                    etc etc so I'm getting correct index to retrieve alpha ?

                    What am I missing ? o.O

                    1 Reply Last reply
                    0
                    • C Offline
                      C Offline
                      Christian Ehrlicher
                      Lifetime Qt Champion
                      wrote on 12 May 2019, 12:26 last edited by
                      #10

                      @Dariusz said in QImage, parsing raw char from image to get alpha?:

                      What am I missing ? o.O

                      Simple math I would say:
                      4 bytes per pixel, row is 300 pixels = 1200 bytes per row

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

                      D 1 Reply Last reply 12 May 2019, 12:29
                      1
                      • C Christian Ehrlicher
                        12 May 2019, 12:26

                        @Dariusz said in QImage, parsing raw char from image to get alpha?:

                        What am I missing ? o.O

                        Simple math I would say:
                        4 bytes per pixel, row is 300 pixels = 1200 bytes per row

                        D Offline
                        D Offline
                        Dariusz
                        wrote on 12 May 2019, 12:29 last edited by
                        #11

                        @Christian-Ehrlicher Hmm char is 2 byte big... so do I offset by 8 instead and add up 2 items together?

                        int alpha = temp[0]<<temp[1] ?

                        1 Reply Last reply
                        0
                        • C Offline
                          C Offline
                          Christian Ehrlicher
                          Lifetime Qt Champion
                          wrote on 12 May 2019, 12:32 last edited by
                          #12

                          @Dariusz said in QImage, parsing raw char from image to get alpha?:

                          char is 2 byte big

                          Ok, now I'm giving up...

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

                          D 1 Reply Last reply 12 May 2019, 12:33
                          0
                          • C Christian Ehrlicher
                            12 May 2019, 12:32

                            @Dariusz said in QImage, parsing raw char from image to get alpha?:

                            char is 2 byte big

                            Ok, now I'm giving up...

                            D Offline
                            D Offline
                            Dariusz
                            wrote on 12 May 2019, 12:33 last edited by
                            #13

                            @Christian-Ehrlicher said in QImage, parsing raw char from image to get alpha?:

                            @Dariusz said in QImage, parsing raw char from image to get alpha?:

                            char is 2 byte big

                            Ok, now I'm giving up...

                            Eh crap its short thats 2 byte big... char is 1 byte... ehhhhhhhh I forgot about shorts :/. Ok so esentially I have to grab 4 chars and add them up to int?

                            1 Reply Last reply
                            0
                            • S Offline
                              S Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on 12 May 2019, 18:58 last edited by
                              #14

                              Hi,

                              Why do you want to combine them back into and int ?

                              If you have a ARGB image, basically you have:

                              uchar *start = (uchar *) img.constBits();
                              uchar *alpha = start + 0;
                              uchar *red = start + 1;
                              uchar *green = start + 2;
                              uchar *blue = start + 3;
                              
                              // First alpha value:
                              uint8 alphaValue = *alpha;
                              // Second value of alpha
                              alphaValue = *(alpha + 4)
                              

                              if you want to sum them, then make alphaValue an int and that's all.

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

                              1 Reply Last reply
                              4

                              10/14

                              12 May 2019, 12:26

                              • Login

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