Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt
  • Unsolved
Collapse
qtws26_logo

The Qt Event of the Year: Qt World Summit 2026

Save Your Spot >
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QImage - Retrieve data from an 8 bit bitmap image
Qt 6.12 LTS is out! See what's new in the release blog

QImage - Retrieve data from an 8 bit bitmap image

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 12.6k 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    Hi,

    I have an 8 bit bitmap image that I need to read, retrieve the data and use that information as the alpha channel for another image. How do I go about doing that? I have tried the below code, but doesn't seem to work.

    @ QImage alpha("C:/images/alpha.bmp");//8 bit bmp

    // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
    qDebug()<<"alpha image format " <<alpha.format();
    
    QColor cola = QColor::fromRgba(alpha.pixel(j,y));
    img2.setPixel(j,y,qRgba(red,gren,blue,cola.alpha()));@
    

    Any suggestions on how to retrieve the 8 bit info from the bitmap?

    Thanks,

    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Doesn't QImage::constBits() or QImage::constScanLine() help you? Sound like you just need raw data, right?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        The alpha QImage is a regular image, the alpha value of any pixel is always 0xff. You have to reconstruct the value from the RGB values.

        1 Reply Last reply
        0
        • D Offline
          D Offline
          dangelog
          wrote on last edited by
          #4

          [quote author="karthiksrini" date="1294747392"]
          @
          // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
          qDebug()<<"alpha image format " <<alpha.format();
          @
          [/quote]

          Watch out, you don't have a 8bpp image, you have a 8bpp INDEXED image, so the color of each pixel is stored in the colortable (which is indexed by the value of each pixel, and stores 32bit colors).

          Software Engineer
          KDAB (UK) Ltd., a KDAB Group company

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            Thanks for your replies!
            I have tried the below code based on Volker's and peppe's replies (and based on what the documentaion says - In case of a 8-bit and monchrome images, the pixel value is only an index from the image's color table) I seem to get what I want. If you have any comments on what I am doing, i.e if I am doing the right/wrong thing, let me know.

            @QImage alpha("C:/images/alpha.bmp");//8 bit bmp

            // This detects and shows 3, which is QImage::Format_Indexed8. Fine so far.
            qDebug()<<"alpha image format " <<alpha.format();

            QVector<QRgb> vect = alpha.colorTable();
            QColor cola = QColor::fromRgba(alpha.pixel(j,y));
            img2.setPixel(j,y,qRgba(red,gren,blue,vect.at(alpha.pixel(j,y) & 0xff)));@

            1 Reply Last reply
            0
            • ? Offline
              ? Offline
              A Former User
              wrote on last edited by
              #6

              Only repeating a warning from the API docs on "QImage::pixel() ":http://doc.qt.nokia.com/4.7-snapshot/qimage.html#pixel:

              bq. Warning: This function is expensive when used for massive pixel manipulations.

              It may slow down your application if you work on big images.

              1 Reply Last reply
              0
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                [quote author="Volker" date="1294922411"]Only repeating a warning from the API docs on "QImage::pixel() ":http://doc.qt.nokia.com/4.7-snapshot/qimage.html#pixel:

                bq. Warning: This function is expensive when used for massive pixel manipulations.

                It may slow down your application if you work on big images.[/quote]

                So what's a better alternative? QIMage::scanLine? Or reading from bits?

                1 Reply Last reply
                0
                • ? Offline
                  ? Offline
                  A Former User
                  wrote on last edited by
                  #8

                  scanLine() can work, but be aware that you must handle the alignment of the data and all that manually.

                  If you have big images/and or do this often, you might consider including an external image library like "GraphicsMagick":http://www.graphicsmagick.org/ for the pixel manipulation. It also has a C++ interface.

                  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
                  • Unsolved