QGIS API Documentation 3.99.0-Master (f78f5286a64)
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 "qgscolorutils.h"
20#include "qgsimageoperation.h"
21#include "qgsrendercontext.h"
22
23#include <algorithm>
24
25QgsPaintEffect *QgsColorEffect::create( const QVariantMap &map )
26{
27 QgsColorEffect *newEffect = new QgsColorEffect();
28 newEffect->readProperties( map );
29 return newEffect;
30}
31
33 : mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
34{
35
36}
37
42
44{
45 if ( !enabled() || !context.painter() || source().isNull() )
46 return;
47
49 {
50 //just draw unmodified source, we can't render this effect when forcing vectors
51 drawSource( *context.painter() );
52 return;
53 }
54
55 QPainter *painter = context.painter();
56
57 //rasterize source and apply modifications
58 QImage image = sourceAsImage( context ).copy();
59
60 QgsImageOperation::adjustBrightnessContrast( image, mBrightness, mContrast / 100.0 + 1, context.feedback() );
61
62 if ( context.feedback() && context.feedback()->isCanceled() )
63 return;
64
65 if ( mGrayscaleMode != QgsImageOperation::GrayscaleOff )
66 {
67 QgsImageOperation::convertToGrayscale( image, static_cast< QgsImageOperation::GrayscaleMode >( mGrayscaleMode ), context.feedback() );
68 }
69
70 if ( context.feedback() && context.feedback()->isCanceled() )
71 return;
72
73 QgsImageOperation::adjustHueSaturation( image, mSaturation, mColorizeOn ? mColorizeColor : QColor(), mColorizeStrength / 100.0, context.feedback() );
74
75 if ( context.feedback() && context.feedback()->isCanceled() )
76 return;
77
78 QgsImageOperation::multiplyOpacity( image, mOpacity, context.feedback() );
79
80 if ( context.feedback() && context.feedback()->isCanceled() )
81 return;
82
83 QgsScopedQPainterState painterState( painter );
84 painter->setCompositionMode( mBlendMode );
85 painter->drawImage( imageOffset( context ), image );
86}
87
88
89QVariantMap QgsColorEffect::properties() const
90{
91 QVariantMap props;
92 props.insert( QStringLiteral( "enabled" ), mEnabled ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
93 props.insert( QStringLiteral( "draw_mode" ), QString::number( int( mDrawMode ) ) );
94 props.insert( QStringLiteral( "blend_mode" ), QString::number( int( mBlendMode ) ) );
95 props.insert( QStringLiteral( "opacity" ), QString::number( mOpacity ) );
96 props.insert( QStringLiteral( "brightness" ), QString::number( mBrightness ) );
97 props.insert( QStringLiteral( "contrast" ), QString::number( mContrast ) );
98 props.insert( QStringLiteral( "saturation" ), QString::number( mSaturation ) );
99 props.insert( QStringLiteral( "grayscale_mode" ), QString::number( int( mGrayscaleMode ) ) );
100 props.insert( QStringLiteral( "colorize" ), mColorizeOn ? QStringLiteral( "1" ) : QStringLiteral( "0" ) );
101 props.insert( QStringLiteral( "colorize_color" ), QgsColorUtils::colorToString( mColorizeColor ) );
102 props.insert( QStringLiteral( "colorize_strength" ), QString::number( mColorizeStrength ) );
103
104 return props;
105}
106
107void QgsColorEffect::readProperties( const QVariantMap &props )
108{
109 bool ok;
110 QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( QStringLiteral( "blend_mode" ) ).toInt( &ok ) );
111 if ( ok )
112 {
113 mBlendMode = mode;
114 }
115 if ( props.contains( QStringLiteral( "transparency" ) ) )
116 {
117 double transparency = props.value( QStringLiteral( "transparency" ) ).toDouble( &ok );
118 if ( ok )
119 {
120 mOpacity = 1.0 - transparency;
121 }
122 }
123 else
124 {
125 double opacity = props.value( QStringLiteral( "opacity" ) ).toDouble( &ok );
126 if ( ok )
127 {
128 mOpacity = opacity;
129 }
130 }
131 mEnabled = props.value( QStringLiteral( "enabled" ), QStringLiteral( "1" ) ).toInt();
132 mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( QStringLiteral( "draw_mode" ), QStringLiteral( "2" ) ).toInt() );
133
134 mBrightness = props.value( QStringLiteral( "brightness" ), QStringLiteral( "0" ) ).toInt();
135 mContrast = props.value( QStringLiteral( "contrast" ), QStringLiteral( "0" ) ).toInt();
136 mSaturation = props.value( QStringLiteral( "saturation" ), QStringLiteral( "1.0" ) ).toDouble();
137 mGrayscaleMode = static_cast< QgsImageOperation::GrayscaleMode >( props.value( QStringLiteral( "grayscale_mode" ), QStringLiteral( "0" ) ).toInt() );
138 mColorizeOn = props.value( QStringLiteral( "colorize" ), QStringLiteral( "0" ) ).toInt();
139 if ( props.contains( QStringLiteral( "colorize_color" ) ) )
140 {
141 setColorizeColor( QgsColorUtils::colorFromString( props.value( QStringLiteral( "colorize_color" ) ).toString() ) );
142 }
143 mColorizeStrength = props.value( QStringLiteral( "colorize_strength" ), QStringLiteral( "100" ) ).toInt();
144}
145
147{
148 QgsColorEffect *newEffect = new QgsColorEffect( *this );
149 return newEffect;
150}
151
152void QgsColorEffect::setBrightness( int brightness )
153{
154 mBrightness = std::clamp( brightness, -255, 255 );
155}
156
157void QgsColorEffect::setContrast( int contrast )
158{
159 mContrast = std::clamp( contrast, -100, 100 );
160}
161
162void QgsColorEffect::setColorizeColor( const QColor &colorizeColor )
163{
164 mColorizeColor = colorizeColor;
165}
@ ForceVector
Always force vector-based rendering, even when the result will be visually different to a raster-base...
@ RequiresRasterization
The effect requires raster-based rendering.
QFlags< PaintEffectFlag > PaintEffectFlags
Flags which control how paint effects behave.
Definition qgis.h:2765
A paint effect which alters the colors (e.g., brightness, contrast) in a source picture.
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.
static QgsPaintEffect * create(const QVariantMap &map)
Creates a new QgsColorEffect effect from a properties string map.
Qgis::PaintEffectFlags flags() const override
Returns flags which specify how the paint effect behaves.
int contrast() const
Returns the contrast modification for the effect.
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:53
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.
Base class for visual effects which can be applied to QPicture drawings.
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.