379 if ( !renderedImageFile.isEmpty() )
389 qDebug(
"QgsRenderChecker::runTest failed - Rendered Image File not set." );
391 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
392 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
393 "Image File not set.</td></tr></table>\n";
394 mMarkdownReport = u
"Failed because rendered image file was not set\n"_s;
395 performPostTestActions( flags );
402 QImage expectedImage( referenceImageFile );
403 if ( expectedImage.isNull() )
405 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load control image from " << referenceImageFile;
407 "<tr><td>Test Result:</td><td>Expected Result:</td></tr>\n"
408 "<tr><td>Nothing rendered</td>\n<td>Failed because control "
409 "image file could not be loaded.</td></tr></table>\n";
410 mMarkdownReport = u
"Failed because expected image file (%1) could not be loaded\n"_s.arg( referenceImageFile );
411 performPostTestActions( flags );
415 const QString expectedImageString = u
"<a href=\"%1\" style=\"color: inherit\" target=\"_blank\">expected</a> image"_s.arg( QUrl::fromLocalFile( referenceImageFile ).toString() );
416 const QString renderedImageString = u
"<a href=\"%2\" style=\"color: inherit\" target=\"_blank\">rendered</a> image"_s.arg( QUrl::fromLocalFile( renderedImageFile ).toString() );
417 auto upperFirst = [](
const QString &string ) -> QString {
418 const int firstNonTagIndex =
string.indexOf(
'>' ) + 1;
419 return string.left( firstNonTagIndex ) +
string.at( firstNonTagIndex ).toUpper() +
string.mid( firstNonTagIndex + 1 );
423 if ( myResultImage.isNull() )
425 qDebug() <<
"QgsRenderChecker::runTest failed - Could not load rendered image from " <<
mRenderedImageFile;
428 "<tr><td>Test Result:</td><td>%1:</td></tr>\n"
429 "<tr><td>Nothing rendered</td>\n<td>Failed because Rendered "
430 "Image File could not be loaded.</td></tr></table>\n"
432 .arg( upperFirst( expectedImageString ) );
434 performPostTestActions( flags );
437 QImage myDifferenceImage( expectedImage.width(), expectedImage.height(), QImage::Format_RGB32 );
438 mDiffImageFile = QDir::tempPath() +
'/' + testName +
"_result_diff.png";
439 myDifferenceImage.fill( qRgb( 152, 219, 249 ) );
442 QString maskImagePath = referenceImageFile;
443 maskImagePath.chop( 4 );
444 maskImagePath +=
"_mask.png"_L1;
445 const QImage maskImage( maskImagePath );
446 const bool hasMask = !maskImage.isNull();
451 mMatchTarget = expectedImage.width() * expectedImage.height();
452 const unsigned int myPixelCount = myResultImage.width() * myResultImage.height();
457 mReport +=
"<tr><td colspan=2>"_L1;
460 "%8 and %9 for %1<br>"
461 "Expected size: %2 w x %3 h (%4 pixels)<br>"
462 "Rendered size: %5 w x %6 h (%7 pixels)"
466 .arg( expectedImage.width() )
467 .arg( expectedImage.height() )
469 .arg( myResultImage.width() )
470 .arg( myResultImage.height() )
472 .arg( upperFirst( expectedImageString ), renderedImageString );
474 "<tr><td colspan=2>\n"
475 "Expected Duration : <= %1 (0 indicates not specified)<br>"
476 "Actual Duration : %2 ms<br></td></tr>"
478 .arg( mElapsedTimeTarget )
484 if ( !expectedImage.isNull() )
486 imgWidth = std::min( expectedImage.width(), imgWidth );
487 imgHeight = expectedImage.height() * imgWidth / expectedImage.width();
491 const QString diffImageFileName = QFileInfo( mDiffImageFile ).fileName();
492 const QString myImagesString = QString(
494 "<td colspan=2>Compare %10 and %11</td>"
495 "<td>Difference (all blue is good, any red is bad)</td>"
497 "<td colspan=2 id=\"td-%1-%7\"></td>\n"
498 "<td align=center><img width=%5 height=%6 src=\"%2\"></td>\n"
501 "<script>\naddComparison(\"td-%1-%7\",\"%3\",\"file://%4\",%5,%6);\n</script>\n"
502 "<p>If the new image looks good, create or update a test mask with<br>"
503 "<code onclick=\"copyToClipboard(this)\" class=\"copy-code\" data-tooltip=\"Click to copy\">scripts/generate_test_mask_image.py \"%8\" \"%9\"</code>"
505 .arg( testName, diffImageFileName, renderedImageFileName, referenceImageFile )
508 .arg( QUuid::createUuid().toString().mid( 1, 6 ), referenceImageFile,
mRenderedImageFile, expectedImageString, renderedImageString );
511 if ( !mControlPathPrefix.isNull() )
513 prefix = u
" (prefix %1)"_s.arg( mControlPathPrefix );
520 if ( !flags.testFlag(
Flag::Silent ) && ( expectedImage.width() != myResultImage.width() || expectedImage.height() != myResultImage.height() ) )
522 qDebug(
"Expected size: %dw x %dh", expectedImage.width(), expectedImage.height() );
523 qDebug(
"Actual size: %dw x %dh", myResultImage.width(), myResultImage.height() );
525 qDebug(
"Mask size: %dw x %dh", maskImage.width(), maskImage.height() );
532 qDebug(
"Expected image and rendered image for %s are different dimensions", testName.toLocal8Bit().constData() );
535 if ( std::abs( expectedImage.width() - myResultImage.width() ) > mMaxSizeDifferenceX || std::abs( expectedImage.height() - myResultImage.height() ) > mMaxSizeDifferenceY )
540 mReport +=
"<tr><td colspan=3>"_L1;
541 mReport += u
"<font color=red>%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions - FAILING!</font>";
543 mMarkdownReport += u
"Failed because rendered image and expected image are different dimensions (%1x%2 v2 %3x%4)\n"_s.arg( myResultImage.width() )
544 .arg( myResultImage.height() )
545 .arg( expectedImage.width() )
546 .arg( expectedImage.height() );
548 const QString diffSizeImagesString = QString(
550 "<td colspan=3>Compare %5 and %6</td>"
552 "<td align=center><img width=%3 height=%4 src=\"%2\"></td>\n"
553 "<td align=center><img src=\"%1\"></td>\n"
557 .arg( renderedImageFileName, referenceImageFile )
560 .arg( expectedImageString, renderedImageString );
562 mReport += diffSizeImagesString;
563 performPostTestActions( flags );
568 mReport +=
"<tr><td colspan=3>"_L1;
569 mReport += u
"%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are different dimensions, but within tolerance";
574 if ( expectedImage.format() == QImage::Format_Indexed8 )
576 if ( myResultImage.format() != QImage::Format_Indexed8 )
581 qDebug() <<
"Expected image and rendered image for " << testName <<
" have different formats (8bit format is expected) - FAILING!";
583 mReport +=
"<tr><td colspan=3>"_L1;
584 mReport +=
"<font color=red>Expected image and rendered image for " + testName +
" have different formats (8bit format is expected) - FAILING!</font>";
588 mMarkdownReport +=
"Failed because rendered image and expected image have different formats (8bit format is expected)\n"_L1;
589 performPostTestActions( flags );
596 myResultImage = myResultImage.convertToFormat( QImage::Format_ARGB32 );
597 expectedImage = expectedImage.convertToFormat( QImage::Format_ARGB32 );
599 if ( expectedImage.format() != QImage::Format_RGB32 && expectedImage.format() != QImage::Format_ARGB32 && expectedImage.format() != QImage::Format_ARGB32_Premultiplied )
601 mReport +=
"<tr><td colspan=3>"_L1;
602 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() ) );
606 mMarkdownReport += u
"Failed because expected image has an incompatible format - %1 (32 bit format is expected)\n"_s.arg(
qgsEnumValueToKey( expectedImage.format() ) );
607 performPostTestActions( flags );
610 if ( myResultImage.format() != QImage::Format_RGB32 && myResultImage.format() != QImage::Format_ARGB32 && myResultImage.format() != QImage::Format_ARGB32_Premultiplied )
612 mReport +=
"<tr><td colspan=3>"_L1;
613 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() ) );
617 mMarkdownReport += u
"Failed because rendered image has an incompatible format - %1 (32 bit format is expected)\n"_s.arg(
qgsEnumValueToKey( myResultImage.format() ) );
618 performPostTestActions( flags );
627 const int maxHeight = std::min( expectedImage.height(), myResultImage.height() );
628 const int maxWidth = std::min( expectedImage.width(), myResultImage.width() );
630 const int maskWidth = maskImage.width();
633 const int colorTolerance =
static_cast< int >( mColorTolerance );
634 for (
int y = 0; y < maxHeight; ++y )
636 const QRgb *expectedScanline =
reinterpret_cast< const QRgb *
>( expectedImage.constScanLine( y ) );
637 const QRgb *resultScanline =
reinterpret_cast< const QRgb *
>( myResultImage.constScanLine( y ) );
638 const QRgb *maskScanline = ( hasMask && maskImage.height() > y ) ?
reinterpret_cast< const QRgb *
>( maskImage.constScanLine( y ) ) :
nullptr;
639 QRgb *diffScanline =
reinterpret_cast< QRgb *
>( myDifferenceImage.scanLine( y ) );
641 for (
int x = 0; x < maxWidth; ++x )
643 const int pixelTolerance = maskScanline ? std::max( colorTolerance, ( maskWidth > x ) ? qRed( maskScanline[x] ) : 0 ) : colorTolerance;
644 if ( pixelTolerance == 255 )
650 const QRgb myExpectedPixel = expectedScanline[x];
651 const QRgb myActualPixel = resultScanline[x];
652 if ( pixelTolerance == 0 )
654 if ( myExpectedPixel != myActualPixel )
657 diffScanline[x] = qRgb( 255, 0, 0 );
662 if ( std::abs( qRed( myExpectedPixel ) - qRed( myActualPixel ) ) > pixelTolerance
663 || std::abs( qGreen( myExpectedPixel ) - qGreen( myActualPixel ) ) > pixelTolerance
664 || std::abs( qBlue( myExpectedPixel ) - qBlue( myActualPixel ) ) > pixelTolerance
665 || std::abs( qAlpha( myExpectedPixel ) - qAlpha( myActualPixel ) ) > pixelTolerance )
668 diffScanline[x] = qRgb( 255, 0, 0 );
690 myDifferenceImage.save( mDiffImageFile );
697 mReport += u
"<tr><td colspan=3>%1/%2 pixels mismatched (allowed threshold: %3, allowed color component tolerance: %4)</td></tr>"_s.arg( mMismatchCount ).arg(
mMatchTarget ).arg(
mismatchCount ).arg( mColorTolerance );
702 if ( mMismatchCount > 0 )
709 mReport +=
"<tr><td colspan = 3>\n"_L1;
710 mReport += u
"%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are matched<br>";
712 if ( mElapsedTimeTarget != 0 && mElapsedTimeTarget <
mElapsedTime )
715 qDebug(
"Test failed because render step took too long" );
716 mReport +=
"<tr><td colspan = 3>\n"_L1;
717 mReport +=
"<font color=red>Test failed because render step took too long</font>"_L1;
721 mMarkdownReport +=
"Test failed because render step took too long\n"_L1;
723 performPostTestActions( flags );
730 performPostTestActions( flags );
735 mReport +=
"<tr><td colspan=3></td></tr>"_L1;
739 "Difference image did not match any known anomaly or mask."
740 " If you feel the difference image should be considered an anomaly "
741 "you can do something like this\n"
747 +
"/\nIf it should be included in the mask run\n"
748 "scripts/generate_test_mask_image.py '"
755 mReport +=
"<tr><td colspan = 3>\n"_L1;
756 mReport += u
"<font color=red>%1 and %2 for "_s.arg( upperFirst( expectedImageString ), renderedImageString ) + testName +
" are mismatched</font><br>";
760 const QString githubSha = qgetenv(
"GITHUB_SHA" );
761 if ( !githubSha.isEmpty() )
763 const QString githubBlobUrl = u
"https://github.com/qgis/QGIS/blob/%1/%2"_s.arg( githubSha, QDir(
sourcePath() ).relativeFilePath( referenceImageFile ) );
764 mMarkdownReport += u
"Rendered image did not match [%1](%2) (found %3 pixels different)\n"_s.arg( QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), githubBlobUrl ).arg( mMismatchCount );
768 mMarkdownReport += u
"Rendered image did not match [%1](%2) (found %3 pixels different)\n"_s
769 .arg( QDir(
sourcePath() ).relativeFilePath( referenceImageFile ), QUrl::fromLocalFile( referenceImageFile ).toString() )
770 .arg( mMismatchCount );
773 performPostTestActions( flags );