Added unit tests for blending images.

This test is added to test modifications of the algorithm. It also adds a
programme to create the test images. And the test images to test whether
the output of the algorithm remains the same after changing the algorithm.

loonycyborg can you add this new programme to scons.
This commit is contained in:
Mark de Wever 2012-03-25 10:06:24 +00:00
parent 36a21319c1
commit aa29be755d
55 changed files with 336 additions and 0 deletions

View file

@ -174,6 +174,8 @@ Version 1.11.0-svn:
* Make Wolf Rider and Wolf raise their heads if and only if unit is * Make Wolf Rider and Wolf raise their heads if and only if unit is
submerged: forums.wesnoth.org/viewtopic.php?f=4&t=36212 submerged: forums.wesnoth.org/viewtopic.php?f=4&t=36212
* Changed: Add a small cache for reading files, giving a minor improvement. * Changed: Add a small cache for reading files, giving a minor improvement.
* Added: Helper tool to create images for unit tests.
* Added: Image manipulation unit tests.
Version 1.10.0: Version 1.10.0:
* Campaigns: * Campaigns:

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 15 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 13 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 12 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 10 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 7.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.6 KiB

View file

@ -918,6 +918,7 @@ if(ENABLE_TESTS)
tests/test_team.cpp tests/test_team.cpp
tests/test_unit_map.cpp tests/test_unit_map.cpp
tests/test_util.cpp tests/test_util.cpp
tests/test_sdl_utils.cpp
tests/test_serialization.cpp tests/test_serialization.cpp
tests/test_version.cpp tests/test_version.cpp
tests/gui/iterator.cpp tests/gui/iterator.cpp
@ -947,5 +948,23 @@ if(ENABLE_TESTS)
set_target_properties(test set_target_properties(test
PROPERTIES OUTPUT_NAME ${BINARY_PREFIX}test${BINARY_SUFFIX} PROPERTIES OUTPUT_NAME ${BINARY_PREFIX}test${BINARY_SUFFIX}
) )
if(ENABLE_TOOLS)
# This tool is used to create the images for the sdl_utils unit test.
# Due to its unique nature the programme is never installed.
set(create_images_SRC
tests/create_images.cpp
tools/dummy_video.cpp
tools/exploder_utils.cpp
sdl_utils.cpp
tracer.cpp
loadscreen_empty.cpp
)
add_executable(create_images ${create_images_SRC})
target_link_libraries(create_images wesnoth-core png ${tools-external-libs})
set_target_properties(create_images PROPERTIES OUTPUT_NAME ${BINARY_PREFIX}create_images${BINARY_SUFFIX})
endif(ENABLE_TOOLS)
endif(ENABLE_TESTS) endif(ENABLE_TESTS)

View file

@ -586,6 +586,7 @@ test_sources = Split("""
tests/test_team.cpp tests/test_team.cpp
tests/test_unit_map.cpp tests/test_unit_map.cpp
tests/test_util.cpp tests/test_util.cpp
tests/test_sdl_utils.cpp
tests/test_serialization.cpp tests/test_serialization.cpp
tests/test_version.cpp tests/test_version.cpp
tests/gui/fire_event.cpp tests/gui/fire_event.cpp

154
src/tests/create_images.cpp Normal file
View file

@ -0,0 +1,154 @@
/* $Id$ */
/*
Copyright (C) 2012 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
/**
* @file
* Tool to create the test images for the unit tests.
*/
#include "filesystem.hpp"
#include "foreach.hpp"
#include "tools/exploder_utils.hpp"
#include "tests/test_sdl_utils.hpp"
#include <SDL_image.h>
#include <boost/bind.hpp>
#include <iostream>
static void
show_usage()
{
std::cerr <<
"Usage:\n"
"test_images -h\n"
"test_images DIR\n"
"Helper programme to create images for the unit tests.\n"
"\n"
"The DIR is the output directory, will be created if doesn't exist.\n"
"OPTIONS:\n"
"-h, --help Show this help and terminate the programme.\n"
;
}
static surface
create_image_base(const std::string& filename)
{
surface result = SDL_CreateRGBSurface(
SDL_SWSURFACE
, 1024
, 1024
, 32
, 0xFF0000
, 0xFF00
, 0xFF
, 0);
surface_lock lock(result);
Uint32 *pixels = lock.pixels();
for(Uint32 r = 0; r < 256; r += 4) {
for(Uint32 g = 0; g < 256; g += 2) {
for(Uint32 b = 0; b < 256; b += 2, ++pixels) {
*pixels = (0xFF << 24) | (r << 16) | (g << 8) | b;
}
}
}
save_image(result, filename);
return result;
}
static void
create_image_blend_functor(
const surface& dst
, const std::string root
, const Uint8 amount
, const Uint32 colour)
{
const std::string filename = blend_get_filename(root, amount, colour);
save_image(dst, filename);
}
static void
create_image_blend(const surface& src, const std::string& root)
{
blend_image(
src
, boost::bind(&create_image_blend_functor, _1, root, _2, _3));
}
typedef void (*tfunctor) (const surface&, const std::string&);
typedef std::pair<std::string, tfunctor> tcreator;
static const tcreator creators[] =
{
std::make_pair("/blend/", &create_image_blend)
};
int
main(int argc, char* argv[])
{
if(argc != 2) {
show_usage();
return EXIT_FAILURE;
}
const std::string root = argv[1];
if(root == "-h" || root == "--help") {
show_usage();
return EXIT_SUCCESS;
}
if(!is_directory(root)) {
if(file_exists(root)) {
std::cerr << "";
return EXIT_FAILURE;
}
if(!make_directory(root)) {
std::cerr << "";
return EXIT_FAILURE;
}
}
BOOST_FOREACH(const tcreator& creator, creators) {
if(!make_directory(root + creator.first)) {
std::cerr << "";
return EXIT_FAILURE;
}
}
try {
const surface base_image = create_image_base(root + "/base.png");
BOOST_FOREACH(const tcreator& creator, creators) {
creator.second(base_image, root + creator.first);
}
} catch(exploder_failure& err) {
std::cerr << "Error: Failed with error »" << err.message << "«.\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}

View file

@ -0,0 +1,107 @@
/* $Id$ */
/*
Copyright (C) 2012 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#define GETTEXT_DOMAIN "wesnoth-test"
#include "tests/test_sdl_utils.hpp"
#include "image.hpp"
#include <boost/bind.hpp>
#include <boost/test/auto_unit_test.hpp>
#include <iomanip>
static const std::string root = "data/test/test/image";
static void
compare_image(
const surface& src
, const surface& dst
, const std::string message)
{
BOOST_REQUIRE_MESSAGE(
src->w == dst->w
, message
<< "source width »"
<< src->w
<< "« destination width »"
<< dst->w
<< "«.\n"
);
BOOST_REQUIRE_MESSAGE(
src->h == dst->h
, message
<< "source height »"
<< src->w
<< "« destination heigth »"
<< dst->w
<< "«.\n"
);
const_surface_lock src_lock(src);
const_surface_lock dst_lock(dst);
const Uint32* src_pixels = src_lock.pixels();
const Uint32* dst_pixels = dst_lock.pixels();
const unsigned pixels = src->w * src->h;
unsigned matches = 0;
for(unsigned i = 0; i < pixels; ++i, ++src_pixels, ++dst_pixels) {
matches += (*src_pixels == *dst_pixels);
}
BOOST_CHECK_MESSAGE(
pixels == matches
, message
<< "of the " << pixels
<< " pixels in the image " << matches
<< " match.\n"
);
}
static void
test_blend(const surface& dst, const Uint8 amount, const Uint32 colour)
{
std::stringstream sstr;
sstr << std::hex << std::setfill('0')
<< "Blend image amount »"
<< std::setw(2) << static_cast<Uint32>(amount)
<< "« colour »"
<< std::setw(8) << colour
<< "« : ";
const std::string filename =
blend_get_filename("data/test/test/image/blend/", amount, colour);
BOOST_REQUIRE_EQUAL(image::exists(filename), true);
const surface& src = image::get_image(filename);
compare_image(src, dst, sstr.str());
}
BOOST_AUTO_TEST_CASE(test_blend_surface)
{
BOOST_REQUIRE_EQUAL(image::exists(root + "/base.png"), true);
const surface base = image::get_image(root + "/base.png");
blend_image(base, boost::bind(&test_blend, _1, _2, _3));
}

View file

@ -0,0 +1,53 @@
/* $Id$ */
/*
Copyright (C) 2012 by Mark de Wever <koraq@xs4all.nl>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef TESTS_TEST_SDL_UTILS_HPP_INCLUDED
#define TESTS_TEST_SDL_UTILS_HPP_INCLUDED
#include "sdl_utils.hpp"
#include <boost/function.hpp>
typedef boost::function<
void(const surface&, const double, const Uint32)>
tblend_functor;
inline void
blend_image(const surface& src, tblend_functor functor)
{
for(Uint32 colour = 0x00FF0000; colour != 0x00000000; colour >>= 8) {
for(int i = 0xf; i < 0x100; i += 0x10) {
const surface dst = blend_surface(src, i / 255., colour);
if(functor) {
functor(dst, i, colour);
}
}
}
}
inline std::string
blend_get_filename(std::string root, const Uint8 amount, const Uint32 colour)
{
// The name of the file is
// A the amount of blended [0..256) as hex.
// C the colour to blend with as hex.
char filename[] = "AA_CCCCCCCC.png";
snprintf(filename, sizeof(filename), "%02X_%08X.png", amount, colour);
return root + filename;
}
#endif