File: dicom_series.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 (189 lines) | stat: -rw-r--r-- 6,449 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/************************************************************************
 *
 * Copyright (C) 2009-2024 IRCAD France
 * Copyright (C) 2012-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 "data/dicom_series.hpp"

#include <core/memory/stream/in/raw.hpp>

#include <data/exception.hpp>
#include <data/registry/macros.hpp>

#include <filesystem>

SIGHT_REGISTER_DATA(sight::data::dicom_series)

namespace sight::data
{

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

void dicom_series::shallow_copy(const object::csptr& _source)
{
    const auto& other = std::dynamic_pointer_cast<const dicom_series>(_source);

    SIGHT_THROW_EXCEPTION_IF(
        exception(
            "Unable to copy " + (_source ? _source->get_classname() : std::string("<NULL>"))
            + " to " + get_classname()
        ),
        !bool(other)
    );

    m_number_of_instances   = other->m_number_of_instances;
    m_dicom_container       = other->m_dicom_container;
    m_sop_class_ui_ds       = other->m_sop_class_ui_ds;
    m_computed_tag_values   = other->m_computed_tag_values;
    m_first_instance_number = other->m_first_instance_number;

    base_class_t::shallow_copy(other);
}

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

void dicom_series::deep_copy(const object::csptr& _source, const std::unique_ptr<deep_copy_cache_t>& _cache)
{
    const auto& other = std::dynamic_pointer_cast<const dicom_series>(_source);

    SIGHT_THROW_EXCEPTION_IF(
        exception(
            "Unable to copy " + (_source ? _source->get_classname() : std::string("<NULL>"))
            + " to " + get_classname()
        ),
        !bool(other)
    );

    m_number_of_instances   = other->m_number_of_instances;
    m_sop_class_ui_ds       = other->m_sop_class_ui_ds;
    m_computed_tag_values   = other->m_computed_tag_values;
    m_first_instance_number = other->m_first_instance_number;

    m_dicom_container.clear();
    for(const auto& elt : other->m_dicom_container)
    {
        const core::memory::buffer_object::sptr& buffer_src = elt.second;
        core::memory::buffer_object::lock_t locker_source(buffer_src);

        if(!buffer_src->is_empty())
        {
            auto buffer_dest = std::make_shared<core::memory::buffer_object>(true);
            core::memory::buffer_object::lock_t locker_dest(buffer_dest);

            buffer_dest->allocate(buffer_src->size());

            char* buff_dest = static_cast<char*>(locker_dest.buffer());
            char* buff_src  = static_cast<char*>(locker_source.buffer());
            std::copy(buff_src, buff_src + buffer_src->size(), buff_dest);

            m_dicom_container[elt.first] = buffer_dest;
        }
    }

    base_class_t::deep_copy(other, _cache);
}

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

void dicom_series::add_dicom_path(std::size_t _instance_index, const std::filesystem::path& _path)
{
    SIGHT_THROW_EXCEPTION_IF(
        std::filesystem::filesystem_error(
            std::string("Dicom path can not be found: "),
            _path,
            std::make_error_code(
                std::errc::no_such_file_or_directory
            )
        ),
        !std::filesystem::exists(_path)
    );

    auto buffer          = std::make_shared<core::memory::buffer_object>(true);
    const auto buff_size = std::filesystem::file_size(_path);
    buffer->set_istream_factory(
        std::make_shared<core::memory::stream::in::raw>(_path),
        static_cast<core::memory::buffer_object::size_t>(buff_size),
        _path,
        core::memory::raw
    );
    m_dicom_container[_instance_index] = buffer;
}

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

void dicom_series::add_binary(std::size_t _instance_index, const core::memory::buffer_object::sptr& _buffer)
{
    m_dicom_container[_instance_index] = _buffer;
}

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

bool dicom_series::is_instance_available(std::size_t _instance_index) const
{
    const auto& dicom_container_iter = m_dicom_container.find(_instance_index);
    return dicom_container_iter != m_dicom_container.end();
}

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

void dicom_series::addsop_class_uid(const std::string& _sop_class_uid)
{
    m_sop_class_ui_ds.insert(_sop_class_uid);
}

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

void dicom_series::add_computed_tag_value(const std::string& _tag_name, const std::string& _value)
{
    m_computed_tag_values[_tag_name] = _value;
}

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

bool dicom_series::has_computed_values(const std::string& _tag_name) const
{
    return m_computed_tag_values.find(_tag_name) != m_computed_tag_values.end();
}

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

bool dicom_series::operator==(const dicom_series& _other) const noexcept
{
    if(m_number_of_instances != _other.m_number_of_instances
       || m_sop_class_ui_ds != _other.m_sop_class_ui_ds
       || m_computed_tag_values != _other.m_computed_tag_values
       || m_first_instance_number != _other.m_first_instance_number
       || !core::is_equal(m_dicom_container, _other.m_dicom_container))
    {
        return false;
    }

    // Super class last
    return base_class_t::operator==(_other);
}

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

bool dicom_series::operator!=(const dicom_series& _other) const noexcept
{
    return !(*this == _other);
}

} // namespace sight::data