QGIS API Documentation  2.18.21-Las Palmas (9fba24a)
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 "qgssymbollayerv2utils.h"
21 
23 {
24  QgsColorEffect* newEffect = new QgsColorEffect();
25  newEffect->readProperties( map );
26  return newEffect;
27 }
28 
30  : QgsPaintEffect()
31  , mTransparency( 0.0 )
32  , mBlendMode( QPainter::CompositionMode_SourceOver )
33  , mBrightness( 0 )
34  , mContrast( 0 )
35  , mSaturation( 1.0 )
36  , mGrayscaleMode( QgsImageOperation::GrayscaleOff )
37  , mColorizeOn( false )
38  , mColorizeColor( QColor::fromRgb( 255, 128, 128 ) )
39  , mColorizeStrength( 100 )
40 {
41 
42 }
43 
45 {
46 
47 }
48 
50 {
51  if ( !source() || !enabled() || !context.painter() )
52  return;
53 
54  QPainter* painter = context.painter();
55 
56  //rasterise source and apply modifications
57  QImage image = sourceAsImage( context )->copy();
58 
59  QgsImageOperation::adjustBrightnessContrast( image, mBrightness, mContrast / 100.0 + 1 );
60  if ( mGrayscaleMode != QgsImageOperation::GrayscaleOff )
61  {
62  QgsImageOperation::convertToGrayscale( image, static_cast< QgsImageOperation::GrayscaleMode >( mGrayscaleMode ) );
63  }
64  QgsImageOperation::adjustHueSaturation( image, mSaturation, mColorizeOn ? mColorizeColor : QColor(), mColorizeStrength / 100.0 );
65 
66  QgsImageOperation::multiplyOpacity( image, 1.0 - mTransparency );
67  painter->save();
68  painter->setCompositionMode( mBlendMode );
69  painter->drawImage( imageOffset( context ), image );
70  painter->restore();
71 }
72 
73 
75 {
76  QgsStringMap props;
77  props.insert( "enabled", mEnabled ? "1" : "0" );
78  props.insert( "draw_mode", QString::number( int( mDrawMode ) ) );
79  props.insert( "blend_mode", QString::number( int( mBlendMode ) ) );
80  props.insert( "transparency", QString::number( mTransparency ) );
81  props.insert( "brightness", QString::number( mBrightness ) );
82  props.insert( "contrast", QString::number( mContrast ) );
83  props.insert( "saturation", QString::number( mSaturation ) );
84  props.insert( "grayscale_mode", QString::number( int( mGrayscaleMode ) ) );
85  props.insert( "colorize", mColorizeOn ? "1" : "0" );
86  props.insert( "colorize_color", QgsSymbolLayerV2Utils::encodeColor( mColorizeColor ) );
87  props.insert( "colorize_strength", QString::number( mColorizeStrength ) );
88 
89  return props;
90 }
91 
93 {
94  bool ok;
95  QPainter::CompositionMode mode = static_cast< QPainter::CompositionMode >( props.value( "blend_mode" ).toInt( &ok ) );
96  if ( ok )
97  {
98  mBlendMode = mode;
99  }
100  double transparency = props.value( "transparency" ).toDouble( &ok );
101  if ( ok )
102  {
103  mTransparency = transparency;
104  }
105  mEnabled = props.value( "enabled", "1" ).toInt();
106  mDrawMode = static_cast< QgsPaintEffect::DrawMode >( props.value( "draw_mode", "2" ).toInt() );
107 
108  mBrightness = props.value( "brightness", "0" ).toInt();
109  mContrast = props.value( "contrast", "0" ).toInt();
110  mSaturation = props.value( "saturation", "1.0" ).toDouble();
111  mGrayscaleMode = static_cast< QgsImageOperation::GrayscaleMode >( props.value( "grayscale_mode", "0" ).toInt() );
112  mColorizeOn = props.value( "colorize", "0" ).toInt();
113  if ( props.contains( "colorize_color" ) )
114  {
115  setColorizeColor( QgsSymbolLayerV2Utils::decodeColor( props.value( "colorize_color" ) ) );
116  }
117  mColorizeStrength = props.value( "colorize_strength", "100" ).toInt();
118 }
119 
121 {
122  QgsColorEffect* newEffect = new QgsColorEffect( *this );
123  return newEffect;
124 }
125 
127 {
128  mColorizeColor = colorizeColor;
129 }
Contains operations and filters which apply to QImages.
bool contains(const Key &key) const
void setCompositionMode(CompositionMode mode)
DrawMode mDrawMode
static void multiplyOpacity(QImage &image, const double factor)
Multiplies opacity of image pixel values by a factor.
static void convertToGrayscale(QImage &image, const GrayscaleMode mode=GrayscaleLuminosity)
Convert a QImage to a grayscale image.
static QString encodeColor(const QColor &color)
void setColorizeColor(const QColor &colorizeColor)
Sets the color used for colorizing a picture.
Base class for visual effects which can be applied to QPicture drawings.
void save()
QImage copy(const QRect &rectangle) const
QImage * sourceAsImage(QgsRenderContext &context)
Returns the source QPicture rendered to a new QImage.
static void adjustBrightnessContrast(QImage &image, const int brightness, const double contrast)
Alter the brightness or contrast of a QImage.
QPointF imageOffset(const QgsRenderContext &context) const
Returns the offset which should be used when drawing the source image on to a destination render cont...
QString number(int n, int base)
bool enabled() const
Returns whether the effect is enabled.
DrawMode
Drawing modes for effects.
virtual QgsColorEffect * clone() const override
Duplicates an effect by creating a deep copy of the effect.
void restore()
virtual QgsStringMap properties() const override
Returns the properties describing the paint effect encoded in a string format.
static void adjustHueSaturation(QImage &image, const double saturation, const QColor &colorizeColor=QColor(), const double colorizeStrength=1.0)
Alter the hue or saturation of a QImage.
Contains information about the context of a rendering operation.
virtual ~QgsColorEffect()
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, QFlags< Qt::ImageConversionFlag > flags)
QPainter * painter()
virtual void draw(QgsRenderContext &context) override
Handles drawing of the effect&#39;s result on to the specified render context.
A paint effect which alters the colors (eg brightness, contrast) in a source picture.
static QgsPaintEffect * create(const QgsStringMap &map)
Creates a new QgsColorEffect effect from a properties string map.
static QColor decodeColor(const QString &str)
iterator insert(const Key &key, const T &value)
const QPicture * source() const
Returns the source QPicture.
virtual 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...
double transparency() const
Returns the transparency for the effect.
GrayscaleMode
Modes for converting a QImage to grayscale.
QColor colorizeColor() const
Returns the color used for colorizing a picture.
const T value(const Key &key) const