File: reader_impl.hxx

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 (245 lines) | stat: -rw-r--r-- 6,721 bytes parent folder | download
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
/************************************************************************
 *
 * Copyright (C) 2023-2025 IRCAD France
 *
 * 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/>.
 *
 ***********************************************************************/

#pragma once

#include "common.hxx"
#include "io/bitmap/reader.hpp"

#include <data/helper/medical_image.hpp>

#ifdef SIGHT_ENABLE_NVJPEG
#include "nvjpeg_reader.hxx"
#endif

#ifdef SIGHT_ENABLE_NVJPEG2K
#include "nvjpeg2k_reader.hxx"
#endif

#include "libjpeg_reader.hxx"
#include "libtiff_reader.hxx"
#include "libpng_reader.hxx"
#include "openjpeg_reader.hxx"

// cspell:ignore nvjpeg LIBJPEG LIBTIFF LIBPNG

namespace sight::io::bitmap::detail
{

class reader_impl final
{
public:

    /// Delete default constructors and assignment operators
    reader_impl()                              = delete;
    reader_impl(const reader_impl&)            = delete;
    reader_impl(reader_impl&&)                 = delete;
    reader_impl& operator=(const reader_impl&) = delete;
    reader_impl& operator=(reader_impl&&)      = delete;

    /// Constructor
    explicit reader_impl(reader* const _reader) :
        m_reader(_reader)
    {
    }

    /// Default destructor
    ~reader_impl() noexcept = default;

    /// Main read function
    void read(std::istream& _istream, backend _backend)
    {
        // Get the image pointer
        auto image = m_reader->get_concrete_object();

        if(image == nullptr)
        {
            image = std::make_shared<data::image>();
            m_reader->set_object(image);
        }

        // Protect the image from dump
        const auto dump_lock = image->dump_lock();

        const auto& backend_impl = get_reader_backend(_backend);
        SIGHT_THROW_IF("No suitable backend found.", backend_impl == nullptr);

        backend_impl->read(
            *image,
            _istream,
            _backend == backend::nvjpeg2k_j2k
            ? flag::j2k_stream
            : _backend == backend::openjpeg_j2k
            ? flag::j2k_stream
            : flag::none
        );

        sight::data::helper::medical_image::check_image_slice_index(*image);
    }

    /// Main read function
    void read(
        const std::uint8_t* const _input,
        std::size_t _input_size,
        std::uint8_t* const _output,
        backend _backend
)
    {
        const auto& backend_impl = get_reader_backend(_backend);

        if(_output == nullptr)
        {
            // Get or create the image shared pointer
            auto image = m_reader->get_concrete_object();

            if(image == nullptr)
            {
                image = std::make_shared<data::image>();
                m_reader->set_object(image);
            }

            // Dump lock in case the image has been created here
            const auto dump_lock = image->dump_lock();

            // Read the image
            backend_impl->read(
                *image,
                _input,
                _input_size,
                nullptr,
                _backend == backend::nvjpeg2k_j2k
                ? flag::j2k_stream
                : _backend == backend::openjpeg_j2k
                ? flag::j2k_stream
                : flag::none
            );

            sight::data::helper::medical_image::check_image_slice_index(*image);
        }
        else
        {
            backend_impl->read(
                std::nullopt,
                _input,
                _input_size,
                _output,
                _backend == backend::nvjpeg2k_j2k
                ? flag::j2k_stream
                : _backend == backend::openjpeg_j2k
                ? flag::j2k_stream
                : flag::none
            );
        }
    }

private:

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

    const std::unique_ptr<reader_backend>& get_reader_backend(backend _backend)
    {
        switch(_backend)
        {
#ifdef SIGHT_ENABLE_NVJPEG
            case backend::nvjpeg:
                if(!nvjpeg())
                {
                    break;
                }

                if(!m_nvjpeg)
                {
                    m_nvjpeg = std::make_unique<nvjpeg_reader>();
                }
                return m_nvjpeg;
#endif
#ifdef SIGHT_ENABLE_NVJPEG2K
            case backend::nvjpeg2k:
            case backend::nvjpeg2k_j2k:
                if(!nvjpeg2k())
                {
                    break;
                }

                if(!m_nvjpeg2k)
                {
                    m_nvjpeg2k = std::make_unique<nvjpeg2k_reader>();
                }
                return m_nvjpeg2k;
#endif
            case backend::libjpeg:
                if(!m_libjpeg)
                {
                    m_libjpeg = std::make_unique<libjpeg_reader>();
                }

                return m_libjpeg;

            case backend::libtiff:
                if(!m_libtiff)
                {
                    m_libtiff = std::make_unique<libtiff_reader>();
                }

                return m_libtiff;

            case backend::libpng:
                if(!m_libpng)
                {
                    m_libpng = std::make_unique<libpng_reader>();
                }

                return m_libpng;

            case backend::openjpeg:
            case backend::openjpeg_j2k:
                if(!m_openjpeg)
                {
                    m_openjpeg = std::make_unique<openjpeg_reader>();
                }

                return m_openjpeg;

            default:
                break;
        }

        SIGHT_THROW("No suitable backend found.");
    }

    /// Pointer to the public interface
    reader* const m_reader;

#ifdef SIGHT_ENABLE_NVJPEG
    std::unique_ptr<reader_backend> m_nvjpeg;
#endif

#ifdef SIGHT_ENABLE_NVJPEG2K
    std::unique_ptr<reader_backend> m_nvjpeg2k;
#endif

    std::unique_ptr<reader_backend> m_libjpeg;
    std::unique_ptr<reader_backend> m_libtiff;
    std::unique_ptr<reader_backend> m_libpng;
    std::unique_ptr<reader_backend> m_openjpeg;
};

} // namespace sight::io::bitmap::detail