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 );
527 && ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() )
530 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
531 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
533 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
540 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
543 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
544 std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
549 mReport += QLatin1String(
"<tr><td colspan=3>" );
550 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
551 mReport += QLatin1String(
"</td></tr>" );
552 mMarkdownReport += QStringLiteral(
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n" )
553 .arg( myResultImage.width() )
554 .arg( myResultImage.height() )
555 .arg( expectedImage.width() )
556 .arg( expectedImage.height() );
558 const QString diffSizeImagesString = QString(
560 "<td colspan=3>Compare %5 and %6</td>"
562 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
563 "<td align=center><img src=\"%1\"></td>\n"
567 renderedImageFileName,
569 .arg( imgWidth ).arg( imgHeight )
570 .arg( expectedImageString, renderedImageString );
572 mReport += diffSizeImagesString;
573 performPostTestActions( flags );
578 mReport += QLatin1String(
"<tr><td colspan=3>" );
579 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
580 mReport += QLatin1String(
"</td></tr>" );
584 if ( expectedImage.format() == QImage::Format_Indexed8 )
586 if ( myResultImage.format() != QImage::Format_Indexed8 )
591 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
593 mReport += QLatin1String(
"<tr><td colspan=3>" );
594 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
595 mReport += QLatin1String(
"</td></tr>" );
598 mMarkdownReport += QLatin1String(
"Failed because rendered image and expected image have different formats (8bit format is expected)\n" );
599 performPostTestActions( flags );
606 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
607 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
609 if ( expectedImage.format() != QImage::Format_RGB32
610 && expectedImage.format() != QImage::Format_ARGB32
611 && expectedImage.format() != QImage::Format_ARGB32_Premultiplied )
613 mReport += QLatin1String(
"<tr><td colspan=3>" );
614 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() ) );
615 mReport += QLatin1String(
"</td></tr>" );
618 mMarkdownReport += QStringLiteral(
"Failed because expected image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( expectedImage.format() ) );
619 performPostTestActions( flags );
622 if ( myResultImage.format() != QImage::Format_RGB32
623 && myResultImage.format() != QImage::Format_ARGB32
624 && myResultImage.format() != QImage::Format_ARGB32_Premultiplied )
626 mReport += QLatin1String(
"<tr><td colspan=3>" );
627 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() ) );
628 mReport += QLatin1String(
"</td></tr>" );
631 mMarkdownReport += QStringLiteral(
"Failed because rendered image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( myResultImage.format() ) );
632 performPostTestActions( flags );
641 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
642 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
644 const int maskWidth = maskImage.width();
647 const int colorTolerance =
static_cast< int >( mColorTolerance );
648 for (
int y = 0; y < maxHeight; ++y )
650 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
651 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
652 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
653 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
655 for (
int x = 0; x < maxWidth; ++x )
657 const int pixelTolerance = maskScanline
658 ? std::max( colorTolerance, ( maskWidth > x ) ? qRed( maskScanline[ x ] ) : 0 )
660 if ( pixelTolerance == 255 )
666 const QRgb myExpectedPixel = expectedScanline[x];
667 const QRgb myActualPixel = resultScanline[x];
668 if ( pixelTolerance == 0 )
670 if ( myExpectedPixel != myActualPixel )
673 diffScanline[ x ] = qRgb( 255, 0, 0 );
678 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance ||
679 std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance ||
680 std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance ||
681 std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
684 diffScanline[ x ] = qRgb( 255, 0, 0 );
706 myDifferenceImage.save( mDiffImageFile );
713 mReport += QStringLiteral(
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>" )
719 if ( mMismatchCount > 0 )
726 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
727 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
728 mReport += QLatin1String(
"</td></tr>" );
729 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
732 qDebug(
"Test failed because render step took too long" );
733 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
734 mReport += QLatin1String(
"<font color=red>Test failed because render step took too long</font>" );
735 mReport += QLatin1String(
"</td></tr>" );
738 mMarkdownReport += QLatin1String(
"Test failed because render step took too long\n" );
740 performPostTestActions( flags );
747 performPostTestActions( flags );
752 mReport += QLatin1String(
"<tr><td colspan=3></td></tr>" );
753 emitDashMessage( QStringLiteral(
"Image mismatch" ),
QgsDartMeasurement::Text,
"Difference image did not match any known anomaly or mask."
754 " If you feel the difference image should be considered an anomaly "
755 "you can do something like this\n"
757 "/\nIf it should be included in the mask run\n"
758 "scripts/generate_test_mask_image.py '" + referenceImageFile +
"' '" +
mRenderedImageFile +
"'\n" );
760 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
761 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
762 mReport += QLatin1String(
"</td></tr>" );
765 const QString githubSha = qgetenv(
"GITHUB_SHA" );
766 if ( !githubSha.isEmpty() )
768 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2" ).arg(
769 githubSha, QDir(
sourcePath() ).relativeFilePath( referenceImageFile ) );
770 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
771 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), githubBlobUrl ).arg( mMismatchCount );
775 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
776 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ),
777 QUrl::fromLocalFile( referenceImageFile ).toString() ).arg( mMismatchCount );
780 performPostTestActions( flags );