361bool QgsRenderChecker::compareImages(
const QString &testName,
const QString &referenceImageFile,
const QString &renderedImageFile,
unsigned int mismatchCount, QgsRenderChecker::Flags flags )
364 if ( ! renderedImageFile.isEmpty() )
374 qDebug(
"QgsRenderChecker::runTest failed - Rendered Image File not set." );
376 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
377 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
378 "Image File not set.</td></tr></table>\n";
379 mMarkdownReport = QStringLiteral(
"Failed because rendered image file was not set\n" );
380 performPostTestActions( flags );
387 QImage expectedImage( referenceImageFile );
388 if ( expectedImage.isNull() )
390 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load control image from " << referenceImageFile;
392 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
393 "<tr><td>Nothing rendered</td>\n<td>Failed because control "
394 "image file could not be loaded.</td></tr></table>\n";
395 mMarkdownReport = QStringLiteral(
"Failed because expected image file (%1) could not be loaded\n" ).arg( referenceImageFile );
396 performPostTestActions( flags );
400 const QString expectedImageString = QStringLiteral(
"<a href=\"%1\" style=\"color: inherit\" target=\"_blank\">expected</a> image" ).arg( QUrl::fromLocalFile( referenceImageFile ).toString() );
401 const QString renderedImageString = QStringLiteral(
"<a href=\"%2\" style=\"color: inherit\" target=\"_blank\">rendered</a> image" ).arg( QUrl::fromLocalFile( renderedImageFile ).toString() );
402 auto upperFirst = [](
const QString & string ) -> QString
404 const int firstNonTagIndex =
string.indexOf(
'>' ) + 1;
405 return string.left( firstNonTagIndex ) +
string.at( firstNonTagIndex ).toUpper() +
string.mid( firstNonTagIndex + 1 );
409 if ( myResultImage.isNull() )
411 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load rendered image from " <<
mRenderedImageFile;
412 mReport = QStringLiteral(
"<table>"
413 "<tr><td>Test Result:</td><td>%1:</td></tr>\n"
414 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
415 "Image File could not be loaded.</td></tr></table>\n" ).arg( upperFirst( expectedImageString ) );
417 performPostTestActions( flags );
420 QImage myDifferenceImage( expectedImage.width(),
421 expectedImage.height(),
422 QImage::Format_RGB32 );
423 mDiffImageFile = QDir::tempPath() +
'/' + testName +
"_result_diff.png";
424 myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
427 QString maskImagePath = referenceImageFile;
428 maskImagePath.chop( 4 );
429 maskImagePath += QLatin1String(
"_mask.png" );
430 const QImage maskImage( maskImagePath );
431 const bool hasMask = !maskImage.isNull();
436 mMatchTarget = expectedImage.width() * expectedImage.height();
437 const unsigned int myPixelCount = myResultImage.width() * myResultImage.height();
441 mReport += QLatin1String(
"<table>" );
442 mReport += QLatin1String(
"<tr><td colspan=2>" );
443 mReport += QStringLiteral(
"<tr><td colspan=2>"
444 "%8 and %9 for %1<br>"
445 "Expected size: %2 w x %3 h (%4 pixels)<br>"
446 "Rendered size: %5 w x %6 h (%7 pixels)"
449 .arg( expectedImage.width() ).arg( expectedImage.height() ).arg(
mMatchTarget )
450 .arg( myResultImage.width() ).arg( myResultImage.height() ).arg( myPixelCount )
451 .arg( upperFirst( expectedImageString ), renderedImageString );
452 mReport += QString(
"<tr><td colspan=2>\n"
453 "Expected Duration : <= %1 (0 indicates not specified)<br>"
454 "Actual Duration : %2 ms<br></td></tr>" )
455 .arg( mElapsedTimeTarget )
461 if ( ! expectedImage.isNull() )
463 imgWidth = std::min( expectedImage.width(), imgWidth );
464 imgHeight = expectedImage.height() * imgWidth / expectedImage.width();
468 const QString diffImageFileName = QFileInfo( mDiffImageFile ).fileName();
469 const QString myImagesString = QString(
471 "<td colspan=2>Compare %10 and %11</td>"
472 "<td>Difference (all blue is good, any red is bad)</td>"
474 "<td colspan=2 id=\"td-%1-%7\"></td>\n"
475 "<td align=center><img width=%5 height=%6 src=\"%2\"></td>\n"
478 "<script>\naddComparison(\"td-%1-%7\",\"%3\",\"file://%4\",%5,%6);\n</script>\n"
479 "<p>If the new image looks good, create or update a test mask with<br>"
480 "<code>scripts/generate_test_mask_image.py \"%8\" \"%9\"</code>" )
483 renderedImageFileName,
485 .arg( imgWidth ).arg( imgHeight )
486 .arg( QUuid::createUuid().toString().mid( 1, 6 ),
494 if ( !mControlPathPrefix.isNull() )
496 prefix = QStringLiteral(
" (prefix %1)" ).arg( mControlPathPrefix );
503 if ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() )
505 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
506 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
508 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
513 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
515 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
516 std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
521 mReport += QLatin1String(
"<tr><td colspan=3>" );
522 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
523 mReport += QLatin1String(
"</td></tr>" );
524 mMarkdownReport += QStringLiteral(
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n" )
525 .arg( myResultImage.width() )
526 .arg( myResultImage.height() )
527 .arg( expectedImage.width() )
528 .arg( expectedImage.height() );
530 const QString diffSizeImagesString = QString(
532 "<td colspan=3>Compare %5 and %6</td>"
534 "<td align=center><img src=\"%1\"></td>\n"
535 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
539 renderedImageFileName,
541 .arg( imgWidth ).arg( imgHeight )
542 .arg( expectedImageString, renderedImageString );
544 mReport += diffSizeImagesString;
545 performPostTestActions( flags );
550 mReport += QLatin1String(
"<tr><td colspan=3>" );
551 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
552 mReport += QLatin1String(
"</td></tr>" );
556 if ( expectedImage.format() == QImage::Format_Indexed8 )
558 if ( myResultImage.format() != QImage::Format_Indexed8 )
563 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
565 mReport += QLatin1String(
"<tr><td colspan=3>" );
566 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
567 mReport += QLatin1String(
"</td></tr>" );
570 mMarkdownReport += QStringLiteral(
"Failed because rendered image and expected image have different formats (8bit format is expected)\n" );
571 performPostTestActions( flags );
578 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
579 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
588 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
589 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
592 const int colorTolerance =
static_cast< int >( mColorTolerance );
593 for (
int y = 0; y < maxHeight; ++y )
595 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
596 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
597 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
598 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
600 for (
int x = 0; x < maxWidth; ++x )
602 const int maskTolerance = ( maskScanline && maskImage.width() > x ) ? qRed( maskScanline[ x ] ) : 0;
603 const int pixelTolerance = std::max( colorTolerance, maskTolerance );
604 if ( pixelTolerance == 255 )
610 const QRgb myExpectedPixel = expectedScanline[x];
611 const QRgb myActualPixel = resultScanline[x];
612 if ( pixelTolerance == 0 )
614 if ( myExpectedPixel != myActualPixel )
617 diffScanline[ x ] = qRgb( 255, 0, 0 );
622 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance ||
623 std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance ||
624 std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance ||
625 std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
628 diffScanline[ x ] = qRgb( 255, 0, 0 );
647 myDifferenceImage.save( mDiffImageFile );
654 mReport += QStringLiteral(
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>" )
660 if ( mMismatchCount > 0 )
667 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
668 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
669 mReport += QLatin1String(
"</td></tr>" );
670 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
673 qDebug(
"Test failed because render step took too long" );
674 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
675 mReport += QLatin1String(
"<font color=red>Test failed because render step took too long</font>" );
676 mReport += QLatin1String(
"</td></tr>" );
679 mMarkdownReport += QStringLiteral(
"Test failed because render step took too long\n" );
681 performPostTestActions( flags );
688 performPostTestActions( flags );
693 mReport += QLatin1String(
"<tr><td colspan=3></td></tr>" );
694 emitDashMessage( QStringLiteral(
"Image mismatch" ),
QgsDartMeasurement::Text,
"Difference image did not match any known anomaly or mask."
695 " If you feel the difference image should be considered an anomaly "
696 "you can do something like this\n"
698 "/\nIf it should be included in the mask run\n"
699 "scripts/generate_test_mask_image.py '" + referenceImageFile +
"' '" +
mRenderedImageFile +
"'\n" );
701 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
702 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
703 mReport += QLatin1String(
"</td></tr>" );
706 mMarkdownReport += QStringLiteral(
"Rendered image did not match %1 (found %2 pixels different)\n" ).arg( referenceImageFile ).arg( mMismatchCount );
708 performPostTestActions( flags );