comparison Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 1834:b1a6f49b21dd

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