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 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
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 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
      0
      • Christian EhrlicherC Christian Ehrlicher

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

        D Offline
        D Offline
        Dariusz
        wrote on 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
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 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
          1
          • Christian EhrlicherC Christian Ehrlicher

            @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 last edited by Dariusz
            #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
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on 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
              1
              • Christian EhrlicherC Christian Ehrlicher

                @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 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
                • Christian EhrlicherC Offline
                  Christian EhrlicherC Offline
                  Christian Ehrlicher
                  Lifetime Qt Champion
                  wrote on 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
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @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 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
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on 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

                      • Login

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