Skip to content

Commit 8cbfb5c

Browse files
committed
Core (Tests): Add test for LV::VideoBlit::blit_overlay_alphasrc().
1 parent 3a7d770 commit 8cbfb5c

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

libvisual/cmake/LVBuildTest.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ FUNCTION(LV_BUILD_TEST TEST_NAME)
3737

3838
TARGET_LINK_LIBRARIES(${TEST_NAME}
3939
PRIVATE
40+
test_common
4041
Libvisual::Libvisual
4142
Threads::Threads
4243
${PARSED_ARGS_LINK_LIBS}

libvisual/tests/CMakeLists.txt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
INCLUDE(LVBuildTest)
22

3-
INCLUDE_DIRECTORIES(
3+
ADD_LIBRARY(test_common STATIC
4+
random.cpp
5+
)
6+
7+
TARGET_INCLUDE_DIRECTORIES(test_common PUBLIC
48
${CMAKE_CURRENT_SOURCE_DIR}
59
)
610

11+
TARGET_LINK_LIBRARIES(test_common PUBLIC libvisual)
12+
713
ADD_SUBDIRECTORY(audio_test)
814
ADD_SUBDIRECTORY(mem_test)
915
ADD_SUBDIRECTORY(video_test)

libvisual/tests/random.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "random.hpp"
2+
#include <random>
3+
4+
namespace LV::Tests
5+
{
6+
LV::VideoPtr create_random_video (int width, int height, VisVideoDepth depth)
7+
{
8+
std::random_device device {};
9+
std::uniform_int_distribution<uint8_t> distrib {0, 255};
10+
11+
auto video {LV::Video::create (width, height, depth)};
12+
13+
auto bytes_per_pixel = video->get_bpp ();
14+
auto pitch = video->get_pitch ();
15+
16+
auto content_bytes_per_row = bytes_per_pixel * video->get_width ();
17+
18+
auto pixel_row_ptr = static_cast<uint8_t *>(video->get_pixels ());
19+
20+
for (int y = 0; y < video->get_height (); y++) {
21+
auto pixel = pixel_row_ptr;
22+
for (int c = 0; c < content_bytes_per_row; c++) {
23+
*pixel = distrib (device);
24+
pixel++;
25+
}
26+
27+
pixel_row_ptr += pitch;
28+
}
29+
30+
return video;
31+
}
32+
} // LV::Tests namespace

libvisual/tests/random.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#ifndef _LV_TESTS_VIDEO_RANDOM_HPP
2+
#define _LV_TESTS_VIDEO_RANDOM_HPP
3+
4+
#include <libvisual/libvisual.h>
5+
6+
namespace LV::Tests
7+
{
8+
LV::VideoPtr create_random_video (int width, int height, VisVideoDepth depth);
9+
10+
} // LV::Tests namespace
11+
12+
#endif // defined(_LV_TESTS_VIDEO_COMMON_HPP)

libvisual/tests/video_test/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ LV_BUILD_TEST(video_check_test
22
SOURCES video_check_test.cpp
33
)
44

5+
LV_BUILD_TEST(video_blit_test
6+
SOURCES video_blit_test.cpp
7+
)
8+
59
IF(HAVE_SDL)
610
LV_BUILD_TEST(video_scale_test
711
SOURCES video_scale_test.cpp
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
#include "test.h"
2+
#include "random.hpp"
3+
#include <libvisual/libvisual.h>
4+
#include <cassert>
5+
6+
namespace
7+
{
8+
LV::VideoPtr clone_video (LV::VideoPtr const& source)
9+
{
10+
auto clone {LV::Video::create (source->get_width (), source->get_height (), source->get_depth ())};
11+
12+
assert (clone->get_pitch () == source->get_pitch ());
13+
std::size_t buffer_size = static_cast<std::size_t> (clone->get_pitch () * clone->get_width ());
14+
15+
visual_mem_copy (clone->get_pixels (), source->get_pixels (), buffer_size);
16+
17+
return clone;
18+
}
19+
20+
void test_blit_overlay_alphasrc ()
21+
{
22+
// Check that blit_overlay_alphasrc results are within +/- 1 of exact computation for each colour channel. The
23+
// errors largely arise from the use of 256 instead of 255 as divisor for performance reasons.
24+
25+
int const test_width = 31;
26+
int const test_height = 31;
27+
28+
auto source = LV::Tests::create_random_video (test_width, test_height, VISUAL_VIDEO_DEPTH_32BIT);
29+
source->set_compose_type (VISUAL_VIDEO_COMPOSE_TYPE_SRC);
30+
31+
auto target = LV::Tests::create_random_video (test_width, test_height, VISUAL_VIDEO_DEPTH_32BIT);
32+
33+
auto actual {clone_video (target)};
34+
actual->blit (source, 0, 0, true);
35+
36+
for (int y = 0; y < test_height; y++) {
37+
auto source_pixel = static_cast<uint8_t const*> (source->get_pixel_ptr (0, y));
38+
auto target_pixel = static_cast<uint8_t const*> (target->get_pixel_ptr (0, y));
39+
auto actual_pixel = static_cast<uint8_t const*> (actual->get_pixel_ptr (0, y));
40+
41+
for (int x = 0; x < test_width; x++) {
42+
LV_TEST_ASSERT (actual_pixel[3] == target_pixel[3]);
43+
44+
float source_alpha = static_cast<float> (source_pixel[3]) / 255.0f;
45+
uint8_t b = source_alpha * source_pixel[0] + (1.0f - source_alpha) * target_pixel[0];
46+
uint8_t g = source_alpha * source_pixel[1] + (1.0f - source_alpha) * target_pixel[1];
47+
uint8_t r = source_alpha * source_pixel[2] + (1.0f - source_alpha) * target_pixel[2];
48+
49+
LV_TEST_ASSERT (std::abs (static_cast<int16_t> (actual_pixel[0]) - static_cast<int16_t> (b)) <= 1);
50+
LV_TEST_ASSERT (std::abs (static_cast<int16_t> (actual_pixel[1]) - static_cast<int16_t> (g)) <= 1);
51+
LV_TEST_ASSERT (std::abs (static_cast<int16_t> (actual_pixel[2]) - static_cast<int16_t> (r)) <= 1);
52+
53+
source_pixel += 4;
54+
target_pixel += 4;
55+
actual_pixel += 4;
56+
}
57+
}
58+
}
59+
} // anonymous namespace
60+
61+
int main(int argc, char *argv[])
62+
{
63+
LV::System::init (argc, argv);
64+
test_blit_overlay_alphasrc ();
65+
LV::System::destroy ();
66+
}

0 commit comments

Comments
 (0)