From 3ed69d2f60394fd015af6668ca29249d2366d4da Mon Sep 17 00:00:00 2001 From: David Hurka Date: Thu, 30 Jul 2020 21:56:05 +0000 Subject: [PATCH] QPainter can setOpacity() now, use that instead of PagePainter::changeImageAlpha(). That saves bit manipulation and deep copies. --- ui/pagepainter.cpp | 52 +++++++++------------------------------------- ui/pagepainter.h | 3 --- 2 files changed, 10 insertions(+), 45 deletions(-) diff --git a/ui/pagepainter.cpp b/ui/pagepainter.cpp index 641f5e69d..91140d840 100644 --- a/ui/pagepainter.cpp +++ b/ui/pagepainter.cpp @@ -531,10 +531,11 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter, acolor = Qt::yellow; acolor.setAlpha(opacity); - // get annotation boundary and drawn rect + // Annotation boundary in destPainter coordinates: QRect annotBoundary = a->transformedBoundingRectangle().geometry(scaledWidth, scaledHeight).translated(-scaledCrop.topLeft()); QRect annotRect = annotBoundary.intersected(limits); - QRect innerRect(annotRect.left() - annotBoundary.left(), annotRect.top() - annotBoundary.top(), annotRect.width(), annotRect.height()); + // Visible portion of the annotation at annotBoundary size: + QRect innerRect = annotRect.translated(-annotBoundary.topLeft()); QRectF dInnerRect(innerRect.x() * dpr, innerRect.y() * dpr, innerRect.width() * dpr, innerRect.height() * dpr); Okular::Annotation::SubType type = a->subType(); @@ -594,22 +595,17 @@ void PagePainter::paintCroppedPageOnPainter(QPainter *destPainter, Okular::StampAnnotation *stamp = (Okular::StampAnnotation *)a; // get pixmap and alpha blend it if needed - QPixmap pixmap = GuiUtils::loadStamp(stamp->stampIconName(), annotBoundary.width()); + QPixmap pixmap = GuiUtils::loadStamp(stamp->stampIconName(), qMax(annotBoundary.width(), annotBoundary.height()) * dpr); if (!pixmap.isNull()) // should never happen but can happen on huge sizes { - const QRect dInnerRect(QRectF(innerRect.x() * dpr, innerRect.y() * dpr, innerRect.width() * dpr, innerRect.height() * dpr).toAlignedRect()); - - QPixmap scaledCroppedPixmap = pixmap.scaled(annotBoundary.width() * dpr, annotBoundary.height() * dpr).copy(dInnerRect); + QPixmap scaledCroppedPixmap = pixmap.scaled(annotBoundary.width() * dpr, annotBoundary.height() * dpr).copy(dInnerRect.toAlignedRect()); scaledCroppedPixmap.setDevicePixelRatio(dpr); - QImage scaledCroppedImage = scaledCroppedPixmap.toImage(); - - if (opacity < 255) - changeImageAlpha(scaledCroppedImage, opacity); - pixmap = QPixmap::fromImage(scaledCroppedImage); - - // draw the scaled and al - mixedPainter->drawPixmap(annotRect.topLeft(), pixmap); + // Draw pixmap with opacity: + mixedPainter->save(); + mixedPainter->setOpacity(mixedPainter->opacity() * opacity / 255.0); + mixedPainter->drawPixmap(annotRect.topLeft(), scaledCroppedPixmap); + mixedPainter->restore(); } } // draw GeomAnnotation @@ -958,34 +954,6 @@ void PagePainter::hueShiftNegative(QImage *image) } } -/** Private Helpers :: Image Drawing **/ -// from Arthur - qt4 -static inline int qt_div_255(int x) -{ - return (x + (x >> 8) + 0x80) >> 8; -} - -void PagePainter::changeImageAlpha(QImage &image, unsigned int destAlpha) -{ - image = image.convertToFormat(QImage::Format_ARGB32); - // iterate over all pixels changing the alpha component value - unsigned int *data = reinterpret_cast(image.bits()); - unsigned int pixels = image.width() * image.height(); - - int source, sourceAlpha; - for (unsigned int i = 0; i < pixels; ++i) { // optimize this loop keeping byte order into account - source = data[i]; - if ((sourceAlpha = qAlpha(source)) == 255) { - // use destAlpha - data[i] = qRgba(qRed(source), qGreen(source), qBlue(source), destAlpha); - } else { - // use destAlpha * sourceAlpha product - sourceAlpha = qt_div_255(destAlpha * sourceAlpha); - data[i] = qRgba(qRed(source), qGreen(source), qBlue(source), sourceAlpha); - } - } -} - void PagePainter::drawShapeOnImage(QImage &image, const NormalizedPath &normPath, bool closeShape, const QPen &pen, const QBrush &brush, double penWidthMultiplier, RasterOperation op // float antiAliasRadius ) diff --git a/ui/pagepainter.h b/ui/pagepainter.h index 6ee868d5a..fc42006b3 100644 --- a/ui/pagepainter.h +++ b/ui/pagepainter.h @@ -82,9 +82,6 @@ private: */ static void hueShiftNegative(QImage *image); - // set the alpha component of the image to a given value - static void changeImageAlpha(QImage &image, unsigned int alpha); - // my pretty dear raster function typedef QList NormalizedPath; enum RasterOperation { Normal, Multiply };