Skip to content

Commit bc18adb

Browse files
committed
Generate diff images in composer tests
1 parent 8256d05 commit bc18adb

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

tests/src/core/qgscompositionchecker.cpp

+14-3
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
#include "qgscompositionchecker.h"
1717
#include "qgscomposition.h"
18+
#include <QDir>
19+
#include <QFileInfo>
1820
#include <QImage>
1921
#include <QPainter>
2022

@@ -71,10 +73,11 @@ bool QgsCompositionChecker::testComposition()
7173
mComposition->render( &p, targetArea, sourceArea );
7274
p.end();
7375

74-
return compareImages( expectedImage, outputImage );
76+
QString diffFilePath = QDir::tempPath() + QDir::separator() + QFileInfo( mExpectedImageFile ).baseName() + "_diff.png";
77+
return compareImages( expectedImage, outputImage, diffFilePath );
7578
}
7679

77-
bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img2 ) const
80+
bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img2, const QString& differenceImagePath ) const
7881
{
7982
if ( img1.width() != img2.width() || img1.height() != img2.height() )
8083
{
@@ -85,6 +88,9 @@ bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img
8588
int imageHeight = img1.height();
8689
int mismatchCount = 0;
8790

91+
QImage differenceImage( imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied );
92+
differenceImage.fill( qRgb( 152, 219, 249 ) );
93+
8894
QRgb pixel1, pixel2;
8995
for ( int i = 0; i < imageHeight; ++i )
9096
{
@@ -95,12 +101,17 @@ bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img
95101
if ( pixel1 != pixel2 )
96102
{
97103
++mismatchCount;
104+
differenceImage.setPixel( j, i, qRgb( 255, 0, 0 ) );
98105
}
99106
}
100107
}
101108

102-
int pixelCount = imageWidth * imageHeight;
109+
if ( !differenceImagePath.isEmpty() )
110+
{
111+
differenceImage.save( differenceImagePath, "PNG" );
112+
}
103113

104114
//allow pixel deviation of 1 percent
115+
int pixelCount = imageWidth * imageHeight;
105116
return (( double )mismatchCount / ( double )pixelCount ) < 0.01;
106117
}

tests/src/core/qgscompositionchecker.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class QgsCompositionChecker
3131

3232
private:
3333
QgsCompositionChecker(); //forbidden
34-
bool compareImages( const QImage& img1, const QImage& img2 ) const;
34+
bool compareImages( const QImage& img1, const QImage& img2, const QString& differenceImagePath = QString() ) const;
3535

3636
QgsComposition* mComposition;
3737
QString mExpectedImageFile;

0 commit comments

Comments
 (0)