QPainter, draw pixel buffer via QImage, but result is composited wrong
-
Hello,
I am working on an app that needs to take raw pixel data and display it on-screen. This is partially working for me, in that I can display the pixel buffer, however, the colors are often wrong. If I try to display a black image, the end result can be dark gray. If I try to display a red image, the image can be a bit off-red.
The raw pixel buffer is represented by a C-style array, with each pixel being 32 bits in size and with four 8-bit channels, laid out as XRGB, with the 'X' channel needing to be ignored. The pixel buffer gets wrapped via QImage, using QImage::Format_RGB32.
The problem I encounter is that the pixel buffer's 'X' channel appears to be getting used as if it were an alpha channel. If this is set to 0xff for each pixel, the tinting goes away. If it is set to 0x00, my image is tinted.
Code that demos this is shown at https://gist.github.com/DavidLudwig/6d460e1c67e9f44ca754e1ef6868ef28
This sample code is supposed to show a black window, however, the result is gray for me. Here's a screenshot:
Any thoughts on what's up, or perhaps how to fix? Modifying each pixel's 'X' channel is not a viable option, unfortunately.
-
The behaviour you describe is as I would expect from RGB32, as it is essentially AARRGGBB where the alpha is 0xff.
Unless you can find a recognized bitmap format that ignores the alpha then you WILL have to manually make sure it is set to 0xff in every sample. That means iterating through the array and manually setting it.
-
Thanks for the reply, Kent.
Do you have any idea if another solution might be to use a second, black widget, to effectively serve as a background behind the image?
-
cannot think of another solution, other than iterating through the source. Of course you can optimize that to some extent with some fancy iteration logic and using pointer references whenever possible.
-
You can try to load it as ARGB32 and then let Qt convert it to RGB32. Then the alpha value should be set to 0xff everywhere. See https://code.woboq.org/qt5/qtbase/src/gui/image/qimage_conversions.cpp.html#_ZL20fetchRGB32ToARGB32PMPjPKhiiPK7QVectorIjEP11QDitherInfo