[Interest] how to cast the returned pointer of QImage::scanLine(int i) to QRgb ? format is Format_RGB32
Till Oliver Knoll
till.oliver.knoll at gmail.com
Fri Jan 10 10:12:59 CET 2014
Am 10.01.2014 um 06:20 schrieb "iMath" <2281570025 at qq.com>:
> how to cast the returned pointer of
> uchar * QImage::scanLine(int i)
>
> to QRgb ? the image format is Format_RGB32
Ahhh, the beauty of C/C++ casts ;) I'm doing this entirely from my head, so it could be completely bogus, but here we go:
Keep in mind that QRgb is a simple typedef to a 32bit unsigned int:
typedef QRgb
An ARGB quadruplet on the format #AARRGGBB, equivalent to an unsigned int.
So as long as the raw data is in that format and order (and not BBGGRRAA or any other permutation), you should be able to "reinterpret" the pointer to char to a pointer to unsigned int (I assume):
QRgb *values = reinterpret_cast<QRgb *>(img->scanLine(i));
When you iterate over the array just recall basic C pointer arithmetic, just to make sure: it's a difference when you call the ++ operator on either a pointer to char or a pointer to unsigned int: the increment steps are different, either e.g. 1 or 4 bytes (maybe even depending on the architecture: a "char" might be 2 bytes, a byte might not even be 8 bits - I am not going to argue here any further, because I've been taught better by other architecture connaisseurs over and over myself ;))
However there is another not so obvious pitfall: the end of the raw pixel scanline might be "padded" (with meaningless data!) to the next multiple of 4 (or even 8?) bytes address boundary for performance reasons. IIRC this is not a real problem when you iterate over scanlines as provided by the method call above, and only read data worth "image width" pixels. But it's definitively something you have to consider when e.g. iterating over the whole raw block of data, as provided by QImage::bits().
And we haven't even touched yet the topic of "premultiplied alpha". So my suggestion is: if you're developing an image processing algorithm, focus on that first! That is, get the QRgb values "the easy" way first by calling QImage::pixel(x, y) and iterate over the cartesian coordinatres (x, y). Yes, that amounts to width * height method calls, but you can optimise that later when your actual algorithm works - especially when you stumble across C/C++ basics ;)
Cheers,
Oliver
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20140110/01a5b9b1/attachment.html>
More information about the Interest
mailing list