Results 1 to 2 of 2

Thread: [SOLVED] QImage setPixel alpha issue

  1. #1
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default [SOLVED] QImage setPixel alpha issue

    Hi,

    I'm replacing pixels in a QImage using that code :

    Qt Code:
    1. for (int i = 0; i < height; i++)
    2. {
    3. for (int j = 0; j < width; j++)
    4. {
    5. int alpha = qAlpha(image.pixel(j, i));
    6. if (alpha > 0)
    7. {
    8. image.setPixel(j, i,
    9. qRgba(color.red(),
    10. color.green(),
    11. color.blue(),
    12. alpha));
    13. }
    14. }
    15. }
    16.  
    17. q->QPixmap::operator=(QPixmap::fromImage(image));
    To copy to clipboard, switch view to plain text mode 

    My image file is a PNG file.

    For some reason, everytime I replace a "transparent" pixel. The pixel goes black instead of the choosen color. Plain pixels are replaced fine.

    Any idea ?
    Last edited by bunjee; 19th September 2008 at 15:57.

  2. #2
    Join Date
    Jan 2007
    Location
    Paris
    Posts
    459
    Thanks
    98
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4 Qt5

    Default Re: QImage setPixel alpha issue

    Solved.

    QImage dissociates alpha and RGB channel.

    Qt Code:
    1. void qkColorPixmapPrivate::refreshPixmap(const QColor & color)
    2. {
    3. Q_Q(qkColorPixmap);
    4.  
    5. if (this->color == color) return;
    6.  
    7. QImage image = basePixmap.toImage();
    8. QImage alpha = image.alphaChannel();
    9.  
    10. int width = basePixmap.width();
    11. int height = basePixmap.height();
    12.  
    13. for (int i = 0; i < height; i++)
    14. {
    15. for (int j = 0; j < width; j++)
    16. {
    17. int a = qAlpha(image.pixel(j, i));
    18. if (a > 0)
    19. {
    20. image.setPixel(j, i,
    21. qRgb(color.red(),
    22. color.green(),
    23. color.blue()));
    24.  
    25. alpha.setPixel(j, i, a);
    26. }
    27. }
    28. }
    29.  
    30. image.setAlphaChannel(alpha);
    31.  
    32. q->QPixmap::operator=(QPixmap::fromImage(image));
    33.  
    34. this->color = color;
    35. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QImage Issue
    By vishal.chauhan in forum Qt Programming
    Replies: 5
    Last Post: 5th February 2007, 04:29

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.