Mercurial > hg > orthanc
annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 2547:8b6b0b6ece6b
MinGW, ORTHANC_FRAMEWORK_URL
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Wed, 18 Apr 2018 10:23:40 +0200 |
parents | a5196a7d98c6 |
children | 5c83e5cf9d79 |
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 |
2447
878b59270859
upgrade to year 2018
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2428
diff
changeset
|
5 * Copyright (C) 2017-2018 Osimis S.A., 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 && |
2464
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
117 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_FULL && |
1978
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
118 (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
|
119 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 120 { |
121 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
122 photometric_->SetInput(image); | |
123 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB); | |
124 if (!photometric_->Change() || | |
125 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB) | |
126 { | |
127 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
128 } | |
129 } | |
130 } | |
131 } | |
132 | |
133 // Possibly convert planar configuration to interleaved | |
134 { | |
135 const gdcm::Image& image = GetImage(); | |
136 if (image.GetPlanarConfiguration() != 0 && | |
137 image.GetPixelFormat().GetSamplesPerPixel() != 1) | |
138 { | |
139 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration()); | |
140 interleaved_->SetInput(image); | |
141 if (!interleaved_->Change() || | |
142 GetImage().GetPlanarConfiguration() != 0) | |
143 { | |
144 throw std::runtime_error("GDCM cannot change the planar configuration to interleaved"); | |
145 } | |
146 } | |
147 } | |
148 } | |
149 }; | |
150 | |
151 GdcmImageDecoder::GdcmImageDecoder(const void* dicom, | |
152 size_t size) : | |
153 pimpl_(new PImpl(dicom, size)) | |
154 { | |
155 // Setup a stream to the memory buffer | |
156 using namespace boost::iostreams; | |
157 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size); | |
158 stream<basic_array_source<char> > stream(source); | |
159 | |
160 // Parse the DICOM instance using GDCM | |
161 pimpl_->reader_.SetStream(stream); | |
162 if (!pimpl_->reader_.Read()) | |
163 { | |
164 throw std::runtime_error("Bad file format"); | |
165 } | |
166 | |
167 pimpl_->Decode(); | |
168 } | |
169 | |
170 | |
171 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const | |
172 { | |
173 const gdcm::Image& image = pimpl_->GetImage(); | |
174 | |
175 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
176 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 || | |
177 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2)) | |
178 { | |
179 switch (image.GetPixelFormat()) | |
180 { | |
181 case gdcm::PixelFormat::UINT16: | |
182 return OrthancPluginPixelFormat_Grayscale16; | |
183 | |
184 case gdcm::PixelFormat::INT16: | |
185 return OrthancPluginPixelFormat_SignedGrayscale16; | |
186 | |
187 case gdcm::PixelFormat::UINT8: | |
188 return OrthancPluginPixelFormat_Grayscale8; | |
189 | |
190 default: | |
191 throw std::runtime_error("Unsupported pixel format"); | |
192 } | |
193 } | |
194 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
|
195 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::RGB || |
2464
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
196 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_FULL || |
1978
0c9c4e36c2b9
support of YBR_RCT photometric interpretation for JPEG2000 lossless
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
1900
diff
changeset
|
197 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 198 { |
199 switch (image.GetPixelFormat()) | |
200 { | |
201 case gdcm::PixelFormat::UINT8: | |
202 return OrthancPluginPixelFormat_RGB24; | |
203 | |
2424
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
204 case gdcm::PixelFormat::UINT16: |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
205 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1) |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
206 return OrthancPluginPixelFormat_RGB48; |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
207 #else |
2428 | 208 throw std::runtime_error("RGB48 pixel format is only supported if compiled against Orthanc SDK >= 1.3.1"); |
2424
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
209 #endif |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
210 |
1834 | 211 default: |
212 break; | |
213 } | |
214 } | |
215 | |
216 throw std::runtime_error("Unsupported pixel format"); | |
217 } | |
218 | |
219 | |
220 unsigned int GdcmImageDecoder::GetWidth() const | |
221 { | |
222 return pimpl_->GetImage().GetColumns(); | |
223 } | |
224 | |
225 | |
226 unsigned int GdcmImageDecoder::GetHeight() const | |
227 { | |
228 return pimpl_->GetImage().GetRows(); | |
229 } | |
230 | |
231 | |
232 unsigned int GdcmImageDecoder::GetFramesCount() const | |
233 { | |
234 return pimpl_->GetImage().GetDimension(2); | |
235 } | |
236 | |
237 | |
238 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format) | |
239 { | |
240 switch (format) | |
241 { | |
242 case OrthancPluginPixelFormat_Grayscale8: | |
243 return 1; | |
244 | |
245 case OrthancPluginPixelFormat_Grayscale16: | |
246 case OrthancPluginPixelFormat_SignedGrayscale16: | |
247 return 2; | |
248 | |
249 case OrthancPluginPixelFormat_RGB24: | |
250 return 3; | |
251 | |
2427 | 252 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1) |
253 case OrthancPluginPixelFormat_RGB48: | |
254 return 6; | |
255 #endif | |
256 | |
1834 | 257 default: |
258 throw std::runtime_error("Unsupport pixel format"); | |
259 } | |
260 } | |
261 | |
2464
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
262 static void ConvertYbrToRgb(uint8_t rgb[3], |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
263 const uint8_t ybr[3]) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
264 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
265 // http://dicom.nema.org/medical/dicom/current/output/chtml/part03/sect_C.7.6.3.html#sect_C.7.6.3.1.2 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
266 // https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
267 |
2485 | 268 // TODO - Check out the outcome of Mathieu's discussion about |
269 // truncation of YCbCr-to-RGB conversion: | |
270 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ | |
271 | |
2464
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
272 const float Y = ybr[0]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
273 const float Cb = ybr[1]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
274 const float Cr = ybr[2]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
275 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
276 const float result[3] = { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
277 Y + 1.402f * (Cr - 128.0f), |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
278 Y - 0.344136f * (Cb - 128.0f) - 0.714136f * (Cr - 128.0f), |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
279 Y + 1.772f * (Cb - 128.0f) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
280 }; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
281 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
282 for (uint8_t i = 0; i < 3 ; i++) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
283 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
284 if (result[i] < 0) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
285 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
286 rgb[i] = 0; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
287 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
288 else if (result[i] > 255) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
289 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
290 rgb[i] = 255; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
291 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
292 else |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
293 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
294 rgb[i] = static_cast<uint8_t>(result[i]); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
295 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
296 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
297 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
298 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
299 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
300 static void FixPhotometricInterpretation(OrthancImageWrapper& image, |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
301 gdcm::PhotometricInterpretation interpretation) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
302 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
303 switch (interpretation) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
304 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
305 case gdcm::PhotometricInterpretation::RGB: |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
306 return; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
307 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
308 case gdcm::PhotometricInterpretation::YBR_FULL: |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
309 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
310 // Fix for Osimis issue WVB-319: Some images are not loading in US_MF |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
311 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
312 uint32_t width = image.GetWidth(); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
313 uint32_t height = image.GetHeight(); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
314 uint32_t pitch = image.GetPitch(); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
315 uint8_t* buffer = reinterpret_cast<uint8_t*>(image.GetBuffer()); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
316 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
317 if (image.GetFormat() != OrthancPluginPixelFormat_RGB24 || |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
318 pitch < 3 * width) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
319 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
320 throw std::runtime_error("Internal error"); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
321 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
322 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
323 for (uint32_t y = 0; y < height; y++) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
324 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
325 uint8_t* p = buffer + y * pitch; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
326 for (uint32_t x = 0; x < width; x++, p += 3) |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
327 { |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
328 const uint8_t ybr[3] = { p[0], p[1], p[2] }; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
329 uint8_t rgb[3]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
330 ConvertYbrToRgb(rgb, ybr); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
331 p[0] = rgb[0]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
332 p[1] = rgb[1]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
333 p[2] = rgb[2]; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
334 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
335 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
336 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
337 return; |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
338 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
339 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
340 default: |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
341 throw std::runtime_error("Unsupported output photometric interpretation"); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
342 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
343 } |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
344 |
1834 | 345 |
346 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context, | |
347 unsigned int frameIndex) const | |
348 { | |
349 unsigned int frames = GetFramesCount(); | |
350 unsigned int width = GetWidth(); | |
351 unsigned int height = GetHeight(); | |
352 OrthancPluginPixelFormat format = GetFormat(); | |
353 size_t bpp = GetBytesPerPixel(format); | |
354 | |
355 if (frameIndex >= frames) | |
356 { | |
357 throw std::runtime_error("Inexistent frame index"); | |
358 } | |
359 | |
360 std::string& decoded = pimpl_->decoded_; | |
361 OrthancImageWrapper target(context, format, width, height); | |
362 | |
363 if (width == 0 || | |
364 height == 0) | |
365 { | |
366 return target.Release(); | |
367 } | |
368 | |
369 if (decoded.empty()) | |
370 { | |
371 decoded.resize(pimpl_->GetImage().GetBufferLength()); | |
372 pimpl_->GetImage().GetBuffer(&decoded[0]); | |
373 } | |
374 | |
375 const void* sourceBuffer = &decoded[0]; | |
376 | |
377 if (target.GetPitch() == bpp * width && | |
378 frames == 1) | |
379 { | |
380 assert(decoded.size() == target.GetPitch() * target.GetHeight()); | |
381 memcpy(target.GetBuffer(), sourceBuffer, decoded.size()); | |
382 } | |
383 else | |
384 { | |
385 size_t targetPitch = target.GetPitch(); | |
386 size_t sourcePitch = width * bpp; | |
387 | |
388 const char* a = &decoded[sourcePitch * height * frameIndex]; | |
389 char* b = target.GetBuffer(); | |
390 | |
391 for (uint32_t y = 0; y < height; y++) | |
392 { | |
393 memcpy(b, a, sourcePitch); | |
394 a += sourcePitch; | |
395 b += targetPitch; | |
396 } | |
397 } | |
2464
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
398 |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
399 FixPhotometricInterpretation(target, pimpl_->GetImage().GetPhotometricInterpretation()); |
61fc5133e5d5
Fix for Osimis issue WVB-319: Some images are not loading in US_MF
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2447
diff
changeset
|
400 |
1834 | 401 return target.Release(); |
402 } | |
403 } |