QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
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 
23 QgsPaintEffect *QgsBlurEffect::create( const QVariantMap &map )
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  const int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale, Qgis::RenderSubcomponentProperty::BlurSize ) );
49 
50  QImage im = sourceAsImage( context )->copy();
51  QgsImageOperation::stackBlur( im, blurLevel, false, context.feedback() );
52  drawBlurredImage( context, im );
53 }
54 
55 void QgsBlurEffect::drawGaussianBlur( QgsRenderContext &context )
56 {
57  const int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale, Qgis::RenderSubcomponentProperty::BlurSize ) );
58 
59  QImage *im = QgsImageOperation::gaussianBlur( *sourceAsImage( context ), blurLevel, context.feedback() );
60  if ( !im->isNull() )
61  drawBlurredImage( context, *im );
62  delete im;
63 }
64 
65 void QgsBlurEffect::drawBlurredImage( QgsRenderContext &context, QImage &image )
66 {
67  //opacity
68  QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
69 
70  QPainter *painter = context.painter();
71  const QgsScopedQPainterState painterState( painter );
72  painter->setCompositionMode( mBlendMode );
73  painter->drawImage( imageOffset( context ), image );
74 }
75 
76 QVariantMap QgsBlurEffect::properties() const
77 {
78  QVariantMap props;
79  props.insert( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
80  props.insert( QStringLiteral( "draw_mode" ), QString::number( static_cast< int >( mDrawMode ) ) );
81  props.insert( QStringLiteral( "blend_mode" ), QString::number( static_cast< int >( mBlendMode ) ) );
82  props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
83  props.insert( QStringLiteral( "blur_level" ), QString::number( mBlurLevel ) );
84  props.insert( QStringLiteral( "blur_unit" ), QgsUnitTypes::encodeUnit( mBlurUnit ) );
85  props.insert( QStringLiteral( "blur_unit_scale" ), QgsSymbolLayerUtils::encodeMapUnitScale( mBlurMapUnitScale ) );
86  props.insert( QStringLiteral( "blur_method" ), QString::number( static_cast< int >( mBlurMethod ) ) );
87  return props;
88 }
89 
90 void QgsBlurEffect::readProperties( const QVariantMap &props )
91 {
92  bool ok;
93  const QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
94  if ( ok )
95  {
96  mBlendMode = mode;
97  }
98  if ( props.contains( QStringLiteral( "transparency" ) ) )
99  {
100  const double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
101  if ( ok )
102  {
103  mOpacity = 1.0 - transparency;
104  }
105  }
106  else
107  {
108  const double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
109  if ( ok )
110  {
111  mOpacity = opacity;
112  }
113  }
114 
115  mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
116  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
117  const double level = props.value( QStringLiteral( "blur_level" ) ).toDouble( &ok );
118  if ( ok )
119  {
120  mBlurLevel = level;
121  if ( !props.contains( QStringLiteral( "blur_unit" ) ) )
122  {
123  // deal with pre blur unit era by assuming 96 dpi and converting pixel values as millimeters
124  mBlurLevel *= 0.2645;
125  }
126  }
127  mBlurUnit = QgsUnitTypes::decodeRenderUnit( props.value( QStringLiteral( "blur_unit" ) ).toString() );
128  mBlurMapUnitScale = QgsSymbolLayerUtils::decodeMapUnitScale( props.value( QStringLiteral( "blur_unit_scale" ) ).toString() );
129  const QgsBlurEffect::BlurMethod method = static_cast< QgsBlurEffect::BlurMethod >( props.value( QStringLiteral( "blur_method" ) ).toInt( &ok ) );
130  if ( ok )
131  {
132  mBlurMethod = method;
133  }
134 }
135 
137 {
138  QgsBlurEffect *newEffect = new QgsBlurEffect( *this );
139  return newEffect;
140 }
141 
142 QRectF QgsBlurEffect::boundingRect( const QRectF &rect, const QgsRenderContext &context ) const
143 {
144  const int blurLevel = std::round( context.convertToPainterUnits( mBlurLevel, mBlurUnit, mBlurMapUnitScale, Qgis::RenderSubcomponentProperty::BlurSize ) );
145 
146  //plus possible extension due to blur, with a couple of extra pixels thrown in for safety
147  const double spread = blurLevel * 2.0 + 10;
148  return rect.adjusted( -spread, -spread, spread, spread );
149 }
QgsBlurEffect::clone
QgsBlurEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
Definition: qgsblureffect.cpp:136
QgsPaintEffect::DrawMode
DrawMode
Drawing modes for effects.
Definition: qgspainteffect.h:103
QgsBlurEffect::readProperties
void readProperties(const QVariantMap &props) override
Reads a string map of an effect's properties and restores the effect to the state described by the pr...
Definition: qgsblureffect.cpp:90
QgsBlurEffect::draw
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
Definition: qgsblureffect.cpp:30
QgsBlurEffect
A paint effect which blurs a source picture, using a number of different blur methods.
Definition: qgsblureffect.h:37
QgsSymbolLayerUtils::encodeMapUnitScale
static QString encodeMapUnitScale(const QgsMapUnitScale &mapUnitScale)
Definition: qgssymbollayerutils.cpp:666
qgssymbollayerutils.h
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
qgsimageoperation.h
QgsImageOperation::gaussianBlur
static QImage * gaussianBlur(QImage &image, int radius, QgsFeedback *feedback=nullptr)
Performs a gaussian blur on an image.
Definition: qgsimageoperation.cpp:651
QgsPaintEffect::mEnabled
bool mEnabled
Definition: qgspainteffect.h:224
QgsBlurEffect::BlurMethod
BlurMethod
Available blur methods (algorithms)
Definition: qgsblureffect.h:43
QgsUnitTypes::decodeRenderUnit
static Q_INVOKABLE QgsUnitTypes::RenderUnit decodeRenderUnit(const QString &string, bool *ok=nullptr)
Decodes a render unit from a string.
Definition: qgsunittypes.cpp:2948
QgsBlurEffect::properties
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
Definition: qgsblureffect.cpp:76
QgsUnitTypes::encodeUnit
static Q_INVOKABLE QString encodeUnit(QgsUnitTypes::DistanceUnit unit)
Encodes a distance unit to a string.
Definition: qgsunittypes.cpp:122
QgsBlurEffect::create
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsBlurEffect effect from a properties string map.
Definition: qgsblureffect.cpp:23
QgsBlurEffect::StackBlur
@ StackBlur
Stack blur, a fast but low quality blur. Valid blur level values are between 0 - 16.
Definition: qgsblureffect.h:45
qgsrendercontext.h
QgsScopedQPainterState
Scoped object for saving and restoring a QPainter object's state.
Definition: qgsrendercontext.h:1336
QgsPaintEffect::imageOffset
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
Definition: qgspainteffect.cpp:170
QgsBlurEffect::blurLevel
double blurLevel() const
Returns the blur level (radius)
Definition: qgsblureffect.h:86
QgsRenderContext::convertToPainterUnits
double convertToPainterUnits(double size, QgsUnitTypes::RenderUnit unit, const QgsMapUnitScale &scale=QgsMapUnitScale(), Qgis::RenderSubcomponentProperty property=Qgis::RenderSubcomponentProperty::Generic) const
Converts a size from the specified units to painter units (pixels).
Definition: qgsrendercontext.cpp:367
QgsPaintEffect::mDrawMode
DrawMode mDrawMode
Definition: qgspainteffect.h:225
QgsRenderContext::feedback
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Definition: qgsrendercontext.cpp:206
QgsPaintEffect::sourceAsImage
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
Definition: qgspainteffect.cpp:145
QgsPaintEffect::source
const QPicture * source() const
Returns the source QPicture.
Definition: qgspainteffect.h:253
QgsBlurEffect::boundingRect
QRectF boundingRect(const QRectF &rect, const QgsRenderContext &context) const override
Returns the bounding rect required for drawing the effect.
Definition: qgsblureffect.cpp:142
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:52
QgsBlurEffect::QgsBlurEffect
QgsBlurEffect()=default
Constructor for QgsBlurEffect.
QgsImageOperation::multiplyOpacity
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
Definition: qgsimageoperation.cpp:335
QgsImageOperation::stackBlur
static void stackBlur(QImage &image, int radius, bool alphaOnly=false, QgsFeedback *feedback=nullptr)
Performs a stack blur on an image.
Definition: qgsimageoperation.cpp:585
QgsBlurEffect::opacity
double opacity() const
Returns the opacity for the effect.
Definition: qgsblureffect.h:156
QgsRenderContext::painter
QPainter * painter()
Returns the destination QPainter for the render operation.
Definition: qgsrendercontext.h:112
QgsPaintEffect::enabled
bool enabled() const
Returns whether the effect is enabled.
Definition: qgspainteffect.h:197
qgsblureffect.h
QgsBlurEffect::GaussianBlur
@ GaussianBlur
Gaussian blur, a slower but high quality blur. Blur level values are the distance in pixels for the b...
Definition: qgsblureffect.h:46
Qgis::RenderSubcomponentProperty::BlurSize
@ BlurSize
Blur size.
QgsSymbolLayerUtils::decodeMapUnitScale
static QgsMapUnitScale decodeMapUnitScale(const QString &str)
Definition: qgssymbollayerutils.cpp:676