Mercurial > hg > orthanc
annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 2220:0fb6e0461105
fix handling of Booleans in Lua
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 13 Dec 2016 11:43:35 +0100 |
parents | 0c9c4e36c2b9 |
children | a3a65de1840f |
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 |
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 && | |
1978
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
115 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
|
116 (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
|
117 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 118 { |
119 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
120 photometric_->SetInput(image); | |
121 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB); | |
122 if (!photometric_->Change() || | |
123 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB) | |
124 { | |
125 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
126 } | |
127 } | |
128 } | |
129 } | |
130 | |
131 // Possibly convert planar configuration to interleaved | |
132 { | |
133 const gdcm::Image& image = GetImage(); | |
134 if (image.GetPlanarConfiguration() != 0 && | |
135 image.GetPixelFormat().GetSamplesPerPixel() != 1) | |
136 { | |
137 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration()); | |
138 interleaved_->SetInput(image); | |
139 if (!interleaved_->Change() || | |
140 GetImage().GetPlanarConfiguration() != 0) | |
141 { | |
142 throw std::runtime_error("GDCM cannot change the planar configuration to interleaved"); | |
143 } | |
144 } | |
145 } | |
146 } | |
147 }; | |
148 | |
149 GdcmImageDecoder::GdcmImageDecoder(const void* dicom, | |
150 size_t size) : | |
151 pimpl_(new PImpl(dicom, size)) | |
152 { | |
153 // Setup a stream to the memory buffer | |
154 using namespace boost::iostreams; | |
155 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size); | |
156 stream<basic_array_source<char> > stream(source); | |
157 | |
158 // Parse the DICOM instance using GDCM | |
159 pimpl_->reader_.SetStream(stream); | |
160 if (!pimpl_->reader_.Read()) | |
161 { | |
162 throw std::runtime_error("Bad file format"); | |
163 } | |
164 | |
165 pimpl_->Decode(); | |
166 } | |
167 | |
168 | |
169 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const | |
170 { | |
171 const gdcm::Image& image = pimpl_->GetImage(); | |
172 | |
173 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
174 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 || | |
175 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2)) | |
176 { | |
177 switch (image.GetPixelFormat()) | |
178 { | |
179 case gdcm::PixelFormat::UINT16: | |
180 return OrthancPluginPixelFormat_Grayscale16; | |
181 | |
182 case gdcm::PixelFormat::INT16: | |
183 return OrthancPluginPixelFormat_SignedGrayscale16; | |
184 | |
185 case gdcm::PixelFormat::UINT8: | |
186 return OrthancPluginPixelFormat_Grayscale8; | |
187 | |
188 default: | |
189 throw std::runtime_error("Unsupported pixel format"); | |
190 } | |
191 } | |
192 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
|
193 (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
|
194 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 195 { |
196 switch (image.GetPixelFormat()) | |
197 { | |
198 case gdcm::PixelFormat::UINT8: | |
199 return OrthancPluginPixelFormat_RGB24; | |
200 | |
201 default: | |
202 break; | |
203 } | |
204 } | |
205 | |
206 throw std::runtime_error("Unsupported pixel format"); | |
207 } | |
208 | |
209 | |
210 unsigned int GdcmImageDecoder::GetWidth() const | |
211 { | |
212 return pimpl_->GetImage().GetColumns(); | |
213 } | |
214 | |
215 | |
216 unsigned int GdcmImageDecoder::GetHeight() const | |
217 { | |
218 return pimpl_->GetImage().GetRows(); | |
219 } | |
220 | |
221 | |
222 unsigned int GdcmImageDecoder::GetFramesCount() const | |
223 { | |
224 return pimpl_->GetImage().GetDimension(2); | |
225 } | |
226 | |
227 | |
228 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format) | |
229 { | |
230 switch (format) | |
231 { | |
232 case OrthancPluginPixelFormat_Grayscale8: | |
233 return 1; | |
234 | |
235 case OrthancPluginPixelFormat_Grayscale16: | |
236 case OrthancPluginPixelFormat_SignedGrayscale16: | |
237 return 2; | |
238 | |
239 case OrthancPluginPixelFormat_RGB24: | |
240 return 3; | |
241 | |
242 default: | |
243 throw std::runtime_error("Unsupport pixel format"); | |
244 } | |
245 } | |
246 | |
247 | |
248 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context, | |
249 unsigned int frameIndex) const | |
250 { | |
251 unsigned int frames = GetFramesCount(); | |
252 unsigned int width = GetWidth(); | |
253 unsigned int height = GetHeight(); | |
254 OrthancPluginPixelFormat format = GetFormat(); | |
255 size_t bpp = GetBytesPerPixel(format); | |
256 | |
257 if (frameIndex >= frames) | |
258 { | |
259 throw std::runtime_error("Inexistent frame index"); | |
260 } | |
261 | |
262 std::string& decoded = pimpl_->decoded_; | |
263 OrthancImageWrapper target(context, format, width, height); | |
264 | |
265 if (width == 0 || | |
266 height == 0) | |
267 { | |
268 return target.Release(); | |
269 } | |
270 | |
271 if (decoded.empty()) | |
272 { | |
273 decoded.resize(pimpl_->GetImage().GetBufferLength()); | |
274 pimpl_->GetImage().GetBuffer(&decoded[0]); | |
275 } | |
276 | |
277 const void* sourceBuffer = &decoded[0]; | |
278 | |
279 if (target.GetPitch() == bpp * width && | |
280 frames == 1) | |
281 { | |
282 assert(decoded.size() == target.GetPitch() * target.GetHeight()); | |
283 memcpy(target.GetBuffer(), sourceBuffer, decoded.size()); | |
284 } | |
285 else | |
286 { | |
287 size_t targetPitch = target.GetPitch(); | |
288 size_t sourcePitch = width * bpp; | |
289 | |
290 const char* a = &decoded[sourcePitch * height * frameIndex]; | |
291 char* b = target.GetBuffer(); | |
292 | |
293 for (uint32_t y = 0; y < height; y++) | |
294 { | |
295 memcpy(b, a, sourcePitch); | |
296 a += sourcePitch; | |
297 b += targetPitch; | |
298 } | |
299 } | |
300 | |
301 return target.Release(); | |
302 } | |
303 } |