File: type.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 (101 lines) | stat: -rw-r--r-- 3,974 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
/************************************************************************
 *
 * Copyright (C) 2017-2023 IRCAD France
 * Copyright (C) 2017 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 "type.hpp"

#include <core/base.hpp>

#include <opencv2/core.hpp>

namespace sight::io::opencv
{

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

std::int32_t type::to_cv(const core::type _type, const std::size_t _num_components)
{
    SIGHT_ASSERT("Number of dimensions should be between 1 and 4", _num_components > 0 && _num_components <= 4);

    static const std::map<core::type, std::array<std::int32_t, 4> > s_IMAGE_FORMAT_TO_CV =
    {{
        {core::type::UINT8, {{CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4}}},
        {core::type::INT8, {{CV_8SC1, CV_8SC2, CV_8SC3, CV_8SC4}}},
        {core::type::UINT16, {{CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4}}},
        {core::type::INT16, {{CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4}}},
        {core::type::INT32, {{CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4}}},
        {core::type::FLOAT, {{CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4}}},
        {core::type::DOUBLE, {{CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4}}},
    }
    };

    const auto it = s_IMAGE_FORMAT_TO_CV.find(_type);
    SIGHT_ASSERT("Format not handled by OpenCV: " + _type.name(), it != s_IMAGE_FORMAT_TO_CV.end());

    return (it->second)[_num_components - 1];
}

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

std::pair<core::type, uint8_t> type::from_cv(int32_t _cv_type)
{
    static const std::map<std::int32_t, std::pair<core::type, std::size_t> > s_IMAGE_FORMAT_FROM_CV =
    {{
        {CV_8UC1, {core::type::UINT8, 1}},
        {CV_8UC2, {core::type::UINT8, 2}},
        {CV_8UC3, {core::type::UINT8, 3}},
        {CV_8UC4, {core::type::UINT8, 4}},
        {CV_8SC1, {core::type::INT8, 1}},
        {CV_8SC2, {core::type::INT8, 2}},
        {CV_8SC3, {core::type::INT8, 3}},
        {CV_8SC4, {core::type::INT8, 4}},
        {CV_16UC1, {core::type::UINT16, 1}},
        {CV_16UC2, {core::type::UINT16, 2}},
        {CV_16UC3, {core::type::UINT16, 3}},
        {CV_16UC4, {core::type::UINT16, 4}},
        {CV_16SC1, {core::type::INT16, 1}},
        {CV_16SC2, {core::type::INT16, 2}},
        {CV_16SC3, {core::type::INT16, 3}},
        {CV_16SC4, {core::type::INT16, 4}},
        {CV_32SC1, {core::type::INT32, 1}},
        {CV_32SC2, {core::type::INT32, 2}},
        {CV_32SC3, {core::type::INT32, 3}},
        {CV_32SC4, {core::type::INT32, 4}},
        {CV_32FC1, {core::type::FLOAT, 1}},
        {CV_32FC2, {core::type::FLOAT, 2}},
        {CV_32FC3, {core::type::FLOAT, 3}},
        {CV_32FC4, {core::type::FLOAT, 4}},
        {CV_64FC1, {core::type::DOUBLE, 1}},
        {CV_64FC2, {core::type::DOUBLE, 2}},
        {CV_64FC3, {core::type::DOUBLE, 3}},
        {CV_64FC4, {core::type::DOUBLE, 4}}
    }
    };

    const auto it = s_IMAGE_FORMAT_FROM_CV.find(_cv_type);
    SIGHT_ASSERT("Format not handled by OpenCV: " + std::to_string(_cv_type), it != s_IMAGE_FORMAT_FROM_CV.end());

    return {it->second.first, uint8_t(it->second.second)};
}

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

} // namespace sight::io::opencv