Browse Source

DebugTools: no need to use std::vector in CompareImage internals.

Containers::Array will do juust fine.
pull/331/head
Vladimír Vondruš 7 years ago
parent
commit
10fec86534
  1. 15
      src/Magnum/DebugTools/CompareImage.cpp
  2. 10
      src/Magnum/DebugTools/CompareImage.h
  3. 16
      src/Magnum/DebugTools/Test/CompareImageTest.cpp

15
src/Magnum/DebugTools/CompareImage.cpp

@ -27,7 +27,6 @@
#include <map>
#include <sstream>
#include <vector>
#include <Corrade/Containers/Optional.h>
#include <Corrade/PluginManager/Manager.h>
@ -47,7 +46,7 @@ template<std::size_t size, class T> Math::Vector<size, T> pixelAt(const char* co
return reinterpret_cast<const Math::Vector<size, T>*>(pixels + stride*pos.y())[pos.x()];
}
template<std::size_t size, class T> Float calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected, std::vector<Float>& output) {
template<std::size_t size, class T> Float calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected, Containers::ArrayView<Float> output) {
CORRADE_INTERNAL_ASSERT(output.size() == std::size_t(expected.size().product()));
/* Precalculate parameters for pixel access */
@ -79,9 +78,9 @@ template<std::size_t size, class T> Float calculateImageDelta(const ImageView2D&
}
std::tuple<std::vector<Float>, Float, Float> calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected) {
std::tuple<Containers::Array<Float>, Float, Float> calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected) {
/* Calculate a delta image */
std::vector<Float> delta(expected.size().product());
Containers::Array<Float> delta{Containers::NoInit, std::size_t(expected.size().product())};
CORRADE_ASSERT(!isPixelFormatImplementationSpecific(expected.format()),
"DebugTools::CompareImage: can't compare implementation-specific pixel formats", {});
@ -145,7 +144,7 @@ std::tuple<std::vector<Float>, Float, Float> calculateImageDelta(const ImageView
precision -- that would result in having false negatives! */
const Float mean = Math::Algorithms::kahanSum(delta.begin(), delta.end())/delta.size();
return std::make_tuple(delta, max, mean);
return std::make_tuple(std::move(delta), max, mean);
}
namespace {
@ -154,7 +153,7 @@ namespace {
const char Characters[] = " .,:~=+?7IZ$08DNM";
}
void printDeltaImage(Debug& out, const std::vector<Float>& deltas, const Vector2i& size, const Float max, const Float maxThreshold, const Float meanThreshold) {
void printDeltaImage(Debug& out, Containers::ArrayView<const Float> deltas, const Vector2i& size, const Float max, const Float maxThreshold, const Float meanThreshold) {
CORRADE_INTERNAL_ASSERT(meanThreshold <= maxThreshold);
/* At most 64 characters per line. The console fonts height is usually 2x
@ -254,7 +253,7 @@ void printPixelAt(Debug& out, const char* const pixels, const std::size_t stride
}
void printPixelDeltas(Debug& out, const std::vector<Float>& delta, const ImageView2D& actual, const ImageView2D& expected, const Float maxThreshold, const Float meanThreshold, std::size_t maxCount) {
void printPixelDeltas(Debug& out, Containers::ArrayView<const Float> delta, const ImageView2D& actual, const ImageView2D& expected, const Float maxThreshold, const Float meanThreshold, std::size_t maxCount) {
/* Precalculate parameters for pixel access */
Math::Vector2<std::size_t> offset, size;
@ -356,7 +355,7 @@ bool ImageComparatorBase::operator()(const ImageView2D& actual, const ImageView2
return false;
}
std::vector<Float> delta;
Containers::Array<Float> delta;
std::tie(delta, _max, _mean) = DebugTools::Implementation::calculateImageDelta(actual, expected);
/* If both values are not above threshold, success */

10
src/Magnum/DebugTools/CompareImage.h

@ -29,7 +29,7 @@
* @brief Class @ref Magnum::DebugTools::CompareImage
*/
#include <vector>
#include <Corrade/Containers/Array.h>
#include <Corrade/Containers/Pointer.h>
#include <Corrade/PluginManager/PluginManager.h>
#include <Corrade/TestSuite/Comparator.h>
@ -42,11 +42,11 @@
namespace Magnum { namespace DebugTools {
namespace Implementation {
MAGNUM_DEBUGTOOLS_EXPORT std::tuple<std::vector<Float>, Float, Float> calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected);
MAGNUM_DEBUGTOOLS_EXPORT std::tuple<Containers::Array<Float>, Float, Float> calculateImageDelta(const ImageView2D& actual, const ImageView2D& expected);
MAGNUM_DEBUGTOOLS_EXPORT void printDeltaImage(Debug& out, const std::vector<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);
MAGNUM_DEBUGTOOLS_EXPORT void printPixelDeltas(Debug& out, const std::vector<Float>& delta, const ImageView2D& actual, const ImageView2D& expected, Float maxThreshold, Float meanThreshold, std::size_t maxCount);
MAGNUM_DEBUGTOOLS_EXPORT void printPixelDeltas(Debug& out, Containers::ArrayView<const Float> delta, const ImageView2D& actual, const ImageView2D& expected, Float maxThreshold, Float meanThreshold, std::size_t maxCount);
}
class CompareImage;
@ -85,7 +85,7 @@ class MAGNUM_DEBUGTOOLS_EXPORT ImageComparatorBase {
State _state{};
const ImageView2D *_actualImage, *_expectedImage;
Float _max, _mean;
std::vector<Float> _delta;
Containers::Array<Float> _delta;
};
}}}

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

@ -173,7 +173,7 @@ const Float ExpectedRedData[] = {
0.02f, 0.0f, 0.0f
};
const std::vector<Float> DeltaRed{
const Float DeltaRed[]{
0.35f, 0.0f, 0.3f,
0.01f, 0.0f, 0.1f,
0.12f, 1.0f, 0.0f
@ -213,11 +213,11 @@ void CompareImageTest::formatImplementationSpecific() {
}
void CompareImageTest::calculateDelta() {
std::vector<Float> delta;
Containers::Array<Float> delta;
Float max, mean;
std::tie(delta, max, mean) = Implementation::calculateImageDelta(ActualRed, ExpectedRed);
CORRADE_COMPARE_AS(delta, DeltaRed, TestSuite::Compare::Container);
CORRADE_COMPARE_AS(delta, Containers::arrayView(DeltaRed), TestSuite::Compare::Container);
CORRADE_COMPARE(max, 1.0f);
CORRADE_COMPARE(mean, 0.208889f);
}
@ -241,14 +241,14 @@ const ImageView2D ExpectedRgb{
PixelFormat::RGB8Unorm, {2, 2}, ExpectedRgbData};
void CompareImageTest::calculateDeltaStorage() {
std::vector<Float> delta;
Containers::Array<Float> delta;
Float max, mean;
std::tie(delta, max, mean) = Implementation::calculateImageDelta(ActualRgb, ExpectedRgb);
CORRADE_COMPARE_AS(delta, (std::vector<Float>{
CORRADE_COMPARE_AS(delta, (Containers::Array<Float>{Containers::InPlaceInit, {
1.0f/3.0f, (55.0f + 1.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(mean, 18.5f);
}
@ -257,7 +257,7 @@ void CompareImageTest::deltaImage() {
std::ostringstream out;
Debug d{&out, Debug::Flag::DisableColors};
std::vector<Float> delta(32*32);
Containers::Array<Float> delta{32*32};
for(std::int_fast32_t x = 0; x != 32; ++x)
for(std::int_fast32_t y = 0; y != 32; ++y)
@ -287,7 +287,7 @@ void CompareImageTest::deltaImageScaling() {
std::ostringstream out;
Debug d{&out, Debug::Flag::DisableColors};
std::vector<Float> delta(65*40);
Containers::Array<Float> delta{65*40};
for(std::int_fast32_t x = 0; x != 65; ++x)
for(std::int_fast32_t y = 0; y != 40; ++y)
delta[y*65 + x] = Vector2{Float(x), Float(y)}.length()/Vector2{65.0f, 40.0f}.length();

Loading…
Cancel
Save