File: DicomDataTools.cpp

package info (click to toggle)
sight 21.1.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 36,592 kB
  • sloc: cpp: 228,341; xml: 19,066; ansic: 9,854; python: 302; sh: 135; makefile: 32
file content (202 lines) | stat: -rw-r--r-- 6,755 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
/************************************************************************
 *
 * Copyright (C) 2009-2022 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 "io/dicom/helper/DicomDataTools.hpp"

#include "io/dicom/helper/Encoding.hpp"

#include <core/base.hpp>
#include <core/tools/IntrinsicTypes.hpp>

#include <gdcmGlobal.h>
#include <gdcmPhotometricInterpretation.h>
#include <gdcmPixelFormat.h>

namespace sight::io::dicom
{

namespace helper
{

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

typedef std::map<core::tools::Type, gdcm::PixelFormat::ScalarType> PixelTypeConversionMapType;

static const PixelTypeConversionMapType s_PIXEL_TYPE_CONVERSION_MAP = {
    {core::tools::Type::create("uint8"), gdcm::PixelFormat::UINT8},
    {core::tools::Type::create("int8"), gdcm::PixelFormat::INT8},
    // {core::tools::Type::create("XXX")    , gdcm::PixelFormat::UINT12}  , // Unsupported by VTK Render
    // {core::tools::Type::create("XXX")    , gdcm::PixelFormat::INT12}   , // Unsupported by VTK Render
    {core::tools::Type::create("uint16"), gdcm::PixelFormat::UINT16},
    {core::tools::Type::create("int16"), gdcm::PixelFormat::INT16},
    {core::tools::Type::create("uint32"), gdcm::PixelFormat::UINT32},
    {core::tools::Type::create("int32"), gdcm::PixelFormat::INT32},
    // { core::tools::Type::create("XXX")   , gdcm::PixelFormat::FLOAT16} , // Unsupported by VTK Render
    {core::tools::Type::create("float"), gdcm::PixelFormat::FLOAT32},
    {core::tools::Type::create("double"), gdcm::PixelFormat::FLOAT64}
};

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

const gdcm::PixelFormat DicomDataTools::getPixelType(const core::tools::Type& type)
{
    auto it = s_PIXEL_TYPE_CONVERSION_MAP.find(type);
    if(it != s_PIXEL_TYPE_CONVERSION_MAP.end())
    {
        return it->second;
    }

    return gdcm::PixelFormat::UNKNOWN;
}

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

const gdcm::PhotometricInterpretation DicomDataTools::getPhotometricInterpretation(const data::Image::csptr& image)
{
    gdcm::PhotometricInterpretation pi;
    const std::size_t components = image->numComponents();

    // Attempt a guess (VTK do the same choice)
    switch(components)
    {
        case 1: // It could well be MONOCHROME1
            pi = gdcm::PhotometricInterpretation::MONOCHROME2;
            break;

        case 3: // It could well be YBR
            pi = gdcm::PhotometricInterpretation::RGB;
            break;

        case 4: // It could well be CMYK
            pi = gdcm::PhotometricInterpretation::ARGB;
            break;

        default:
            SIGHT_ERROR("Photometric interpretation not found");
            pi = gdcm::PhotometricInterpretation::UNKNOWN;
            break;
    }

    return pi;
}

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

gdcm::Surface::VIEWType DicomDataTools::convertToPresentationType(
    data::Material::RepresentationType representationMode
)
{
    switch(representationMode)
    {
        case data::Material::SURFACE:
            return gdcm::Surface::SURFACE;

        case data::Material::POINT:
            return gdcm::Surface::POINTS;

        case data::Material::WIREFRAME:
            return gdcm::Surface::WIREFRAME;

        default:
            SIGHT_WARN("Representation type not handle (changed to : SURFACE)");
            return gdcm::Surface::SURFACE;
    }
}

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

data::Material::RepresentationType DicomDataTools::convertToRepresentationMode(
    gdcm::Surface::VIEWType presentationType
)
{
    switch(presentationType)
    {
        case gdcm::Surface::SURFACE:
            return data::Material::SURFACE;

        case gdcm::Surface::WIREFRAME:
            return data::Material::WIREFRAME;

        case gdcm::Surface::POINTS:
            return data::Material::POINT;

        default:
            SIGHT_WARN("Presentation type not handle (changed to : SURFACE)");
            return data::Material::SURFACE;
    }
}

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

std::size_t DicomDataTools::convertPointToFrameNumber(
    const data::Image::csptr& image,
    const data::Point::csptr& point
)
{
    // Retrieve Z spacing
    const double zSpacing = (image->numDimensions() > 2) ? (image->getSpacing()[2]) : 1;

    // Retrieve Z coordinate of image origin
    const double zOrigin = (image->numDimensions() > 2) ? (image->getOrigin()[2]) : 0;

    // Retrieve Z coordinate
    const double zCoordinate = static_cast<double>(point->getCoord()[2]);

    // Compute frame number
    const std::size_t frameNumber = static_cast<std::size_t>(floor((zCoordinate - zOrigin) / zSpacing + 0.5)) + 1;
    SIGHT_THROW_EXCEPTION_IF(
        io::dicom::exception::Failed("Coordinates out of image bounds."),
        frameNumber<1 || frameNumber> image->getSize()[2]
    );

    return frameNumber;
}

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

double DicomDataTools::convertFrameNumberToZCoordinate(
    const data::Image::csptr& image,
    const std::size_t frameNumber
)
{
    // Retrieve Z spacing
    const double zSpacing = (image->numDimensions() > 2) ? (image->getSpacing()[2]) : 1;

    // Retrieve Z coordinate of image origin
    const double zOrigin = (image->numDimensions() > 2) ? (image->getOrigin()[2]) : 0;

    // Compute coordinate
    const std::size_t frameIndex = (frameNumber - 1);
    SIGHT_THROW_EXCEPTION_IF(
        io::dicom::exception::Failed("Coordinates out of image bounds."),
        frameIndex >= image->getSize()[2]
    );
    const double zCoordinate = zOrigin + static_cast<double>(frameIndex) * zSpacing;

    return zCoordinate;
}

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

} //namespace helper

} //namespace sight::io::dicom