QGIS API Documentation  3.26.3-Buenos Aires (65e4edfdad)
qgscoloreffect.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  qgscoloreffect.cpp
3  ------------------
4  begin : March 2015
5  copyright : (C) 2015 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 "qgscoloreffect.h"
19 #include "qgsimageoperation.h"
20 #include "qgssymbollayerutils.h"
21 #include <algorithm>
22 
23 QgsPaintEffect *QgsColorEffect::create( const QVariantMap &map )
24 {
25  QgsColorEffect *newEffect = new QgsColorEffect();
26  newEffect->readProperties( map );
27  return newEffect;
28 }
29 
31  : mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
32 {
33 
34 }
35 
37 {
38  if ( !source() || !enabled() || !context.painter() )
39  return;
40 
41  QPainter *painter = context.painter();
42 
43  //rasterize source and apply modifications
44  QImage image = sourceAsImage( context )->copy();
45 
46  QgsImageOperation::adjustBrightnessContrast( image, mBrightness, mContrast / 100.0 + 1, context.feedback() );
47 
48  if ( context.feedback() && context.feedback()->isCanceled() )
49  return;
50 
51  if ( mGrayscaleMode != QgsImageOperation::GrayscaleOff )
52  {
53  QgsImageOperation::convertToGrayscale( image, static_cast< QgsImageOperation::GrayscaleMode >( mGrayscaleMode ), context.feedback() );
54  }
55 
56  if ( context.feedback() && context.feedback()->isCanceled() )
57  return;
58 
59  QgsImageOperation::adjustHueSaturation( image, mSaturation, mColorizeOn ? mColorizeColor : QColor(), mColorizeStrength / 100.0, context.feedback() );
60 
61  if ( context.feedback() && context.feedback()->isCanceled() )
62  return;
63 
64  QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
65 
66  if ( context.feedback() && context.feedback()->isCanceled() )
67  return;
68 
69  QgsScopedQPainterState painterState( painter );
70  painter->setCompositionMode( mBlendMode );
71  painter->drawImage( imageOffset( context ), image );
72 }
73 
74 
75 QVariantMap QgsColorEffect::properties() const
76 {
77  QVariantMap props;
78  props.insert( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
79  props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
80  props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
81  props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
82  props.insert( QStringLiteral( "brightness" ), QString::number( mBrightness ) );
83  props.insert( QStringLiteral( "contrast" ), QString::number( mContrast ) );
84  props.insert( QStringLiteral( "saturation" ), QString::number( mSaturation ) );
85  props.insert( QStringLiteral( "grayscale_mode" ), QString::number( int( mGrayscaleMode ) ) );
86  props.insert( QStringLiteral( "colorize" ), mColorizeOn ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
87  props.insert( QStringLiteral( "colorize_color" ), QgsSymbolLayerUtils::encodeColor( mColorizeColor ) );
88  props.insert( QStringLiteral( "colorize_strength" ), QString::number( mColorizeStrength ) );
89 
90  return props;
91 }
92 
93 void QgsColorEffect::readProperties( const QVariantMap &props )
94 {
95  bool ok;
96  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
97  if ( ok )
98  {
99  mBlendMode = mode;
100  }
101  if ( props.contains( QStringLiteral( "transparency" ) ) )
102  {
103  double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
104  if ( ok )
105  {
106  mOpacity = 1.0 - transparency;
107  }
108  }
109  else
110  {
111  double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
112  if ( ok )
113  {
114  mOpacity = opacity;
115  }
116  }
117  mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
118  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
119 
120  mBrightness = props.value( QStringLiteral( "brightness" ), QStringLiteral( "0" ) ).toInt();
121  mContrast = props.value( QStringLiteral( "contrast" ), QStringLiteral( "0" ) ).toInt();
122  mSaturation = props.value( QStringLiteral( "saturation" ), QStringLiteral( "1.0" ) ).toDouble();
123  mGrayscaleMode = static_cast< QgsImageOperation::GrayscaleMode >( props.value( QStringLiteral( "grayscale_mode" ), QStringLiteral( "0" ) ).toInt() );
124  mColorizeOn = props.value( QStringLiteral( "colorize" ), QStringLiteral( "0" ) ).toInt();
125  if ( props.contains( QStringLiteral( "colorize_color" ) ) )
126  {
127  setColorizeColor( QgsSymbolLayerUtils::decodeColor( props.value( QStringLiteral( "colorize_color" ) ).toString() ) );
128  }
129  mColorizeStrength = props.value( QStringLiteral( "colorize_strength" ), QStringLiteral( "100" ) ).toInt();
130 }
131 
133 {
134  QgsColorEffect *newEffect = new QgsColorEffect( *this );
135  return newEffect;
136 }
137 
138 void QgsColorEffect::setBrightness( int brightness )
139 {
140  mBrightness = std::clamp( brightness, -255, 255 );
141 }
142 
143 void QgsColorEffect::setContrast( int contrast )
144 {
145  mContrast = std::clamp( contrast, -100, 100 );
146 }
147 
148 void QgsColorEffect::setColorizeColor( const QColor &colorizeColor )
149 {
150  mColorizeColor = colorizeColor;
151 }
QgsColorEffect::opacity
double opacity() const
Returns the opacity for the effect.
Definition: qgscoloreffect.h:194
QgsPaintEffect::DrawMode
DrawMode
Drawing modes for effects.
Definition: qgspainteffect.h:103
QgsSymbolLayerUtils::encodeColor
static QString encodeColor(const QColor &color)
Definition: qgssymbollayerutils.cpp:64
QgsColorEffect::brightness
int brightness() const
Returns the brightness modification for the effect.
Definition: qgscoloreffect.h:70
QgsColorEffect::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: qgscoloreffect.cpp:93
QgsColorEffect::colorizeColor
QColor colorizeColor() const
Returns the color used for colorizing a picture.
Definition: qgscoloreffect.h:158
QgsColorEffect::setColorizeColor
void setColorizeColor(const QColor &colorizeColor)
Sets the color used for colorizing a picture.
Definition: qgscoloreffect.cpp:148
qgssymbollayerutils.h
QgsFeedback::isCanceled
bool isCanceled() const SIP_HOLDGIL
Tells whether the operation has been canceled already.
Definition: qgsfeedback.h:67
QgsRenderContext
Contains information about the context of a rendering operation.
Definition: qgsrendercontext.h:59
QgsColorEffect
A paint effect which alters the colors (e.g., brightness, contrast) in a source picture.
Definition: qgscoloreffect.h:35
QgsImageOperation::GrayscaleOff
@ GrayscaleOff
No change.
Definition: qgsimageoperation.h:60
qgsimageoperation.h
QgsSymbolLayerUtils::decodeColor
static QColor decodeColor(const QString &str)
Definition: qgssymbollayerutils.cpp:69
QgsPaintEffect::mEnabled
bool mEnabled
Definition: qgspainteffect.h:224
qgscoloreffect.h
QgsColorEffect::clone
QgsColorEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
Definition: qgscoloreffect.cpp:132
QgsColorEffect::setBrightness
void setBrightness(int brightness)
Sets the brightness modification for the effect.
Definition: qgscoloreffect.cpp:138
QgsColorEffect::setContrast
void setContrast(int contrast)
Sets the contrast modification for the effect.
Definition: qgscoloreffect.cpp:143
QgsColorEffect::create
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsColorEffect effect from a properties string map.
Definition: qgscoloreffect.cpp:23
QgsImageOperation::adjustHueSaturation
static void adjustHueSaturation(QImage &image, double saturation, const QColor &colorizeColor=QColor(), double colorizeStrength=1.0, QgsFeedback *feedback=nullptr)
Alter the hue or saturation of a QImage.
Definition: qgsimageoperation.cpp:280
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
QgsColorEffect::QgsColorEffect
QgsColorEffect()
Definition: qgscoloreffect.cpp:30
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
QgsColorEffect::properties
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
Definition: qgscoloreffect.cpp:75
QgsPaintEffect
Base class for visual effects which can be applied to QPicture drawings.
Definition: qgspainteffect.h:52
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
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
QgsColorEffect::contrast
int contrast() const
Returns the contrast modification for the effect.
Definition: qgscoloreffect.h:88
QgsImageOperation::GrayscaleMode
GrayscaleMode
Modes for converting a QImage to grayscale.
Definition: qgsimageoperation.h:55
QgsImageOperation::convertToGrayscale
static void convertToGrayscale(QImage &image, GrayscaleMode mode=GrayscaleLuminosity, QgsFeedback *feedback=nullptr)
Convert a QImage to a grayscale image.
Definition: qgsimageoperation.cpp:195
QgsImageOperation::adjustBrightnessContrast
static void adjustBrightnessContrast(QImage &image, int brightness, double contrast, QgsFeedback *feedback=nullptr)
Alter the brightness or contrast of a QImage.
Definition: qgsimageoperation.cpp:256
QgsColorEffect::draw
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
Definition: qgscoloreffect.cpp:36