Mercurial > hg > orthanc
annotate Plugins/Samples/GdcmDecoder/GdcmImageDecoder.cpp @ 3492:cfaa9eded17e
Added flag to control the emscripten backend (which, in turn, tweaks the compiler flags)
author | Benjamin Golinvaux <bgo@osimis.io> |
---|---|
date | Mon, 19 Aug 2019 10:17:01 +0200 |
parents | 5069af20932a |
children | 94f4a18a79cc |
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 |
3060
4e43e67f8ecf
preparing for 2019
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
2798
diff
changeset
|
5 * Copyright (C) 2017-2019 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 { |
2798
5c83e5cf9d79
fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents:
2485
diff
changeset
|
305 case gdcm::PhotometricInterpretation::MONOCHROME1: |
5c83e5cf9d79
fix decoding of MONOCHROME1 & MONOCHROME2 images
am@osimis.io
parents:
2485
diff
changeset
|
306 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
|
307 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
|
308 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
|
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 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
|
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 // 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
|
313 |
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 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
|
315 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
|
316 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
|
317 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
|
318 |
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 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
|
320 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
|
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 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
|
323 } |
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 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
|
326 { |
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 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
|
328 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
|
329 { |
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 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
|
331 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
|
332 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
|
333 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
|
334 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
|
335 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
|
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 } |
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 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
|
340 } |
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 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
|
343 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
|
344 } |
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 |
1834 | 347 |
348 OrthancPluginImage* GdcmImageDecoder::Decode(OrthancPluginContext* context, | |
349 unsigned int frameIndex) const | |
350 { | |
351 unsigned int frames = GetFramesCount(); | |
352 unsigned int width = GetWidth(); | |
353 unsigned int height = GetHeight(); | |
354 OrthancPluginPixelFormat format = GetFormat(); | |
355 size_t bpp = GetBytesPerPixel(format); | |
356 | |
357 if (frameIndex >= frames) | |
358 { | |
359 throw std::runtime_error("Inexistent frame index"); | |
360 } | |
361 | |
362 std::string& decoded = pimpl_->decoded_; | |
363 OrthancImageWrapper target(context, format, width, height); | |
364 | |
365 if (width == 0 || | |
366 height == 0) | |
367 { | |
368 return target.Release(); | |
369 } | |
370 | |
371 if (decoded.empty()) | |
372 { | |
373 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
|
374 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
|
375 { |
5069af20932a
Web viewer: Check the return value of gdcm::Bitmap::GetBuffer()
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
3060
diff
changeset
|
376 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
|
377 } |
1834 | 378 } |
379 | |
380 const void* sourceBuffer = &decoded[0]; | |
381 | |
382 if (target.GetPitch() == bpp * width && | |
383 frames == 1) | |
384 { | |
385 assert(decoded.size() == target.GetPitch() * target.GetHeight()); | |
386 memcpy(target.GetBuffer(), sourceBuffer, decoded.size()); | |
387 } | |
388 else | |
389 { | |
390 size_t targetPitch = target.GetPitch(); | |
391 size_t sourcePitch = width * bpp; | |
392 | |
393 const char* a = &decoded[sourcePitch * height * frameIndex]; | |
394 char* b = target.GetBuffer(); | |
395 | |
396 for (uint32_t y = 0; y < height; y++) | |
397 { | |
398 memcpy(b, a, sourcePitch); | |
399 a += sourcePitch; | |
400 b += targetPitch; | |
401 } | |
402 } | |
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
|
403 |
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 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
|
405 |
1834 | 406 return target.Release(); |
407 } | |
408 } |