[Qt-interest] in fact, QImage::ARGB32 is BGRA32 ?

Rainer Wiesenfarth Rainer.Wiesenfarth at inpho.de
Wed Mar 18 09:09:02 CET 2009


From: qt-interest-bounces at trolltech.com 
> i need to access buff of QImage and modify them directly in 
> ARGB32 format.
> always, i found i just got error result, so i write a code to 
> test like following:
> 
>         //play with buff of theTest
>         unsigned char* p = theTest.bits();
>         int            w = theTest.width();
>         int            h = theTest.height();
>         int            s = theTest.bytesPerLine();
>         for(int i=0;i<h;i++)
>         {
>             for(int j=0;j<w;j++)
>             {
>                 int t = i*s + j*4;
>                 
>                 // it works in BGRA32
>                 p[t + 0] = 0;  // change B
>                 p[t + 1] = 0;  // change G
>                 p[t + 2] = 0;  // change R
> 
>                 /* my original version
>                 p[t + 1] = 0;  // change R
>                 p[t + 2] = 0;  // change G
>                 p[t + 3] = 0;  // change B
>                */
>             }
>         }
> 
> so, it looks like QImage::ARGB32 is BGRA32 actually?

No, you are on a platform using little endian byte order. :-)

So, p[t+0] is the least significant byte of the 32bit value, and p[t+3] is
the most significant one. Thus your "my original version" code tries to do
BGRA32 instead of ARGB32.

For both portability and readability I would like to recommend the "Qt-way"
to set pixels:

    //play with buff of theTest
    int            w = theTest.width();
    int            h = theTest.height();
    for(int i=0;i<h;i++)
    {
         QRgb   *p = (QRgb*) theTest.scanLine(i);
         for(int j=0;j<w;j++)
         {
             p[j] = qRgba (/*red=*/ 0, /*green=*/ 0, /*blue=*/ 0, /*alpha=*/
255);
         }
    }

Best Regards / Mit freundlichen Grüßen
Rainer Wiesenfarth

-- 
INPHO GmbH * Smaragdweg 1 * 70174 Stuttgart * Germany
phone: +49 711 2288 10 * fax: +49 711 2288 111 * web: www.inpho.de
place of business: Stuttgart * managing director: Johannes Saile
commercial register: Stuttgart, HRB 9586
Leader in Photogrammetry and Digital Surface Modelling
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3359 bytes
Desc: not available
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090318/ce51ee7e/attachment.bin 


More information about the Qt-interest-old mailing list