Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Change pixel color of QPixmap
Forum Updated to NodeBB v4.3 + New Features

Change pixel color of QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 8.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Jakob Clausen

    If I comment out the line setPixel(), then the picture will look like the original:
    original.png
    If I do as described, then it looks like this:
    distorted.png
    So it is changing the pixelvalues. But it is a little rough on the edges.

    KroMignonK Offline
    KroMignonK Offline
    KroMignon
    wrote on last edited by KroMignon
    #21

    @Jakob-Clausen Perhaps you should try to force pixel format to what you need:

    QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
    {
        QImage temp = pixmap.toImage();
        
        qDebug() << "Pixel format is:" << temp.pixelFormat();
        
        if(temp.pixelFormat() != QImage::Format_ARGB32)
            temp.convertTo(QImage::Format_ARGB32);
        
        for (int y = 0; y < temp.height(); ++y)
        {
            QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y));
    
            for (int x= 0; x < temp.width(); ++x, ++s)
            {
                QColor color = QColor::fromRgba(*s);
                uint8_t alpha = color.alpha();
    
                to.setAlpha(alpha);
                from.setAlpha(alpha);
    
                if (color == from)
                   *s = to.rgba();
            }
        }
    
        return QPixmap::fromImage(temp);
    }
    

    It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

    J 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #22

      QImage::Format_ARGB32_Premultiplied is not a good idea to do the pixel stuff then... see the docs: https://doc.qt.io/qt-5/qimage.html#Format-enum

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      1
      • KroMignonK KroMignon

        @Jakob-Clausen Perhaps you should try to force pixel format to what you need:

        QPixmap IconWidget::changeColor(QPixmap& pixmap, QColor to, QColor from)
        {
            QImage temp = pixmap.toImage();
            
            qDebug() << "Pixel format is:" << temp.pixelFormat();
            
            if(temp.pixelFormat() != QImage::Format_ARGB32)
                temp.convertTo(QImage::Format_ARGB32);
            
            for (int y = 0; y < temp.height(); ++y)
            {
                QRgb* s = reinterpret_cast<QRgb *>(temp.scanLine(y));
        
                for (int x= 0; x < temp.width(); ++x, ++s)
                {
                    QColor color = QColor::fromRgba(*s);
                    uint8_t alpha = color.alpha();
        
                    to.setAlpha(alpha);
                    from.setAlpha(alpha);
        
                    if (color == from)
                       *s = to.rgba();
                }
            }
        
            return QPixmap::fromImage(temp);
        }
        
        J Offline
        J Offline
        Jakob Clausen
        wrote on last edited by
        #23

        @KroMignon Thats a good idea. convertTo() is introduced in 5.13. But if I use

        temp = temp.convertToFormat(QImage::Format_ARGB32);
        

        Then it seems to work.
        Thank you very much.

        1 Reply Last reply
        0
        • JoeCFDJ JoeCFD referenced this topic on

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved