Skip to content

Commit

Permalink
Better hash comparison implementation for render checker
Browse files Browse the repository at this point in the history
  • Loading branch information
timlinux committed Apr 15, 2012
1 parent 373dee2 commit 3e6ea5c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/providers/gdal/qgsgdalprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1866,7 +1866,7 @@ QgsRasterBandStats QgsGdalProvider::bandStatistics( int theBandNo )
{
GDALRasterBandH myGdalBand = GDALGetRasterBand( mGdalDataset, theBandNo );
QgsRasterBandStats myRasterBandStats;
int bApproxOK = false;
int bApproxOK = true;
double pdfMin;
double pdfMax;
double pdfMean;
Expand Down
54 changes: 34 additions & 20 deletions tests/src/core/qgsrenderchecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <QCryptographicHash>
#include <QByteArray>
#include <QDebug>
#include <QBuffer>

QgsRenderChecker::QgsRenderChecker( ) :
mReport( "" ),
Expand Down Expand Up @@ -52,7 +53,20 @@ void QgsRenderChecker::setControlName(const QString theName)
+ theName + ".png";
}

bool QgsRenderChecker::isKnownAnomaly( QImage theDifferenceImage )
QString QgsRenderChecker::imageToHash( QString theImageFile )
{
QImage myImage;
myImage.load( theImageFile );
QByteArray myByteArray;
QBuffer myBuffer( &myByteArray );
myImage.save(&myBuffer, "PNG");
QString myImageString = QString::fromUtf8( myByteArray.toBase64().data() );
QCryptographicHash myHash( QCryptographicHash::Md5 );
myHash.addData(myImageString.toUtf8());
return myHash.result().toHex().constData();
}

bool QgsRenderChecker::isKnownAnomaly( QString theDiffImageFile )
{
QString myControlImageDir = controlImagePath() + mControlName
+ QDir::separator();
Expand All @@ -64,32 +78,32 @@ bool QgsRenderChecker::isKnownAnomaly( QImage theDifferenceImage )
//remove the control file from teh list as the anomalies are
//all files except the control file
myList.removeAt(myList.indexOf(mExpectedImageFile));
//todo compare each hash to diff path
QByteArray myData((const char*)theDifferenceImage.bits(),
theDifferenceImage.numBytes());
QByteArray mySourceHash = QCryptographicHash::hash(
myData, QCryptographicHash::Md5);

QString myImageHash = imageToHash( theDiffImageFile );


for (int i = 0; i < myList.size(); ++i)
{
QString myFile = myList.at(i);
mReport += "<tr><td colspan=3>"
"Checking if " + myFile + " is a known anomaly.";
mReport += "</td></tr>";
QImage myAnomalyImage( myFile );
QByteArray myData((const char*)myAnomalyImage.bits(),
myAnomalyImage.numBytes());
QByteArray myAnomolyHash = QCryptographicHash::hash(
myData, QCryptographicHash::Md5);
QString myHashMessage = QString("Source image hash %1 : Anomaly hash: %2").arg(
QString(mySourceHash.toHex())).arg(QString(myAnomolyHash.toHex()));
//fro CDash
QString myAnomalyHash = imageToHash( controlImagePath() + mControlName
+ QDir::separator() + myFile );
QString myHashMessage = QString(
"Checking if anomaly %1 (hash %2)")
.arg( myFile )
.arg( myAnomalyHash );
myHashMessage += QString( " matches %1 (hash %2)" )
.arg( theDiffImageFile )
.arg( myImageHash );
//foo CDash
QString myMeasureMessage = "<DartMeasurement name=\"Anomoly check"
"\" type=\"text/text\">" + myHashMessage +
"</DartMeasurement>";
qDebug() << myMeasureMessage;
mReport += "<tr><td colspan=3>" + myHashMessage + "</td></tr>";
if ( mySourceHash.toHex() == myAnomolyHash.toHex() )
if ( myImageHash == myAnomalyHash )
{
mReport += "<tr><td colspan=3>"
"Anomaly found! " + myFile;
Expand Down Expand Up @@ -179,7 +193,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
QImage myDifferenceImage( myExpectedImage.width(),
myExpectedImage.height(),
QImage::Format_RGB32 );
QString myResultDiffImage = QDir::tempPath() + QDir::separator() +
QString myDiffImageFile = QDir::tempPath() + QDir::separator() +
QDir::separator() +
theTestName + "_result_diff.png";
myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
Expand Down Expand Up @@ -213,7 +227,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
"\"></td>\n<td><img src=\"file://" +
mExpectedImageFile +
"\"></td><td><img src=\"file://" +
myResultDiffImage +
myDiffImageFile +
"\"></td>\n</tr>\n</table>";
//
// To get the images into CDash
Expand All @@ -224,7 +238,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
"<DartMeasurementFile name=\"Expected Image\" type=\"image/png\">" +
mExpectedImageFile + "</DartMeasurementFile>"
"<DartMeasurementFile name=\"Difference Image\" type=\"image/png\">" +
myResultDiffImage + "</DartMeasurementFile>";
myDiffImageFile + "</DartMeasurementFile>";
qDebug( ) << myDashMessage;

//
Expand Down Expand Up @@ -266,7 +280,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
//
//save the diff image to disk
//
myDifferenceImage.save( myResultDiffImage );
myDifferenceImage.save( myDiffImageFile );

//
// Send match result to debug
Expand All @@ -293,7 +307,7 @@ bool QgsRenderChecker::compareImages( QString theTestName,
"</DartMeasurement>";
qDebug( ) << myDashMessage;

bool myAnomalyMatchFlag = isKnownAnomaly( myDifferenceImage );
bool myAnomalyMatchFlag = isKnownAnomaly( myDiffImageFile );

if ( myAnomalyMatchFlag )
{
Expand Down
5 changes: 4 additions & 1 deletion tests/src/core/qgsrenderchecker.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class QgsRenderChecker
* controlImagePath + '/' + mControlName + '/' + mControlName + '.png'
*/
void setControlName(const QString theName);
/** Get an md5 hash that uniquely identifies an image */
QString imageToHash( QString theImageFile );

void setRenderedImage (QString theImageFileName) { mRenderedImageFile = theImageFileName; };
void setMapRenderer ( QgsMapRenderer * thepMapRenderer) { mpMapRenderer = thepMapRenderer; };
/**
Expand Down Expand Up @@ -81,7 +84,7 @@ class QgsRenderChecker
* acceptible.
* @return a bool indicating if the diff matched one of the anomaly files
*/
bool isKnownAnomaly( QImage theDifferenceImage );
bool isKnownAnomaly( QString theDiffImageFile );

private:

Expand Down

0 comments on commit 3e6ea5c

Please sign in to comment.