Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Tetragramm
GitHub Repository: Tetragramm/opencv
Path: blob/master/modules/imgcodecs/src/grfmt_gdcm.cpp
16337 views
1
/*M///////////////////////////////////////////////////////////////////////////////////////
2
//
3
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4
//
5
// By downloading, copying, installing or using the software you agree to this license.
6
// If you do not agree to this license, do not download, install,
7
// copy or use the software.
8
//
9
//
10
// License Agreement
11
// For Open Source Computer Vision Library
12
//
13
// Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14
// Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15
// Third party copyrights are property of their respective owners.
16
//
17
// Redistribution and use in source and binary forms, with or without modification,
18
// are permitted provided that the following conditions are met:
19
//
20
// * Redistribution's of source code must retain the above copyright notice,
21
// this list of conditions and the following disclaimer.
22
//
23
// * Redistribution's in binary form must reproduce the above copyright notice,
24
// this list of conditions and the following disclaimer in the documentation
25
// and/or other materials provided with the distribution.
26
//
27
// * The name of the copyright holders may not be used to endorse or promote products
28
// derived from this software without specific prior written permission.
29
//
30
// This software is provided by the copyright holders and contributors "as is" and
31
// any express or implied warranties, including, but not limited to, the implied
32
// warranties of merchantability and fitness for a particular purpose are disclaimed.
33
// In no event shall the Intel Corporation or contributors be liable for any direct,
34
// indirect, incidental, special, exemplary, or consequential damages
35
// (including, but not limited to, procurement of substitute goods or services;
36
// loss of use, data, or profits; or business interruption) however caused
37
// and on any theory of liability, whether in contract, strict liability,
38
// or tort (including negligence or otherwise) arising in any way out of
39
// the use of this software, even if advised of the possibility of such damage.
40
//
41
//M*/
42
43
#include "precomp.hpp"
44
#include "grfmt_gdcm.hpp"
45
46
#ifdef HAVE_GDCM
47
48
//#define DBG(...) printf(__VA_ARGS__)
49
#define DBG(...)
50
51
#include <gdcmImageReader.h>
52
53
static const size_t preamble_skip = 128;
54
static const size_t magic_len = 4;
55
56
inline cv::String getMagic()
57
{
58
return cv::String("\x44\x49\x43\x4D", 4);
59
}
60
61
namespace cv
62
{
63
64
/************************ DICOM decoder *****************************/
65
66
DICOMDecoder::DICOMDecoder()
67
{
68
// DICOM preamble is 128 bytes (can have any value, defaults to 0) + 4 bytes magic number (DICM)
69
m_signature = String(preamble_skip, (char)'\x0') + getMagic();
70
m_buf_supported = false;
71
}
72
73
bool DICOMDecoder::checkSignature( const String& signature ) const
74
{
75
if (signature.size() >= preamble_skip + magic_len)
76
{
77
if (signature.substr(preamble_skip, magic_len) == getMagic())
78
{
79
return true;
80
}
81
}
82
DBG("GDCM | Signature does not match\n");
83
return false;
84
}
85
86
ImageDecoder DICOMDecoder::newDecoder() const
87
{
88
return makePtr<DICOMDecoder>();
89
}
90
91
bool DICOMDecoder::readHeader()
92
{
93
gdcm::ImageReader csImageReader;
94
csImageReader.SetFileName(m_filename.c_str());
95
if(!csImageReader.Read())
96
{
97
DBG("GDCM | Failed to open DICOM file\n");
98
return(false);
99
}
100
101
const gdcm::Image &csImage = csImageReader.GetImage();
102
bool bOK = true;
103
switch (csImage.GetPhotometricInterpretation().GetType())
104
{
105
case gdcm::PhotometricInterpretation::MONOCHROME1:
106
case gdcm::PhotometricInterpretation::MONOCHROME2:
107
{
108
switch (csImage.GetPixelFormat().GetScalarType())
109
{
110
case gdcm::PixelFormat::INT8: m_type = CV_8SC1; break;
111
case gdcm::PixelFormat::UINT8: m_type = CV_8UC1; break;
112
case gdcm::PixelFormat::INT16: m_type = CV_16SC1; break;
113
case gdcm::PixelFormat::UINT16: m_type = CV_16UC1; break;
114
case gdcm::PixelFormat::INT32: m_type = CV_32SC1; break;
115
case gdcm::PixelFormat::FLOAT32: m_type = CV_32FC1; break;
116
case gdcm::PixelFormat::FLOAT64: m_type = CV_64FC1; break;
117
default: bOK = false; DBG("GDCM | Monochrome scalar type not supported\n"); break;
118
}
119
break;
120
}
121
122
case gdcm::PhotometricInterpretation::RGB:
123
{
124
switch (csImage.GetPixelFormat().GetScalarType())
125
{
126
case gdcm::PixelFormat::UINT8: m_type = CV_8UC3; break;
127
default: bOK = false; DBG("GDCM | RGB scalar type not supported\n"); break;
128
}
129
break;
130
}
131
132
default:
133
{
134
bOK = false;
135
DBG("GDCM | PI not supported: %s\n", csImage.GetPhotometricInterpretation().GetString());
136
break;
137
}
138
}
139
140
if(bOK)
141
{
142
unsigned int ndim = csImage.GetNumberOfDimensions();
143
if (ndim != 2)
144
{
145
DBG("GDCM | Invalid dimensions number: %d\n", ndim);
146
bOK = false;
147
}
148
}
149
if (bOK)
150
{
151
const unsigned int *piDimension = csImage.GetDimensions();
152
m_height = piDimension[0];
153
m_width = piDimension[1];
154
if( ( m_width <=0 ) || ( m_height <=0 ) )
155
{
156
DBG("GDCM | Invalid dimensions: %d x %d\n", piDimension[0], piDimension[1]);
157
bOK = false;
158
}
159
}
160
161
return(bOK);
162
}
163
164
165
bool DICOMDecoder::readData( Mat& csImage )
166
{
167
csImage.create(m_width,m_height,m_type);
168
169
gdcm::ImageReader csImageReader;
170
csImageReader.SetFileName(m_filename.c_str());
171
if(!csImageReader.Read())
172
{
173
DBG("GDCM | Failed to Read\n");
174
return false;
175
}
176
177
const gdcm::Image &img = csImageReader.GetImage();
178
179
unsigned long len = img.GetBufferLength();
180
if (len > csImage.elemSize() * csImage.total())
181
{
182
DBG("GDCM | Buffer is bigger than Mat: %ld > %ld * %ld\n", len, csImage.elemSize(), csImage.total());
183
return false;
184
}
185
186
if (!img.GetBuffer((char*)csImage.ptr()))
187
{
188
DBG("GDCM | Failed to GetBuffer\n");
189
return false;
190
}
191
DBG("GDCM | Read OK\n");
192
return true;
193
}
194
195
}
196
197
#endif // HAVE_GDCM
198