QGIS API Documentation  3.20.0-Odense (decaadbb31)
qgsrenderchecker.h
Go to the documentation of this file.
1 /***************************************************************************
2  qgsrenderchecker.h - check maprender output against an expected image
3  --------------------------------------
4  Date : 18 Jan 2008
5  Copyright : (C) 2008 by Tim Sutton
6  email : tim @ linfiniti.com
7  ***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #ifndef QGSRENDERCHECKER_H
17 #define QGSRENDERCHECKER_H
18 
19 #include "qgis_core.h"
20 #include "qgis_sip.h"
21 #include <QDir>
22 #include <QString>
23 #include <QRegExp>
24 #include <QList>
25 
26 #include "qgslogger.h"
27 #include "qgsmapsettings.h"
28 #include "qgsdartmeasurement.h"
29 
30 class QImage;
31 
38 class CORE_EXPORT QgsRenderChecker
39 {
40  public:
41 
46 
54  QString controlImagePath() const;
55 
62  void setControlImagePath( const QString &path );
63 
67  QString report() { return mReport; }
68 
72  float matchPercent() const
73  {
74  return static_cast<float>( mMismatchCount ) /
75  static_cast<float>( mMatchTarget ) * 100;
76  }
77 
81  unsigned int mismatchCount() const { return mMismatchCount; }
82 
86  unsigned int matchTarget() const { return mMatchTarget; }
87 
93  int elapsedTime() { return mElapsedTime; }
94  void setElapsedTimeTarget( int target ) { mElapsedTimeTarget = target; }
95 
103  void setControlName( const QString &name );
104 
109  void setControlExtension( const QString &extension ) { mControlExtension = extension; }
110 
115  void setControlPathPrefix( const QString &name ) { mControlPathPrefix = name + '/'; }
116 
117  void setControlPathSuffix( const QString &name );
118 
120  QString imageToHash( const QString &imageFile );
121 
125  void setRenderedImage( const QString &imageFileName ) { mRenderedImageFile = imageFileName; }
126 
132  QString renderedImage() const { return mRenderedImageFile; }
133 
135  void setMapSettings( const QgsMapSettings &mapSettings );
136 
144  void setColorTolerance( unsigned int colorTolerance ) { mColorTolerance = colorTolerance; }
145 
152  void setSizeTolerance( int xTolerance, int yTolerance ) { mMaxSizeDifferenceX = xTolerance; mMaxSizeDifferenceY = yTolerance; }
153 
164  bool runTest( const QString &testName, unsigned int mismatchCount = 0 );
165 
177  bool compareImages( const QString &testName, unsigned int mismatchCount = 0, const QString &renderedImageFile = QString() );
178 
184  bool compareImages( const QString &testName, const QString &referenceImageFile, const QString &renderedImageFile, unsigned int mismatchCount = 0 );
185 
194  bool isKnownAnomaly( const QString &diffImageFile );
195 
200  static void drawBackground( QImage *image );
201 
207  QString expectedImageFile() const { return mExpectedImageFile; }
208 
216  void enableDashBuffering( bool enable ) { mBufferDashMessages = enable; }
217 
224  QVector<QgsDartMeasurement> dartMeasurements() const { return mDashMessages; }
225 
226  protected:
227  QString mReport;
228  unsigned int mMatchTarget = 0;
229  int mElapsedTime = 0;
232 
233  private:
234  void emitDashMessage( const QgsDartMeasurement &dashMessage );
235  void emitDashMessage( const QString &name, QgsDartMeasurement::Type type, const QString &value );
236 
237  QString mBasePath;
238 
239  QString mControlName;
240  unsigned int mMismatchCount = 0;
241  unsigned int mColorTolerance = 0;
242  int mMaxSizeDifferenceX = 0;
243  int mMaxSizeDifferenceY = 0;
244  int mElapsedTimeTarget = 0;
245  QgsMapSettings mMapSettings;
246  QString mControlExtension = QStringLiteral( "png" );
247  QString mControlPathPrefix;
248  QString mControlPathSuffix;
249  QVector<QgsDartMeasurement> mDashMessages;
250  bool mBufferDashMessages = false;
251 }; // class QgsRenderChecker
252 
253 
262 inline bool compareWkt( const QString &a, const QString &b, double tolerance = 0.000001 )
263 {
264  QgsDebugMsg( QStringLiteral( "a:%1 b:%2 tol:%3" ).arg( a, b ).arg( tolerance ) );
265  QRegExp re( "-?\\d+(?:\\.\\d+)?(?:[eE]\\d+)?" );
266 
267  QString a0( a ), b0( b );
268  a0.replace( re, QStringLiteral( "#" ) );
269  b0.replace( re, QStringLiteral( "#" ) );
270 
271  QgsDebugMsg( QStringLiteral( "a0:%1 b0:%2" ).arg( a0, b0 ) );
272 
273  if ( a0 != b0 )
274  return false;
275 
276  QList<double> al, bl;
277 
278  int pos;
279  for ( pos = 0; ( pos = re.indexIn( a, pos ) ) != -1; pos += re.matchedLength() )
280  {
281  al << re.cap( 0 ).toDouble();
282  }
283  for ( pos = 0; ( pos = re.indexIn( b, pos ) ) != -1; pos += re.matchedLength() )
284  {
285  bl << re.cap( 0 ).toDouble();
286  }
287 
288  if ( al.size() != bl.size() )
289  return false;
290 
291  for ( int i = 0; i < al.size(); i++ )
292  {
293  if ( !qgsDoubleNear( al[i], bl[i], tolerance ) )
294  return false;
295  }
296 
297  return true;
298 }
299 
300 #endif
The QgsMapSettings class contains configuration for rendering of the map.
This is a helper class for unit tests that need to write an image and compare it to an expected resul...
void setControlExtension(const QString &extension)
Sets file extension for the control image.
int elapsedTime()
Returns the total elapsed time for the rendering test.
float matchPercent() const
Returns the percent of pixels which matched the control image.
unsigned int matchTarget() const
Returns the total number of pixels in the control image.
QString renderedImage() const
Returns the path of the rendered image generated by the test.
QVector< QgsDartMeasurement > dartMeasurements() const
Gets access to buffered dash messages.
void setElapsedTimeTarget(int target)
void setControlPathPrefix(const QString &name)
Sets the path prefix where the control images are kept.
void setRenderedImage(const QString &imageFileName)
Sets the file name of the rendered image generated by the test.
void setSizeTolerance(int xTolerance, int yTolerance)
Sets the largest allowable difference in size between the rendered and the expected image.
void enableDashBuffering(bool enable)
Call this to enable internal buffering of dash messages.
unsigned int mismatchCount() const
Returns the number of pixels which did not match the control image.
void setColorTolerance(unsigned int colorTolerance)
Set tolerance for color components used by runTest() and compareImages().
QString report()
Returns the HTML report describing the results of the test run.
QString expectedImageFile() const
Returns the path to the expected image file.
bool qgsDoubleNear(double a, double b, double epsilon=4 *std::numeric_limits< double >::epsilon())
Compare two doubles (but allow some difference)
Definition: qgis.h:598
#define QgsDebugMsg(str)
Definition: qgslogger.h:38
bool compareWkt(const QString &a, const QString &b, double tolerance=0.000001)
Compare two WKT strings with some tolerance.