QPainter loosing color of transparent pixels (Critical)
-
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-33277I 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-66590More detailed explanation:
I have fololwing image in Photoshop. Photoshop automatically set filling color for transparent pixels to prevert grafical artifacts.
This is same image with stripped alhpa channel(i.e. I just draw it whitout alpha-blending in another application based on OpenGL)
This is same image rendered with alpha-blending in OpenGL apllication:
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:
This is broken image with blending in OpenGL application:
Broken image has black artifacts on letter edges:
Correct image for comparsion:
How to fix or avoid this? I need a common solution
-
@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 ...
-
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.
-
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.