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. QPainter loosing color of transparent pixels (Critical)
Forum Updated to NodeBB v4.3 + New Features

QPainter loosing color of transparent pixels (Critical)

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 3.6k Views 2 Watching
  • 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.
  • Q Offline
    Q Offline
    Qtpissmeoff
    wrote on last edited by Qtpissmeoff
    #1

    Currently QImage(QPixmap) and QPainter always paint fully transparent pixels as transparent black. E.g. I have image filling with transparent pixels RGBA(100, 100, 100, 0), after drawing via QPainter to another image that pixels will be RGBA(0, 0, 0, 0). Same bug is with QImage::scaled method.

    I found bug from 2013! year, but it closed without any fixes.
    https://bugreports.qt.io/browse/QTBUG-33277

    I also fount following topic:
    https://forum.qt.io/topic/73787/qimage-qpixmal-loses-alpha-color-when-drawing
    It's also haven't any working solution.

    And I open bugreport:
    https://bugreports.qt.io/browse/QTBUG-66590

    More detailed explanation:
    I have fololwing image in Photoshop. Photoshop automatically set filling color for transparent pixels to prevert grafical artifacts.
    alt text

    This is same image with stripped alhpa channel(i.e. I just draw it whitout alpha-blending in another application based on OpenGL)
    alt text

    This is same image rendered with alpha-blending in OpenGL apllication:
    alt text

    So, but following code breaks image

    QImage sourceImage("image.png");
    QImage destImage(sourceImage.width(), sourceImage.height(), QImage::Format_ARGB32);
    QPainter painter(&destImage);
    
    painter.fill(Qt::transparent);
    painter.setCompositionMode(QPainter::CompositionMode_Source);
    painter.drawImage(0, 0, sourceImage);
    painter.end();
    

    Following code also breaks image

    QImage sourceImage("image.png");
    QImage scaledImage = sourceImage.scaled(...)
    

    QPainter::drawImage using alpha-blending in any cases(!), even with CompositionMode_Source composition mode. It's completely incorrect. In that case transparent pixels are multipling by zero, and lose color information.

    This is broken image without blending:
    alt text

    This is broken image with blending in OpenGL application:
    alt text

    Broken image has black artifacts on letter edges:
    alt text

    Correct image for comparsion:
    alt text

    How to fix or avoid this? I need a common solution

    kshegunovK 1 Reply Last reply
    0
    • Q Qtpissmeoff

      Currently QImage(QPixmap) and QPainter always paint fully transparent pixels as transparent black. E.g. I have image filling with transparent pixels RGBA(100, 100, 100, 0), after drawing via QPainter to another image that pixels will be RGBA(0, 0, 0, 0). Same bug is with QImage::scaled method.

      I found bug from 2013! year, but it closed without any fixes.
      https://bugreports.qt.io/browse/QTBUG-33277

      I also fount following topic:
      https://forum.qt.io/topic/73787/qimage-qpixmal-loses-alpha-color-when-drawing
      It's also haven't any working solution.

      And I open bugreport:
      https://bugreports.qt.io/browse/QTBUG-66590

      More detailed explanation:
      I have fololwing image in Photoshop. Photoshop automatically set filling color for transparent pixels to prevert grafical artifacts.
      alt text

      This is same image with stripped alhpa channel(i.e. I just draw it whitout alpha-blending in another application based on OpenGL)
      alt text

      This is same image rendered with alpha-blending in OpenGL apllication:
      alt text

      So, but following code breaks image

      QImage sourceImage("image.png");
      QImage destImage(sourceImage.width(), sourceImage.height(), QImage::Format_ARGB32);
      QPainter painter(&destImage);
      
      painter.fill(Qt::transparent);
      painter.setCompositionMode(QPainter::CompositionMode_Source);
      painter.drawImage(0, 0, sourceImage);
      painter.end();
      

      Following code also breaks image

      QImage sourceImage("image.png");
      QImage scaledImage = sourceImage.scaled(...)
      

      QPainter::drawImage using alpha-blending in any cases(!), even with CompositionMode_Source composition mode. It's completely incorrect. In that case transparent pixels are multipling by zero, and lose color information.

      This is broken image without blending:
      alt text

      This is broken image with blending in OpenGL application:
      alt text

      Broken image has black artifacts on letter edges:
      alt text

      Correct image for comparsion:
      alt text

      How to fix or avoid this? I need a common solution

      kshegunovK Offline
      kshegunovK Offline
      kshegunov
      Moderators
      wrote on last edited by
      #2

      @Qtpissmeoff said in QPainter loosing color of transparent pixels (Critical):

      painter.setCompositionMode(QPainter::CompositionMode_Source);
      painter.drawImage(0, 0, sourceImage);
      painter.end();
      

      Drawing an alpha image onto an opaque black will naturally blend the transparent pixel with the black background, then rendering that on any other color of course will have black undertones. I don't get what you're trying to do. In the linked qt forum thread I showed that blending works without issue on my machine and produces what is expected, I even provided the source I used to test it ...

      Read and abide by the Qt Code of Conduct

      1 Reply Last reply
      2
      • Q Offline
        Q Offline
        Qtpissmeoff
        wrote on last edited by Qtpissmeoff
        #3

        Oh, sorry, I fixed the sample. I think it now more understandable.

        QImage sourceImage("image.png");
        QImage destImage(sourceImage.width(), sourceImage.height(), QImage::Format_ARGB32);
        QPainter painter(&destImage);
        
        painter.fill(Qt::transparent);
        painter.setCompositionMode(QPainter::CompositionMode_Source);
        painter.drawImage(0, 0, sourceImage);
        painter.end();
        

        So, I draw RGBA image to another fully transparent RGBA image with CompositionMode_Source, but QPainter still blend these images instead of just copying pixels.

        Transparent pixels of first image has a color info. E.g. it white pixels, but with zero alpha. And after blending it will be black pixels with zero alpha. It's critical issue for images using as OpenGL\DirectX textures, because black pixels generate artifacts as on exlample images.

        kshegunovK 1 Reply Last reply
        0
        • Q Qtpissmeoff

          Oh, sorry, I fixed the sample. I think it now more understandable.

          QImage sourceImage("image.png");
          QImage destImage(sourceImage.width(), sourceImage.height(), QImage::Format_ARGB32);
          QPainter painter(&destImage);
          
          painter.fill(Qt::transparent);
          painter.setCompositionMode(QPainter::CompositionMode_Source);
          painter.drawImage(0, 0, sourceImage);
          painter.end();
          

          So, I draw RGBA image to another fully transparent RGBA image with CompositionMode_Source, but QPainter still blend these images instead of just copying pixels.

          Transparent pixels of first image has a color info. E.g. it white pixels, but with zero alpha. And after blending it will be black pixels with zero alpha. It's critical issue for images using as OpenGL\DirectX textures, because black pixels generate artifacts as on exlample images.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by
          #4

          Could you provide MWE that I can compile and run directly to test on my machine, also please include the links to the image(s) in question.

          Read and abide by the Qt Code of Conduct

          1 Reply Last reply
          2

          • Login

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