From 9e4ef5e3800ce2ef702e170bc43e50b19ed090ce Mon Sep 17 00:00:00 2001 From: Christian Muehlhaeuser Date: Tue, 12 Aug 2014 20:18:47 +0200 Subject: [PATCH] * Custom blur method included. May be replace by Qt's internal one eventually. --- src/libtomahawk/utils/TomahawkUtilsGui.cpp | 83 ++++++++++++++++++++++ src/libtomahawk/utils/TomahawkUtilsGui.h | 2 + 2 files changed, 85 insertions(+) diff --git a/src/libtomahawk/utils/TomahawkUtilsGui.cpp b/src/libtomahawk/utils/TomahawkUtilsGui.cpp index c260fdbcb..21d1008c3 100644 --- a/src/libtomahawk/utils/TomahawkUtilsGui.cpp +++ b/src/libtomahawk/utils/TomahawkUtilsGui.cpp @@ -932,6 +932,89 @@ squareCenterPixmap( const QPixmap& sourceImage ) } +QImage +blurred( const QImage& image, const QRect& rect, int radius, bool alphaOnly, bool blackWhite ) +{ + int tab[] = { 14, 10, 8, 6, 5, 5, 4, 3, 3, 3, 3, 2, 2, 2, 2, 2, 2 }; + int alpha = (radius < 1) ? 16 : (radius > 17) ? 1 : tab[radius-1]; + + QImage result = image.convertToFormat(QImage::Format_ARGB32_Premultiplied); + if ( blackWhite ) + { + for ( int i = 0; i < result.width(); ++i ) + { + for (int j = 0; j < result.height(); ++j) + { + const QRgb col = result.pixel(i, j); + const int gray = qGray(col); + result.setPixel(i, j, qRgb(gray*0.3, gray*0.3, gray*0.3)); + } + } + } + + int r1 = rect.top(); + int r2 = rect.bottom(); + int c1 = rect.left(); + int c2 = rect.right(); + + int bpl = result.bytesPerLine(); + int rgba[4]; + unsigned char* p; + + int i1 = 0; + int i2 = 3; + + if (alphaOnly) + i1 = i2 = (QSysInfo::ByteOrder == QSysInfo::BigEndian ? 0 : 3); + + for (int col = c1; col <= c2; col++) { + p = result.scanLine(r1) + col * 4; + for (int i = i1; i <= i2; i++) + rgba[i] = p[i] << 4; + + p += bpl; + for (int j = r1; j < r2; j++, p += bpl) + for (int i = i1; i <= i2; i++) + p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; + } + + for (int row = r1; row <= r2; row++) { + p = result.scanLine(row) + c1 * 4; + for (int i = i1; i <= i2; i++) + rgba[i] = p[i] << 4; + + p += 4; + for (int j = c1; j < c2; j++, p += 4) + for (int i = i1; i <= i2; i++) + p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; + } + + for (int col = c1; col <= c2; col++) { + p = result.scanLine(r2) + col * 4; + for (int i = i1; i <= i2; i++) + rgba[i] = p[i] << 4; + + p -= bpl; + for (int j = r1; j < r2; j++, p -= bpl) + for (int i = i1; i <= i2; i++) + p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; + } + + for (int row = r1; row <= r2; row++) { + p = result.scanLine(row) + c2 * 4; + for (int i = i1; i <= i2; i++) + rgba[i] = p[i] << 4; + + p -= 4; + for (int j = c1; j < c2; j++, p -= 4) + for (int i = i1; i <= i2; i++) + p[i] = (rgba[i] += ((p[i] << 4) - rgba[i]) * alpha / 16) >> 4; + } + + return result; +} + + void drawCompositedPopup( QWidget* widget, const QPainterPath& outline, diff --git a/src/libtomahawk/utils/TomahawkUtilsGui.h b/src/libtomahawk/utils/TomahawkUtilsGui.h index 109edc67d..cdc4ef8e9 100644 --- a/src/libtomahawk/utils/TomahawkUtilsGui.h +++ b/src/libtomahawk/utils/TomahawkUtilsGui.h @@ -73,6 +73,8 @@ namespace TomahawkUtils DLLEXPORT QPixmap addDropShadow( const QPixmap& sourceImage, const QSize& targetSize ); DLLEXPORT QPixmap squareCenterPixmap( const QPixmap& sourceImage ); + DLLEXPORT QImage blurred( const QImage& image, const QRect& rect, int radius, bool alphaOnly = false, bool blackWhite = false ); + DLLEXPORT void drawCompositedPopup( QWidget* widget, const QPainterPath& outline, const QColor& lineColor, const QBrush& backgroundBrush, qreal opacity ); }