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