384 if ( ! renderedImageFile.isEmpty() )
394 qDebug(
"QgsRenderChecker::runTest failed - Rendered Image File not set." );
396 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
397 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
398 "Image File not set.</td></tr></table>\n";
399 mMarkdownReport = QStringLiteral(
"Failed because rendered image file was not set\n" );
400 performPostTestActions( flags );
407 QImage expectedImage( referenceImageFile );
408 if ( expectedImage.isNull() )
410 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load control image from " << referenceImageFile;
412 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
413 "<tr><td>Nothing rendered</td>\n<td>Failed because control "
414 "image file could not be loaded.</td></tr></table>\n";
415 mMarkdownReport = QStringLiteral(
"Failed because expected image file (%1) could not be loaded\n" ).arg( referenceImageFile );
416 performPostTestActions( flags );
420 const QString expectedImageString = QStringLiteral(
"<a href=\"%1\" style=\"color: inherit\" target=\"_blank\">expected</a> image" ).arg( QUrl::fromLocalFile( referenceImageFile ).toString() );
421 const QString renderedImageString = QStringLiteral(
"<a href=\"%2\" style=\"color: inherit\" target=\"_blank\">rendered</a> image" ).arg( QUrl::fromLocalFile( renderedImageFile ).toString() );
422 auto upperFirst = [](
const QString & string ) -> QString
424 const int firstNonTagIndex =
string.indexOf(
'>' ) + 1;
425 return string.left( firstNonTagIndex ) +
string.at( firstNonTagIndex ).toUpper() +
string.mid( firstNonTagIndex + 1 );
429 if ( myResultImage.isNull() )
431 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load rendered image from " <<
mRenderedImageFile;
432 mReport = QStringLiteral(
"<table>"
433 "<tr><td>Test Result:</td><td>%1:</td></tr>\n"
434 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
435 "Image File could not be loaded.</td></tr></table>\n" ).arg( upperFirst( expectedImageString ) );
437 performPostTestActions( flags );
440 QImage myDifferenceImage( expectedImage.width(),
441 expectedImage.height(),
442 QImage::Format_RGB32 );
443 mDiffImageFile = QDir::tempPath() +
'/' + testName +
"_result_diff.png";
444 myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
447 QString maskImagePath = referenceImageFile;
448 maskImagePath.chop( 4 );
449 maskImagePath += QLatin1String(
"_mask.png" );
450 const QImage maskImage( maskImagePath );
451 const bool hasMask = !maskImage.isNull();
456 mMatchTarget = expectedImage.width() * expectedImage.height();
457 const unsigned int myPixelCount = myResultImage.width() * myResultImage.height();
461 mReport += QLatin1String(
"<table>" );
462 mReport += QLatin1String(
"<tr><td colspan=2>" );
463 mReport += QStringLiteral(
"<tr><td colspan=2>"
464 "%8 and %9 for %1<br>"
465 "Expected size: %2 w x %3 h (%4 pixels)<br>"
466 "Rendered size: %5 w x %6 h (%7 pixels)"
469 .arg( expectedImage.width() ).arg( expectedImage.height() ).arg(
mMatchTarget )
470 .arg( myResultImage.width() ).arg( myResultImage.height() ).arg( myPixelCount )
471 .arg( upperFirst( expectedImageString ), renderedImageString );
472 mReport += QString(
"<tr><td colspan=2>\n"
473 "Expected Duration : <= %1 (0 indicates not specified)<br>"
474 "Actual Duration : %2 ms<br></td></tr>" )
475 .arg( mElapsedTimeTarget )
481 if ( ! expectedImage.isNull() )
483 imgWidth = std::min( expectedImage.width(), imgWidth );
484 imgHeight = expectedImage.height() * imgWidth / expectedImage.width();
488 const QString diffImageFileName = QFileInfo( mDiffImageFile ).fileName();
489 const QString myImagesString = QString(
491 "<td colspan=2>Compare %10 and %11</td>"
492 "<td>Difference (all blue is good, any red is bad)</td>"
494 "<td colspan=2 id=\"td-%1-%7\"></td>\n"
495 "<td align=center><img width=%5 height=%6 src=\"%2\"></td>\n"
498 "<script>\naddComparison(\"td-%1-%7\",\"%3\",\"file://%4\",%5,%6);\n</script>\n"
499 "<p>If the new image looks good, create or update a test mask with<br>"
500 "<code>scripts/generate_test_mask_image.py \"%8\" \"%9\"</code>" )
503 renderedImageFileName,
505 .arg( imgWidth ).arg( imgHeight )
506 .arg( QUuid::createUuid().toString().mid( 1, 6 ),
514 if ( !mControlPathPrefix.isNull() )
516 prefix = QStringLiteral(
" (prefix %1)" ).arg( mControlPathPrefix );
523 if ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() )
525 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
526 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
528 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
533 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
535 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
536 std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
541 mReport += QLatin1String(
"<tr><td colspan=3>" );
542 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
543 mReport += QLatin1String(
"</td></tr>" );
544 mMarkdownReport += QStringLiteral(
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n" )
545 .arg( myResultImage.width() )
546 .arg( myResultImage.height() )
547 .arg( expectedImage.width() )
548 .arg( expectedImage.height() );
550 const QString diffSizeImagesString = QString(
552 "<td colspan=3>Compare %5 and %6</td>"
554 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
555 "<td align=center><img src=\"%1\"></td>\n"
559 renderedImageFileName,
561 .arg( imgWidth ).arg( imgHeight )
562 .arg( expectedImageString, renderedImageString );
564 mReport += diffSizeImagesString;
565 performPostTestActions( flags );
570 mReport += QLatin1String(
"<tr><td colspan=3>" );
571 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
572 mReport += QLatin1String(
"</td></tr>" );
576 if ( expectedImage.format() == QImage::Format_Indexed8 )
578 if ( myResultImage.format() != QImage::Format_Indexed8 )
583 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
585 mReport += QLatin1String(
"<tr><td colspan=3>" );
586 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
587 mReport += QLatin1String(
"</td></tr>" );
590 mMarkdownReport += QLatin1String(
"Failed because rendered image and expected image have different formats (8bit format is expected)\n" );
591 performPostTestActions( flags );
598 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
599 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
601 if ( expectedImage.format() != QImage::Format_RGB32
602 && expectedImage.format() != QImage::Format_ARGB32
603 && expectedImage.format() != QImage::Format_ARGB32_Premultiplied )
605 mReport += QLatin1String(
"<tr><td colspan=3>" );
606 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() ) );
607 mReport += QLatin1String(
"</td></tr>" );
610 mMarkdownReport += QStringLiteral(
"Failed because expected image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( expectedImage.format() ) );
611 performPostTestActions( flags );
614 if ( myResultImage.format() != QImage::Format_RGB32
615 && myResultImage.format() != QImage::Format_ARGB32
616 && myResultImage.format() != QImage::Format_ARGB32_Premultiplied )
618 mReport += QLatin1String(
"<tr><td colspan=3>" );
619 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() ) );
620 mReport += QLatin1String(
"</td></tr>" );
623 mMarkdownReport += QStringLiteral(
"Failed because rendered image has an incompatible format - %1 (32 bit format is expected)\n" ).arg(
qgsEnumValueToKey( myResultImage.format() ) );
624 performPostTestActions( flags );
633 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
634 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
636 const int maskWidth = maskImage.width();
639 const int colorTolerance =
static_cast< int >( mColorTolerance );
640 for (
int y = 0; y < maxHeight; ++y )
642 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
643 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
644 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
645 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
647 for (
int x = 0; x < maxWidth; ++x )
649 const int pixelTolerance = maskScanline
650 ? std::max( colorTolerance, ( maskWidth > x ) ? qRed( maskScanline[ x ] ) : 0 )
652 if ( pixelTolerance == 255 )
658 const QRgb myExpectedPixel = expectedScanline[x];
659 const QRgb myActualPixel = resultScanline[x];
660 if ( pixelTolerance == 0 )
662 if ( myExpectedPixel != myActualPixel )
665 diffScanline[ x ] = qRgb( 255, 0, 0 );
670 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance ||
671 std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance ||
672 std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance ||
673 std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
676 diffScanline[ x ] = qRgb( 255, 0, 0 );
695 myDifferenceImage.save( mDiffImageFile );
702 mReport += QStringLiteral(
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>" )
708 if ( mMismatchCount > 0 )
715 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
716 mReport += QStringLiteral(
"%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
717 mReport += QLatin1String(
"</td></tr>" );
718 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
721 qDebug(
"Test failed because render step took too long" );
722 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
723 mReport += QLatin1String(
"<font color=red>Test failed because render step took too long</font>" );
724 mReport += QLatin1String(
"</td></tr>" );
727 mMarkdownReport += QLatin1String(
"Test failed because render step took too long\n" );
729 performPostTestActions( flags );
736 performPostTestActions( flags );
741 mReport += QLatin1String(
"<tr><td colspan=3></td></tr>" );
742 emitDashMessage( QStringLiteral(
"Image mismatch" ),
QgsDartMeasurement::Text,
"Difference image did not match any known anomaly or mask."
743 " If you feel the difference image should be considered an anomaly "
744 "you can do something like this\n"
746 "/\nIf it should be included in the mask run\n"
747 "scripts/generate_test_mask_image.py '" + referenceImageFile +
"' '" +
mRenderedImageFile +
"'\n" );
749 mReport += QLatin1String(
"<tr><td colspan = 3>\n" );
750 mReport += QStringLiteral(
"<font color=red>%1 and %2 for " ).arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
751 mReport += QLatin1String(
"</td></tr>" );
754 const QString githubSha = qgetenv(
"GITHUB_SHA" );
755 if ( !githubSha.isEmpty() )
757 const QString githubBlobUrl = QStringLiteral(
"https://github.com/qgis/QGIS/blob/%1/%2" ).arg(
758 githubSha, QDir(
sourcePath() ).relativeFilePath( referenceImageFile ) );
759 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
760 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), githubBlobUrl ).arg( mMismatchCount );
764 mMarkdownReport += QStringLiteral(
"Rendered image did not match [%1](%2) (found %3 pixels different)\n" ).arg(
765 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ),
766 QUrl::fromLocalFile( referenceImageFile ).toString() ).arg( mMismatchCount );
769 performPostTestActions( flags );