Skip to content

Commit

Permalink
Generate diff images in composer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mhugent committed Jul 6, 2012
1 parent 8256d05 commit bc18adb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
17 changes: 14 additions & 3 deletions tests/src/core/qgscompositionchecker.cpp
Expand Up @@ -15,6 +15,8 @@

#include "qgscompositionchecker.h"
#include "qgscomposition.h"
#include <QDir>
#include <QFileInfo>
#include <QImage>
#include <QPainter>

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

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

bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img2 ) const
bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img2, const QString& differenceImagePath ) const
{
if ( img1.width() != img2.width() || img1.height() != img2.height() )
{
Expand All @@ -85,6 +88,9 @@ bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img
int imageHeight = img1.height();
int mismatchCount = 0;

QImage differenceImage( imageWidth, imageHeight, QImage::Format_ARGB32_Premultiplied );
differenceImage.fill( qRgb( 152, 219, 249 ) );

QRgb pixel1, pixel2;
for ( int i = 0; i < imageHeight; ++i )
{
Expand All @@ -95,12 +101,17 @@ bool QgsCompositionChecker::compareImages( const QImage& img1, const QImage& img
if ( pixel1 != pixel2 )
{
++mismatchCount;
differenceImage.setPixel( j, i, qRgb( 255, 0, 0 ) );
}
}
}

int pixelCount = imageWidth * imageHeight;
if ( !differenceImagePath.isEmpty() )
{
differenceImage.save( differenceImagePath, "PNG" );
}

//allow pixel deviation of 1 percent
int pixelCount = imageWidth * imageHeight;
return (( double )mismatchCount / ( double )pixelCount ) < 0.01;
}
2 changes: 1 addition & 1 deletion tests/src/core/qgscompositionchecker.h
Expand Up @@ -31,7 +31,7 @@ class QgsCompositionChecker

private:
QgsCompositionChecker(); //forbidden
bool compareImages( const QImage& img1, const QImage& img2 ) const;
bool compareImages( const QImage& img1, const QImage& img2, const QString& differenceImagePath = QString() ) const;

QgsComposition* mComposition;
QString mExpectedImageFile;
Expand Down

0 comments on commit bc18adb

Please sign in to comment.