0

QPainter is responsible for drawing and compositing in Qt. There is a section in the documentation that talks about performance. My question is regarding the bolded sentence from the following paragraph.

Raster - This backend implements all rendering in pure software and is always used to render into QImages. For optimal performance only use the format types QImage::Format_ARGB32_Premultiplied, QImage::Format_RGB32 or QImage::Format_RGB16. Any other format, including QImage::Format_ARGB32, has significantly worse performance. This engine is used by default for QWidget and QPixmap.

I understand that multiplying the color channels by the alpha is something that is done in the source-over operation. This multiplication can be done ahead-of-time to avoid doing it in the compositor. Performing this multiplication involves multiplying the RGB channels by the alpha and then dividing by 255 (or multiplying by some magic number that overflows in the right way to mimick the division). That's six integer multiplications per pixel. Surely performing an extra six integer multiplications does not have "significantly worse performance"?

Is the alpha multiplication really that slow? Perhaps they are merely stating that they don't try to optimize that code path as much as the other so there are no guarantees as to how it performs?

Indiana Kernick
  • 5,041
  • 2
  • 20
  • 50
  • 2
    `That's six integer multiplications per pixel`, so for 4k movie, it is 6 x 3840 x 2160 x 60 = ~3000M multiplications per second – Iłya Bursov Feb 07 '19 at 00:13
  • @IłyaBursov That's a lot of multiplications! I'm not dealing with that kind of throughput in my application though. – Indiana Kernick Feb 07 '19 at 00:17
  • `That's six integer multiplications per pixel` oh that's bad. very bad. Any decent resolution has more than 500k pixels. 3 millions multiplication for a single image is not a joke. – UmNyobe Feb 07 '19 at 14:39

2 Answers2

0

Please have a look at the detailed explanation here: https://pspdfkit.com/blog/2016/a-curious-case-of-android-alpha/ Of course, it does not refer direclty to Qt, but to the case why premultiplied bitmaps make sense.

Marcel Petrick
  • 454
  • 3
  • 15
  • 1
    I read the article and it doesn't answer my question. Using premultiplied alpha images makes sense 99% of the time but not in my case. – Indiana Kernick Feb 07 '19 at 11:14
0

It makes some sense in your case, since I presume that some widget paints the image, and the assumption is that it may paint it more than once. In any case, the widget, during painting, will premultiply the alpha. So you might as well be very explicit about it - after all, image format conversions are one-liners, so it's not as if you had to write a page of code to handle it. So:

class MyViewer : public QWidget {
  Q_OBJECT
  QImage m_image;
public:
  Q_SLOT void setImage(const QImage &image) {
    m_image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
    update();
  }
  ...
};
Kuba hasn't forgotten Monica
  • 95,931
  • 16
  • 151
  • 313