Mercurial > hg > orthanc
annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 2312:8700dcaa02e5 issue-46-anonymization
GenerateAnonymizationProfile.py
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 12 Jul 2017 16:44:33 +0200 |
parents | a3a65de1840f |
children | 7ef9207f31d4 |
rev | line source |
---|---|
1834 | 1 /** |
2 * Orthanc - A Lightweight, RESTful DICOM Store | |
1900 | 3 * Copyright (C) 2012-2016 Sebastien Jodogne, Medical Physics |
1834 | 4 * Department, University Hospital of Liege, Belgium |
2244
a3a65de1840f
shared copyright with osimis
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1978
diff
changeset
|
5 * Copyright (C) 2017 Osimis, Belgium |
1834 | 6 * |
7 * This program is free software: you can redistribute it and/or | |
8 * modify it under the terms of the GNU General Public License as | |
9 * published by the Free Software Foundation, either version 3 of the | |
10 * License, or (at your option) any later version. | |
11 * | |
12 * This program is distributed in the hope that it will be useful, but | |
13 * WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 * General Public License for more details. | |
16 * | |
17 * You should have received a copy of the GNU General Public License | |
18 * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 **/ | |
20 | |
21 | |
22 #include "GdcmImageDecoder.h" | |
23 | |
24 #include "OrthancImageWrapper.h" | |
25 | |
26 #include <gdcmImageReader.h> | |
27 #include <gdcmImageApplyLookupTable.h> | |
28 #include <gdcmImageChangePlanarConfiguration.h> | |
29 #include <gdcmImageChangePhotometricInterpretation.h> | |
30 #include <stdexcept> | |
31 #include <boost/iostreams/stream.hpp> | |
32 #include <boost/iostreams/device/array.hpp> | |
33 | |
34 | |
35 namespace OrthancPlugins | |
36 { | |
37 struct GdcmImageDecoder::PImpl | |
38 { | |
39 const void* dicom_; | |
40 size_t size_; | |
41 | |
42 gdcm::ImageReader reader_; | |
43 std::auto_ptr<gdcm::ImageApplyLookupTable> lut_; | |
44 std::auto_ptr<gdcm::ImageChangePhotometricInterpretation> photometric_; | |
45 std::auto_ptr<gdcm::ImageChangePlanarConfiguration> interleaved_; | |
46 std::string decoded_; | |
47 | |
48 PImpl(const void* dicom, | |
49 size_t size) : | |
50 dicom_(dicom), | |
51 size_(size) | |
52 { | |
53 } | |
54 | |
55 | |
56 const gdcm::DataSet& GetDataSet() const | |
57 { | |
58 return reader_.GetFile().GetDataSet(); | |
59 } | |
60 | |
61 | |
62 const gdcm::Image& GetImage() const | |
63 { | |
64 if (interleaved_.get() != NULL) | |
65 { | |
66 return interleaved_->GetOutput(); | |
67 } | |
68 | |
69 if (lut_.get() != NULL) | |
70 { | |
71 return lut_->GetOutput(); | |
72 } | |
73 | |
74 if (photometric_.get() != NULL) | |
75 { | |
76 return photometric_->GetOutput(); | |
77 } | |
78 | |
79 return reader_.GetImage(); | |
80 } | |
81 | |
82 | |
83 void Decode() | |
84 { | |
85 // Change photometric interpretation or apply LUT, if required | |
86 { | |
87 const gdcm::Image& image = GetImage(); | |
88 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
89 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::PALETTE_COLOR) | |
90 { | |
91 lut_.reset(new gdcm::ImageApplyLookupTable()); | |
92 lut_->SetInput(image); | |
93 if (!lut_->Apply()) | |
94 { | |
95 throw std::runtime_error( "GDCM cannot apply the lookup table"); | |
96 } | |
97 } | |
98 else if (image.GetPixelFormat().GetSamplesPerPixel() == 1) | |
99 { | |
100 if (image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME1 && | |
101 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2) | |
102 { | |
103 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
104 photometric_->SetInput(image); | |
105 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2); | |
106 if (!photometric_->Change() || | |
107 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2) | |
108 { | |
109 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
110 } | |
111 } | |
112 } | |
113 else | |
114 { | |
115 if (image.GetPixelFormat().GetSamplesPerPixel() == 3 && | |
1978
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
116 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB && |
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
117 (image.GetTransferSyntax() != gdcm::TransferSyntax::JPEG2000Lossless || |
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
118 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 119 { |
120 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
121 photometric_->SetInput(image); | |
122 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB); | |
123 if (!photometric_->Change() || | |
124 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB) | |
125 { | |
126 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
127 } | |
128 } | |
129 } | |
130 } | |
131 | |
132 // Possibly convert planar configuration to interleaved | |
133 { | |
134 const gdcm::Image& image = GetImage(); | |
135 if (image.GetPlanarConfiguration() != 0 && | |
136 image.GetPixelFormat().GetSamplesPerPixel() != 1) | |
137 { | |
138 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration()); | |
139 interleaved_->SetInput(image); | |
140 if (!interleaved_->Change() || | |
141 GetImage().GetPlanarConfiguration() != 0) | |
142 { | |
143 throw std::runtime_error("GDCM cannot change the planar configuration to interleaved"); | |
144 } | |
145 } | |
146 } | |
147 } | |
148 }; | |
149 | |
150 GdcmImageDecoder::GdcmImageDecoder(const void* dicom, | |
151 size_t size) : | |
152 pimpl_(new PImpl(dicom, size)) | |
153 { | |
154 // Setup a stream to the memory buffer | |
155 using namespace boost::iostreams; | |
156 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size); | |
157 stream<basic_array_source<char> > stream(source); | |
158 | |
159 // Parse the DICOM instance using GDCM | |
160 pimpl_->reader_.SetStream(stream); | |
161 if (!pimpl_->reader_.Read()) | |
162 { | |
163 throw std::runtime_error("Bad file format"); | |
164 } | |
165 | |
166 pimpl_->Decode(); | |
167 } | |
168 | |
169 | |
170 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const | |
171 { | |
172 const gdcm::Image& image = pimpl_->GetImage(); | |
173 | |
174 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
175 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 || | |
176 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2)) | |
177 { | |
178 switch (image.GetPixelFormat()) | |
179 { | |
180 case gdcm::PixelFormat::UINT16: | |
181 return OrthancPluginPixelFormat_Grayscale16; | |
182 | |
183 case gdcm::PixelFormat::INT16: | |
184 return OrthancPluginPixelFormat_SignedGrayscale16; | |
185 | |
186 case gdcm::PixelFormat::UINT8: | |
187 return OrthancPluginPixelFormat_Grayscale8; | |
188 | |
189 default: | |
190 throw std::runtime_error("Unsupported pixel format"); | |
191 } | |
192 } | |
193 else if (image.GetPixelFormat().GetSamplesPerPixel() == 3 && | |
1978
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
194 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::RGB || |
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
195 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 196 { |
197 switch (image.GetPixelFormat()) | |
198 { | |
199 case gdcm::PixelFormat::UINT8: | |
200 return OrthancPluginPixelFormat_RGB24; | |
201 | |
202 default: | |
203 break; | |
204 } | |
205 } | |
206 | |
207 throw std::runtime_error("Unsupported pixel format"); | |
208 } | |
209 | |
210 | |
211 unsigned int GdcmImageDecoder::GetWidth() const | |
212 { | |
213 return pimpl_->GetImage().GetColumns(); | |
214 } | |
215 | |
216 | |
217 unsigned int GdcmImageDecoder::GetHeight() const | |
218 { | |
219 return pimpl_->GetImage().GetRows(); | |
220 } | |
221 | |
222 | |
223 unsigned int GdcmImageDecoder::GetFramesCount() const | |
224 { | |
225 return pimpl_->GetImage().GetDimension(2); | |
226 } | |
227 | |
228 | |
229 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format) | |
230 { | |
231 switch (format) | |
232 { | |
233 case OrthancPluginPixelFormat_Grayscale8: | |
234 return 1; | |
235 | |
236 case OrthancPluginPixelFormat_Grayscale16: | |
237 case OrthancPluginPixelFormat_SignedGrayscale16: | |
238 return 2; | |
239 | |
240 case OrthancPluginPixelFormat_RGB24: | |
241 return 3; | |
242 | |
243 default: | |
244 throw std::runtime_error("Unsupport pixel format"); | |
245 } | |
246 } | |
247 | |
248 | |
249 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context, | |
250 unsigned int frameIndex) const | |
251 { | |
252 unsigned int frames = GetFramesCount(); | |
253 unsigned int width = GetWidth(); | |
254 unsigned int height = GetHeight(); | |
255 OrthancPluginPixelFormat format = GetFormat(); | |
256 size_t bpp = GetBytesPerPixel(format); | |
257 | |
258 if (frameIndex >= frames) | |
259 { | |
260 throw std::runtime_error("Inexistent frame index"); | |
261 } | |
262 | |
263 std::string& decoded = pimpl_->decoded_; | |
264 OrthancImageWrapper target(context, format, width, height); | |
265 | |
266 if (width == 0 || | |
267 height == 0) | |
268 { | |
269 return target.Release(); | |
270 } | |
271 | |
272 if (decoded.empty()) | |
273 { | |
274 decoded.resize(pimpl_->GetImage().GetBufferLength()); | |
275 pimpl_->GetImage().GetBuffer(&decoded[0]); | |
276 } | |
277 | |
278 const void* sourceBuffer = &decoded[0]; | |
279 | |
280 if (target.GetPitch() == bpp * width && | |
281 frames == 1) | |
282 { | |
283 assert(decoded.size() == target.GetPitch() * target.GetHeight()); | |
284 memcpy(target.GetBuffer(), sourceBuffer, decoded.size()); | |
285 } | |
286 else | |
287 { | |
288 size_t targetPitch = target.GetPitch(); | |
289 size_t sourcePitch = width * bpp; | |
290 | |
291 const char* a = &decoded[sourcePitch * height * frameIndex]; | |
292 char* b = target.GetBuffer(); | |
293 | |
294 for (uint32_t y = 0; y < height; y++) | |
295 { | |
296 memcpy(b, a, sourcePitch); | |
297 a += sourcePitch; | |
298 b += targetPitch; | |
299 } | |
300 } | |
301 | |
302 return target.Release(); | |
303 } | |
304 } |