QGIS API Documentation  3.8.0-Zanzibar (11aff65)
qgsblureffect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgsblureffect.cpp
3  -----------------
4  begin : December 2014
5  copyright : (C) 2014 Nyall Dawson
6  email : nyall dot dawson at gmail dot com
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #include "qgsblureffect.h"
19 #include "qgsimageoperation.h"
20 #include "qgsrendercontext.h"
21 #include "qgssymbollayerutils.h"
22 
24 {
25  QgsBlurEffect *newEffect = new QgsBlurEffect();
26  newEffect->readProperties( map );
27  return newEffect;
28 }
29 
31 {
32  if ( !source() || !enabled() || !context.painter() )
33  return;
34 
35  switch ( mBlurMethod )
36  {
37  case StackBlur:
38  drawStackBlur( context );
39  break;
40  case GaussianBlur:
41  drawGaussianBlur( context );
42  break;
43  }
44 }
45 
46 void QgsBlurEffect::drawStackBlur( QgsRenderContext &context )
47 {
48  int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale ) );
49  QImage im = sourceAsImage( context )->copy();
50  QgsImageOperation::stackBlur( im, blurLevel );
51  drawBlurredImage( context, im );
52 }
53 
54 void QgsBlurEffect::drawGaussianBlur( QgsRenderContext &context )
55 {
56  int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale ) );
57  QImage *im = QgsImageOperation::gaussianBlur( *sourceAsImage( context ), blurLevel );
58  drawBlurredImage( context, *im );
59  delete im;
60 }
61 
62 void QgsBlurEffect::drawBlurredImage( QgsRenderContext &context, QImage &image )
63 {
64  //opacity
65  QgsImageOperation::multiplyOpacity( image, mOpacity );
66 
67  QPainter *painter = context.painter();
68  painter->save();
69  painter->setCompositionMode( mBlendMode );
70  painter->drawImage( imageOffset( context ), image );
71  painter->restore();
72 }
73 
75 {
76  QgsStringMap props;
77  props.insert( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
78  props.insert( QStringLiteral( "draw_mode" ), QString::number( static_cast< int >( mDrawMode ) ) );
79  props.insert( QStringLiteral( "blend_mode" ), QString::number( static_cast< int >( mBlendMode ) ) );
80  props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
81  props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
82  props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
83  props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
84  props.insert( QStringLiteral( "blur_method" ), QString::number( static_cast< int >( mBlurMethod ) ) );
85  return props;
86 }
87 
89 {
90  bool ok;
91  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
92  if ( ok )
93  {
94  mBlendMode = mode;
95  }
96  if ( props.contains( QStringLiteral( "transparency" ) ) )
97  {
98  double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
99  if ( ok )
100  {
101  mOpacity = 1.0 - transparency;
102  }
103  }
104  else
105  {
106  double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
107  if ( ok )
108  {
109  mOpacity = opacity;
110  }
111  }
112 
113  mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
114  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
115  double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
116  if ( ok )
117  {
118  mBlurLevel = level;
119  if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
120  {
121  // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
122  mBlurLevel *= 0.2645;
123  }
124  }
125  mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ) );
126  mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ) );
127  QgsBlurEffect::BlurMethod method = static_cast< QgsBlurEffect::BlurMethod >( props.value( QStringLiteral( "blur_method" ) ).toInt( &ok ) );
128  if ( ok )
129  {
130  mBlurMethod = method;
131  }
132 }
133 
135 {
136  QgsBlurEffect *newEffect = new QgsBlurEffect( *this );
137  return newEffect;
138 }
139 
140 QRectF QgsBlurEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
141 {
142  int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale ) );
143  //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
144  double spread = blurLevel * 2.0 + 10;
145  return rect.adjusted( -spread, -spread, spread, spread );
146 }
double opacity() const
Returns the opacity for the effect.
static void multiplyOpacity(QImage &image, double factor)
Multiplies opacity of image pixel values by a factor.
DrawMode mDrawMode
QgsStringMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QgsBlurEffect()=default
Constructor for QgsBlurEffect.
Base class for visual effects which can be applied to QPicture drawings.
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
Gaussian blur, a slower but high quality blur. Blur level values are the distance in pixels for the b...
Definition: qgsblureffect.h:43
QMap< QString, QString > QgsStringMap
Definition: qgis.h:587
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
Stack blur, a fast but low quality blur. Valid blur level values are between 0 - 16.
Definition: qgsblureffect.h:42
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
BlurMethod
Available blur methods (algorithms)
Definition: qgsblureffect.h:40
bool enabled() const
Returns whether the effect is enabled.
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:34
DrawMode
Drawing modes for effects.
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
static QImage * gaussianBlur(QImage &image, int radius)
Performs a gaussian blur on an image.
Contains information about the context of a rendering operation.
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale()) const
Converts a size from the specified units to painter units (pixels).
void draw(QgsRenderContext &context) override
Handles drawing of the effect&#39;s result on to the specified render context.
QPainter * painter()
Returns the destination QPainter for the render operation.
static void stackBlur(QImage &image, int radius, bool alphaOnly=false)
Performs a stack blur on an image.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
const QPicture * source() const
Returns the source QPicture.
double blurLevel() const
Returns the blur level (radius)
Definition: qgsblureffect.h:83
void readProperties(const QgsStringMap &props) override
Reads a string map of an effect&#39;s properties and restores the effect to the state described by the pr...
QgsBlurEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.