14

I am building a QT GUI application and use QImage for opening images. My problem is that I can't figure out how to use QImage's bit() and scanline() methods to get access at per pixel level.

I've seen this post Qt QImage pixel manipulation problems but this is only for the first pixel of each row. Is this correct or I got it all wrong?

thanks in advance

Community
  • 1
  • 1
theosem
  • 1,144
  • 3
  • 10
  • 21
  • oopps !! sorry! after posting the question I found it at QT forums http://qt.nokia.com/doc/qq/qq17-imageio.html and the answer is: for (quint32 y = 0; y < h; ++y) { QRgb *scanLine = (QRgb *)img.scanLine(y); for (quint32 x = 0; x < w; ++x){ pix = qBlue(scanLine[x]); } } – theosem Jan 19 '10 at 16:41

3 Answers3

14

The scanlines correspond to the the height of image, the columns correspond to the width of the image.

According to the docs, the prototype looks like uchar* QImage::scanline(int i), or a similar const version.

But, as a commenter pointed out, because the data is dependent on the machine architecture and image, you should NOT use the uchar * directly. Instead, use something like the following:

QRgb *rowData = (QRgb*)img.scanLine(row);
QRgb pixelData = rowData[col];
int red = qRed(pixelData);
Kaleb Pederson
  • 43,537
  • 19
  • 96
  • 144
  • 11
    -1: Attention! As this answer appears in Google-search even before the official Qt-API docu, I thought a word of warning is appropriate. You CAN'T use [row][col] to access pixel data, as [col] would select an uchar and the pixel data is very depending on the bit-per-pixel format. Please see the warning in official docu "You cannot use the uchar* pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels." http://qt-project.org/doc/qt-4.8/qimage.html#scanLine – Valentin Heinitz May 03 '13 at 13:26
  • 1
    I've updated the answer to reflect the correct way to access it. – Kaleb Pederson Feb 19 '14 at 16:47
13

It may not be immediately obvious from Kaleb's post, but the following works for setting a pixel on a Format_RGB32 image.

// Get the line we want
QRgb *line = (QRgb *)image->scanLine(row_index);

// Go to the pixel we want
line += col_index;

// Actually set the pixel
*line = qRgb(qRed(color), qGreen(color), qBlue(color));
LeviX
  • 2,846
  • 3
  • 24
  • 37
0

The answer did not work for me. It looks like, the data is not 32bit aligned on my system. To get the correct data, on my system i had to do this:

for(uint32_t Y = 0; Y < mHeight; ++Y)
{
    uint8_t* pPixel = Image.scanLine(Y);

    for(uint32_t X = 0; X < mWidth; ++X)
    {
      const int Blue = *pPixel++;
      const int Green = *pPixel++;
      const int Red = *pPixel++;

      uint8_t GrayscalePixel = (0.21f * Red) + (0.72f * Green) + (0.07 * Blue);
    }
}
MvHorssen
  • 79
  • 1
  • 10
  • 1
    The alignment depends of the pixel format! You have RGB888 unaligned pixel format here :) Always convert the rource image to format you want to work with, e.g. RGBA32 or ARGB32 or the like – Петър Петров Mar 21 '19 at 11:45