Skip to content
Permalink
Browse files

Generate diff images in composer tests

  • Loading branch information
mhugent committed Jul 6, 2012
1 parent 8256d05 commit bc18adb02e611eb192280d1c93d816c362bb114a
Showing with 15 additions and 4 deletions.
  1. +14 −3 tests/src/core/qgscompositionchecker.cpp
  2. +1 −1 tests/src/core/qgscompositionchecker.h
@@ -15,6 +15,8 @@

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

@@ -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() )
{
@@ -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 )
{
@@ -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;
}
@@ -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;

0 comments on commit bc18adb

Please sign in to comment.
You can’t perform that action at this time.