Browse Source

DebugTools: remove unnecessary std::tuple usage from CompareImage.

pull/605/head
Vladimír Vondruš 4 years ago
parent
commit
4785bbe9da
  1. 14
      src/Magnum/DebugTools/CompareImage.cpp
  2. 3
      src/Magnum/DebugTools/CompareImage.h
  3. 45
      src/Magnum/DebugTools/Test/CompareImageTest.cpp

14
src/Magnum/DebugTools/CompareImage.cpp

@ -28,11 +28,12 @@
#include <map> #include <map>
#include <sstream> #include <sstream>
#include <Corrade/Containers/Array.h> #include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/Pair.h> #include <Corrade/Containers/Pair.h>
#include <Corrade/Containers/StridedArrayView.h> #include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/Containers/String.h> #include <Corrade/Containers/String.h>
#include <Corrade/Containers/StringStl.h> /** @todo remove once AbstractImporter is <string>-free */ #include <Corrade/Containers/StringStl.h> /** @todo remove once AbstractImporter is <string>-free */
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Triple.h>
#include <Corrade/PluginManager/Manager.h> #include <Corrade/PluginManager/Manager.h>
#include <Corrade/TestSuite/Comparator.h> #include <Corrade/TestSuite/Comparator.h>
#include <Corrade/Utility/DebugStl.h> #include <Corrade/Utility/DebugStl.h>
@ -95,7 +96,7 @@ template<std::size_t size, class T> Float calculateImageDelta(const Containers::
} }
std::tuple<Containers::Array<Float>, Float, Float> calculateImageDelta(const PixelFormat actualFormat, const Containers::StridedArrayView3D<const char>& actualPixels, const ImageView2D& expected) { Containers::Triple<Containers::Array<Float>, Float, Float> calculateImageDelta(const PixelFormat actualFormat, const Containers::StridedArrayView3D<const char>& actualPixels, const ImageView2D& expected) {
/* Calculate a delta image */ /* Calculate a delta image */
Containers::Array<Float> deltaData{NoInit, Containers::Array<Float> deltaData{NoInit,
std::size_t(expected.size().product())}; std::size_t(expected.size().product())};
@ -203,7 +204,7 @@ std::tuple<Containers::Array<Float>, Float, Float> calculateImageDelta(const Pix
left that could cause the comparison to fail. */ left that could cause the comparison to fail. */
const Float mean = Math::Algorithms::kahanSum(deltaData.begin(), deltaData.end())/deltaData.size(); const Float mean = Math::Algorithms::kahanSum(deltaData.begin(), deltaData.end())/deltaData.size();
return std::make_tuple(std::move(deltaData), max, mean); return {std::move(deltaData), max, mean};
} }
namespace { namespace {
@ -484,8 +485,9 @@ TestSuite::ComparisonStatusFlags ImageComparatorBase::compare(const PixelFormat
return TestSuite::ComparisonStatusFlag::Failed; return TestSuite::ComparisonStatusFlag::Failed;
} }
Containers::Array<Float> delta; Containers::Triple<Containers::Array<Float>, Float, Float> deltaMaxMean = DebugTools::Implementation::calculateImageDelta(actualFormat, actualPixels, expected);
std::tie(delta, _state->max, _state->mean) = DebugTools::Implementation::calculateImageDelta(actualFormat, actualPixels, expected); _state->max = deltaMaxMean.second();
_state->mean = deltaMaxMean.third();
/* Verify the max/mean is never below zero so we didn't mess up when /* Verify the max/mean is never below zero so we didn't mess up when
calculating specials. Note the inverted condition to catch NaNs in calculating specials. Note the inverted condition to catch NaNs in
@ -511,7 +513,7 @@ TestSuite::ComparisonStatusFlags ImageComparatorBase::compare(const PixelFormat
} else return TestSuite::ComparisonStatusFlags{}; } else return TestSuite::ComparisonStatusFlags{};
/* Otherwise save the deltas and fail */ /* Otherwise save the deltas and fail */
_state->delta = std::move(delta); _state->delta = std::move(deltaMaxMean.first());
return flags; return flags;
} }

3
src/Magnum/DebugTools/CompareImage.h

@ -32,7 +32,6 @@
#include <Corrade/Containers/Pointer.h> #include <Corrade/Containers/Pointer.h>
#include <Corrade/PluginManager/PluginManager.h> #include <Corrade/PluginManager/PluginManager.h>
#include <Corrade/TestSuite/TestSuite.h> #include <Corrade/TestSuite/TestSuite.h>
#include <Corrade/Utility/StlForwardTuple.h>
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/PixelFormat.h" #include "Magnum/PixelFormat.h"
@ -49,7 +48,7 @@
namespace Magnum { namespace DebugTools { namespace Magnum { namespace DebugTools {
namespace Implementation { namespace Implementation {
MAGNUM_DEBUGTOOLS_EXPORT std::tuple<Containers::Array<Float>, Float, Float> calculateImageDelta(PixelFormat actualFormat, const Containers::StridedArrayView3D<const char>& actualPixels, const ImageView2D& expected); MAGNUM_DEBUGTOOLS_EXPORT Containers::Triple<Containers::Array<Float>, Float, Float> calculateImageDelta(PixelFormat actualFormat, const Containers::StridedArrayView3D<const char>& actualPixels, const ImageView2D& expected);
MAGNUM_DEBUGTOOLS_EXPORT void printDeltaImage(Debug& out, Containers::ArrayView<const Float> delta, const Vector2i& size, Float max, Float maxThreshold, Float meanThreshold); MAGNUM_DEBUGTOOLS_EXPORT void printDeltaImage(Debug& out, Containers::ArrayView<const Float> delta, const Vector2i& size, Float max, Float maxThreshold, Float meanThreshold);

45
src/Magnum/DebugTools/Test/CompareImageTest.cpp

@ -29,6 +29,7 @@
#include <Corrade/Containers/StridedArrayView.h> #include <Corrade/Containers/StridedArrayView.h>
#include <Corrade/Containers/Optional.h> #include <Corrade/Containers/Optional.h>
#include <Corrade/Containers/String.h> #include <Corrade/Containers/String.h>
#include <Corrade/Containers/Triple.h>
#include <Corrade/PluginManager/Manager.h> #include <Corrade/PluginManager/Manager.h>
#include <Corrade/TestSuite/Tester.h> #include <Corrade/TestSuite/Tester.h>
#include <Corrade/TestSuite/Compare/Container.h> #include <Corrade/TestSuite/Compare/Container.h>
@ -280,13 +281,12 @@ void CompareImageTest::formatImplementationSpecific() {
} }
void CompareImageTest::calculateDelta() { void CompareImageTest::calculateDelta() {
Containers::Array<Float> delta; Containers::Triple<Containers::Array<Float>, Float, Float> deltaMaxMean = Implementation::calculateImageDelta(ActualRed.format(), ActualRed.pixels(), ExpectedRed);
Float max, mean; CORRADE_COMPARE_AS(deltaMaxMean.first(),
std::tie(delta, max, mean) = Implementation::calculateImageDelta(ActualRed.format(), ActualRed.pixels(), ExpectedRed); Containers::arrayView(DeltaRed),
TestSuite::Compare::Container);
CORRADE_COMPARE_AS(delta, Containers::arrayView(DeltaRed), TestSuite::Compare::Container); CORRADE_COMPARE(deltaMaxMean.second(), 1.0f);
CORRADE_COMPARE(max, 1.0f); CORRADE_COMPARE(deltaMaxMean.third(), 0.208889f);
CORRADE_COMPARE(mean, 0.208889f);
} }
/* Different storage for each */ /* Different storage for each */
@ -308,16 +308,13 @@ const ImageView2D ExpectedRgb{
PixelFormat::RGB8Unorm, {2, 2}, ExpectedRgbData}; PixelFormat::RGB8Unorm, {2, 2}, ExpectedRgbData};
void CompareImageTest::calculateDeltaStorage() { void CompareImageTest::calculateDeltaStorage() {
Containers::Array<Float> delta; Containers::Triple<Containers::Array<Float>, Float, Float> deltaMaxMean = Implementation::calculateImageDelta(ActualRgb.format(), ActualRgb.pixels(), ExpectedRgb);
Float max, mean; CORRADE_COMPARE_AS(deltaMaxMean.first(), Containers::arrayView<Float>({
std::tie(delta, max, mean) = Implementation::calculateImageDelta(ActualRgb.format(), ActualRgb.pixels(), ExpectedRgb);
CORRADE_COMPARE_AS(delta, Containers::arrayView<Float>({
1.0f/3.0f, (55.0f + 1.0f)/3.0f, 1.0f/3.0f, (55.0f + 1.0f)/3.0f,
48.0f/3.0f, 117.0f/3.0f 48.0f/3.0f, 117.0f/3.0f
}), TestSuite::Compare::Container); }), TestSuite::Compare::Container);
CORRADE_COMPARE(max, 117.0f/3.0f); CORRADE_COMPARE(deltaMaxMean.second(), 117.0f/3.0f);
CORRADE_COMPARE(mean, 18.5f); CORRADE_COMPARE(deltaMaxMean.third(), 18.5f);
} }
/* Variants: /* Variants:
@ -352,18 +349,16 @@ const ImageView2D ActualSpecials{PixelFormat::R32F, {9, 1}, ActualDataSpecials};
const ImageView2D ExpectedSpecials{PixelFormat::R32F, {9, 1}, ExpectedDataSpecials}; const ImageView2D ExpectedSpecials{PixelFormat::R32F, {9, 1}, ExpectedDataSpecials};
void CompareImageTest::calculateDeltaSpecials() { void CompareImageTest::calculateDeltaSpecials() {
Containers::Array<Float> delta; Containers::Triple<Containers::Array<Float>, Float, Float> deltaMaxMean = Implementation::calculateImageDelta(ActualSpecials.format(), ActualSpecials.pixels(), ExpectedSpecials);
Float max, mean; CORRADE_COMPARE_AS(Containers::arrayView(deltaMaxMean.first()),
std::tie(delta, max, mean) = Implementation::calculateImageDelta(ActualSpecials.format(), ActualSpecials.pixels(), ExpectedSpecials);
CORRADE_COMPARE_AS(Containers::arrayView(delta),
Containers::arrayView(DeltaSpecials), Containers::arrayView(DeltaSpecials),
TestSuite::Compare::Container); TestSuite::Compare::Container);
/* Max should be calculated *without* the specials because otherwise every /* Max should be calculated *without* the specials because otherwise every
other potential difference will be zero compared to infinity. OTOH mean other potential difference will be zero compared to infinity. OTOH mean
needs to get "poisoned" by those in order to have *something* to fail needs to get "poisoned" by those in order to have *something* to fail
the test with. */ the test with. */
CORRADE_COMPARE(max, 3.1f); CORRADE_COMPARE(deltaMaxMean.second(), 3.1f);
CORRADE_COMPARE(mean, -Constants::nan()); CORRADE_COMPARE(deltaMaxMean.third(), -Constants::nan());
} }
void CompareImageTest::calculateDeltaSpecials3() { void CompareImageTest::calculateDeltaSpecials3() {
@ -373,17 +368,15 @@ void CompareImageTest::calculateDeltaSpecials3() {
const ImageView2D actualSpecials3{PixelFormat::RGB32F, {3, 1}, ActualDataSpecials}; const ImageView2D actualSpecials3{PixelFormat::RGB32F, {3, 1}, ActualDataSpecials};
const ImageView2D expectedSpecials3{PixelFormat::RGB32F, {3, 1}, ExpectedDataSpecials}; const ImageView2D expectedSpecials3{PixelFormat::RGB32F, {3, 1}, ExpectedDataSpecials};
Containers::Array<Float> delta; Containers::Triple<Containers::Array<Float>, Float, Float> deltaMaxMean = Implementation::calculateImageDelta(actualSpecials3.format(), actualSpecials3.pixels(), expectedSpecials3);
Float max, mean; CORRADE_COMPARE_AS(deltaMaxMean.first(), Containers::arrayView<Float>({
std::tie(delta, max, mean) = Implementation::calculateImageDelta(actualSpecials3.format(), actualSpecials3.pixels(), expectedSpecials3);
CORRADE_COMPARE_AS(delta, Containers::arrayView<Float>({
Constants::nan(), Constants::nan(), 1.15f Constants::nan(), Constants::nan(), 1.15f
}), TestSuite::Compare::Container); }), TestSuite::Compare::Container);
/* Max and mean should be calculated *without* the specials because /* Max and mean should be calculated *without* the specials because
otherwise every other potential difference will be zero compared to otherwise every other potential difference will be zero compared to
infinity */ infinity */
CORRADE_COMPARE(max, 1.15f); CORRADE_COMPARE(deltaMaxMean.second(), 1.15f);
CORRADE_COMPARE(mean, -Constants::nan()); CORRADE_COMPARE(deltaMaxMean.third(), -Constants::nan());
} }
void CompareImageTest::deltaImage() { void CompareImageTest::deltaImage() {

Loading…
Cancel
Save