|
| 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