391 if ( ! renderedImageFile.isEmpty() )
401 qDebug(
"QgsRenderChecker::runTest failed - Rendered Image File not set." );
403 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
404 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
405 "Image File not set.</td></tr></table>\n";
406 mMarkdownReport = u
"Failed because rendered image file was not set\n"_s;
407 performPostTestActions( flags );
414 QImage expectedImage( referenceImageFile );
415 if ( expectedImage.isNull() )
417 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load control image from " << referenceImageFile;
419 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
420 "<tr><td>Nothing rendered</td>\n<td>Failed because control "
421 "image file could not be loaded.</td></tr></table>\n";
422 mMarkdownReport = u
"Failed because expected image file (%1) could not be loaded\n"_s.arg( referenceImageFile );
423 performPostTestActions( flags );
427 const QString expectedImageString = u
"<a href=\"%1\" style=\"color: inherit\" target=\"_blank\">expected</a> image"_s.arg( QUrl::fromLocalFile( referenceImageFile ).toString() );
428 const QString renderedImageString = u
"<a href=\"%2\" style=\"color: inherit\" target=\"_blank\">rendered</a> image"_s.arg( QUrl::fromLocalFile( renderedImageFile ).toString() );
429 auto upperFirst = [](
const QString & string ) -> QString
431 const int firstNonTagIndex =
string.indexOf(
'>' ) + 1;
432 return string.left( firstNonTagIndex ) +
string.at( firstNonTagIndex ).toUpper() +
string.mid( firstNonTagIndex + 1 );
436 if ( myResultImage.isNull() )
438 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load rendered image from " <<
mRenderedImageFile;
439 mReport = QStringLiteral(
"<table>"
440 "<tr><td>Test Result:</td><td>%1:</td></tr>\n"
441 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
442 "Image File could not be loaded.</td></tr></table>\n" ).arg( upperFirst( expectedImageString ) );
444 performPostTestActions( flags );
447 QImage myDifferenceImage( expectedImage.width(),
448 expectedImage.height(),
449 QImage::Format_RGB32 );
450 mDiffImageFile = QDir::tempPath() +
'/' + testName +
"_result_diff.png";
451 myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
454 QString maskImagePath = referenceImageFile;
455 maskImagePath.chop( 4 );
456 maskImagePath +=
"_mask.png"_L1;
457 const QImage maskImage( maskImagePath );
458 const bool hasMask = !maskImage.isNull();
463 mMatchTarget = expectedImage.width() * expectedImage.height();
464 const unsigned int myPixelCount = myResultImage.width() * myResultImage.height();
469 mReport +=
"<tr><td colspan=2>"_L1;
470 mReport += QStringLiteral(
"<tr><td colspan=2>"
471 "%8 and %9 for %1<br>"
472 "Expected size: %2 w x %3 h (%4 pixels)<br>"
473 "Rendered size: %5 w x %6 h (%7 pixels)"
476 .arg( expectedImage.width() ).arg( expectedImage.height() ).arg(
mMatchTarget )
477 .arg( myResultImage.width() ).arg( myResultImage.height() ).arg( myPixelCount )
478 .arg( upperFirst( expectedImageString ), renderedImageString );
479 mReport += QString(
"<tr><td colspan=2>\n"
480 "Expected Duration : <= %1 (0 indicates not specified)<br>"
481 "Actual Duration : %2 ms<br></td></tr>" )
482 .arg( mElapsedTimeTarget )
488 if ( ! expectedImage.isNull() )
490 imgWidth = std::min( expectedImage.width(), imgWidth );
491 imgHeight = expectedImage.height() * imgWidth / expectedImage.width();
495 const QString diffImageFileName = QFileInfo( mDiffImageFile ).fileName();
496 const QString myImagesString = QString(
498 "<td colspan=2>Compare %10 and %11</td>"
499 "<td>Difference (all blue is good, any red is bad)</td>"
501 "<td colspan=2 id=\"td-%1-%7\"></td>\n"
502 "<td align=center><img width=%5 height=%6 src=\"%2\"></td>\n"
505 "<script>\naddComparison(\"td-%1-%7\",\"%3\",\"file://%4\",%5,%6);\n</script>\n"
506 "<p>If the new image looks good, create or update a test mask with<br>"
507 "<code onclick=\"copyToClipboard(this)\" class=\"copy-code\" data-tooltip=\"Click to copy\">scripts/generate_test_mask_image.py \"%8\" \"%9\"</code>"
511 renderedImageFileName,
513 .arg( imgWidth ).arg( imgHeight )
514 .arg( QUuid::createUuid().toString().mid( 1, 6 ),
522 if ( !mControlPathPrefix.isNull() )
524 prefix = u
" (prefix %1)"_s.arg( mControlPathPrefix );
532 && ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() )
535 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
536 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
538 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
545 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
548 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX ||
549 std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
554 mReport +=
"<tr><td colspan=3>"_L1;
555 mReport += u
"<font color=red>%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
557 mMarkdownReport += u
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n"_s
558 .arg( myResultImage.width() )
559 .arg( myResultImage.height() )
560 .arg( expectedImage.width() )
561 .arg( expectedImage.height() );
563 const QString diffSizeImagesString = QString(
565 "<td colspan=3>Compare %5 and %6</td>"
567 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
568 "<td align=center><img src=\"%1\"></td>\n"
572 renderedImageFileName,
574 .arg( imgWidth ).arg( imgHeight )
575 .arg( expectedImageString, renderedImageString );
577 mReport += diffSizeImagesString;
578 performPostTestActions( flags );
583 mReport +=
"<tr><td colspan=3>"_L1;
584 mReport += u
"%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
589 if ( expectedImage.format() == QImage::Format_Indexed8 )
591 if ( myResultImage.format() != QImage::Format_Indexed8 )
596 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
598 mReport +=
"<tr><td colspan=3>"_L1;
599 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
603 mMarkdownReport +=
"Failed because rendered image and expected image have different formats (8bit format is expected)\n"_L1;
604 performPostTestActions( flags );
611 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
612 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
614 if ( expectedImage.format() != QImage::Format_RGB32
615 && expectedImage.format() != QImage::Format_ARGB32
616 && expectedImage.format() != QImage::Format_ARGB32_Premultiplied )
618 mReport +=
"<tr><td colspan=3>"_L1;
619 mReport += u
"<font color=red>Expected image for %1 is not a compatible format (%2). Must be 32 bit RGB format.</font>"_s.arg( testName,
qgsEnumValueToKey( expectedImage.format() ) );
623 mMarkdownReport += u
"Failed because expected image has an incompatible format - %1 (32 bit format is expected)\n"_s.arg(
qgsEnumValueToKey( expectedImage.format() ) );
624 performPostTestActions( flags );
627 if ( myResultImage.format() != QImage::Format_RGB32
628 && myResultImage.format() != QImage::Format_ARGB32
629 && myResultImage.format() != QImage::Format_ARGB32_Premultiplied )
631 mReport +=
"<tr><td colspan=3>"_L1;
632 mReport += u
"<font color=red>Rendered image for %1 is not a compatible format (%2). Must be 32 bit RGB format.</font>"_s.arg( testName,
qgsEnumValueToKey( myResultImage.format() ) );
636 mMarkdownReport += u
"Failed because rendered image has an incompatible format - %1 (32 bit format is expected)\n"_s.arg(
qgsEnumValueToKey( myResultImage.format() ) );
637 performPostTestActions( flags );
646 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
647 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
649 const int maskWidth = maskImage.width();
652 const int colorTolerance =
static_cast< int >( mColorTolerance );
653 for (
int y = 0; y < maxHeight; ++y )
655 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
656 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
657 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
658 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
660 for (
int x = 0; x < maxWidth; ++x )
662 const int pixelTolerance = maskScanline
663 ? std::max( colorTolerance, ( maskWidth > x ) ? qRed( maskScanline[ x ] ) : 0 )
665 if ( pixelTolerance == 255 )
671 const QRgb myExpectedPixel = expectedScanline[x];
672 const QRgb myActualPixel = resultScanline[x];
673 if ( pixelTolerance == 0 )
675 if ( myExpectedPixel != myActualPixel )
678 diffScanline[ x ] = qRgb( 255, 0, 0 );
683 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance ||
684 std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance ||
685 std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance ||
686 std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
689 diffScanline[ x ] = qRgb( 255, 0, 0 );
711 myDifferenceImage.save( mDiffImageFile );
718 mReport += u
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>"_s
724 if ( mMismatchCount > 0 )
731 mReport +=
"<tr><td colspan = 3>\n"_L1;
732 mReport += u
"%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
734 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
737 qDebug(
"Test failed because render step took too long" );
738 mReport +=
"<tr><td colspan = 3>\n"_L1;
739 mReport +=
"<font color=red>Test failed because render step took too long</font>"_L1;
743 mMarkdownReport +=
"Test failed because render step took too long\n"_L1;
745 performPostTestActions( flags );
752 performPostTestActions( flags );
757 mReport +=
"<tr><td colspan=3></td></tr>"_L1;
758 emitDashMessage( u
"Image mismatch"_s,
QgsDartMeasurement::Text,
"Difference image did not match any known anomaly or mask."
759 " If you feel the difference image should be considered an anomaly "
760 "you can do something like this\n"
762 "/\nIf it should be included in the mask run\n"
763 "scripts/generate_test_mask_image.py '" + referenceImageFile +
"' '" +
mRenderedImageFile +
"'\n" );
765 mReport +=
"<tr><td colspan = 3>\n"_L1;
766 mReport += u
"<font color=red>%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
770 const QString githubSha = qgetenv(
"GITHUB_SHA" );
771 if ( !githubSha.isEmpty() )
773 const QString githubBlobUrl = u
"https://github.com/qgis/QGIS/blob/%1/%2"_s.arg(
774 githubSha, QDir(
sourcePath() ).relativeFilePath( referenceImageFile ) );
775 mMarkdownReport += u
"Rendered image did not match [%1](%2) (found %3 pixels different)\n"_s.arg(
776 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), githubBlobUrl ).arg( mMismatchCount );
780 mMarkdownReport += u
"Rendered image did not match [%1](%2) (found %3 pixels different)\n"_s.arg(
781 QDir(
sourcePath() ).relativeFilePath( referenceImageFile ),
782 QUrl::fromLocalFile( referenceImageFile ).toString() ).arg( mMismatchCount );
785 performPostTestActions( flags );