summaryrefslogtreecommitdiffhomepage
path: root/test/bitmask.cpp
blob: 1b3deea993ac6c69e0733c42b78ce3f9a05ccc10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include "app.hpp"
#include "compat/assert.hpp"
#include "src/anim-atlas.hpp"
#include "loader/loader.hpp"
#include <iterator>
#include <Corrade/Containers/ArrayView.h>
#include <Corrade/Containers/StridedArrayView.h>
#include <Magnum/Trade/ImageData.h>
#include <Magnum/ImageView.h>
#include <Magnum/PixelFormat.h>
#include <chrono>

//#define DO_GENERATE

namespace floormat {

namespace {

constexpr size_t data_nbytes = 128;
const unsigned char data_door_close[] = {
#include "bitmask.embed.inc"
};

[[maybe_unused]] void bitmask_benchmark()
{
    std::chrono::high_resolution_clock clock;

    auto img = loader.texture(loader.SCENERY_PATH, "door-close"_s);
    auto bitmask = anim_atlas::make_bitmask(img);
    constexpr int runs = 10, warmup = 500, cycles = 1000;
    for (int i = 0; i < warmup; i++)
        anim_atlas::make_bitmask_(img, bitmask);
    for (int i = 0; i < runs; i++)
    {
        auto time0 = clock.now();
        for (int i = 0; i < cycles; i++)
            (void)anim_atlas::make_bitmask_(img, bitmask);
        std::chrono::duration<double, std::milli> time = clock.now() - time0;

        fm_log("[BENCH] bitmask %d/%d took %.1f ms", i, runs, time.count());
    }
}

void bitmask_test()
{
    auto img = loader.texture(loader.SCENERY_PATH, "door-close"_s);
    auto bitmask = anim_atlas::make_bitmask(img);
    fm_assert(img.pixelSize() == 4 && (size_t)img.size().product() >= data_nbytes);
#ifdef DO_GENERATE
    for (auto i = 0_uz; i < data_nbytes; i++)
    {
        if (i && i % 16 == 0)
            printf("\n");
        printf("0x%02hhx,", bitmask.data()[i]);
        if ((i+1) % 16 != 0)
            printf(" ");
    }
    printf("\n");
    fflush(stdout);
#endif
    const auto len = std::min(data_nbytes, (size_t)bitmask.size()+7 >> 3);
    for (auto i = 0_uz; i < len; i++)
        if ((unsigned char)bitmask.data()[i] != data_door_close[i])
            fm_abort("wrong value at bit %zu, should be' 0x%02hhx'", i, data_door_close[i]);
}

} // namespace

void test_app::test_bitmask()
{
    bitmask_test();
    //bitmask_benchmark();
}

} // namespace floormat