-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathImageUtils.cpp
87 lines (71 loc) · 2.74 KB
/
ImageUtils.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include "ImageUtils.h"
// Qt 4.6.2 includes a wonderfully optimized blur function in /src/gui/image/qpixmapfilter.cpp
// I'll just hook into their implementation here, instead of reinventing the wheel.
extern void qt_blurImage(QImage &blurImage, qreal radius, bool quality, int transposed = 0);
const qreal radiusScale = qreal(1.5);
// Copied from QPixmapBlurFilter::boundingRectFor(const QRectF &rect)
QRectF ImageUtils::blurredBoundingRectFor(const QRectF &rect, int radius)
{
const qreal delta = radiusScale * radius + 1;
return rect.adjusted(-delta, -delta, delta, delta);
}
QSizeF ImageUtils::blurredSizeFor(const QSizeF &size, int radius)
{
const qreal delta = radiusScale * radius + 1;
QSizeF newSize(size.width() + delta,
size.height() + delta);
return newSize;
}
// Modifies the input image, no copying
void ImageUtils::blurImage(QImage& image, int radius, bool highQuality)
{
qt_blurImage(image, radius, highQuality);
}
// Blur the image according to the blur radius
// Based on exponential blur algorithm by Jani Huhtanen
// (maximum radius is set to 16)
QImage ImageUtils::blurred(const QImage& image, const QRect& /*rect*/, int radius)
{
QImage copy = image.copy();
qt_blurImage(copy, radius, false);
return copy;
}
QImage ImageUtils::addDropShadow(QImage markerGroup, double shadowSize)
{
// Add in drop shadow
if(shadowSize > 0.0)
{
// double shadowOffsetX = 0.0;
// double shadowOffsetY = 0.0;
QColor shadowColor = Qt::black;
// create temporary pixmap to hold a copy of the text
QSizeF blurSize = blurredSizeFor(markerGroup.size(), (int)shadowSize);
//qDebug() << "Blur size:"<<blurSize<<", doc:"<<doc.size()<<", shadowSize:"<<shadowSize;
QImage tmpImage(blurSize.toSize(),QImage::Format_ARGB32_Premultiplied);
memset(tmpImage.bits(),0,tmpImage.byteCount()); // init transparent
// render the text
QPainter tmpPainter(&tmpImage);
tmpPainter.save();
tmpPainter.translate(shadowSize, shadowSize);
tmpPainter.drawImage(0,0,markerGroup);
tmpPainter.restore();
// blacken the image by applying a color to the copy using a QPainter::CompositionMode_DestinationIn operation.
// This produces a homogeneously-colored pixmap.
QRect rect = tmpImage.rect();
tmpPainter.setCompositionMode(QPainter::CompositionMode_SourceIn);
tmpPainter.fillRect(rect, shadowColor);
tmpPainter.end();
// blur the colored image
blurImage(tmpImage, (int)shadowSize);
// Render the original image back over the shadow
tmpPainter.begin(&tmpImage);
tmpPainter.save();
// tmpPainter.translate(shadowOffsetX - shadowSize,
// shadowOffsetY - shadowSize);
tmpPainter.translate(shadowSize, shadowSize);
tmpPainter.drawImage(0,0,markerGroup);
tmpPainter.restore();
markerGroup = tmpImage;
}
return markerGroup;
}