File: image_diff.cpp

package info (click to toggle)
sight 25.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 43,252 kB
  • sloc: cpp: 310,629; xml: 17,622; ansic: 9,960; python: 1,379; sh: 144; makefile: 33
file content (154 lines) | stat: -rw-r--r-- 4,870 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
/************************************************************************
 *
 * 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.hpp"

namespace sight::filter::image
{

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

image_diff::image_diff(const std::size_t _image_element_size, const std::size_t _reserved_elements) :
    m_img_elt_size(_image_element_size),
    m_elt_size(_image_element_size * 2 + sizeof(data::image::index_t))
{
    m_buffer.reserve(_reserved_elements);
}

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

void image_diff::add_diff(const image_diff& _diff)
{
    SIGHT_ASSERT("Diff elements must be the same size.", m_elt_size == _diff.m_elt_size);

    const std::size_t old_size = this->size();
    const std::size_t new_size = old_size + _diff.size();

    m_buffer.reserve(new_size);
    std::copy(_diff.m_buffer.begin(), _diff.m_buffer.end(), std::back_inserter(m_buffer));
}

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

void image_diff::add_diff(
    data::image::index_t _index,
    const data::image::buffer_t* _old_value,
    const data::image::buffer_t* _new_value
)
{
    const std::size_t old_size = m_buffer.size();

    // Double the size of the buffer to prevent systematic reallocation when building a diff
    if(old_size + m_elt_size > m_buffer.capacity())
    {
        const std::size_t new_size = std::max(m_elt_size, old_size) * 2;
        m_buffer.reserve(new_size);
    }

    std::copy_n(reinterpret_cast<std::uint8_t*>(&_index), sizeof(data::image::index_t), std::back_inserter(m_buffer));
    std::copy_n(_old_value, m_img_elt_size, std::back_inserter(m_buffer));
    std::copy_n(_new_value, m_img_elt_size, std::back_inserter(m_buffer));
}

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

void image_diff::apply_diff(const data::image::sptr& _img) const
{
    const auto dump_lock = _img->dump_lock();

    for(std::size_t i = 0 ; i < num_elements() ; ++i)
    {
        apply_diff_elt(_img, i);
    }
}

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

void image_diff::revert_diff(const data::image::sptr& _img) const
{
    const auto dump_lock = _img->dump_lock();

    for(std::size_t i = 0 ; i < num_elements() ; ++i)
    {
        revert_diff_elt(_img, i);
    }
}

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

std::size_t image_diff::size() const
{
    return m_buffer.size();
}

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

std::size_t image_diff::num_elements() const
{
    return size() / m_elt_size;
}

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

void image_diff::clear()
{
    m_buffer.clear();
}

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

void image_diff::shrink()
{
    m_buffer.shrink_to_fit();
}

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

image_diff::element_t image_diff::get_element(std::size_t _index) const
{
    element_t elt {};

    elt.m_index = *reinterpret_cast<const data::image::index_t*>(&m_buffer[_index * m_elt_size]);

    elt.m_old_value = &m_buffer[_index * m_elt_size + sizeof(data::image::index_t)];
    elt.m_new_value = &m_buffer[_index * m_elt_size + sizeof(data::image::index_t) + m_img_elt_size];

    return elt;
}

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

void image_diff::apply_diff_elt(const data::image::sptr& _img, std::size_t _elt_index) const
{
    element_t elt = get_element(_elt_index);
    _img->set_pixel(elt.m_index, elt.m_new_value);
}

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

void image_diff::revert_diff_elt(const data::image::sptr& _img, std::size_t _elt_index) const
{
    element_t elt = get_element(_elt_index);
    _img->set_pixel(elt.m_index, elt.m_old_value);
}

} // namespace sight::filter::image