Results 1 to 2 of 2

Thread: QImage setPixel() problem

  1. #1
    Join Date
    Jun 2011
    Posts
    56
    Thanks
    7
    Qt products
    Qt4

    Default QImage setPixel() problem

    I see this code in setPixel() from qimage.cpp:

    Qt Code:
    1. case Format_ARGB32:
    2. case Format_ARGB32_Premultiplied:
    3. ((uint *)s)[x] = index_or_rgb;
    4. break;
    To copy to clipboard, switch view to plain text mode 

    This means, that the RGB values are not premultiplied with alpha, when the pixel is set. To set the pixel values correctly, do I need to do this?

    Qt Code:
    1. image.setPixel(x, y, qRgba(qt_div_255(color.alpha() * color.red()),
    2. qt_div_255(color.alpha() * color.green()),
    3. qt_div_255(color.alpha() * color.blue()),
    4. color.alpha()));
    To copy to clipboard, switch view to plain text mode 

    Or when and how do the RGB values get premultiplied? In other words, how to set the pixel so it contains the correct value?
    Last edited by ugluk; 21st January 2013 at 14:24.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QImage setPixel() problem

    You always need to set pixel data according to the format you specified while creating/loading the image. If you want to set the pixel value in the alpha premultiplied format, you need to multiply each of the RGB components by the alpha value (ranging from 0.0 to 1.0). So the correct calculation is color.red()*color.alpha()/255.0
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. The following user says thank you to wysota for this useful post:

    ugluk (21st January 2013)

Similar Threads

  1. Replies: 5
    Last Post: 16th May 2011, 21:15
  2. prob on setpixel in Qimage ?
    By Askar in forum Qt Programming
    Replies: 1
    Last Post: 3rd March 2010, 06:23
  3. QImage setPixel does not set alpha value
    By sanjayshelke in forum Qt Programming
    Replies: 7
    Last Post: 20th February 2009, 10:15
  4. [SOLVED] QImage setPixel alpha issue
    By bunjee in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2008, 15:06
  5. problem with setPixel
    By sar_van81 in forum Qt Programming
    Replies: 6
    Last Post: 2nd February 2007, 10:16

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.