386 if ( ! renderedImageFile.isEmpty() )
396 qDebug(
"QgsRenderChecker::runTest failed - Rendered Image File not set." );
398 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
399 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
400 "Image File not set.</td></tr></table>\n";
401 mMarkdownReport = QStringLiteral(
"Failed because rendered image file was not set\n" );
402 performPostTestActions( flags );
409 QImage expectedImage( referenceImageFile );
410 if ( expectedImage.isNull() )
412 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load control image from " << referenceImageFile;
414 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
415 "<tr><td>Nothing rendered</td>\n<td>Failed because control "
416 "image file could not be loaded.</td></tr></table>\n";
417 mMarkdownReport = QStringLiteral(
"Failed because expected image file (%1) could not be loaded\n" ).arg( referenceImageFile );
418 performPostTestActions( flags );
422 const QString expectedImageString = QStringLiteral(
"<a href=\"%1\" style=\"color: inherit\" target=\"_blank\">expected</a> image" ).arg( QUrl::fromLocalFile( referenceImageFile ).toString() );
423 const QString renderedImageString = QStringLiteral(
"<a href=\"%2\" style=\"color: inherit\" target=\"_blank\">rendered</a> image" ).arg( QUrl::fromLocalFile( renderedImageFile ).toString() );
424 auto upperFirst = [](
const QString & string ) -> QString
426 const int firstNonTagIndex =
string.indexOf(
'>' ) + 1;
427 return string.left( firstNonTagIndex ) +
string.at( firstNonTagIndex ).toUpper() +
string.mid( firstNonTagIndex + 1 );
431 if ( myResultImage.isNull() )
433 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load rendered image from " <<
mRenderedImageFile;
434 mReport = QStringLiteral(
"<table>"
435 "<tr><td>Test Result:</td><td>%1:</td></tr>\n"
436 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
437 "Image File could not be loaded.</td></tr></table>\n" ).arg( upperFirst( expectedImageString ) );
439 performPostTestActions( flags );
442 QImage myDifferenceImage( expectedImage.width(),
443 expectedImage.height(),
444 QImage::Format_RGB32 );
445 mDiffImageFile = QDir::tempPath() +
'/' + testName +
"_result_diff.png";
446 myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
449 QString maskImagePath = referenceImageFile;
450 maskImagePath.chop( 4 );
451 maskImagePath += QLatin1String(
"_mask.png" );
452 const QImage maskImage( maskImagePath );
453 const bool hasMask = !maskImage.isNull();
458 mMatchTarget = expectedImage.width() * expectedImage.height();
459 const unsigned int myPixelCount = myResultImage.width() * myResultImage.height();
463 mReport += QLatin1String(
"<table>" );
464 mReport += QLatin1String(
"<tr><td colspan=2>" );
465 mReport += QStringLiteral(
"<tr><td colspan=2>"
466 "%8 and %9 for %1<br>"
467 "Expected size: %2 w x %3 h (%4 pixels)<br>"
468 "Rendered size: %5 w x %6 h (%7 pixels)"
471 .arg( expectedImage.width() ).arg( expectedImage.height() ).arg(
mMatchTarget )
472 .arg( myResultImage.width() ).arg( myResultImage.height() ).arg( myPixelCount )
473 .arg( upperFirst( expectedImageString ), renderedImageString );
474 mReport += QString(
"<tr><td colspan=2>\n"
475 "Expected Duration : <= %1 (0 indicates not specified)<br>"
476 "Actual Duration : %2 ms<br></td></tr>" )
477 .arg( mElapsedTimeTarget )
483 if ( ! expectedImage.isNull() )
485 imgWidth = std::min( expectedImage.width(), imgWidth );
486 imgHeight = expectedImage.height() * imgWidth / expectedImage.width();
490 const QString diffImageFileName = QFileInfo( mDiffImageFile ).fileName();
491 const QString myImagesString = QString(
493 "<td colspan=2>Compare %10 and %11</td>"
494 "<td>Difference (all blue is good, any red is bad)</td>"
496 "<td colspan=2 id=\"td-%1-%7\"></td>\n"
497 "<td align=center><img width=%5 height=%6 src=\"%2\"></td>\n"
500 "<script>\naddComparison(\"td-%1-%7\",\"%3\",\"file://%4\",%5,%6);\n</script>\n"
501 "<p>If the new image looks good, create or update a test mask with<br>"
502 "<code onclick=\"copyToClipboard(this)\" class=\"copy-code\" data-tooltip=\"Click to copy\">scripts/generate_test_mask_image.py \"%8\" \"%9\"</code>"
506 renderedImageFileName,
508 .arg( imgWidth ).arg( imgHeight )
509 .arg( QUuid::createUuid().toString().mid( 1, 6 ),
517 if ( !mControlPathPrefix.isNull() )
519 prefix = QStringLiteral(
" (prefix %1)" ).arg( mControlPathPrefix );
526 if ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() )
528 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
529 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
531 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
536 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
538 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
539 std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
544 mReport += QLatin1String(
"<tr><td colspan=3>" );
545 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
546 mReport += QLatin1String(
"</td></tr>" );
547 mMarkdownReport += QStringLiteral(
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n" )
548 .arg( myResultImage.width() )
549 .arg( myResultImage.height() )
550 .arg( expectedImage.width() )
551 .arg( expectedImage.height() );
553 const QString diffSizeImagesString = QString(
555 "<td colspan=3>Compare %5 and %6</td>"
557 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
558 "<td align=center><img src=\"%1\"></td>\n"
562 renderedImageFileName,
564 .arg( imgWidth ).arg( imgHeight )
565 .arg( expectedImageString, renderedImageString );
567 mReport += diffSizeImagesString;
568 performPostTestActions( flags );
573 mReport += QLatin1String(
"<tr><td colspan=3>" );
574 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
575 mReport += QLatin1String(
"</td></tr>" );
579 if ( expectedImage.format() == QImage::Format_Indexed8 )
581 if ( myResultImage.format() != QImage::Format_Indexed8 )
586 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
588 mReport += QLatin1String(
"<tr><td colspan=3>" );
589 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
590 mReport += QLatin1String(
"</td></tr>" );
593 mMarkdownReport += QLatin1String(
"Failed because rendered image and expected image have different formats (8bit format is expected)\n" );
594 performPostTestActions( flags );
601 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
602 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
604 if ( expectedImage.format() != QImage::Format_RGB32
605 && expectedImage.format() != QImage::Format_ARGB32
606 && expectedImage.format() != QImage::Format_ARGB32_Premultiplied )
608 mReport += QLatin1String(
"<tr><td colspan=3>" );
609 mReport += QStringLiteral(
"<font color=red>Expected image for %1 is not a compatible format (%2). Must be 32 bit RGB format.</font>" ).arg( testName,
qgsEnumValueToKey( expectedImage.format() ) );
610 mReport += QLatin1String(
"</td></tr>" );
613 mMarkdownReport += QStringLiteral(
"Failed because expected image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( expectedImage.format() ) );
614 performPostTestActions( flags );
617 if ( myResultImage.format() != QImage::Format_RGB32
618 && myResultImage.format() != QImage::Format_ARGB32
619 && myResultImage.format() != QImage::Format_ARGB32_Premultiplied )
621 mReport += QLatin1String(
"<tr><td colspan=3>" );
622 mReport += QStringLiteral(
"<font color=red>Rendered image for %1 is not a compatible format (%2). Must be 32 bit RGB format.</font>" ).arg( testName,
qgsEnumValueToKey( myResultImage.format() ) );
623 mReport += QLatin1String(
"</td></tr>" );
626 mMarkdownReport += QStringLiteral(
"Failed because rendered image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( myResultImage.format() ) );
627 performPostTestActions( flags );
636 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
637 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
639 const int maskWidth = maskImage.width();
642 const int colorTolerance =
static_cast< int >( mColorTolerance );
643 for (
int y = 0; y < maxHeight; ++y )
645 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
646 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
647 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
648 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
650 for (
int x = 0; x < maxWidth; ++x )
652 const int pixelTolerance = maskScanline
653 ? std::max( colorTolerance, ( maskWidth > x ) ? qRed( maskScanline[ x ] ) : 0 )
655 if ( pixelTolerance == 255 )
661 const QRgb myExpectedPixel = expectedScanline[x];
662 const QRgb myActualPixel = resultScanline[x];
663 if ( pixelTolerance == 0 )
665 if ( myExpectedPixel != myActualPixel )
668 diffScanline[ x ] = qRgb( 255, 0, 0 );
673 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance ||
674 std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance ||
675 std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance ||
676 std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
679 diffScanline[ x ] = qRgb( 255, 0, 0 );
698 myDifferenceImage.save( mDiffImageFile );
705 mReport += QStringLiteral(
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>" )
711 if ( mMismatchCount > 0 )
718 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
719 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
720 mReport += QLatin1String(
"</td></tr>" );
721 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
724 qDebug(
"Test failed because render step took too long" );
725 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
726 mReport += QLatin1String(
"<font color=red>Test failed because render step took too long</font>" );
727 mReport += QLatin1String(
"</td></tr>" );
730 mMarkdownReport += QLatin1String(
"Test failed because render step took too long\n" );
732 performPostTestActions( flags );
739 performPostTestActions( flags );
744 mReport += QLatin1String(
"<tr><td colspan=3></td></tr>" );
745 emitDashMessage( QStringLiteral(
"Image mismatch" ),
QgsDartMeasurement::Text,
"Difference image did not match any known anomaly or mask."
746 " If you feel the difference image should be considered an anomaly "
747 "you can do something like this\n"
749 "/\nIf it should be included in the mask run\n"
750 "scripts/generate_test_mask_image.py '" + referenceImageFile +
"' '" +
mRenderedImageFile +
"'\n" );
752 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
753 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
754 mReport += QLatin1String(
"</td></tr>" );
757 const QString githubSha = qgetenv(
"GITHUB_SHA" );
758 if ( !githubSha.isEmpty() )
760 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2" ).arg(
761 githubSha, QDir(
sourcePath() ).relativeFilePath( referenceImageFile ) );
762 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
763 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), githubBlobUrl ).arg( mMismatchCount );
767 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
768 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ),
769 QUrl::fromLocalFile( referenceImageFile ).toString() ).arg( mMismatchCount );
772 performPostTestActions( flags );