QGIS API Documentation 4.3.0-Master (35968c35f7e)
Loading...
Searching...
No Matches
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
20#include <algorithm>
21
22#include "qgscolorutils.h"
23#include "qgsimageoperation.h"
24#include "qgspainting.h"
25#include "qgsrendercontext.h"
26
27#include <QString>
28
29using namespace Qt::StringLiterals;
30
31std::unique_ptr<QgsPaintEffect> QgsColorEffect::create( const QVariantMap &map )
32{
33 auto newEffect = std::make_unique<QgsColorEffect>();
34 newEffect->readProperties( map );
35 return newEffect;
36}
37
39 : mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
40{}
41
46
48{
49 if ( !enabled() || !context.painter() || source().isNull() )
50 return;
51
53 {
54 //just draw unmodified source, we can't render this effect when forcing vectors
55 drawSource( *context.painter() );
56 return;
57 }
58
59 QPainter *painter = context.painter();
60
61 //rasterize source and apply modifications
62 QImage image = sourceAsImage( context ).copy();
63
64 QgsImageOperation::adjustBrightnessContrast( image, mBrightness, mContrast / 100.0 + 1, context.feedback() );
65
66 if ( context.feedback() && context.feedback()->isCanceled() )
67 return;
68
69 if ( mGrayscaleMode != QgsImageOperation::GrayscaleOff )
70 {
71 QgsImageOperation::convertToGrayscale( image, static_cast< QgsImageOperation::GrayscaleMode >( mGrayscaleMode ), context.feedback() );
72 }
73
74 if ( context.feedback() && context.feedback()->isCanceled() )
75 return;
76
77 QgsImageOperation::adjustHueSaturation( image, mSaturation, mColorizeOn ? mColorizeColor : QColor(), mColorizeStrength / 100.0, context.feedback() );
78
79 if ( context.feedback() && context.feedback()->isCanceled() )
80 return;
81
82 QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
83
84 if ( context.feedback() && context.feedback()->isCanceled() )
85 return;
86
87 QgsScopedQPainterState painterState( painter );
88 painter->setCompositionMode( mBlendMode );
89 painter->drawImage( imageOffset( context ), image );
90}
91
92
93QVariantMap QgsColorEffect::properties() const
94{
95 QVariantMap props;
96 props.insert( u"enabled"_s, mEnabled ? u"1"_s : u"0"_s );
97 props.insert( u"draw_mode"_s, QString::number( int( mDrawMode ) ) );
98 props.insert( u"blend_mode"_s, QString::number( int( mBlendMode ) ) );
99 props.insert( u"opacity"_s, QString::number( mOpacity ) );
100 props.insert( u"brightness"_s, QString::number( mBrightness ) );
101 props.insert( u"contrast"_s, QString::number( mContrast ) );
102 props.insert( u"saturation"_s, QString::number( mSaturation ) );
103 props.insert( u"grayscale_mode"_s, QString::number( int( mGrayscaleMode ) ) );
104 props.insert( u"colorize"_s, mColorizeOn ? u"1"_s : u"0"_s );
105 props.insert( u"colorize_color"_s, QgsColorUtils::colorToString( mColorizeColor ) );
106 props.insert( u"colorize_strength"_s, QString::number( mColorizeStrength ) );
107
108 return props;
109}
110
111void QgsColorEffect::readProperties( const QVariantMap &props )
112{
113 bool ok;
114 QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( u"blend_mode"_s ).toInt( &ok ) );
115 if ( ok )
116 {
117 mBlendMode = mode;
118 }
119 if ( props.contains( u"transparency"_s ) )
120 {
121 double transparency = props.value( u"transparency"_s ).toDouble( &ok );
122 if ( ok )
123 {
124 mOpacity = 1.0 - transparency;
125 }
126 }
127 else
128 {
129 double opacity = props.value( u"opacity"_s ).toDouble( &ok );
130 if ( ok )
131 {
132 mOpacity = opacity;
133 }
134 }
135 mEnabled = props.value( u"enabled"_s, u"1"_s ).toInt();
136 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( u"draw_mode"_s, u"2"_s ).toInt() );
137
138 mBrightness = props.value( u"brightness"_s, u"0"_s ).toInt();
139 mContrast = props.value( u"contrast"_s, u"0"_s ).toInt();
140 mSaturation = props.value( u"saturation"_s, u"1.0"_s ).toDouble();
141 mGrayscaleMode = static_cast< QgsImageOperation::GrayscaleMode >( props.value( u"grayscale_mode"_s, u"0"_s ).toInt() );
142 mColorizeOn = props.value( u"colorize"_s, u"0"_s ).toInt();
143 if ( props.contains( u"colorize_color"_s ) )
144 {
145 setColorizeColor( QgsColorUtils::colorFromString( props.value( u"colorize_color"_s ).toString() ) );
146 }
147 mColorizeStrength = props.value( u"colorize_strength"_s, u"100"_s ).toInt();
148}
149
151{
152 QgsColorEffect *newEffect = new QgsColorEffect( *this );
153 return newEffect;
154}
155
157{
158 mBrightness = std::clamp( brightness, -255, 255 );
159}
160
162{
163 mContrast = std::clamp( contrast, -100, 100 );
164}
165
167{
168 mColorizeColor = colorizeColor;
169}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
Definition qgis.h:2919
@ RequiresRasterization
The effect requires raster-based rendering.
Definition qgis.h:3023
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:3032
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...
double opacity() const
Returns the opacity for the effect.
QgsColorEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
void setColorizeColor(const QColor &colorizeColor)
Sets the color used for colorizing a picture.
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
int contrast() const
Returns the contrast modification for the effect.
static std::unique_ptr< QgsPaintEffect > create(const QVariantMap &map)
Creates a new QgsColorEffect effect from a properties string map.
int brightness() const
Returns the brightness modification for the effect.
void setContrast(int contrast)
Sets the contrast modification for the effect.
void draw(QgsRenderContext &context) override
Handles drawing of the effect's result on to the specified render context.
void setBrightness(int brightness)
Sets the brightness modification for the effect.
QVariantMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
QColor colorizeColor() const
Returns the color used for colorizing a picture.
static QColor colorFromString(const QString &string)
Decodes a string into a color value.
static QString colorToString(const QColor &color)
Encodes a color into a string value.
bool isCanceled() const
Tells whether the operation has been canceled already.
Definition qgsfeedback.h:56
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.
static void multiplyOpacity(QImage &image, double factor, QgsFeedback *feedback=nullptr)
Multiplies opacity of image pixel values by a factor.
static void adjustBrightnessContrast(QImage &image, int brightness, double contrast, QgsFeedback *feedback=nullptr)
Alter the brightness or contrast of a QImage.
static void convertToGrayscale(QImage &image, GrayscaleMode mode=GrayscaleLuminosity, QgsFeedback *feedback=nullptr)
Convert a QImage to a grayscale image.
GrayscaleMode
Modes for converting a QImage to grayscale.
void drawSource(QPainter &painter)
Draws the source QPicture onto the specified painter.
QImage sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
const QPicture & source() const
Returns the source QPicture.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
bool enabled() const
Returns whether the effect is enabled.
DrawMode
Drawing modes for effects.
Contains information about the context of a rendering operation.
QPainter * painter()
Returns the destination QPainter for the render operation.
QgsFeedback * feedback() const
Returns the feedback object that can be queried regularly during rendering to check if rendering shou...
Qgis::RasterizedRenderingPolicy rasterizedRenderingPolicy() const
Returns the policy controlling when rasterisation of content during renders is permitted.
Scoped object for saving and restoring a QPainter object's state.