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. QSvgRenderer rendering on QImage leads to transparent color bleeding into result image
Qt 6.11 is out! See what's new in the release blog

QSvgRenderer rendering on QImage leads to transparent color bleeding into result image

Scheduled Pinned Locked Moved Solved General and Desktop
19 Posts 5 Posters 1.7k 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.
  • H Offline
    H Offline
    hexaden
    wrote on last edited by hexaden
    #1

    When rendering .svg image with QSvgRenderer into QImage, filled with transparent 'color' (alpha channel 0), resulting image has color bleeding on edges.
    Whenever I use Qt::transparent, its black. If I use any qRgba(r,g,b,0), that color will also bleed (e.g. white).
    Is there any way to fix that?

    Screenshot with a problem:
    bleeding-colors.png

    Here you can notice a dark border around two left shapes (dark border around circle, dark border around triangle), and you might notice (yet its almost invisible) a white border around right ones. It is better visible in image editing tools (like Paint).

    Qt 5.15.5, on Windows
    Code is simple as that:

    QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
    image.fill(Qt::transparent);
    ... other things ...
    QSvgRenderer renderer;
    ... read svg ...
    QPainter painter(&image);
    renderer.render(&painter);
    
    JoeCFDJ 1 Reply Last reply
    0
    • H Offline
      H Offline
      hexaden
      wrote on last edited by hexaden
      #19

      In the end, the mistake was very simple, in final image reconstruction Format_ARGB32 was used instead of Format_ARGB32_Premultiplied, causing these problems with drawing. Thanks everyone for help!

      1 Reply Last reply
      1
      • H hexaden

        When rendering .svg image with QSvgRenderer into QImage, filled with transparent 'color' (alpha channel 0), resulting image has color bleeding on edges.
        Whenever I use Qt::transparent, its black. If I use any qRgba(r,g,b,0), that color will also bleed (e.g. white).
        Is there any way to fix that?

        Screenshot with a problem:
        bleeding-colors.png

        Here you can notice a dark border around two left shapes (dark border around circle, dark border around triangle), and you might notice (yet its almost invisible) a white border around right ones. It is better visible in image editing tools (like Paint).

        Qt 5.15.5, on Windows
        Code is simple as that:

        QImage image(width, height, QImage::Format_ARGB32_Premultiplied);
        image.fill(Qt::transparent);
        ... other things ...
        QSvgRenderer renderer;
        ... read svg ...
        QPainter painter(&image);
        renderer.render(&painter);
        
        JoeCFDJ Offline
        JoeCFDJ Offline
        JoeCFD
        wrote on last edited by
        #2

        @hexaden I use QPixmap for many svg files and never had an issue. Try it out to see if it helps.

        H 2 Replies Last reply
        0
        • S Offline
          S Offline
          SamiV123
          wrote on last edited by SamiV123
          #3

          This looks like a filtering issue, you gotta look into premultiplying the alpha before resampling.

          Basically pixels with alpha end up making an (incorrect) contribution to the final pixel color when combining multiple source pixels into a single pixel during resampling.

          https://shawnhargreaves.com/blog/texture-filtering-alpha-cutouts.html

          I'm using SvgViewWidget from Qt samples and it's using

          QImage::Format_ARGB32_Premultiplied
          

          as the image format.

          https://doc.qt.io/qt-5/qtsvg-svgviewer-example.html

          EDIT: Sorry I somehow just noticed that your code is also using ARGB32_Premultiplied as the QImageFormat so nm.

          1 Reply Last reply
          1
          • JoeCFDJ JoeCFD

            @hexaden I use QPixmap for many svg files and never had an issue. Try it out to see if it helps.

            H Offline
            H Offline
            hexaden
            wrote on last edited by
            #4

            @JoeCFD thanks, I will try later. I hope it is trivial to change QImage to QPixmap.

            1 Reply Last reply
            0
            • JoeCFDJ JoeCFD

              @hexaden I use QPixmap for many svg files and never had an issue. Try it out to see if it helps.

              H Offline
              H Offline
              hexaden
              wrote on last edited by
              #5

              @JoeCFD that didn't work, unfortunately. Same result as QImage.

              1 Reply Last reply
              0
              • H Offline
                H Offline
                hexaden
                wrote on last edited by
                #6

                Also as I found out, that resulting image is not directly painted. QImage is transformed in uchar array by it's methods, then transferred in another function, and then QImage is reconstructed from that uchar array and this image is painted by QPainter. Might it be the issue?

                Christian EhrlicherC 1 Reply Last reply
                0
                • H hexaden

                  Also as I found out, that resulting image is not directly painted. QImage is transformed in uchar array by it's methods, then transferred in another function, and then QImage is reconstructed from that uchar array and this image is painted by QPainter. Might it be the issue?

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

                  @hexaden said in QSvgRenderer rendering on QImage leads to transparent color bleeding into result image:

                  Might it be the issue

                  A QImage is not more than a bunch of bytes... So there is no 'conversion' from QImage to uchar and back.

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

                  H 1 Reply Last reply
                  0
                  • Christian EhrlicherC Christian Ehrlicher

                    @hexaden said in QSvgRenderer rendering on QImage leads to transparent color bleeding into result image:

                    Might it be the issue

                    A QImage is not more than a bunch of bytes... So there is no 'conversion' from QImage to uchar and back.

                    H Offline
                    H Offline
                    hexaden
                    wrote on last edited by hexaden
                    #8

                    @Christian-Ehrlicher so, you mean that such conversion is invalid?

                    QImage img(w, h, format);
                    *fill image*
                    auto bits = new uchar[img.sizeInBytes()];
                    memcpy(bits, img.bits(), img.sizeInBytes());
                    QImage restored(bits, w, h, format);
                    
                    jsulmJ 1 Reply Last reply
                    0
                    • H hexaden

                      @Christian-Ehrlicher so, you mean that such conversion is invalid?

                      QImage img(w, h, format);
                      *fill image*
                      auto bits = new uchar[img.sizeInBytes()];
                      memcpy(bits, img.bits(), img.sizeInBytes());
                      QImage restored(bits, w, h, format);
                      
                      jsulmJ Offline
                      jsulmJ Offline
                      jsulm
                      Lifetime Qt Champion
                      wrote on last edited by
                      #9

                      @hexaden I don't see any conversion in this code. The image data is simply copied into a buffer (bits) and later restored from there.

                      https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply
                      0
                      • jsulmJ jsulm

                        @hexaden I don't see any conversion in this code. The image data is simply copied into a buffer (bits) and later restored from there.

                        H Offline
                        H Offline
                        hexaden
                        wrote on last edited by
                        #10

                        @jsulm There is a lot of code between things, but that what I meant: uchar buffer is made from a first QImage, and then much later second QImage is made from that buffer, and I asked if that might cause problems.

                        jsulmJ 1 Reply Last reply
                        0
                        • H hexaden

                          @jsulm There is a lot of code between things, but that what I meant: uchar buffer is made from a first QImage, and then much later second QImage is made from that buffer, and I asked if that might cause problems.

                          jsulmJ Offline
                          jsulmJ Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on last edited by
                          #11

                          @hexaden said in QSvgRenderer rendering on QImage leads to transparent color bleeding into result image:

                          and I asked if that might cause problems

                          No, unless the buffered data is changed before second image is created from this buffer. You also have to be careful: "The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer.", see https://doc.qt.io/qt-6/qimage.html#QImage-3

                          https://forum.qt.io/topic/113070/qt-code-of-conduct

                          1 Reply Last reply
                          0
                          • H Offline
                            H Offline
                            hexaden
                            wrote on last edited by hexaden
                            #12

                            @jsulm restored QImage is very local, and buffer lives in outer scope for much longer time. I will double check if that buffer is ever modified, but I'm pretty sure it is not ever touched for writing (there is no public interface for it to be changed).

                            JoeCFDJ 1 Reply Last reply
                            0
                            • H hexaden

                              @jsulm restored QImage is very local, and buffer lives in outer scope for much longer time. I will double check if that buffer is ever modified, but I'm pretty sure it is not ever touched for writing (there is no public interface for it to be changed).

                              JoeCFDJ Offline
                              JoeCFDJ Offline
                              JoeCFD
                              wrote on last edited by JoeCFD
                              #13

                              @hexaden can you post your svg here for me to try it out?

                              H 1 Reply Last reply
                              0
                              • JoeCFDJ JoeCFD

                                @hexaden can you post your svg here for me to try it out?

                                H Offline
                                H Offline
                                hexaden
                                wrote on last edited by
                                #14

                                @JoeCFD it's of different color but it doesn't matter.

                                <?xml version="1.0" encoding="utf-8"?>
                                <!-- Generator: Adobe Illustrator 19.1.0, SVG Export Plug-In . SVG Version: 6.00 Build 0)  -->
                                <svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
                                	 viewBox="0 0 51 51" enable-background="new 0 0 51 51" xml:space="preserve">
                                <path id="SymbolPreviewStroke" fill="#24D900" transform="rotate(0,26,26)" d="M25.5,13.6L19,32h13L25.5,13.6z M25.5,18.1l4.4,12.4h-8.8L25.5,18.1z"/>
                                </svg>
                                
                                1 Reply Last reply
                                0
                                • JoeCFDJ Offline
                                  JoeCFDJ Offline
                                  JoeCFD
                                  wrote on last edited by JoeCFD
                                  #15

                                  It is a green triangle. I replaced one of my icons with yours and can not see any border.
                                  Ubuntu 22.04 and Qt 5.15.3

                                      QFile file(filename);
                                      file.open( QIODevice::ReadOnly );
                                      QString str( QString::fromUtf8( file.readAll() ) );
                                  
                                      QSvgRenderer svgRenderer( str.toUtf8() );
                                  
                                      QPixmap * pixmap = new QPixmap( QSize( image_width, image_height ) );
                                      pixmap->fill( Qt::transparent );
                                  
                                      QPainter pixPainter( pixmap );
                                      svgRenderer.render(&pixPainter);
                                  
                                  H 1 Reply Last reply
                                  0
                                  • JoeCFDJ JoeCFD

                                    It is a green triangle. I replaced one of my icons with yours and can not see any border.
                                    Ubuntu 22.04 and Qt 5.15.3

                                        QFile file(filename);
                                        file.open( QIODevice::ReadOnly );
                                        QString str( QString::fromUtf8( file.readAll() ) );
                                    
                                        QSvgRenderer svgRenderer( str.toUtf8() );
                                    
                                        QPixmap * pixmap = new QPixmap( QSize( image_width, image_height ) );
                                        pixmap->fill( Qt::transparent );
                                    
                                        QPainter pixPainter( pixmap );
                                        svgRenderer.render(&pixPainter);
                                    
                                    H Offline
                                    H Offline
                                    hexaden
                                    wrote on last edited by
                                    #16

                                    @JoeCFD are you drawing a QPixmap or QImage on the actual screen?

                                    JoeCFDJ 1 Reply Last reply
                                    0
                                    • H hexaden

                                      @JoeCFD are you drawing a QPixmap or QImage on the actual screen?

                                      JoeCFDJ Offline
                                      JoeCFDJ Offline
                                      JoeCFD
                                      wrote on last edited by
                                      #17

                                      @hexaden QPixmap

                                      1 Reply Last reply
                                      0
                                      • H Offline
                                        H Offline
                                        hexaden
                                        wrote on last edited by
                                        #18

                                        So, I just tested things, turns out the problem is indeed in QImage reconstruction (at least I think so). QPixmap/QImage is painted just fine, without any issues, before deconstruction into array, and it is painted with color bleeding after reconstruction (in non-isolated example, with all program code inbetween).

                                        1 Reply Last reply
                                        0
                                        • H Offline
                                          H Offline
                                          hexaden
                                          wrote on last edited by hexaden
                                          #19

                                          In the end, the mistake was very simple, in final image reconstruction Format_ARGB32 was used instead of Format_ARGB32_Premultiplied, causing these problems with drawing. Thanks everyone for help!

                                          1 Reply Last reply
                                          1
                                          • H hexaden has marked this topic as solved on

                                          • Login

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