Skip to content

Commit

Permalink
DebugTools: initial implementation of CompareImage class.
Browse files Browse the repository at this point in the history
Currently just does per-pixel comparison and calculates absolute delta,
failing the comparison if max/mean delta threshold is above specified
values. Useful enough for the case I have right now, might fail in other
case -- but still better than whatever else I was using before :)
  • Loading branch information
mosra committed Jan 12, 2017
1 parent bb69a88 commit c45472a
Show file tree
Hide file tree
Showing 13 changed files with 1,250 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CMakeLists.txt
Expand Up @@ -335,3 +335,8 @@ set(MAGNUM_PLUGINS_AUDIOIMPORTER_RELEASE_DIR ${MAGNUM_PLUGINS_RELEASE_DIR}/audio

add_subdirectory(modules)
add_subdirectory(src)

# Build snippets as part of testing
if(BUILD_TESTS)
add_subdirectory(doc/snippets)
endif()
4 changes: 3 additions & 1 deletion Doxyfile
Expand Up @@ -892,7 +892,8 @@ EXCLUDE_SYMBOLS = Magnum::*Implementation \
# that contain example code fragments that are included (see the \include
# command).

EXAMPLE_PATH = ../magnum-examples/src/
EXAMPLE_PATH = doc/snippets/ \
../magnum-examples/src/

# If the value of the EXAMPLE_PATH tag contains directories, you can use the
# EXAMPLE_PATTERNS tag to specify one or more wildcard pattern (like *.cpp and
Expand All @@ -913,6 +914,7 @@ EXAMPLE_RECURSIVE = NO
# \image command).

IMAGE_PATH = doc/ \
doc/snippets/ \
../magnum-examples/src/

# The INPUT_FILTER tag can be used to specify a program that doxygen should
Expand Down
40 changes: 40 additions & 0 deletions doc/snippets/CMakeLists.txt
@@ -0,0 +1,40 @@
#
# This file is part of Magnum.
#
# Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
# Vladimír Vondruš <mosra@centrum.cz>
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#

find_package(Corrade COMPONENTS TestSuite)

if(WITH_DEBUGTOOLS AND Corrade_TestSuite_FOUND)
set(SNIPPETS_DIR ${CMAKE_CURRENT_SOURCE_DIR})
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/configure.h.cmake
${CMAKE_CURRENT_BINARY_DIR}/configure.h)

# CompareImage documentation snippet. I need it executable so I can
# copy&paste the output to the documentation. Also mot using
# corrade_add_test() because it shouldn't be run as part of CTest as it
# purposedly fails.
add_executable(debugtools-compareimage debugtools-compareimage.cpp)
target_link_libraries(debugtools-compareimage PRIVATE MagnumDebugTools)
target_include_directories(debugtools-compareimage PRIVATE ${CMAKE_CURRENT_BINARY_DIR})
endif()
27 changes: 27 additions & 0 deletions doc/snippets/configure.h.cmake
@@ -0,0 +1,27 @@
/*
This file is part of Magnum.

Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
Vladimír Vondruš <mosra@centrum.cz>

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#define MAGNUM_PLUGINS_IMPORTER_DIR "${MAGNUM_PLUGINS_IMPORTER_DIR}"
#define SNIPPETS_DIR "${SNIPPETS_DIR}"
82 changes: 82 additions & 0 deletions doc/snippets/debugtools-compareimage.cpp
@@ -0,0 +1,82 @@
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/

#include <Corrade/PluginManager/Manager.h>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/Utility/Directory.h>

#include "Magnum/Image.h"
#include "Magnum/Trade/ImageData.h"
#include "Magnum/Trade/AbstractImporter.h"
#include "Magnum/DebugTools/CompareImage.h"

#include "configure.h"

using namespace Magnum;

namespace {

Image2D doProcessing() {
PluginManager::Manager<Trade::AbstractImporter> manager{MAGNUM_PLUGINS_IMPORTER_DIR};
std::unique_ptr<Trade::AbstractImporter> importer = manager.loadAndInstantiate("TgaImporter");
importer->openFile(Utility::Directory::join(SNIPPETS_DIR, "image2.tga"));
auto image = importer->image2D(0);
CORRADE_INTERNAL_ASSERT(image);
return Image2D{image->storage(), image->format(), image->type(), image->size(), image->release()};
}

Image2D loadExpectedImage() {
PluginManager::Manager<Trade::AbstractImporter> manager{MAGNUM_PLUGINS_IMPORTER_DIR};
std::unique_ptr<Trade::AbstractImporter> importer = manager.loadAndInstantiate("TgaImporter");
importer->openFile(Utility::Directory::join(SNIPPETS_DIR, "image1.tga"));
auto image = importer->image2D(0);
CORRADE_INTERNAL_ASSERT(image);
return Image2D{image->storage(), image->format(), image->type(), image->size(), image->release()};
}

}

struct ProcessingTest: TestSuite::Tester {
explicit ProcessingTest();

void process();
};

ProcessingTest::ProcessingTest() {
addTests({&ProcessingTest::process});
}

/** [0] */
void ProcessingTest::process() {
Image2D actual = doProcessing();
Image2D expected = loadExpectedImage();

CORRADE_COMPARE_WITH(actual, expected,
(DebugTools::CompareImage{170.0f, 96.0f}));
}
/** [0] */

CORRADE_TEST_MAIN(ProcessingTest)

Binary file added doc/snippets/debugtools-compareimage.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added doc/snippets/image1.tga
Binary file not shown.
Binary file added doc/snippets/image2.tga
Binary file not shown.
13 changes: 13 additions & 0 deletions src/Magnum/DebugTools/CMakeLists.txt
Expand Up @@ -43,6 +43,16 @@ if(MAGNUM_TARGET_GLES AND NOT MAGNUM_TARGET_GLES2)
list(APPEND MagnumDebugTools_SRCS ${MagnumDebugTools_RESOURCES})
endif()

# Build the TestSuite-related functionality only if it is present
find_package(Corrade COMPONENTS TestSuite)
if(Corrade_TestSuite_FOUND)
list(APPEND MagnumDebugTools_SRCS
CompareImage.cpp)

list(APPEND MagnumDebugTools_HEADERS
CompareImage.h)
endif()

if(NOT MAGNUM_TARGET_WEBGL)
list(APPEND MagnumDebugTools_SRCS
BufferData.cpp)
Expand Down Expand Up @@ -107,6 +117,9 @@ if(BUILD_STATIC_PIC)
endif()

target_link_libraries(MagnumDebugTools Magnum)
if(Corrade_TestSuite_FOUND)
target_link_libraries(MagnumDebugTools Corrade::TestSuite)
endif()
if(WITH_SCENEGRAPH)
target_link_libraries(MagnumDebugTools MagnumSceneGraph)
endif()
Expand Down

0 comments on commit c45472a

Please sign in to comment.