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

Change pixel color of QPixmap

Scheduled Pinned Locked Moved Solved General and Desktop
23 Posts 5 Posters 8.6k 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.
  • kkoehneK Offline
    kkoehneK Offline
    kkoehne
    Moderators
    wrote on last edited by
    #13

    Note the caveats mentioned in https://doc.qt.io/qt-5/qimage.html#setPixel :

    If the image's format is either monochrome or paletted, the given index_or_rgb value must be an index in
    the image's color table, otherwise the parameter must be a QRgb value.
    
    If position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount() in the case of monochrome and paletted images, the result is undefined.
    

    So, is your image maybe using a palette? There's more details here: https://doc.qt.io/qt-5/qimage.html#pixel-manipulation

    Director R&D, The Qt Company

    1 Reply Last reply
    2
    • J Jakob Clausen

      @Christian-Ehrlicher It is being called. If I remove the line, then the picture looks like the original. For me setPixel() is a magic function that should replace one pixel color with another. If it does not, then I am lost.

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #14

      @Jakob-Clausen
      As @Christian-Ehrlicher has just asked --- if you think your code is changing the passed-in QPixmap& pixmap, it is not!

      1 Reply Last reply
      1
      • Christian EhrlicherC Christian Ehrlicher

        So do you actually use the pixmap returned by the function?

        J Offline
        J Offline
        Jakob Clausen
        wrote on last edited by
        #15

        @Christian-Ehrlicher yes.

        1 Reply Last reply
        0
        • J Offline
          J Offline
          Jakob Clausen
          wrote on last edited by
          #16

          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 1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #17

            Try to use setPixelColor(int x, int y, const QColor &color).

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

            J 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Try to use setPixelColor(int x, int y, const QColor &color).

              J Offline
              J Offline
              Jakob Clausen
              wrote on last edited by
              #18

              @Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.

              Christian EhrlicherC 1 Reply Last reply
              0
              • J Jakob Clausen

                @Christian-Ehrlicher If I do that, then it works perfectly. But setPixelColor() is introduced before 5.6, and I am forced to use 5.3.

                Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #19

                @Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?

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

                J 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  @Jakob-Clausen Did you read @kkoehne 's comment? What pixel format do you have?

                  J Offline
                  J Offline
                  Jakob Clausen
                  wrote on last edited by
                  #20

                  @Christian-Ehrlicher QImage::Format_ARGB32_Premultiplied

                  1 Reply Last reply
                  0
                  • 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