File: image.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 (176 lines) | stat: -rw-r--r-- 5,676 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
/************************************************************************
 *
 * 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/>.
 *
 ***********************************************************************/

// cspell:ignore NOLINTNEXTLINE

#include "image.hpp"

#include "io/opencv/type.hpp"

#include <data/array.hpp>

namespace sight::io::opencv
{

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

static cv::Mat to_cv(const data::image::csptr& _image, bool _copy)
{
    const auto image_type = _image->type();
    const auto image_comp = _image->num_components();

    const auto cv_type = io::opencv::type::to_cv(image_type, image_comp);

    const auto dump_lock = _image->dump_lock();

    cv::Mat cv_image;
    if(_image->buffer() != nullptr)
    {
        const auto image_size = _image->size();
        std::vector<int> cv_size;
        for(std::size_t i = 0 ; i < _image->num_dimensions() ; ++i)
        {
            cv_size.push_back(static_cast<int>(image_size[i]));
        }

        if(cv_size.size() == 1)
        {
            // If we have a single row, we want to initialize the cv::Math with (1, N) since it takes (rows,cols)
            cv_size.push_back(1);
        }

        // Reverse from (w,h,d) to (d,h,w) because OpenCV uses a row major format
        std::reverse(cv_size.begin(), cv_size.end());

        if(_copy)
        {
            // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
            cv::Mat mat = cv::Mat(cv_size, cv_type, const_cast<void*>(_image->buffer()));
            cv_image = mat.clone();
        }
        else
        {
            // NOLINTNEXTLINE(cppcoreguidelines-pro-type-const-cast)
            cv_image = cv::Mat(cv_size, cv_type, const_cast<void*>(_image->buffer()));
        }
    }

    return cv_image;
}

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

cv::Mat image::move_to_cv(data::image::sptr& _image)
{
    return to_cv(_image, false);
}

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

cv::Mat image::move_to_cv(const data::image::csptr& _image)
{
    return to_cv(_image, false);
}

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

void image::copy_from_cv(data::image& _image, const cv::Mat& _cv_image)
{
    const auto prev_image_type = _image.type();
    const auto prev_image_comp = _image.num_components();

    const auto image_format = io::opencv::type::from_cv(_cv_image.type());
    const auto image_type   = image_format.first;
    const auto image_comp   = image_format.second;
    SIGHT_ASSERT("Number of components should be between 1 and 4", image_comp >= 1 && image_comp <= 4);
    SIGHT_ASSERT("Number of dimension should be between 1 and 3", _cv_image.dims >= 1 && _cv_image.dims <= 3);

    data::image::size_t image_size = {0, 0, 0};

    if(_cv_image.dims == 1)
    {
        image_size[0] = std::size_t(_cv_image.size[0]);
    }
    else if(_cv_image.dims == 2 && _cv_image.rows == 1)
    {
        // This means this is actually a 1D image so remove the first dimension (==1)
        image_size[0] = std::size_t(_cv_image.size[1]);
        image_size[1] = 0;
    }
    else if(_cv_image.dims == 2)
    {
        image_size[0] = std::size_t(_cv_image.size[1]);
        image_size[1] = std::size_t(_cv_image.size[0]);
    }
    else // 3D
    {
        image_size[0] = std::size_t(_cv_image.size[2]);
        image_size[1] = std::size_t(_cv_image.size[1]);
        image_size[2] = std::size_t(_cv_image.size[0]);
    }

    const auto prev_image_size = _image.size();
    if(prev_image_comp != image_comp || prev_image_type != image_type || image_size != prev_image_size)
    {
        enum data::image::pixel_format_t format = data::image::pixel_format_t::gray_scale;
        switch(image_comp)
        {
            case 1:
                format = data::image::pixel_format_t::gray_scale;
                break;

            case 2:
                format = data::image::pixel_format_t::rg;
                break;

            case 3:
                format = data::image::pixel_format_t::rgb;
                break;

            case 4:
                format = data::image::pixel_format_t::rgba;
                break;

            default:
                SIGHT_FATAL("Unhandled OpenCV format");
        }

        _image.resize(image_size, image_type, format);
    }

    const auto dump_lock = _image.dump_lock();
    SIGHT_ASSERT("Empty image buffer", _image.allocated_size_in_bytes() > 0);

    auto buffer = _image.begin<std::uint8_t>();
    std::copy(_cv_image.data, _cv_image.data + _image.size_in_bytes(), buffer);
}

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

cv::Mat image::copy_to_cv(const data::image::csptr& _image)
{
    return to_cv(_image, true);
}

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

} //namespace sight::io::opencv