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 - Retrieve data from an 8 bit bitmap image
Forum Updated to NodeBB v4.3 + New Features

QImage - Retrieve data from an 8 bit bitmap image

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 11.0k 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.
  • K Offline
    K Offline
    karthiksrini
    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
      • G Offline
        G Offline
        goetz
        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.

        http://www.catb.org/~esr/faqs/smart-questions.html

        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
          • K Offline
            K Offline
            karthiksrini
            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
            • G Offline
              G Offline
              goetz
              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.

              http://www.catb.org/~esr/faqs/smart-questions.html

              1 Reply Last reply
              0
              • K Offline
                K Offline
                karthiksrini
                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
                • G Offline
                  G Offline
                  goetz
                  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.

                  http://www.catb.org/~esr/faqs/smart-questions.html

                  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