0

I'm developing application for editing raster graphic. In this application I have to create scanline function which will do same thing as scanline function in QImage class. But I'm little confused with the way that scanline function works and with scanline generally. For example, when I call bytesPerLine() for image which height is 177px I was expecting that value will be 531 (3 bytes for each pixel) but this function is returning 520?

Also, when I use uchar data = image->scanLine(y)[x] for R=249 G=249 B=249 value in variable data is 255. I really don't understand this value. Thanks in advance :)

Mirzet
  • 1
  • 1
  • 1
  • i would expect that each pixel be 4 bytes, RGBA, the 255 is probably the alpha channel. Check the next two bytes. – Evan Teran Dec 03 '11 at 02:49
  • Follow up, do you know what Format the `QImage` was constructed with? This directly effects the size in bytes and format of each given pixel. From the docs: > If you are accessing 32-bpp image data, cast the returned pointer to QRgb* (QRgb has a 32-bit size) and use it to read/write the pixel value. 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. – Evan Teran Dec 03 '11 at 02:53
  • @EvanTeran: QImage was constructed with Format_RGB32 format. I understand that I have to cast returning pointer to qRgb but I 'm interested in accessing a color without casting. From value returned by image->scanLine(y)[x] I can't understand which color value was accessed. – Mirzet Dec 03 '11 at 02:58

1 Answers1

1

For reliable behavior you should check the return value of QImage::format() to see what underlying format is used before accessing the raw image data.

Qt seems to prefer RGB32/ARGB32 format for true-colors, where each pixel takes 4 bytes, whether an alpha channel exists or not (for RGB32 format it's simply filled with 0xff). If you load a true-color image, it's probably in one of these two formats.

Besides, the byte order can be different across platforms, use QRgb to access 32-bit pixels whenever possible.

BTW, shouldn't a scanline be horizontal? I think you should use width() instead of height() to calculate the length of a scanline.

hpsMouse
  • 2,004
  • 15
  • 20