Mercurial > hg > orthanc
annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 3858:3ab2d48c8f69 c-get
integration mainline->c-get
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Tue, 21 Apr 2020 16:37:25 +0200 |
parents | 6110a4995ace |
children | 7e33516965f8 |
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 |
3640
94f4a18a79cc
upgrade to year 2020
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3351
diff
changeset
|
5 * Copyright (C) 2017-2020 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 | |
3768
6110a4995ace
replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
24 #include "../../../Core/Compatibility.h" |
1834 | 25 #include "OrthancImageWrapper.h" |
26 | |
27 #include <gdcmImageReader.h> | |
28 #include <gdcmImageApplyLookupTable.h> | |
29 #include <gdcmImageChangePlanarConfiguration.h> | |
30 #include <gdcmImageChangePhotometricInterpretation.h> | |
31 #include <stdexcept> | |
32 #include <boost/iostreams/stream.hpp> | |
33 #include <boost/iostreams/device/array.hpp> | |
34 | |
35 | |
36 namespace OrthancPlugins | |
37 { | |
38 struct GdcmImageDecoder::PImpl | |
39 { | |
40 const void* dicom_; | |
41 size_t size_; | |
42 | |
43 gdcm::ImageReader reader_; | |
3768
6110a4995ace
replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
44 std::unique_ptr<gdcm::ImageApplyLookupTable> lut_; |
6110a4995ace
replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
45 std::unique_ptr<gdcm::ImageChangePhotometricInterpretation> photometric_; |
6110a4995ace
replacing std::auto_ptr by std::unique_ptr in GDCM sample plugin
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3640
diff
changeset
|
46 std::unique_ptr<gdcm::ImageChangePlanarConfiguration> interleaved_; |
1834 | 47 std::string decoded_; |
48 | |
49 PImpl(const void* dicom, | |
50 size_t size) : | |
51 dicom_(dicom), | |
52 size_(size) | |
53 { | |
54 } | |
55 | |
56 | |
57 const gdcm::DataSet& GetDataSet() const | |
58 { | |
59 return reader_.GetFile().GetDataSet(); | |
60 } | |
61 | |
62 | |
63 const gdcm::Image& GetImage() const | |
64 { | |
65 if (interleaved_.get() != NULL) | |
66 { | |
67 return interleaved_->GetOutput(); | |
68 } | |
69 | |
70 if (lut_.get() != NULL) | |
71 { | |
72 return lut_->GetOutput(); | |
73 } | |
74 | |
75 if (photometric_.get() != NULL) | |
76 { | |
77 return photometric_->GetOutput(); | |
78 } | |
79 | |
80 return reader_.GetImage(); | |
81 } | |
82 | |
83 | |
84 void Decode() | |
85 { | |
86 // Change photometric interpretation or apply LUT, if required | |
87 { | |
88 const gdcm::Image& image = GetImage(); | |
89 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
90 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::PALETTE_COLOR) | |
91 { | |
92 lut_.reset(new gdcm::ImageApplyLookupTable()); | |
93 lut_->SetInput(image); | |
94 if (!lut_->Apply()) | |
95 { | |
96 throw std::runtime_error( "GDCM cannot apply the lookup table"); | |
97 } | |
98 } | |
99 else if (image.GetPixelFormat().GetSamplesPerPixel() == 1) | |
100 { | |
101 if (image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME1 && | |
102 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2) | |
103 { | |
104 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
105 photometric_->SetInput(image); | |
106 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::MONOCHROME2); | |
107 if (!photometric_->Change() || | |
108 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::MONOCHROME2) | |
109 { | |
110 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
111 } | |
112 } | |
113 } | |
114 else | |
115 { | |
116 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
|
117 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
|
118 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
|
119 (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
|
120 image.GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 121 { |
122 photometric_.reset(new gdcm::ImageChangePhotometricInterpretation()); | |
123 photometric_->SetInput(image); | |
124 photometric_->SetPhotometricInterpretation(gdcm::PhotometricInterpretation::RGB); | |
125 if (!photometric_->Change() || | |
126 GetImage().GetPhotometricInterpretation() != gdcm::PhotometricInterpretation::RGB) | |
127 { | |
128 throw std::runtime_error("GDCM cannot change the photometric interpretation"); | |
129 } | |
130 } | |
131 } | |
132 } | |
133 | |
134 // Possibly convert planar configuration to interleaved | |
135 { | |
136 const gdcm::Image& image = GetImage(); | |
137 if (image.GetPlanarConfiguration() != 0 && | |
138 image.GetPixelFormat().GetSamplesPerPixel() != 1) | |
139 { | |
140 interleaved_.reset(new gdcm::ImageChangePlanarConfiguration()); | |
141 interleaved_->SetInput(image); | |
142 if (!interleaved_->Change() || | |
143 GetImage().GetPlanarConfiguration() != 0) | |
144 { | |
145 throw std::runtime_error("GDCM cannot change the planar configuration to interleaved"); | |
146 } | |
147 } | |
148 } | |
149 } | |
150 }; | |
151 | |
152 GdcmImageDecoder::GdcmImageDecoder(const void* dicom, | |
153 size_t size) : | |
154 pimpl_(new PImpl(dicom, size)) | |
155 { | |
156 // Setup a stream to the memory buffer | |
157 using namespace boost::iostreams; | |
158 basic_array_source<char> source(reinterpret_cast<const char*>(dicom), size); | |
159 stream<basic_array_source<char> > stream(source); | |
160 | |
161 // Parse the DICOM instance using GDCM | |
162 pimpl_->reader_.SetStream(stream); | |
163 if (!pimpl_->reader_.Read()) | |
164 { | |
165 throw std::runtime_error("Bad file format"); | |
166 } | |
167 | |
168 pimpl_->Decode(); | |
169 } | |
170 | |
171 | |
172 OrthancPluginPixelFormat GdcmImageDecoder::GetFormat() const | |
173 { | |
174 const gdcm::Image& image = pimpl_->GetImage(); | |
175 | |
176 if (image.GetPixelFormat().GetSamplesPerPixel() == 1 && | |
177 (image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME1 || | |
178 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::MONOCHROME2)) | |
179 { | |
180 switch (image.GetPixelFormat()) | |
181 { | |
182 case gdcm::PixelFormat::UINT16: | |
183 return OrthancPluginPixelFormat_Grayscale16; | |
184 | |
185 case gdcm::PixelFormat::INT16: | |
186 return OrthancPluginPixelFormat_SignedGrayscale16; | |
187 | |
188 case gdcm::PixelFormat::UINT8: | |
189 return OrthancPluginPixelFormat_Grayscale8; | |
190 | |
191 default: | |
192 throw std::runtime_error("Unsupported pixel format"); | |
193 } | |
194 } | |
195 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
|
196 (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
|
197 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
|
198 image.GetPhotometricInterpretation() == gdcm::PhotometricInterpretation::YBR_RCT)) |
1834 | 199 { |
200 switch (image.GetPixelFormat()) | |
201 { | |
202 case gdcm::PixelFormat::UINT8: | |
203 return OrthancPluginPixelFormat_RGB24; | |
204 | |
2424
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
205 case gdcm::PixelFormat::UINT16: |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
206 #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
|
207 return OrthancPluginPixelFormat_RGB48; |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
208 #else |
2428 | 209 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
|
210 #endif |
7ef9207f31d4
New pixel formats exposed in plugin SDK
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2244
diff
changeset
|
211 |
1834 | 212 default: |
213 break; | |
214 } | |
215 } | |
216 | |
217 throw std::runtime_error("Unsupported pixel format"); | |
218 } | |
219 | |
220 | |
221 unsigned int GdcmImageDecoder::GetWidth() const | |
222 { | |
223 return pimpl_->GetImage().GetColumns(); | |
224 } | |
225 | |
226 | |
227 unsigned int GdcmImageDecoder::GetHeight() const | |
228 { | |
229 return pimpl_->GetImage().GetRows(); | |
230 } | |
231 | |
232 | |
233 unsigned int GdcmImageDecoder::GetFramesCount() const | |
234 { | |
235 return pimpl_->GetImage().GetDimension(2); | |
236 } | |
237 | |
238 | |
239 size_t GdcmImageDecoder::GetBytesPerPixel(OrthancPluginPixelFormat format) | |
240 { | |
241 switch (format) | |
242 { | |
243 case OrthancPluginPixelFormat_Grayscale8: | |
244 return 1; | |
245 | |
246 case OrthancPluginPixelFormat_Grayscale16: | |
247 case OrthancPluginPixelFormat_SignedGrayscale16: | |
248 return 2; | |
249 | |
250 case OrthancPluginPixelFormat_RGB24: | |
251 return 3; | |
252 | |
2427 | 253 #if ORTHANC_PLUGINS_VERSION_IS_ABOVE(1, 3, 1) |
254 case OrthancPluginPixelFormat_RGB48: | |
255 return 6; | |
256 #endif | |
257 | |
1834 | 258 default: |
259 throw std::runtime_error("Unsupport pixel format"); | |
260 } | |
261 } | |
262 | |
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
|
263 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
|
264 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
|
265 { |
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 // 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
|
267 // 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
|
268 |
2485 | 269 // TODO - Check out the outcome of Mathieu's discussion about |
270 // truncation of YCbCr-to-RGB conversion: | |
271 // https://groups.google.com/forum/#!msg/comp.protocols.dicom/JHuGeyWbTz8/ARoTWrJzAQAJ | |
272 | |
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
|
273 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
|
274 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
|
275 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
|
276 |
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 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
|
278 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
|
279 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
|
280 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
|
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 |
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 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
|
284 { |
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 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
|
286 { |
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 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
|
288 } |
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 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
|
290 { |
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 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
|
292 } |
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 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
|
294 { |
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 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
|
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 |
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 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
|
302 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
|
303 { |
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 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
|
305 { |
2798
5c83e5cf9d79
fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents:
2485
diff
changeset
|
306 case gdcm::PhotometricInterpretation::MONOCHROME1: |
5c83e5cf9d79
fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents:
2485
diff
changeset
|
307 case gdcm::PhotometricInterpretation::MONOCHROME2: |
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
|
308 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
|
309 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
|
310 |
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 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
|
312 { |
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 // 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
|
314 |
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 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
|
316 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
|
317 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
|
318 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
|
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 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
|
321 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
|
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 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
|
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 |
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 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
|
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 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
|
329 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
|
330 { |
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 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
|
332 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
|
333 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
|
334 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
|
335 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
|
336 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
|
337 } |
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 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
|
341 } |
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 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
|
344 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
|
345 } |
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
|
346 } |
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
|
347 |
1834 | 348 |
349 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context, | |
350 unsigned int frameIndex) const | |
351 { | |
352 unsigned int frames = GetFramesCount(); | |
353 unsigned int width = GetWidth(); | |
354 unsigned int height = GetHeight(); | |
355 OrthancPluginPixelFormat format = GetFormat(); | |
356 size_t bpp = GetBytesPerPixel(format); | |
357 | |
358 if (frameIndex >= frames) | |
359 { | |
360 throw std::runtime_error("Inexistent frame index"); | |
361 } | |
362 | |
363 std::string& decoded = pimpl_->decoded_; | |
364 OrthancImageWrapper target(context, format, width, height); | |
365 | |
366 if (width == 0 || | |
367 height == 0) | |
368 { | |
369 return target.Release(); | |
370 } | |
371 | |
372 if (decoded.empty()) | |
373 { | |
374 decoded.resize(pimpl_->GetImage().GetBufferLength()); | |
3351
5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
375 if (!pimpl_->GetImage().GetBuffer(&decoded[0])) |
5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
376 { |
5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
377 throw std::runtime_error("Image not properly decoded to a memory buffer"); |
5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
378 } |
1834 | 379 } |
380 | |
381 const void* sourceBuffer = &decoded[0]; | |
382 | |
383 if (target.GetPitch() == bpp * width && | |
384 frames == 1) | |
385 { | |
386 assert(decoded.size() == target.GetPitch() * target.GetHeight()); | |
387 memcpy(target.GetBuffer(), sourceBuffer, decoded.size()); | |
388 } | |
389 else | |
390 { | |
391 size_t targetPitch = target.GetPitch(); | |
392 size_t sourcePitch = width * bpp; | |
393 | |
394 const char* a = &decoded[sourcePitch * height * frameIndex]; | |
395 char* b = target.GetBuffer(); | |
396 | |
397 for (uint32_t y = 0; y < height; y++) | |
398 { | |
399 memcpy(b, a, sourcePitch); | |
400 a += sourcePitch; | |
401 b += targetPitch; | |
402 } | |
403 } | |
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
|
404 |
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
|
405 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
|
406 |
1834 | 407 return target.Release(); |
408 } | |
409 } |