File: image_diff_command_test.cpp

package info (click to toggle)
sight 25.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 42,180 kB
  • sloc: cpp: 289,476; xml: 17,257; ansic: 9,878; python: 1,379; sh: 144; makefile: 33
file content (169 lines) | stat: -rw-r--r-- 6,282 bytes parent folder | download | duplicates (2)
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/************************************************************************
 *
 * Copyright (C) 2017-2024 IRCAD France
 * Copyright (C) 2017-2020 IHU Strasbourg
 *
 * This file is part of Sight.
 *
 * Sight is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Sight is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with Sight. If not, see <https://www.gnu.org/licenses/>.
 *
 ***********************************************************************/

#include "image_diff_command_test.hpp"

#include <ui/history/image_diff_command.hpp>

#include <utest_data//generator/image.hpp>

// Registers the fixture into the 'registry'
CPPUNIT_TEST_SUITE_REGISTRATION(sight::ui::history::ut::image_diff_command_test);

namespace sight::ui::history::ut
{

//------------------------------------------------------------------------------

void image_diff_command_test::setUp()
{
}

//------------------------------------------------------------------------------

void image_diff_command_test::tearDown()
{
}

//------------------------------------------------------------------------------

void image_diff_command_test::undoredo_test()
{
    const data::image::size_t size                = {32, 32, 32};
    const data::image::spacing_t spacing          = {1., 1., 1.};
    const data::image::origin_t origin            = {0., 0., 0.};
    const data::image::orientation_t orientation  = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};
    const core::type type                         = core::type::UINT8;
    const enum data::image::pixel_format_t format = data::image::gray_scale;

    data::image::sptr image = std::make_shared<data::image>();

    utest_data::generator::image::generate_image(image, size, spacing, origin, orientation, type, format);

    const auto dump_lock = image->dump_lock();

    filter::image::image_diff diff(image->type().size());

    std::uint8_t newvalue = 1;

    auto* new_buffer_value = reinterpret_cast<data::image::buffer_t*>(&newvalue);

    const std::vector<data::image::index_t> indices = {{51, 10, 8, 123, 1098, 23456, 6, 9999}};

    // Add 8 elements to the diff. Write new values to the image.
    for(std::size_t i = 0 ; i < 8 ; ++i)
    {
        const data::image::index_t index = indices[i];

        const data::image::buffer_t* pix_buf =
            reinterpret_cast<data::image::buffer_t*>(image->get_pixel(index));

        diff.add_diff(index, pix_buf, new_buffer_value);
        image->set_pixel(index, new_buffer_value);

        CPPUNIT_ASSERT_EQUAL(i + 1, diff.num_elements());
        CPPUNIT_ASSERT_EQUAL(index, diff.get_element_diff_index(i));
    }

    // Create an imageDiffCommand to test
    ui::history::image_diff_command image_diff_command(image, diff);

    // Revert diff. Ensure that the image is the same as before (all values equal to zero).
    CPPUNIT_ASSERT(image_diff_command.undo());

    for(std::size_t it = 0 ; it < image->size_in_bytes() ; ++it)
    {
        CPPUNIT_ASSERT_EQUAL(std::uint8_t(0), *reinterpret_cast<std::uint8_t*>(image->get_pixel(it)));
    }

    // Apply diff. Ensure all values are zero except the ones at the selected indices.
    CPPUNIT_ASSERT(image_diff_command.redo());

    for(std::size_t i = 0 ; i < image->size_in_bytes() ; ++i)
    {
        // Check if 'i' is an index
        auto index_it = std::find(indices.begin(), indices.end(), i);

        if(index_it != indices.end())
        {
            CPPUNIT_ASSERT_EQUAL(newvalue, *reinterpret_cast<std::uint8_t*>(image->get_pixel(i)));
        }
        else
        {
            CPPUNIT_ASSERT_EQUAL(std::uint8_t(0), *reinterpret_cast<std::uint8_t*>(image->get_pixel(i)));
        }
    }
}

//------------------------------------------------------------------------------

void image_diff_command_test::get_size_test()
{
    const data::image::size_t size                = {32, 32, 32};
    const data::image::spacing_t spacing          = {1., 1., 1.};
    const data::image::origin_t origin            = {0., 0., 0.};
    const data::image::orientation_t orientation  = {0.36, 0.48, -0.8, -0.8, 0.6, 0.0, 0.48, 0.64, 0.6};
    const core::type type                         = core::type::UINT8;
    const enum data::image::pixel_format_t format = data::image::gray_scale;

    data::image::sptr image = std::make_shared<data::image>();

    utest_data::generator::image::generate_image(image, size, spacing, origin, orientation, type, format);

    const auto dump_lock = image->dump_lock();

    filter::image::image_diff diff(image->type().size() * 64);

    std::uint8_t newvalue = 1;

    auto* new_buffer_value = reinterpret_cast<data::image::buffer_t*>(&newvalue);

    const std::vector<data::image::index_t> indices = {{51, 10, 8, 123, 1098, 23456, 6, 9999}};

    // Add 8 elements to the diff. Write new values to the image.
    for(std::size_t i = 0 ; i < 8 ; ++i)
    {
        const data::image::index_t index = indices[i];

        const data::image::buffer_t* pix_buf =
            reinterpret_cast<data::image::buffer_t*>(image->get_pixel(index));

        diff.add_diff(index, pix_buf, new_buffer_value);
        image->set_pixel(index, new_buffer_value);

        CPPUNIT_ASSERT_EQUAL(std::size_t(i + 1), diff.num_elements());
        CPPUNIT_ASSERT_EQUAL(index, diff.get_element_diff_index(i));
    }

    // Create an imageDiffCommand to test
    ui::history::image_diff_command image_diff_command(image, diff);

    // Ensure that the real size is at least bigger than the naive sizeof
    CPPUNIT_ASSERT(image_diff_command.size() > sizeof(image_diff_command));

    // Ensure that the real size is at least bigger than the size of the diff
    CPPUNIT_ASSERT(image_diff_command.size() > diff.size());
}

//------------------------------------------------------------------------------

} // namespace sight::ui::history::ut